
$(document).ready(function(){
							
	
	$("#submit").click(function(){	
	
		
		jQuery(".error").empty().stop();
		
		
		var hasError = false;
		var emailReg = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		var emailFromVal = $("#txtMail").val();
		if(emailFromVal == '') {
			
			$("#erroreMail").html('<span class="error" style="font-weight:bold; color:#CC0000">Il campo "E-mail" &egrave; obbligatorio</span>',function(){$(this).fadeIn('slow')});
			hasError = true;
		}
		
		 else if(!emailReg.test(emailFromVal)) {	
			$("#erroreMail").html('<span class="error" style="font-weight:bold; color:#CC0000">Inserire un indirizzo email valido.</span>',function(){$(this).fadeIn('slow')});
			hasError = true;
		}

		var nomeVal = $("#txtNome").val();
		if(nomeVal == '') {
			$("#erroreNome").html('<span class="error" style="font-weight:bold; color:#CC0000">"Nome&Cognome" &egrave; obbligatorio</span>',function(){$(this).fadeIn('slow')});
			hasError = true;
		}

		var txtRagSocVal = $("#txtRagSoc").val();
		var txtTelVal = $("#txtTel").val();
		var txtSitoVal = $("#txtSito").val();
		var prodottiVal = $("#prodotti").val();
		var noteVal = $("#note").val();
		
		
		if(hasError == false) {
			
			$(this).hide();
			$('#risultato').append('<div class="loading" alt="Loading" ></div>');
			
			$.ajax({
						url: "/contatti",
						data:{     
									txtMail: emailFromVal, 
									txtNome: nomeVal, 
									txtMail:emailFromVal,
									txtRagSoc: txtRagSocVal,
									txtSito: txtSitoVal,
									prodotti: prodottiVal, 
									note: noteVal, 
									txtTel: txtTelVal, 
									Submit: 1
						},
						type: 'POST',
						
						success : function(data){
											setTimeout('hidePreloader()',2000);
									},
						error : function(data){
											setTimeout('hidePreloaderError()',2000);
									}
							}
						 );
		}
		return false;
	});
	
 					   
});
function hidePreloader(){
		$("#cntct").fadeOut("normal");
		$("#sendEmail").fadeOut("normal", function() {	
			$('.loading').fadeOut( function(){
				$("#risultato").before('<p class="wellDone">Grazie per averci contattato.<br />Saremo lieti di risponderVi il pi&ugrave; presto possibile.</p>').fadeIn();
				});
			});
		}
function hidePreloaderError(){
		$("#cntct").fadeOut("normal");
		$("#sendEmail").fadeOut("normal", function() {	
			$('.loading').fadeOut( function(){
				$("#risultato").before('<p class="error">Non &egrave; stato possibile inviare la richiesta.<br />Riprovare pi&ugrave; tardi. Ci scusiamo per il disagio.</p>').fadeIn();
				})										
			});
		}		
