
var optionSendContatto = { 
       // target:        '#risposta',   // target element(s) to be updated with server response 
		dataType : 		'json',
        //beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponseSendContatto  // post-submit callback 
};

function sendContatto(){
	
	error="";
	
	if ($('#nome').val()==""){
		evidenziaErrore ('nome', 'inputTextErr', 'nomelbl', 'error');		
		error="inserire il nome";
	}else{
		evidenziaErrore ('nome', 'inputText', 'nomelbl', 'nOerror');	
	}
	
	if ($('#cognome').val()==""){
		evidenziaErrore ('cognome', 'inputTextErr', 'cognomelbl', 'error');		
		error="inserire il cognome";
	}else{
		evidenziaErrore ('cognome', 'inputText', 'cognomelbl', 'nOerror');	
	}
	
	if (($('#email').val()=="") || (!validEmail($('#email').val()))){
		evidenziaErrore ('email', 'inputTextErr', 'emaillbl', 'error');		
		error="inserire l'email";
	}else{
		evidenziaErrore ('email', 'inputText', 'emaillbl', 'nOerror');	
	}
	
	if ($('#messaggio').val()==""){
		evidenziaErrore ('messaggio', 'inputTextErr', 'messaggiolbl', 'error');	
		error="inserire il messaggio";
	}else {
		evidenziaErrore ('messaggio', 'inputText', 'messaggiolbl', 'nOerror');	
	}
	
	if ($('#oggetto').val()==""){
		evidenziaErrore ('oggetto', 'inputTextErr', 'oggettolbl', 'error');	
		error="inserire l'oggetto";
	}else {
		evidenziaErrore ('oggetto', 'inputText', 'oggettolbl', 'nOerror');	
	}
	
	
	if (error !="" ) $('#msg').html("Correggere gli errori di compilazione");
	else {
		$.blockUI();
		
		$('#contatti').ajaxForm(optionSendContatto);
		$('#contatti').submit();
	}
	
}

function validEmail(email){
	if (email=="") return false;
	if (email.indexOf("@")==-1) return false;
	if (email.indexOf("@")>email.lastIndexOf(".")) return false;
	return true;
	
}

function evidenziaErrore (campo, classErrCampo, label, classErrLabel){
	$('#' + campo).attr('class',classErrCampo);
	$('#' + label).attr('class',classErrLabel);
}





function showResponseSendContatto(responseText, statusText){
	$.unblockUI();
	if (responseText.result == 'false'){
		alert (responseText.msg);			
	}else{
		$('#msg').html(responseText.msg);
		$('#contatti').clearForm();
	}
}