function capitalize(obj) {
  var s = trim(obj.value);
  obj.value = doCapitalize(s);
}

function doCapitalize(s) {
  var cSeparator = ' ';
  if (s.indexOf(cSeparator) < 0 && s.indexOf('\'') > 0)
    cSeparator = '\'';
    
  var lastLet='';
  var isLettAcc=false;
  lastLet = s.substring(s.length-1,s.length);
  if (lastLet=='à' || lastLet=='è'|| lastLet=='é' || lastLet=='ì' || lastLet=='ò' || lastLet=='ù'){
  	isLettAcc = true;
  }
  var a = s.split(/\W+/);
  
  for (i=0; i<a.length;i++)
    a[i] = a[i].substring(0,1).toUpperCase() + a[i].substring(1, a[i].length).toLowerCase();
  
  s = '';
  for (i=0; i<a.length;i++) {
    if (s != '') s = s + cSeparator;
    s = s + a[i];
  }
  if (isLettAcc)
  	s = s + lastLet;
  return s;
}

function validaCaratteri(s){
	var c,i;
	var valida=false;
	for (i=0;i<s.length;i++){
		c = s.charCodeAt(i);
		// lettere minuscole,     lettere maiuscole,    numeri
		if ( (c>=97 && c<=122) || (c>=65 && c<=90) || (c>=48 && c<=57) ) {
			valida=true;
		}
		else {
			valida=false;
			break;
		}
	}
	return valida;
}
function emailCorretta(stringa){
		var valida = false;
		var c;
		var found_chiocciola = 0;
		var found_punto = false;
		var found_spazio = false;
		var found_virgola = false;
	  	
	  	// CONTROLLO solo 1 "@" e almeno 1 "." e nessuno " " e nessuna ","
	  	if (stringa.length > 6) {
		  	for (i=0;i<stringa.length;i++){
		  		c = stringa.charAt(i);
		  		if (c==".") found_punto = true;
		  		if (c=="@") found_chiocciola++;
		  		if (c==" ") found_spazio = true;
		  		if (c==",") found_virgola = true;
		  	}
		}
		if (found_chiocciola==1 && found_punto) valida = true;
		if (found_spazio) valida = false;
		if (found_virgola) valida = false;
				
	  	
	  	// CONTROLLO "@" e "." all'inizio oppure alla fine
	  	if (valida) {
		  	c = stringa.charAt(0);
		  	if (c=="@" || c==".") {
		  		valida = false;
		  	}
		  	c = stringa.charAt(stringa.length-1);
		  	if (c=="@" || c==".") {
		  		valida = false;
		  	}
		}
		
		// CONTROLLO "@." e ".@"
		if (valida) {
			var aParts = stringa.split("@");
		  	var err=false;
		  	for (i=0;i<aParts.length;i++) {
		  		if (aParts[i].charAt(aParts[i].length-1)==".") {
		  			valida=false;
		  			break;
		  		}
		  		if (aParts[i].charAt(0)==".") {
		  			valida=false;
		  			break;
		  		}
		  	}
		}
	return valida;
}
function trim(MyString) {
  
  var NoExit;
  var x;
  NoExit = true;
  do {
  	x = MyString.substring(0,1);
  	if (x == ' ')
  		MyString = MyString.substring(1,100);
  	else
  		NoExit = false;
  }
  while (NoExit)
  return (MyString);
}
function valida() {
  var validacampi=true;
  
	  // Nome
	  var tmp = doCapitalize(window.document.frmRegister.p_nome.value);
	  if ((trim(tmp) == '') && (validacampi==true)) {
			validacampi=false;
			window.document.frmRegister.buttvalue.value='NO';
			alert("Insert Your FirstName");
	  } else {
		window.document.frmRegister.p_nome.value = tmp;
	  }
	 // User
	  var tmp = doCapitalize(window.document.frmRegister.p_user.value);
	  if ((trim(tmp) == '') && (validacampi==true)) {
			validacampi=false;
			window.document.frmRegister.buttvalue.value='NO';
			alert("Insert Your Username");
	  } else {
		window.document.frmRegister.p_user.value = tmp;
	  }
	// Cognome
	tmp = doCapitalize(window.document.frmRegister.p_cognome.value);
	if ((trim(tmp) == '')&& (validacampi==true)) {
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your LastName");
	} else {
	  window.document.frmRegister.p_cognome.value = tmp;
	}	
	
	if ((trim(window.document.frmRegister.p_email.value) =='')&& (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your Email");
	}
	
	if (!emailCorretta(window.document.frmRegister.p_email.value) && validacampi){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Is not a valid email.");
	}
	
	if ((trim(window.document.frmRegister.p_password.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Is not a valid Password");
	}
	
	if (!validaCaratteri(trim(window.document.frmRegister.p_password.value)) && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Invalid Character in your Password");
	}
	
	if (window.document.frmRegister.p_password.value != window.document.frmRegister.p_password1.value){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("The Passwords was not egual");
		}
	if (window.document.frmRegister.p_email.value != window.document.frmRegister.p_email1.value){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("The Emails was not egual");
		}	
	
	if ((trim(window.document.frmRegister.p_telefono.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your Telephone");
	}
	
	if ((trim(window.document.frmRegister.p_indirizzo.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your Adress");
	}
	
	if ((trim(window.document.frmRegister.p_cap.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Zip Code");
	}
	
	if ((trim(window.document.frmRegister.p_citta.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your City");
	}
	
	if ((trim(window.document.frmRegister.p_nazione.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your Nation");
	}
	
	if ((window.document.frmRegister.p_Accept[0].checked == false) &&  ( validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Informativa ai sensi dell'articolo ART. 13 DEL D.LGS 196/03 NON ACCETTATA. Impossibile registrarsi");
	}
	
	if (validacampi==true){
		window.document.frmRegister.buttvalue.value="OK";
		window.document.frmRegister.submit();
	}
}

function valida_rq() {
  var validacampi=true;
  	// Nome
	  var tmp = doCapitalize(window.document.frmRQ.p_nome.value);
	  if ((trim(tmp) == '') && (validacampi==true)) {
			validacampi=false;
			window.document.frmRQ.buttvalue.value='NO';
			alert("Insert Your FirstName");
	  } else {
		window.document.frmRQ.p_nome.value = tmp;
	  }
	// Cognome
	tmp = doCapitalize(window.document.frmRQ.p_cognome.value);
	if ((trim(tmp) == '')&& (validacampi==true)) {
		validacampi=false;
		window.document.frmRQ.buttvalue.value='NO';
		alert("Insert Your LastName");
	} else {
	  window.document.frmRQ.p_cognome.value = tmp;
	}
	
	// Nazione
	tmp = doCapitalize(window.document.frmRQ.p_nazione.value);
	if ((trim(tmp) == '')&& (validacampi==true)) {
		validacampi=false;
		window.document.frmRQ.buttvalue.value='NO';
		alert("Insert Your LastName");
	} else {
	  window.document.frmRQ.p_nazione.value = tmp;
    }

    // Eta delle persone
    tmp = doCapitalize(window.document.frmRQ.p_eta.value);    if ((trim(tmp) == '') && (validacampi == true)) {        validacampi = false;        window.document.frmRQ.buttvalue.value = 'NO';        alert("Insert the Ages of the persons");    } else {
        window.document.frmRQ.p_eta.value = tmp;    }
	
	if ((trim(window.document.frmRQ.p_telefono.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRQ.buttvalue.value='NO';
		alert("Insert Telephone");
	}
	if ((trim(window.document.frmRQ.p_indirizzo.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRQ.buttvalue.value='NO';
		alert("Insert Adress");
	}
	if ((trim(window.document.frmRQ.p_citta.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRQ.buttvalue.value='NO';
		alert("Insert Telephone");
	}
	if ((trim(window.document.frmRQ.p_cap.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRQ.buttvalue.value='NO';
		alert("Insert Telephone");
	}
	if ((trim(window.document.frmRQ.p_dt_dal.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRQ.buttvalue.value='NO';
		alert("Insert arrival date");
	}
	if ((trim(window.document.frmRQ.p_dt_al.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRQ.buttvalue.value='NO';
		alert("Insert departure date");
	}
	if ((window.document.frmRQ.p_tipologia.checked == false) &&  ( validacampi==true)){
		validacampi=false;
		window.document.frmRQ.buttvalue.value='NO';
		alert("Select 1 of Tipology");
	}
	if ((trim(window.document.frmRQ.p_email.value) =='')&& (validacampi==true)){
		validacampi=false;
		window.document.frmRQ.buttvalue.value='NO';
		alert("Insert Your Email");
	}
	if (!emailCorretta(window.document.frmRQ.p_email.value) && validacampi){
		validacampi=false;
		window.document.frmRQ.buttvalue.value='NO';
		alert("Is not a valid email.");
	}
	if ((window.document.frmRQ.p_Accept[0].checked == false) &&  ( validacampi==true)){
		validacampi=false;
		window.document.frmRQ.buttvalue.value='NO';
		alert("Pleas Accept our condition!");
	}
	if (validacampi==true){
		window.document.frmRQ.buttvalue.value="OK";
		window.document.frmRQ.submit();
	}
}


function validarqinfo() {
  var validacampi=true;
  
	  // Nome
	  var tmp = doCapitalize(window.document.frmRegister.p_nome.value);
	  if ((trim(tmp) == '') && (validacampi==true)) {
			validacampi=false;
			window.document.frmRegister.buttvalue.value='NO';
			alert("Insert Your FirstName");
	  } else {
		window.document.frmRegister.p_nome.value = tmp;
	  }
	 // User
//	  var tmp = doCapitalize(window.document.frmRegister.p_user.value);
//	  if ((trim(tmp) == '') && (validacampi==true)) {
//			validacampi=false;
//			window.document.frmRegister.buttvalue.value='NO';
//			alert("Insert Your Username");
//	  } else {
//		window.document.frmRegister.p_user.value = tmp;
//	  }
	// Cognome
	tmp = doCapitalize(window.document.frmRegister.p_cognome.value);
	if ((trim(tmp) == '')&& (validacampi==true)) {
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your LastName");
	} else {
	  window.document.frmRegister.p_cognome.value = tmp;
	}	
	
	if ((trim(window.document.frmRegister.p_email.value) =='')&& (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your Email");
	}
	
	if (!emailCorretta(window.document.frmRegister.p_email.value) && validacampi){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Is not a valid email.");
	}
	
//	if ((trim(window.document.frmRegister.p_password.value)=='') && (validacampi==true)){
//		validacampi=false;
//		window.document.frmRegister.buttvalue.value='NO';
//		alert("Is not a valid Password");
//	}
	
//	if (!validaCaratteri(trim(window.document.frmRegister.p_password.value)) && (validacampi==true)){
//		validacampi=false;
//		window.document.frmRegister.buttvalue.value='NO';
//		alert("Invalid Character in your Password");
//	}
	
//	if (window.document.frmRegister.p_password.value != window.document.frmRegister.p_password1.value){
//		validacampi=false;
//		window.document.frmRegister.buttvalue.value='NO';
//		alert("The Passwords was not egual");
//		}

//	if (window.document.frmRegister.p_email.value != window.document.frmRegister.p_email1.value){
//		validacampi=false;
//		window.document.frmRegister.buttvalue.value='NO';
//		alert("The Emails was not egual");
//		}	
	
	if ((trim(window.document.frmRegister.p_telefono.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your Telephone");
	}
	
	if ((trim(window.document.frmRegister.p_indirizzo.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your Adress");
	}
	
	if ((trim(window.document.frmRegister.p_cap.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Zip Code");
	}
	
	if ((trim(window.document.frmRegister.p_citta.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your City");
	}
	
	if ((trim(window.document.frmRegister.p_nazione.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your Nation");
	}
	
	if ((window.document.frmRegister.p_Accept[0].checked == false) &&  ( validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Informativa ai sensi dell'articolo ART. 13 DEL D.LGS 196/03 NON ACCETTATA. Impossibile registrarsi");
	}
	
	if (validacampi==true){
		window.document.frmRegister.buttvalue.value="OK";
		window.document.frmRegister.submit();
	}
}


function validarqinfonoprivacy() {
  var validacampi=true;
  
	  // Nome
	  var tmp = doCapitalize(window.document.frmRegister.p_nome.value);
	  if ((trim(tmp) == '') && (validacampi==true)) {
			validacampi=false;
			window.document.frmRegister.buttvalue.value='NO';
			alert("Insert Your FirstName");
	  } else {
		window.document.frmRegister.p_nome.value = tmp;
	  }
	 // User
//	  var tmp = doCapitalize(window.document.frmRegister.p_user.value);
//	  if ((trim(tmp) == '') && (validacampi==true)) {
//			validacampi=false;
//			window.document.frmRegister.buttvalue.value='NO';
//			alert("Insert Your Username");
//	  } else {
//		window.document.frmRegister.p_user.value = tmp;
//	  }
	// Cognome
	tmp = doCapitalize(window.document.frmRegister.p_cognome.value);
	if ((trim(tmp) == '')&& (validacampi==true)) {
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your LastName");
	} else {
	  window.document.frmRegister.p_cognome.value = tmp;
	}	
	
	if ((trim(window.document.frmRegister.p_email.value) =='')&& (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your Email");
	}
	
	if (!emailCorretta(window.document.frmRegister.p_email.value) && validacampi){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Is not a valid email.");
	}
	
//	if ((trim(window.document.frmRegister.p_password.value)=='') && (validacampi==true)){
//		validacampi=false;
//		window.document.frmRegister.buttvalue.value='NO';
//		alert("Is not a valid Password");
//	}
	
//	if (!validaCaratteri(trim(window.document.frmRegister.p_password.value)) && (validacampi==true)){
//		validacampi=false;
//		window.document.frmRegister.buttvalue.value='NO';
//		alert("Invalid Character in your Password");
//	}
	
//	if (window.document.frmRegister.p_password.value != window.document.frmRegister.p_password1.value){
//		validacampi=false;
//		window.document.frmRegister.buttvalue.value='NO';
//		alert("The Passwords was not egual");
//		}

//	if (window.document.frmRegister.p_email.value != window.document.frmRegister.p_email1.value){
//		validacampi=false;
//		window.document.frmRegister.buttvalue.value='NO';
//		alert("The Emails was not egual");
//		}	
	
	if ((trim(window.document.frmRegister.p_telefono.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your Telephone");
	}
	
	if ((trim(window.document.frmRegister.p_indirizzo.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your Adress");
	}
	
	if ((trim(window.document.frmRegister.p_cap.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Zip Code");
	}
	
	if ((trim(window.document.frmRegister.p_citta.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your City");
	}
	
	if ((trim(window.document.frmRegister.p_nazione.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Insert Your Nation");
	}
	
//	if ((window.document.frmRegister.p_Accept[0].checked == false) &&  ( validacampi==true)){
//		validacampi=false;
//		window.document.frmRegister.buttvalue.value='NO';
//		alert("Informativa ai sensi dell'articolo ART. 13 DEL D.LGS 196/03 NON ACCETTATA. Impossibile registrarsi");
//	}
	
	if (validacampi==true){
		window.document.frmRegister.buttvalue.value="OK";
		window.document.frmRegister.submit();
	}
}
