function trim(stringa){
	while(stringa.substring(0, 1) == " ")
		stringa = stringa.substring(1, stringa.length);
	
	while(stringa.substring(stringa.length-1, stringa.length) == " ")
		stringa = stringa.substring(0, stringa.length-1);
	
	return stringa;
}

function mailTo(user, dominio){
	document.location.href = "mailto:" + user + "@" + dominio;
}

function checkForm(){
	var goodEmail = document.forms[0].mail.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	
	if(trim(document.forms[0].nome.value) == ""){
		alert("Attenzione!\nIl campo NOME non e' stato compilato correttamente.");
		document.forms[0].nome.focus();
	}
	else if(trim(document.forms[0].cognome.value) == ""){
		alert("Attenzione!\nIl campo COGNOME non e' stato compilato correttamente.");
		document.forms[0].cognome.focus();
	}
	else if(!goodEmail){
		alert("Attenzione!\nIl campo E-MAIL non e' stato compilato correttamente.");
		document.forms[0].mail.focus();
	}
	else if(trim(document.forms[0].richiesta.value) == ""){
		alert("Attenzione!\nIl campo RICHIESTA non e' stato compilato correttamente.");
		document.forms[0].richiesta.focus();
	}
	else{
		$(document).ajaxStop($.unblockUI);
		
		$.blockUI.defaults.applyPlatformOpacityRules = false;
		
		$.blockUI({
			message: $("#loader")
		});
		
		$.ajax({
			url: "_mail.php",
			type: "POST",
			data: "nome=" + document.forms[0].nome.value + "&cognome=" + document.forms[0].cognome.value + "&mail=" + document.forms[0].mail.value + "&richiesta=" + document.forms[0].richiesta.value,
			cache: false,
			success: function(html){
				$("#contacts").hide();
				$("#description").html(html);
			}
		});
	}
}

