/**
 * Main javascript for Skypemote
 *
 * @author		Giovambattista Fazioli
 * @email		g.fazioli@undolog.com
 * @web			http://www.undolog.com
 *
 */
google.setOnLoadCallback( main ); 

/**
 * Main
 */
function main() {
	// insert flash obejct by swfobject library
    var flashvars = {};
    var params = {
        wmode: 'opaque'
    };
    var attributes = {};
    swfobject.embedSWF("flash/skypeeditor.swf?v=0.8", "claimflash", "780", "480", "8.2.0", "flash/expressInstall.swf", flashvars, params, attributes);
	// init shadowbox library
    Shadowbox.init();
} 

/**
 * Show/Hide the form contacts
 */
function showForm(v) {
	if(v) {
		if( $('#sendform').is(':hidden') ) {
			$('#sendform').slideDown('slow', function(){ document.forms['sendmail_form'].name.focus(); } );
		} else document.forms['sendmail_form'].name.focus();
	} else {
		if( !$('#sendform').is(':hidden') ) {
			$('#sendform').slideUp('slow');
		}
	}
}

/**
 * Show/Hide the form contacts
 
function showForm(v) {
	if( v ) 
		if( !$('sendform').visible() ) {
			new Effect.BlindDown('sendform', 
				{ 
					duration: 1.0,
					afterFinish: function() { 
						document.forms['sendmail_form'].name.focus(); 
					}
				}
			);
		} else document.forms['sendmail_form'].name.focus();
		
	if( !v ) if( $('sendform').visible() ) new Effect.BlindUp('sendform', { duration: 0.5, transition:Effect.Transitions.exponential });
}*/


/**
 * Trigged on "onsubmit" in the form#sendmail_form. Check field and show an alert()
 * If no error occour use Ajax request to send in POST method data to php page for
 * send mail.
 *
 * @param			void
 * @return			{boolean}	Every FALSE
 */
function onSendmail() {
    // regex mail
    var __filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    // ie fix
    var f = document.forms['sendmail_form'];
    // test all field
    if ($F('name') == "") {
        alert("ATTENZIONE!!\n\nIl campo 'Nome' e' vuoto!" + "\n\n" + 
			  "WARNING!!\n\n'Name' field is empty!" );
        // use this ofr return false
        f.name.focus();
        return false;
    }
    if ($F('email') == "") {
        alert("ATTENZIONE!!\n\nIl campo 'Email' e' vuoto!" + "\n\n" + 
			  "WARNING!!\n\n'Email' field is empty!" );
        f.email.focus();
        return false;
    }
    if (!__filter.test($F('email'))) {
        alert("ATTENZIONE!!\n\nIl campo 'Email' NON e' valido!" + "\n\n" + 
			  "WARNING!!\n\n'Email' field is wrong!" );
        f.email.select();
        f.email.focus();
        return false;
    }
    if ($F('subject') == "") {
        alert("ATTENZIONE!!\n\nIl campo 'Oggetto' e' vuoto!" + "\n\n" + 
			  "WARNING!!\n\n'Subject' field is empty!" );
        f.subject.focus();
        return false;
    }
    if ($F('body') == "") {
        alert("ATTENZIONE!!\n\nIl campo 'Messaggio' e' vuoto!" + "\n\n" + 
			  "WARNING!!\n\n'Message' field is empty!" );
        f.body.focus();
        return false;
    }
    if ($F('privacy') != 1) {
        alert("ATTENZIONE!!\n\nDevi autorizzare l'informativa sulla Privacy!" + "\n\n" + 
			  "WARNING!!\n\nCheck privacy authorization!");
        f.privacy.focus();
        return false;
    }
    // in waiting
    // showHideIdle(true);
	$('mainbox').setStyle( {cursor: 'wait' } );
    // Ajax request    
    $('sendmail_form').request({
        onSuccess: function(transport){
            var status = transport.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue;
            var mes = transport.responseXML.getElementsByTagName('message')[0].firstChild.nodeValue;
            
            // open result
            Shadowbox.open({
				player:		'html',		   
                title: 		'Invio Messaggio/Send Message',
                content: 	mes,
                height: 	200,
                width: 		450
            }, {
                onClose: function(){
                    $('mainbox').setStyle( {cursor: 'default' } );
                    //
                    if (status == 0) {
                        // clear the form
                        var f = document.forms['sendmail_form'];
                        f.name.value = f.email.value = f.subject.value = f.body.value = "";
                        f.privacy.checked = false;
						showForm(false);
                    }
                    else {
                        Form.Element.activate('name')
                    }
                    //
                }
            });
        }
        //onComplete: function(){ alert('Form data saved!') }
    })
	return false;
}