var msgNoData = "Attenzione. Dato mancante."; //"<%=getMsg(10029, "Attenzione. Dato mancante.")%>"
var msgCharNotCorrect = "Attenzione. Il campo contiene caratteri non ammessi."; //"<%=getMsg(10030, "Attenzione. Il campo contiene caratteri non ammessi.")%>";

	function isBlank(s)
	{
		for(var i = 0; i < s.length; i++)
		{
			var c = s.charAt(i);
			if ((c != ' ')&&(c != '')&&(c != '  ')&&(c != '')&&(c != null)) return false;
		}
		return true;
	}
	
	function isOnlyNumber(s)
	{
		var digits="0123456789";
		for (var i=0;i<s.length;i++)
		{
			  temp=s.substring(i,i+1);
			  if (digits.indexOf(temp)==-1)
			  {
				return false;
			  }
		}
		return true;
	}
	
	function isOnlyPhoneNumber(s)
	{
		var digits=" +.()*#0123456789";
		for (var i=0;i<s.length;i++)
		{
			  temp=s.substring(i,i+1);
			  if (digits.indexOf(temp)==-1)
			  {
				return false;
			  }
		}
		return true;
	}
	
	
	function ControllaCF(cf)
	{
		var validi, i, s, set1, set2, setpari, setdisp;
		if( cf == '' )  return '';
		cf = cf.toUpperCase();
		if( cf.length != 16 )
			return "La lunghezza del Codice Fiscale non è corretta: dovrebbe essere lungo esattamente 16 caratteri."; //"<%=getMsg(10037, )%>";
		validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		for( i = 0; i < 16; i++ ){
			if( validi.indexOf( cf.charAt(i) ) == -1 )
				return "Il codice fiscale contiene un carattere non valido `" +
					cf.charAt(i) +
					"'.\nI caratteri validi sono le lettere e le cifre.\n";
		}
		set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
		setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
		s = 0;
		for( i = 1; i <= 13; i += 2 )
			s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
		for( i = 0; i <= 14; i += 2 )
			s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
		if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
			return "Il codice fiscale non è corretto:\n il codice di controllo non corrisponde."; //"<%=getMsg(10038, )%>";
		return "";
	}
	
	
	function ControllaPIVA(pi)
	{
		if( pi == '' )  return '';
		if( pi.length != 11 )
			return "La lunghezza della partita IVA non è corretta: dovrebbe essere lunga esattamente 11 caratteri."; //"<%=getMsg(10039, )%>";
		validi = "0123456789";
		for( i = 0; i < 11; i++ ){
			if( validi.indexOf( pi.charAt(i) ) == -1 )
				return "La partita IVA contiene un carattere non valido `" +
					pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
		}
		s = 0;
		for( i = 0; i <= 9; i += 2 )
			s += pi.charCodeAt(i) - '0'.charCodeAt(0);
		for( i = 1; i <= 9; i += 2 ){
			c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
			if( c > 9 )  c = c - 9;
			s += c;
		}
		if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
			return "La partita IVA non è valida: il codice di controllo non corrisponde."; //"<%=getMsg(10040, )%>";
		return '';
	}
	
	function isValidChar(c)
	{
		if (c.indexOf("'") > -1) return true;
		return false;   
	}
	
	function checkEmail(e) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(e)){
			return (true);
		}
		return (false);
	}
	
function checkCustomerRegForm(f, pwd)
    {
        var msg ;
        if (isBlank(f.Ragione.value))       
        {
            msg = "Specificare la ragione sociale della propria azienda";
            alert(msg);
			f.Ragione.focus();
            return false;
        }
        if (isBlank(f.pIva.value))      
        {
            msg = "Specificare Partita IVA";
            alert(msg);
			f.pIva.focus();
            return false;
        }
		msg = ControllaPIVA(f.pIva.value)
        if (msg!='')      
        {
            alert(msg);
			f.pIva.focus();
            return false;
        }
		msg = ControllaPIVA(f.codFisc.value)
		if (msg!='')
		{
			msg = ControllaCF(f.codFisc.value)
			if (msg!='')      
        	{
				alert(msg);
				f.codFisc.focus();
				return false;
			}
        }
        if (isBlank(f.address.value))       
        {
            msg = "Specificare l'indirizzo";
            alert(msg);
			f.address.focus();
            return false;
        }
    	if (isBlank(f.CAP.value))       
        {
            msg = "Specificare il codice di avviamento postale (CAP)";
            alert(msg);
			f.CAP.focus();
            return false;
        }
        if (isValidChar(f.CAP.value))       
        {
            msg = "Il campo CAP contiene il carattere ' che non è un carattere valido ";
            alert(msg);
			f.CAP.focus();
            return false;
        }
        if (!isOnlyNumber(f.CAP.value))     
        {
            msg = "Il CAP contiene dei caratteri non numerici";
            alert(msg);
			f.CAP.focus();
            return false;
        }
    	if (isBlank(f.CITTA.value))     
        {
            msg = "Specificare Città";
            alert(msg);
			f.CITTA.focus();
            return false;
        }
    	if (isBlank(f.provincia.value))     
        {
            msg = "Selezionare Provincia";
            alert(msg);
			f.provincia.focus();
            return false;
        }
   		if (isBlank(f.Tel.value))       
        {
            msg = "Specificare il numero di telefono";
            alert(msg);
			f.Tel.focus();
            return false;
        }
        if (!isOnlyPhoneNumber(f.Tel.value))     
        {
            msg = "Il campo Telefono contiene dei caratteri non ammessi";
            alert(msg);
			f.Tel.focus();
            return false;
        }
        if (isBlank(f.Fax.value))       
        {
            msg = "Specificare il numero di fax";
            alert(msg);
			f.Fax.focus();
            return false;
        }
        if (!isOnlyPhoneNumber(f.Fax.value))     
        {
            msg = "Il campo Fax contiene dei caratteri non ammessi";
            alert(msg);
			f.Fax.focus();
            return false;
        }
        if (isBlank(f.responsabile.value))      
        {
            msg = "Specificare un resposabile acquisti o una persona di riferimento";
            alert(msg);
			f.responsabile.focus();
            return false;
        }
        if (isBlank(f.emailComm.value))       
        {
            msg = "Specificare un indirizzo e-mail";
            alert(msg);
			f.emailComm.focus();
            return false;
        }
        if (!checkEmail(f.emailComm.value))       
        {
            msg = "L'indirizzo specificato non è un indirizzo e-mail valido";
            alert(msg);
			f.emailComm.focus();
            return false;
        }
        if (f.emailComm.value.toUpperCase() != f.emailComm2.value.toUpperCase())
        {
            msg = "L'indirizzo email non è stato ripetuto correttamente.";
            alert(msg);
			f.emailComm2.focus();
            return false;
        }
		if (f.password.value.length < 6)       
        {
            msg = "Scegliere una password di almeno 6 caratteri";
            alert(msg);
			f.password.focus();
            return false;
        }
		if (f.password.value != f.password2.value)       
		{
			msg = "La password non è stata ripetuta correttamente.";
			alert(msg);
			f.password2.focus();
			return false;
		}
        if (f.cartaCredito[0].checked==0 && f.cartaCredito[1].checked==0)     
        {
            msg = "Specificare se si è in possesso di una carta di credito aziendale";
            alert(msg);
			f.cartaCredito[0].focus();
            return false;
        }
        if (f.tipologia[0].checked==0 && 
			f.tipologia[1].checked==0 && 
			f.tipologia[2].checked==0 &&
			f.tipologia[3].checked==0 &&
			f.tipologia[4].checked==0 &&
			f.tipologia[5].checked==0 && 
			f.tipologia[6].checked==0)
        {
            msg = "Specificare la tipologia della propria attività";
            alert(msg);
			f.tipologia[0].focus();
            return false;
        }
        if (f.softwareFinson[0].checked==0 && f.softwareFinson[1].checked==0)     
        {
            msg = "Specificare se ha già trattato software Finson";
            alert(msg);
			f.softwareFinson[0].focus();
            return false;
        }               

        return true;
    }// end check

function checkFormModPwd(f)
    {
      var msg ;

      if (isBlank(f.imagecheck.value))
	  { 
	    alert(msgNoData); 
		f.imagecheck.focus(); 
		return false; 
	  }

	  if (f.oldPassword.value.length = 0)
	  {
		alert(msgNoData);
		f.oldPassword.focus();
		return (false);
	  }
	  
	  if (f.newPassword.value.length < 6)
	  {
		msg = "Scegli una password di almeno 6 caratteri."; //"<%=getMsg(10035, )%>";
		alert(msg);
		f.newPassword.focus();
		return (false);
	  }
	  
	  if (f.newPassword.value != f.newPassword2.value)
	  {
		msg = "La password non è stata ripetuta correttamente."; //"<%=getMsg(10036, )%>";
		alert(msg);
		f.newPassword2.value = ""
		f.newPassword2.focus();
		return (false);
	  }

      return true;
    }// end check

function checkCustomerModForm(f)
    {
        var msg ;
        if (isBlank(f.customerName.value) && isBlank(f.lastName.value) && isBlank(f.customerCompany.value)) 
        {
            msg = "E' necessario inserire il nome e cognome o la ragione sociale della propria azienda."; //"<%=getMsg(10028, )%>";
            alert(msg);
			f.customerName.focus();
            return false;
        }
        if (!isOnlyPhoneNumber(f.phone.value))     
        {
            alert(msgCharNotCorrect);
			f.phone.focus();
            return false;
        }
        if (!isOnlyPhoneNumber(f.user3.value))     
        {
            alert(msgCharNotCorrect);
			f.user3.focus();
            return false;
        }
    	if (isBlank(f.countryCode.value))     
        {
            alert(msgNoData);
			f.countryCode.focus();
            return false;
        }
        if (isBlank(f.address.value))       
        {
            alert(msgNoData);
			f.address.focus();
            return false;
        }
    	if (isBlank(f.zip.value))       
        {
            alert(msgNoData);
			f.zip.focus();
            return false;
        }
    	if (isBlank(f.city.value))
        {
            alert(msgNoData);
			f.city.focus();
            return false;
        }
		if (f.needState.value=="true") {
			if (f.useStateCode.value=="true") {
				if (isBlank(f.stateCode.value)) {
					alert(msgNoData);
					f.stateCode.focus();
					return false;
				}
			}
		}
		if (f.countryCode.value.toUpperCase() == "IT"){
			if (!isBlank(f.user2.value))      
			{
				msg = ControllaPIVA(f.user2.value);
				if (msg!='')      
				{
					alert(msg);
					f.user2.focus();
					return false;
				}
			}
			if (!isBlank(f.user1.value))      
			{
				msg = ControllaCF(f.user1.value);
				if (msg!='')      
				{
					alert(msg);
					f.user1.focus();
					return false;
				}
			}
		}

        return true;
    }// end check

	
function checkOrderCheckout2(f)
    {
        var msg ;
		f.vatNumberKind.Value = "";

		if (isBlank(f.vatNumber.value))
		{
			alert("Inserire il numero della Partita IVA oppure del Codice Fiscale. Grazie.");
			f.vatNumber.focus();
			return false;
		}
		else
		{
			if (f.countryCode.value!="IT")     
			{
				if (f.vatNumber.Length < 5)
				{
					alert("Il numero della Partita IVA o del Codice Fiscale non è stato inserito correttamente!");
					f.vatNumber.focus();
					return false;
				}
				else
				{
					f.vatNumberKind.Value = "EE";
				}
			}
			else
			{
				msg = ControllaPIVA(f.vatNumber.value);
				if (msg!="")      
				{
					msg = ControllaCF(f.vatNumber.value);
					if (msg!="")      
					{
						alert("Il numero della Partita IVA o del Codice Fiscale non è stato inserito correttamente!");
						f.vatNumber.focus();
						return false;
					}
					else
					{
						f.vatNumberKind.Value = "CF";
					}
				}
				else
				{
					f.vatNumberKind.Value = "PI";
				}
			}
		}
		
        return true;
    }// end check

function checkFormModEmail(f)
    {
      var msg ;

      if (isBlank(f.imagecheck.value))
	  { 
	    alert(msgNoData); 
		f.imagecheck.focus(); 
		return false; 
	  }

	  if (f.oldEmail.value.length = 0)
	  {
		alert(msgNoData);
		f.oldEmail.focus();
		return false;
	  }
	  
      if (!checkEmail(f.oldEmail.value))       
      {
          msg = "L'indirizzo specificato non è un indirizzo e-mail valido."; //"<%=getMsg(10032, )%>";
          alert(msg);
		  f.oldEmail.focus();
        return false;
      }

	  if (f.newEmail.value.length = 0)
	  {
		alert(msgNoData);
		f.newEmail.focus();
		return false;
	  }
	  
      if (!checkEmail(f.newEmail.value))       
      {
          msg = "L'indirizzo specificato non è un indirizzo e-mail valido."; //"<%=getMsg(10032, )%>";
          alert(msg);
		  f.newEmail.focus();
        return false;
      }

  	  if (f.newEmail.value.toUpperCase() != f.newEmail2.value.toUpperCase())       
	  {
		msg = "L'indirizzo email non è stato ripetuto correttamente."; //"<%=getMsg(10034, )%>";
		alert(msg);
		f.newEmail2.value = ""
		f.newEmail2.focus();
		return false;
	  }

      return true;
    }// end check


