var error = "" ;

function controlloTuttoClienti() {
 	checkDataArrivo();
	checkDataPartenza();
	checkPersone();
	checkCamere();
	checkName();
 	checkCognome();
	//isEmail();
	isNumericTel();

 	if(error == "")
 		return true ;
 
 	else {
 		alert (error);
    	error = "";
		return false;
	}
}

function checkDataArrivo() 
{
	var arrivo_giorno = document.getElementById("arrivo_giorno").value ;
	var arrivo_mese = document.getElementById("arrivo_mese").value ;
	var arrivo_anno = document.getElementById("arrivo_anno").value ;
	
	var data = arrivo_giorno + "/" + arrivo_mese + "/" + arrivo_anno;
	
	if (data.length <= 8) {
    	error += "Il campo Data Arrivo \u00E9 obbligatorio" + "\n";
		return false;
  	}
	
 	return true ;
}

function checkDataPartenza() 
{
	var partenza_giorno = document.getElementById("partenza_giorno").value ;
	var partenza_mese = document.getElementById("partenza_mese").value ;
	var partenza_anno = document.getElementById("partenza_anno").value ;
	
	var data = partenza_giorno + "/" + partenza_mese + "/" + partenza_anno;
	
	if (data.length <= 8)	{
    	error += "Il campo Data Partenza \u00E9 obbligatorio" + "\n";
		return false;
  	}
	
 	return true ;
}

function checkPersone() 
{
	var persone = document.getElementById("persone").value ;
	
	if (persone == "-")	{
    	error += "Il campo Persone \u00E9 obbligatorio" + "\n";
		return false;
  	}
	
 	return true ;
}

function checkCamere() 
{
	var camere = document.getElementById("camere").value ;
	
	if (camere == "-")	{
    	error += "Il campo Camere \u00E9 obbligatorio" + "\n";
		return false;
  	}
	
 	return true ;
}

function checkName() 
{
	var nome = document.getElementById("nome").value ;
	
	if (nome == "")	{
    	error += "Il campo Nome \u00E9 obbligatorio" + "\n";
		return false;
  	}
	
 	return true ;
}

function checkCognome() 
{
	var cognome = document.getElementById("cognome").value ;
	
	if (cognome == "") {
		error += "Il campo Cognome \u00E9 obbligatorio" + "\n";
  		return false;
	}
	
	return true;
}

function isEmail(){
  	value = document.getElementById("email").value;
  
	if (value == ""){
    	error += "Il campo email \u00E9 obbligatorio" + "\n"; 
       	return false;
  	}
  	
	var emailstring = value;
	var ampIndex = emailstring.indexOf("@");
	var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
		// find a dot in the portion of the string after the ampersand only
	var dotIndex = afterAmp.indexOf(".");
		// determine dot position in entire string (not just after amp portion)
	dotIndex = dotIndex + ampIndex + 1;
		// afterAmp will be portion of string from ampersand to dot
	afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
		// afterDot will be portion of string from dot to end of string
	var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
	var beforeAmp = emailstring.substring(0,(ampIndex));
	var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
		// index of -1 means "not found"
	if ((emailstring.indexOf("@") != "-1") &&
		(emailstring.length > 5) &&
		(afterAmp.length > 0) &&
		(beforeAmp.length > 1) &&
		(afterDot.length > 1) &&
		(email_regex.test(emailstring)) ) {
		  return true;
	} else {
		    error += "controlla l'email inserita" + "\n" ;
			return false;
	}
	
 	return true;
}

function isNumericTel(){
   	value = document.getElementById("telefono").value;
   	var ValidChars = "0123456789";
   	var IsNumber=true;
   	var Char;
   
	if (value == ""){
    	error += "Il campo Telefono \u00E9 obbligatorio" + "\n";
    	return true;
  	}
  
  	if (value.length < 6)
  	{
    	error += "Il numero di Telefono \u00E9 troppo corto" + "\n" ;
      	return false;
  	}
  
  
   	for (i = 0; i < value.length && IsNumber == true; i++) 
    { 
     	Char = value.charAt(i); 
      	if (ValidChars.indexOf(Char) == -1) {
         	IsNumber = false;
          	error += "controlla il numero del telefono" + "\n" ;
          	return false;
         }
    }
    
	return true;
}

function isNumeric()
{
   value = document.getElementById("numeri").value;
   var ValidChars = "0123456789.,";
   var IsNumber=true;
   var Char;
   
	if (value == "")
		error += "hai dimenticato il numero" + "\n";

   for (i = 0; i < value.length && IsNumber == true; i++) 
      { 
      Char = value.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
		 error += "controlla il numero" + "\n" ;
		 return false ;
         }
      }
    return IsNumber;
}
