//SEND FORM MIXIAN 11/11/2011
/////////////////////////////
function sendForm(mailProprietario,mailList,nomeForm,nomeFormIdent) {

	//ANTISPAM
	var phpTimestamp = $("#datetime-"+nomeFormIdent).val();
		
	var time = Math.round((new Date()).getTime() / 1000);	
	var diff = time - phpTimestamp;
	
	//controllo se il form è stato compilato in meno di 3 secondi
	//in tal caso può essere SPAM così come se il campo nascosto NICKNAME è stato compilato
	//cosa che non può avvenire se a compilare è un umano.
	var nickname = $("#nickname-"+nomeFormIdent).val();
	
	if ((diff <= 3) || (nickname!="")){
	return false; //->SEI UNO SPAMMER---NON INVIARE MAIL---STOP
	}
	
	////END ANTISPAM
	
	$("#button-"+nomeFormIdent).hide();
	$("#loader-"+nomeFormIdent).show();
	$("#response-"+nomeFormIdent).hide();
	
	 
	$(".error-message-"+nomeFormIdent).hide();
	$(".email-error-message-"+nomeFormIdent).hide();
	
	var datiModulo = [];				
	var required = false;
	var emailError = false;
	
	datiModulo.push('<h3>'+nomeForm+'</h3>');
	
	$('.fieldList-'+nomeFormIdent+' > div').each(function(index) {
		var currentId = $(this).attr('id');
		var addVar = '-field';
		var addLabel = '-label';
		
		var currentVal =  $("#"+currentId+addVar).val();
		var currentLabel = $("#"+currentId+addLabel).text();
		
		
		//CONTROLLO ESATTEZZA DATI
		//se l'input ha class="required" ed è vuoto...creo dinamicamente il codice necessario per
		
		if($("#"+currentId+'-field').hasClass('required') && currentVal==""){
			$("#error-message-"+currentId).show();
			required = true; //condizione di blcco
		}
		
		//se l'input è di tipo MAIL controlliamo l'esattezza della email
		if($("#"+currentId+'-field').hasClass('e_mail')){
		   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		   var address = $("#"+currentId+'-field').val();						   
		   
		    if(reg.test(address) == false) {
			   $("#email-error-message-"+currentId).show();
			   emailError = true; //condizione di blcco
			 }
		 }
		
		//se l'input è di tipo MAIL controlliamo l'esattezza della email
		if($("#"+currentId+'-field').hasClass('check_box')){
		
			var check = $("#"+currentId+'-field:checked').size();
			
			if (check=='0'){
				currentVal = 'No';
			}
			
			if (check=='1'){
				currentVal = 'Si';
			}
			
			
		}
		
		/////////////COSTRUISCO ARRAY CON HTML DEL BODY-MAIL
		datiModulo.push(
				'<h4>'+currentLabel+': <b>'+currentVal+'</b></h4><br />'
		);
	    
	});
	
	//BLOCCO LO SCRIPT SE I CAMPI SONO OBBLIGATORI O SE EMAIL NON CORRETTA
	if (required==true || emailError==true){
		$("#loader-"+nomeFormIdent).hide();
		$("#button-"+nomeFormIdent).show();					
		return false;
	}
	
	
	//INVIO EMAIL IN AJAX
	$.ajax({
		type: 'POST',
		url: "/admin/core/form/sendform/",
		data: {
			mailList : mailList,
			nomeForm : nomeForm,
			mailProprietario : mailProprietario,
			html : datiModulo
		},
		cache: false,
		success: function(data){
			$("#"+nomeFormIdent).show();
			$("#loader-"+nomeFormIdent).hide();
			$("#button-"+nomeFormIdent).show();
			$("#response-"+nomeFormIdent).show();
			
			//reset hidden datetime for antispam			
			var time = Math.round((new Date()).getTime() / 1000);
			$("#datetime-"+nomeFormIdent).val(time);
				
			}
		});
		
		
		return false;
	}
