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.")%>";

var msgLungErrCF = "La lunghezza del Codice Fiscale non è corretta: dovrebbe essere lungo esattamente 16 caratteri.";
var msgCarNonValCF1 = "Il codice fiscale contiene un carattere non valido '";
var msgCarNonValCF2 = "'.\nI caratteri validi sono le lettere e le cifre.\n";
var msgErrCodCtrlCF = "Il codice fiscale non è corretto:\n il codice di controllo non corrisponde."; //"<%=getMsg(10038, )%>";

var msgLungErrPIVA = "La lunghezza della partita IVA non è corretta: dovrebbe essere lunga esattamente 11 caratteri."; //"<%=getMsg(10039, )%>";
var msgCarNonValPIVA1 = "La partita IVA contiene un carattere non valido '";
var msgCarNonValPIVA2 = "'.\nI caratteri validi sono le cifre.\n";
var msgErrCodCtrlPIVA = "La partita IVA non è valida: il codice di controllo non corrisponde."; //"<%=getMsg(10040, )%>";

var msgNeedEmail = "È obbligatorio specificare un indirizzo e-mail. Verrà usato in seguito per le comunicazioni e come login."; //"<%=getMsg(10031, )%>";
var msgInvalidEmail = "L'indirizzo specificato non è un indirizzo e-mail valido."; //"<%=getMsg(10032, )%>";
var msgErrRepeatEmail = "L'indirizzo email non è stato ripetuto correttamente."; //"<%=getMsg(10034, )%>";
					
var msgErrLungPwd = "Scegli una password lunga da 6 a 15 caratteri."; //"<%=getMsg(10035, )%>";
var msgErrRepPwd = "La password non è stata ripetuta correttamente."; //"<%=getMsg(10036, )%>";
var msgNeedName = "È necessario inserire il nome e cognome o la ragione sociale della propria azienda."; //"<%=getMsg(10028, )%>";
var msgNoValidPIoCF = "Il numero della Partita IVA o del Codice Fiscale non è stato inserito correttamente!";
var msgNeedPIoCF = "Inserire il numero della Partita IVA oppure del Codice Fiscale. Grazie.";

	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 msgLungErrCF;
		validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		for( i = 0; i < 16; i++ ){
			if( validi.indexOf( cf.charAt(i) ) == -1 )
				return msgCarNonValCF1 + cf.charAt(i) + msgCarNonValCF2;
		}
		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 msgErrCodCtrlCF;
		return "";
	}
	
	
	function ControllaPIVA(pi)
	{
		if( pi == '' )  return '';
		if( pi.length != 11 )
			return msgLungErrPIVA;
		validi = "0123456789";
		for( i = 0; i < 11; i++ ){
			if( validi.indexOf( pi.charAt(i) ) == -1 )
				return msgCarNonValPIVA1 + pi.charAt(i) + msgCarNonValPIVA2;
		}
		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 msgErrCodCtrlPIVA;
		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.imagecheck.value))       
        {
            alert(msgNoData);
			f.imagecheck.focus();
            return false;
        }
		if (isBlank(f.email.value))
        {
            alert(msgNeedEmail);
			f.email.focus();
            return false;
        }
        if (!checkEmail(f.email.value))       
        {
            alert(msgInvalidEmail);
			f.email.focus();
            return false;
        }
        if (f.email.value.toUpperCase() != f.email2.value.toUpperCase())       
        {
            alert(msgErrRepeatEmail);
			f.email2.focus();
            return false;
        }
		if (isBlank(f.countryCode.value))     
        {
            alert(msgNoData);
			f.countryCode.focus();
            return false;
        }
		
		/*
		 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 (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;
				}
			}
		}
		if (pwd == 0)
		{
			if (f.password.value.length < 6)       
			{
				msg = "Scegli una password di almeno 6 caratteri."; //"<%=getMsg(10035, )%>";
				alert(msg);
				f.password.focus();
				return false;
			}
			if (f.password.value != f.password2.value)       
			{
				msg = "La password non è stata ripetuta correttamente."; //"<%=getMsg(10036, )%>";
				alert(msg);
				f.password2.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 (!isOnlyPhoneNumber(f.phone.value))     
        {
            alert(msgCharNotCorrect);
			f.phone.focus();
            return false;
        }
        if (!isOnlyPhoneNumber(f.user3.value))     
        {
            alert(msgCharNotCorrect);
			f.user3.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) || (f.newPassword.value.length > 15))
	  {
		alert(msgErrLungPwd);
		f.newPassword.focus();
		return (false);
	  }
	  
	  if (f.newPassword.value != f.newPassword2.value)
	  {
		alert(msgErrRepPwd);
		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)) 
        {
            alert(msgNeedName);
			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(msgNeedPIoCF);
			f.vatNumber.focus();
			return false;
		}
		else
		{
			if (f.countryCode.value!="IT")     
			{
				if (f.vatNumber.Length < 5)
				{
					alert(msgNoValidPIoCF);
					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(msgNoValidPIoCF);
						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))       
      {
          alert(msgInvalidEmail);
		  f.oldEmail.focus();
        return false;
      }

	  if (f.newEmail.value.length = 0)
	  {
		alert(msgNoData);
		f.newEmail.focus();
		return false;
	  }
	  
      if (!checkEmail(f.newEmail.value))       
      {
          alert(msgInvalidEmail);
		  f.newEmail.focus();
        return false;
      }

  	  if (f.newEmail.value.toUpperCase() != f.newEmail2.value.toUpperCase())       
	  {
		alert(msgErrRepeatEmail);
		f.newEmail2.value = ""
		f.newEmail2.focus();
		return false;
	  }

      return true;
    }// end check


