//------------------------------------------------------------------------------------------
//---  Validazione del nome a dominio
//------------------------------------------------------------------------------------------

function check_domain(dom, ext) {
	var eext = ext.toLowerCase();
	if (eext==".name") return is_valid_name(dom,ext);
	return is_valid_domain_name(dom,ext);
}

function is_valid_domain_name(dom,ext) {
	// Procedura che verifica un dominio generico
	var st = String(dom);
	// Il dominio deve contenere solo numeri 0-9 o lettere o "-"
	for (i = 0; i <=st.length-1 ; i++){
    	if (!isDigit( st.charAt(i) ) ){
        	if (!isLetter( st.charAt(i) ) ) return false
      	}
   	}
	min_len=4;
	if (ext==".eu" || ext=='.it') min_len=3;
	// Il dominio deve avere una lunghezza di almeno 3 caratteri
	if (st.length<min_len) return false;
	// Il primo carattere non può essere il "-"
	if (st.substring(0,1)=="-") return false;
	// L'ultimo carattere non può essere il "-"
	if (st.substring(st.length-1,st.length) == "-") return false;
	// Controllo passato
	return true;
}

function is_valid_name(dom,ext) {
	// Procedura che verifica un dominio con estensione .name
	var st = String(dom);
	dot_count=0;
	for (i=0; i <=st.length-1; i++){
	    if (st.charAt(i)!=".") {
			if (!isDigit(st.charAt(i))) {
				if (!isLetter(st.charAt(i))) return false;
			}
		} else {
			dot_count++;
		}
	}
	// Non ci possono essere più di un punto
	if (dot_count>1) {return false;}
	// Il dominio deve avere una lunghezza di almeno 3 caratteri
	if (st.length<4) {return false;}
	// Il primo carattere non può essere il "-"
	if (st.substring(0,1)=="-") {return false;}
	// L'ultimo carattere non può essere il "-"
	if (st.substring(st.length-1,st.length) == "-") {return false;}
	// Controllo passato
	return true;
}
//------------------------------------------------------------------------------------------
//---  Fine Validazione del nome a dominio
//------------------------------------------------------------------------------------------


function isValidIPAddress(ipaddr) {
   var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
   if (re.test(ipaddr)) {
      var parts = ipaddr.split(".");
      if (parseInt(parseFloat(parts[0])) == 0) { return false; }
      for (var i=0; i<parts.length; i++) {
         if (parseInt(parseFloat(parts[i])) > 255) { return false; }
      }
      return true;
   } else {
      return false;
   }
}

/*
	http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C0800602A52
*/
function isValidDate(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || _
      (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
   } else { // The year must be third
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
   }
   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else _
      if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else _
      if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else _
      if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}




function OpenFAQ( Page )
{
	mywin = window.open( '/FAQ/' + Page + '.asp', 'win', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=350,height=375' );
	mywin.focus();
}

function OpenURL( Page )
{
	mywin = window.open( Page, 'win', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=450,height=375' );
	mywin.focus();
}
//------------------------------------------------------------------------------------------
//---  Controlla che il carattere sia una lettera
//------------------------------------------------------------------------------------------
function isLetter(theChar)
{
   var charArray = new Array(
      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
      'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 
      'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 
      'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 
      'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 
      's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '-')

      for (j = 0; j < charArray.length; j++)
         if (theChar == charArray[j])
            return true
   return false
}

//------------------------------------------------------------------------------------------
//---  Controlla che il carattere sia un numero
//------------------------------------------------------------------------------------------
function isDigit(theDigit)
{
   var digitArray = new Array('0','1','2','3','4',
         '5','6','7','8','9')

      for (j = 0; j < digitArray.length; j++)
         if (theDigit == digitArray[j])
            return true
   return false
}

//------------------------------------------------------------------------------------------
//---  Controlla che la stringa contenga numeri e lettere
//------------------------------------------------------------------------------------------
function isAlphaNumeric(TheField)
{
   var myStringa = String(TheField.value)

   for (i = 0; i <=myStringa.length-1 ; i++){
      if (!isDigit( myStringa.charAt(i) ) ){
        if (!isLetter( myStringa.charAt(i) ) ) return false
      }
   }
   return true
}

//------------------------------------------------------------------------------------------
//---  Controlla che il campo inserito sia un numero
//------------------------------------------------------------------------------------------
function isNumber(TheField)
{
   var ThePhone = new String(TheField.value)
   var theChar
   
   for (var i = 0; i < ThePhone.length; i++)
   {
      theChar = ThePhone.charAt(i)
      if ( !isDigit(theChar) ) return false
   }
   return true
}

//------------------------------------------------------------------------------------------
//---  Controlla che il campo inserito sia un prezzo
//------------------------------------------------------------------------------------------
function isCurrency(TheField)
{
   //var TheValue = new String(TheField.value)
   //return /^\d+(\.\d)?$/.test(theValue)
}

//------------------------------------------------------------------------------------------
//---  Controlla la validità del numero di telefono
//------------------------------------------------------------------------------------------
function isValidPhone(TheField)
{
   var ThePhone = new String(TheField.value)
   var theChar
   for (var i = 0; i < ThePhone.length; i++)
   {
      theChar = ThePhone.charAt(i)
      if ( !isDigit(theChar) )
      {
         if ( !( (theChar == '(') || (theChar == ')') || (theChar == '-') || (theChar == '+')) ) return false
      }
   }
   return true
}

//------------------------------------------------------------------------------------------
//---  Controlla la validità dell'indirizzo e-mail
//------------------------------------------------------------------------------------------
function isEmail(TheField)
{
   var theEmail = new String(TheField.value)
   var theChar

   if ( theEmail.indexOf(" ")>=0 ) return false
   PosET     = theEmail.indexOf("@")
   PosPnt    = theEmail.lastIndexOf(".")
   BeforeET  = theEmail.charAt(PosET-1)
   BeforePnt = theEmail.charAt(PosPnt-1)

   var cont = 0
   for (j = theEmail.length; j >=0 ; j--){
      if (theEmail.charAt(j)!=".") 
         cont+=1
      else 
         break
   }

  if ( (PosET==-1)||(PosPnt==-1)||(BeforeET=="")||(BeforePnt=="@")||(PosET>PosPnt)||(cont-1<2) ) return false

   return true
}

//------------------------------------------------------------------------------------------
//---  Controlla la validità del Social Security Number USA
//------------------------------------------------------------------------------------------
function isValidSSN(value) { 
    var re = /^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/; 
    if (!re.test(value)) { return false; } 
    var temp = value; 
    if (value.indexOf("-") != -1) { temp = (value.split("-")).join(""); } 
    if (value.indexOf(" ") != -1) { temp = (value.split(" ")).join(""); } 
    if (temp.substring(0, 3) == "000") { return false; } 
    if (temp.substring(3, 5) == "00") { return false; } 
    if (temp.substring(5, 9) == "0000") { return false; } 
    return true; 
}


/*==============================================================================
Description:   Used to check the validity of a UK National Insurance Number
Parameters:    toCheck - National insurance number to be checked. 
This function checks the validty of the supplied number. 

If the number is found to be in a valid format, the function returns a value of 
true, otherwise a value of false.

See http://www.govtalk.gov.uk/gdsc/html/frames/NationalInsuranceNumber-2-1-Release.htm
for a formal specification.
  
Example call:
  
  if (checkNINO (myNINO) {
    alert ("National Insurance Number has a valid format")
  } 
  else {alert ("National Insurance Number has invalid format")};
                    
------------------------------------------------------------------------------*/
function checkNINO (toCheck) {

  var valid = false;
  var exp = /^[A-CEGHJ-NOPR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[ABCD\s]{1}/
  if (toCheck.match(exp)) {
    valid = true;
  }
  exp = /(^GB)|(^BG)|(^NK)|(^KN)|(^TN)|(^NT)|(^ZZ).+/
  if (toCheck.match(exp)) {
    valid = false;
  }
  
  // Return with the error status
  return valid;
}





//------------------------------------------------------------------------------------------
//---  Controlla la lunghezza minima del campo
//------------------------------------------------------------------------------------------
function isMinLength(TheField, minLength) {
	if (TheField.value.length >= minLength)
		return true
	else {
		return false
	     }
}

function isMaxLength(TheField, maxLength) {
	if (TheField.value.length <= maxLength)
		return true
	else {
		return false
	     }
}


//------------------------------------------------------------------------------------------
//---  Controlla la lunghezza minima del campo
//------------------------------------------------------------------------------------------
function Hilite(TheField, msg) {
	TheField.focus()
	TheField.select()
	alert(msg)
	return false
}





/**************************************************************************************/
/*
	Questa è la funzione principale per la verifica della partita iva per gli stati UE
	Da qui in giu ci sono tutte le funzioni per ogni singola nazione
*/
function isValidVat(TheValue)
{
	var TheValue = TheValue.toUpperCase();
	var cCode = TheValue.slice (0,2);
	switch (cCode) {
		case "AT": return isValidVat_AT(TheValue); break;
		case "BE": return isValidVat_BE(TheValue); break;
		case "BG": return isValidVat_BG(TheValue); break;
		case "CY": return isValidVat_CY(TheValue); break;
		case "CZ": return isValidVat_CZ(TheValue); break;
		case "DK": return isValidVat_DK(TheValue); break;
		case "EE": return isValidVat_EE(TheValue); break;
		case "FI": return isValidVat_FI(TheValue); break;
		case "FR": return isValidVat_FR(TheValue); break;
		case "DE": return isValidVat_DE(TheValue); break;
		case "EL": return isValidVat_EL(TheValue); break;
		case "HU": return isValidVat_HU(TheValue); break;
		case "IE": return isValidVat_IE(TheValue); break;
		case "IT": return isValidVat_IT(TheValue); break;
		case "LV": return isValidVat_LV(TheValue); break;
		case "LT": return isValidVat_LT(TheValue); break;
		case "LU": return isValidVat_LU(TheValue); break;
		case "MT": return isValidVat_MT(TheValue); break;
		case "NL": return isValidVat_NL(TheValue); break;
		case "PL": return isValidVat_PL(TheValue); break;
		case "PT": return isValidVat_PI(TheValue); break;
		case "RO": return isValidVat_RO(TheValue); break;
		case "SK": return isValidVat_SK(TheValue); break;
		case "SI": return isValidVat_SI(TheValue); break;
		case "ES": return isValidVat_ES(TheValue); break;
		case "SE": return isValidVat_SE(TheValue); break;
		case "GB": return isValidVat_GB(TheValue); break;
	}
	return true;
}

/*
	Verific la validità di una partita iva dell'austria
*/
function isValidVat_AT(vatnumber)
{
	var re = /^(AT)U(\d{8})$/;
	if (!re.test(vatnumber)) return false;
   
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number

	// Checks the check digits of an Austrian VAT number.
	var total = 0;
	var multipliers = [1,2,1,2,1,2,1];
	var temp = 0;
	
	// Extract the next digit and multiply by the appropriate multiplier.  
	for (var i = 0; i < 7; i++) {
	temp = Number(cNumber.charAt(i)) * multipliers[i];
	if (temp > 9)
	  total = total + Math.floor(temp/10) + temp%10
	else
	  total = total + temp;
	}  
	
	// Establish check digit.
	total = 10 - (total+4) % 10; 
	if (total == 10) total = 0;
	
	// Compare it with the last character of the VAT number. If it is the same, 
	// then it's a valid check digit.
	if (total == cNumber.slice (7,8)) 
		return true
	else 
		return false;
}

function isValidVat_BE(vatnumber)
{
	var re = /^(BE)(\d{9,10})$/;
	if (!re.test(vatnumber)) return false;
   
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number

	// Checks the check digits of a Belgium VAT number.
	
	// First character of 10 digit numbers should be 0
	if (cNumber.length == 10 && cNumber.slice(0,1) != "0") return false;
	
	// Nine digit numbers have a 0 inserted at the front.
	if (cNumber.length == 9) cNumber = "0" + cNumber;
	
	// Modulus 97 check on last nine digits
	if (97 - cNumber.slice (0,8) % 97 == cNumber.slice (8,10)) 
		return true
	else 
		return false;
}

function isValidVat_CY(vatnumber)
{
	var re = /^(CY)(\d{8}[A-Z])$/;
	if (!re.test(vatnumber)) return false;
   
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number

	// Checks the check digits of a Cypriot VAT number.
	
	// Extract the next digit and multiply by the counter.
	var total = 0;
	for (var i = 0; i < 8; i++) {
	var temp = Number(cNumber.charAt(i));
	if (i % 2 == 0) {
	  switch (temp) {
		case 0: temp = 1; break;
		case 1: temp = 0; break;
		case 2: temp = 5; break;
		case 3: temp = 7; break;
		case 4: temp = 9; break;
		default: temp = temp*2 + 3;
	  }
	}
	total = total + temp;
	}
	
	// Establish check digit using modulus 26, and translate to char. equivalent.
	total = total % 26;
	total = String.fromCharCode(total+65);
	
	// Check to see if the check digit given is correct
	if (total == cNumber.substr (8,1)) 
		return true 
	else 
		return false;
}

function isValidVat_CZ(vatnumber)
{
	var re = /^(CZ)(\d{8,10})(\d{3})?$/;
	if (!re.test(vatnumber)) return false
   
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number

	// Checks the check digits of a Czech Republic VAT number.
	var total = 0;
	var multipliers = [8,7,6,5,4,3,2];
	
	// Only do check digit validation for standard VAT numbers
	if (cNumber.length != 8) return true;
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 7; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digit.
	total = 11 - total % 11;
	if (total == 10) total = 0; 
	if (total == 11) total = 1; 
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (7,8)) 
		return true
	else 
		return false;
}

function isValidVat_DE(vatnumber)
{
	var re = /^(DE)(\d{9})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number

	// Checks the check digits of a German VAT number.
	var product = 10;
	var sum = 0;     
	var checkdigit = 0;                      
	for (var i = 0; i < 8; i++) {
	
	// Extract the next digit and implement perculiar algorithm!.
	sum = (Number(cNumber.charAt(i)) + product) % 10;
	if (sum == 0) {sum = 10};
	product = (2 * sum) % 11;
	}
	
	// Establish check digit.  
	if (11 - product == 10) {checkdigit = 0} else {checkdigit = 11 - product};
	
	// Compare it with the last two characters of the VAT number. If the same, then it is a valid check digit.
	if (checkdigit == cNumber.slice (8,9))
		return true
	else 
		return false;
}

function isValidVat_DK(vatnumber)
{
	var re = /^(DK)((\d{8}))$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number

	// Checks the check digits of a Danish VAT number.
	var total = 0;
	var multipliers = [2,7,6,5,4,3,2,1];
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 8; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digit.
	total = total % 11;
	
	// The remainder should be 0 for it to be valid..
	if (total == 0) 
		return true
	else 
		return false;
}

function isValidVat_EE(vatnumber)
{
	var re = /^(EE)(\d{9})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number
	
	// Checks the check digits of an Estonian VAT number.
	var total = 0;
	var multipliers = [3,7,1,3,7,1,3,7];
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 8; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digits using modulus 10.
	total = 10 - total % 10;
	if (total == 10) total = 0;
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (8,9))
		return true
	else 
		return false;
}

function isValidVat_EL(vatnumber)
{
	var re1 = /^(EL)(\d{8,9})$/;
	var re2 = /^(GR)(\d{8,9})$/;
	if (!re1.test(vatnumber) && !re2.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	
	
	// Checks the check digits of a Greek VAT number.
	var total = 0;
	var multipliers = [256,128,64,32,16,8,4,2];
	
	//eight character numbers should be prefixed with an 0.
	if (cNumber.length == 8) {cNumber = "0" + cNumber};
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 8; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digit.
	total = total % 11;
	if (total > 9) {total = 0;};  
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (8,9)) 
		return true
	else 
		return false;
}

function isValidVat_ES(vatnumber)
{
	var re1 = /^(ES)([A-Z]\d{8})$/;
	var re2 = /^(ES)(\d{8}[A-Z])$/;
	var re3 = /^(ES)([A-Z]\d{7}[A-Z])$/;
	if (!re1.test(vatnumber) && !re2.test(vatnumber) && !re3.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number
	
	// Checks the check digits of a Spanish VAT number.
	var total = 0; 
	var temp = 0;
	var multipliers = [2,1,2,1,2,1,2];
	var esexp = new Array ();
	esexp.push (/^[A-H]\d{8}$/);
	esexp.push (/^[N|P|Q|S]\d{7}[A-Z]$/);
	esexp.push (/^[0-9]{8}[A-Z]$/);
	var i = 0;
  
	// With profit companies
	if (esexp[0].test(cNumber))
	{
		// Extract the next digit and multiply by the counter.
		for (i = 0; i < 7; i++) {
		  temp = Number(cNumber.charAt(i+1)) * multipliers[i];
		  if (temp > 9) 
			total = total + Math.floor(temp/10) + temp%10 
		  else 
			total = total + temp;
		}   
		
		// Now calculate the check digit itself. 
		total = 10 - total % 10;
		if (total == 10) {total = 0;}
		
		// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
		if (total == cNumber.slice (8,9)) 
		  return true
		else 
		  return false;
	}
  
	// Non-profit companies
	else if (esexp[1].test(cNumber)) {
		
		// Extract the next digit and multiply by the counter.
		for (i = 0; i < 7; i++) {
		  temp = Number(cNumber.charAt(i+1)) * multipliers[i];
		  if (temp > 9) 
			total = total + Math.floor(temp/10) + temp%10 
		  else 
			total = total + temp;
		}    
		
		// Now calculate the check digit itself.
		total = 10 - total % 10;
		total = String.fromCharCode(total+64);
		
		// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
		if (total == cNumber.slice (8,9)) 
		  return true
		else 
		  return false;
	}
	
	// Personal number (NIF)
	else if (esexp[2].test(cNumber)) {
		return cNumber.charAt(8) == 'TRWAGMYFPDXBNJZSQVHLCKE'.charAt(Number(cNumber.substring(0, 8)) % 23);
	}
	
  else return true;
}


function isValidVat_FI(vatnumber)
{
	var re = /^(FI)(\d{8})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number

	// Checks the check digits of a Finnish VAT number.
	var total = 0; 
	var multipliers = [7,9,10,5,8,4,2];
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 7; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digit.
	total = 11 - total % 11;
	if (total > 9) {total = 0;};  
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (7,8)) 
		return true
	else 
		return false;
}

function isValidVat_FR(vatnumber)
{
	var re1 = /^(FR)(\d{11})$/;
	var re2 = /^(FR)[(A-H)|(J-N)|(P-Z)]\d{10}$/;
	var re3 = /^(FR)\d[(A-H)|(J-N)|(P-Z)]\d{9}$/;
	var re4 = /^(FR)[(A-H)|(J-N)|(P-Z)]{2}\d{9}$/;
	if (!re1.test(vatnumber) && !re2.test(vatnumber) && !re3.test(vatnumber) && !re4.test(vatnumber)) return false

    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number
	
	// Extract the last nine digits as an integer.
	var total = cNumber.substring(2); 
	
	// Establish check digit.
	total = (total*100+12) % 97;
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (0,2)) 
		return true
	else 
		return false;
}

function isValidVat_HU(vatnumber)
{
	var re = /^(HU)(\d{8})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number

	// Checks the check digits of a Hungarian VAT number.
	var total = 0;
	var multipliers = [9,7,3,1,9,7,3];
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 7; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digit.
	total = 10 - total % 10; 
	if (total == 10) total = 0;
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (7,8)) 
		return true
	else 
		return false;
}

function isValidVat_HU(vatnumber)
{
	var re1 = /^(IE)(\d{7}[A-W])$/;
	var re2 = /^(IE)([7-9][A-Z\*\+)]\d{5}[A-W])$/;
	if (!re1.test(vatnumber) && !re2.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	// Checks the check digits of an Irish VAT number.
	var total = 0; 
	var multipliers = [8,7,6,5,4,3,2];
	
	// If the code is in the old format, we need to convert it to the new.
	if (/^\d[A-Z\*\+]/.test(cNumber)) {
		cNumber = "0" + cNumber.substring(2,7) + cNumber.substring(0,1) + cNumber.substring(7,8);
	}
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 7; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digit using modulus 23, and translate to char. equivalent.
	total = total % 23;
	if (total == 0)
		total = "W"
	else
		total = String.fromCharCode(total+64);
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (7,8)) 
		return true
	else 
		return false;
}

function isValidVat_IT(vatnumber)
{
	var re = /^(IT)(\d{11})$/;
	if (!re.test(vatnumber)) return false

    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number
	  
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( cNumber.charAt(i) ) == -1 ) return false;
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += cNumber.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( cNumber.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != cNumber.charCodeAt(10) - '0'.charCodeAt(0) )
		return false;

	return true;
}

function isValidVat_LT(vatnumber)
{
	var re = /^(LT)(\d{9}|\d{12})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	// Checks the check digits of a Lithuanian VAT number.
	
	// Only do check digit validation for standard VAT numbers
	if (cNumber.length != 9) return true;
	
	// Extract the next digit and multiply by the counter+1.
	var total = 0;
	for (var i = 0; i < 8; i++) total = total + Number(cNumber.charAt(i)) * (i+1);
	
	// Can have a double check digit calculation!
	if (total % 11 == 10) {
	var multipliers = [3,4,5,6,7,8,9,1];
	total = 0;
	for (i = 0; i < 8; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	}
	
	// Establish check digit.
	total = total % 11;
	if (total == 10) {total = 0;}; 
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (8,9)) 
		return true
	else 
		return false;
}

function isValidVat_LU(vatnumber)
{
	var re = /^(LU)(\d{8})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	// Checks the check digits of a Luxembourg VAT number.
	if (vatnumber.slice (0,6) % 89 == cNumber.slice (6,8)) 
		return true
	else 
		return false;
}

function isValidVat_LV(vatnumber)
{
	var re = /^(LV)(\d{11})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	// Checks the check digits of a Latvian VAT number.
	// Only check the legal bodies
	if ((/^[0-3]/).test(cNumber)) return true; 
	
	var total = 0;
	var multipliers = [9,1,4,8,3,10,2,5,7,6];
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 10; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digits by getting modulus 11.
	if (total%11 == 4 && cNumber[0] ==9) total = total - 45;
	if (total%11 == 4) 
		total = 4 - total%11
	else if (total%11 > 4) 
		total = 14 - total%11
	else if (total%11 < 4) 
		total = 3 - total%11;
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (10,11)) 
		return true
	else 
		return false;
}

function isValidVat_MT(vatnumber)
{
	var re = /^(MT)(\d{8})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	// Checks the check digits of a Maltese VAT number.
	var total = 0;
	var multipliers = [3,4,6,7,8,9];
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 6; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digits by getting modulus 37.
	total = 37 - total % 37;
	
	// Compare it with the last character of the VAT number. If it is the same, 
	// then it's a valid check digit.
	if (total == cNumber.slice (6,8) * 1) 
		return true
	else 
		return false;
}

function isValidVat_NL(vatnumber)
{
	var re = /^(NL)(\d{9})B\d{2}$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	// Checks the check digits of a Dutch VAT number.
	var total = 0;
	var multipliers = [9,8,7,6,5,4,3,2];
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 8; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digits by getting modulus 11.
	total = total % 11;
	if (total > 9) {total = 0;};  
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (8,9)) 
		return true
	else 
		return false;
}

function isValidVat_PL(vatnumber)
{
	var re = /^(PL)(\d{10})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	// Checks the check digits of a Polish VAT number.
	var total = 0;
	var multipliers = [6,5,7,2,3,4,5,6,7];
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 9; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digits subtracting modulus 11 from 11.
	total = total % 11;
	if (total > 9) {total = 0;};
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (9,10)) 
		return true
	else 
		return false;
}

function isValidVat_PT(vatnumber)
{
	var re = /^(PT)(\d{9})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	// Checks the check digits of a Portugese VAT number.
	var total = 0;
	var multipliers = [9,8,7,6,5,4,3,2];
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 8; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digits subtracting modulus 11 from 11.
	total = 11 - total % 11;
	if (total > 9) {total = 0;};
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (8,9)) 
		return true
	else 
		return false;
}

function isValidVat_RO(vatnumber)
{
	var re = /^(RO)(\d{2,10})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	// Checks the check digits of a Romanian VAT number.
	var multipliers = [7,5,3,2,1,7,5,3,2,1];
	
	// Extract the next digit and multiply by the counter.
	var VATlen = cNumber.length;
	multipliers = multipliers.slice (10-VATlen);
	var total = 0;
	for (var i = 0; i < cNumber.length-1; i++) {
	total = total + Number(cNumber.charAt(i)) * multipliers[i];
	}  
	
	// Establish check digits by getting modulus 11.
	total = (10 * total) % 11;
	if (total == 10) total = 0; 
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (cNumber.length-1, cNumber.length))
		return true
	else 
		return false;
}

function isValidVat_SE(vatnumber)
{
	var re = /^(SE)(\d{10}\d[1-4])$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	// Checks the check digits of a Swedish VAT number.
	var total = 0;
	var multipliers = [2,1,2,1,2,1,2,1,2];
	var temp = 0;
	
	// Extract the next digit and multiply by the appropriate multiplier.
	for (var i = 0; i < 9; i++) {
		temp = Number(cNumber.charAt(i)) * multipliers[i];
		if (temp > 9)
		  total = total + Math.floor(temp/10) + temp%10
		else 
		  total = total + temp;
	}
	
	// Establish check digits by subtracting mod 10 of total from 10.
	total = 10 - (total % 10); 
	if (total == 10) total = 0;
	
	// Compare it with the 10th character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (9,10)) 
		return true
	else 
		return false;
}

function isValidVat_SK(vatnumber)
{
	var re = /^(SK)(\d{9}|\d{10})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	// Checks the check digits of a Slovak VAT number.
	var total = 0; 
	var multipliers = [8,7,6,5,4,3,2];
	
	// Extract the next digit and multiply by the counter.
	for (var i = 3; i < 9; i++) {
		total = total + Number(cNumber.charAt(i)) * multipliers[i-3];
	}  
	
	// Establish check digits by getting modulus 11.
	total = 11 - total % 11;
	if (total > 9) total = total - 10;  
	
	// Compare it with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (9,10)) 
		return true
	else 
		return false;
}

function isValidVat_SI(vatnumber)
{
	var re = /^(SI)(\d{8})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	// Checks the check digits of a Slovenian VAT number.
	var total = 0; 
	var multipliers = [8,7,6,5,4,3,2];
	
	// Extract the next digit and multiply by the counter.
	for (var i = 0; i < 7; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
	
	// Establish check digits by subtracting 97 from total until negative.
	total = 11 - total % 11;
	if (total > 9) {total = 0;};  
	
	// Compare the number with the last character of the VAT number. If it is the same, then it's a valid check digit.
	if (total == cNumber.slice (7,8)) 
		return true
	else 
		return false;
}

function isValidVat_GB(vatnumber)
{
	var re1 = /^(GB)(\d{9})$/;
	var re2 = /^(GB)(\d{10})$/;
	var re3 = /^(GB)(\d{12})$/;
	var re4 = /^(GB)(\d{13})$/;
	var re5 = /^(GB)(GD\d{3})$/;
	var re6 = /^(GB)(HA\d{3})$/;
	if (!re1.test(vatnumber) && !re2.test(vatnumber) && !re3.test(vatnumber) && !re4.test(vatnumber) && 
		!re5.test(vatnumber) && !re6.test(vatnumber) ) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	
	
	// Checks the check digits of a UK VAT number.
	var multipliers = [8,7,6,5,4,3,2];
	
	// Government departments
	if (cNumber.substr(0,2) == 'GD') {
		if (cNumber.substr(2,3) < 500) 
		  return true 
		else 
		  return false;
	}
	
	// Health authorities
	if (cNumber.substr(0,2) == 'HA') {
		if (cNumber.substr(2,3) > 499) 
		  return true 
		else 
		  return false;
	}
	
	// Standard and commercial numbers
	if (cNumber.length == 9 || cNumber.length == 10) {
		var total = 0;
		if (cNumber.length == 10 && cNumber.slice (9,10) != '3') return false;
		
		// Extract the next digit and multiply by the counter.
		for (var i = 0; i < 7; i++) total = total + Number(cNumber.charAt(i)) * multipliers[i];
		
		// Establish check digits by subtracting 97 from total until negative.
		while (total > 0) {total = total - 97;}    
		
		// Get the absolute value and compare it with the last two characters of the VAT number. If the same, then it is a valid check digit.
		total = Math.abs(total);
		if (total == cNumber.slice (7,9)) 
		  return true 
		else  
		  return false;
	}
	
	// We don't check 12 and 13 digit UK numbers - not only can we not find any, 
	// but the information found on the format is contradictory.
	
	return true;
}

function isValidVat_BG(vatnumber)
{
	var re = /^(BG)(\d{9,10})$/;
	if (!re.test(vatnumber)) return false
	
    var cCode = RegExp.$1;   	// Isolate country code
    var cNumber = RegExp.$2;	// Isolate the number	

	return true;
}
