var found = 0;

/*function checkMail(mail){     
	
	if (mail != "")
	{
		httpObject = getHTTPObject();    
		if (httpObject != null) 
		{        
			var url = "./ajax/check_email.php";
			var params = "mail="+escape(mail);
			httpObject.open("POST", url, true);
			
			//Send the proper header information along with the request
			httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			httpObject.setRequestHeader("Content-length", params.length);
			httpObject.setRequestHeader("Connection", "close");
			
			httpObject.send(params);         
			httpObject.onreadystatechange = function ()
			{    
				if ((httpObject.readyState == 4) && (httpObject.status == 200))
				{      
					  xmlDoc = httpObject.responseXML;
					  document.getElementById("response-email").innerHTML = xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
       if(xmlDoc.getElementsByTagName("value")[0].childNodes[0].nodeValue == 1) 
					  	found = 1;
					  else 
					  	found = 0;
				} 
			} 
		}
	}	
}*/

function checkMail(mail){ 
 $.ajax({
        type: "POST",
        url: "ajax/check_email.php",
        data: "email="+escape(mail),
        dataType: "xml",
        success: function(xml) {
          $(xml).find('response').each(function(){
            var id = $(this).attr('id');
				        var text = $(this).find('text').text();
				        var value = $(this).find('value').text();
				        
				        $('#response-email').html(text);
            if(value == 0) {
             $('#response-email').removeClass('error_message').addClass('success_message');
             found = 0;
            }
            /*else if(value == 1) {
             $('#response-email').removeClass('success_message').addClass('error_message');
             found = 1;
            }*/
            else {
             $('#response-email').removeClass('success_message').addClass('error_message');
             found = 1;
            }
          });
        }
 });
}


function checkForm(idForm) {
	
	missinginfo = "";
	var error = false;
	var email2Check = idForm.email.value;
	checkMail(email2Check);
	
	if(idForm.nome.value == "" && idForm.cognome.value == "" && idForm.ragsoc.value == "") {
		missinginfo += "\n-  Nome e Cognome oppure la Ragione Sociale!";
		error = true;
	}	
	else if(idForm.indirizzo.value == "") {
		missinginfo += "\n-  Indirizzo!";
		error = true;
		idForm.indirizzo.focus();
	}
	else if(idForm.cap.value == "") {
		missinginfo += "\n-  CAP!";
		error = true;
		idForm.cap.focus();
	}
	else if(idForm.citta.value == "") {
		missinginfo += "\n-  Città!";
		error = true;
		idForm.citta.focus();
	}
	else if(idForm.provincia.value == "") {
		missinginfo += "\n-  Provincia!";
		error = true;
		idForm.provincia.focus();
	}
	else if(idForm.email.value == "" ||
		idForm.email.value.indexOf('@') == -1 ||
		idForm.email.value.indexOf('.') == -1) {
		missinginfo += "\n-  Indirizzo Email";
		error = true;
		idForm.email.focus();
	}
	else if(found == 1) {
		missinginfo += "\n-  Indirizzo Email gia presente in archivio";
		error = true;
		idForm.email.focus();
	}
	else if(idForm.cellulare.value == "") {
		missinginfo += "\n-  Cellulare!";
		error = true;
		idForm.cellulare.focus();
	}

	if(!error){	
	 if(idForm.altrodomicilio.checked == true) {
   if(idForm.altrocognome.value == "") {
	 	 missinginfo += "\n-  Cognome (Altro domicilio)!";
	 	 error = true;
	 	 idForm.altrocognome.focus();
	  }
   else if(idForm.altronome.value == "") {
	 	 missinginfo += "\n-  Nome (Altro domicilio)!";
	 	 error = true;
	 	 idForm.altronome.focus();
	  }
	  else if(idForm.altroindirizzo.value == "") {
	  	missinginfo += "\n-  Indirizzo (Altro domicilio)!";
	  	error = true;
	  	idForm.altroindirizzo.focus();
	  }
	  else if(idForm.altrocap.value == "") {
	  	missinginfo += "\n-  CAP (Altro domicilio)!";
	  	error = true;
	  	idForm.altrocap.focus();
	  }
	  else if(idForm.altrocitta.value == "") {
	  	missinginfo += "\n-  Città (Altro domicilio)!";
	  	error = true;
	  	idForm.altrocitta.focus();
	  }
	  else if(idForm.altroprovincia.value == "") {
	  	missinginfo += "\n-  Provincia (Altro domicilio)!";
	  	error = true;
	  	idForm.altroprovincia.focus();
	  }
	 } 
 }
	
	if(!error){
	 if(idForm.privacy.checked == false) {
		 missinginfo += "\n-  Devi acconsentire al trattamento dei dati!";
		 error = true;
	 }
	 else if(idForm.captcha.value == "") {
		 missinginfo += "\n-  Inserisci il codice che vedi nell'immagine!";
		 error = true;
	 }
 }

	if(error) {
		missinginfo ="Campo non corretto:\n" + "_________________________________\n" +
		missinginfo + "\n_________________________________" +	"\n\n";
		alert(missinginfo);
		return false;
	}
	else {
	 $('#form2hide').hide();
  $('.response').html('<b>Attendere: elaborazione dell\'ordine in corso...</b><br /><br /><img src="./images/loadingAnimation.gif" />');
	 return true;
	}
}
