	var busy = 0;
	
	function setCustomFault(faultcode, faultstring, detail)
	{
	
		var s = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>";
		s += "<Fault>";
		s += "<faultcode>" + faultcode + "</faultcode>";
		s += "<faultstring>" + faultstring + "</faultstring>";
		s += "<detail>" + detail + "</detail>";
		s += "</Fault>";
		
		return s;
		
	} 
  
	function setCustomVATCheck(countryCode, vatNumber)
	{
		var s = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>";
		s += "<VATData>";
		s += "<countryCode>" + countryCode + "</countryCode>";
		s += "<vatNumber>" + vatNumber + "</vatNumber>";
		s += "<valid>true</valid>";
		s += "</VATData>";
		
		return s;
	}

	function ValidateVAT(vatElement, callback) {
  
		//countryCode: [A-Z]{2} 
		//VATnumber:   [0-9A-Za-z\+\*\.]{2,12}

		//basic checks
		var vat = vatElement.value;
		//alert(vat);
		var countryCode = vat.substr(0,2).toUpperCase();
		//alert(countryCode);
		var vatNumber = vat.substr(2);			
		
		var xml = "";
		
		if (vat == null || vat.length < 3)
		{
			xml = setCustomFault("Client.userException", "VAT.FormatError{ 'ABORT_VAT_FULFILMENT' }", "Waarschijnlijk geen intentie tot invullen van BTW nummer");
			callback(xml);
			return;			
		}
				
		re = /^[A-Z]{2}$/
		if (re.exec(countryCode) == null)
		{
			//alert('error in countryCode');
			xml = setCustomFault("Client.userException", "CountryCode.FormatError{ 'INVALID_FORMAT' }", "Country code formaat is 2 hoofdletters");
			callback(xml);
			return;
		}
		
		if (countryCode.toUpperCase() == "NL")
		{
			xml = setCustomFault("Client.userException", "CountryCode.NotAllowedError{ 'VATNUMBER_NOT_ALLOWED_FOR_NL' }", "In Nederland dient btw te worden afgedragen.");
			callback(xml);
			return;
		}
		
		re = /^[0-9A-Za-z\+\*\.]{2,12}$/
		if ( re.exec(vatNumber) == null)
		{
			//alert('error in vatNumber');
			xml = setCustomFault("Client.userException", "VATNumber.FormatError{ 'INVALID_FORMAT' }", "VAT number niet correct");
			callback(xml);
			return;
		}

		//for now skip checking the VAT against the webservice
		xml = setCustomVATCheck(countryCode, vatNumber);
		callback(xml);
		return;
		
		//format allright, let's check the VAT against the webservice
		if (busy==0) { 
			
			var placeholder = document.getElementById("VATERROR");
			placeholder.innerHTML = "<img class=\"imgInfo\" src=\"images/ajax-loader.gif\" width=\"20\" height=\"20\"/>";
			
			 var xmlHttp = createXMLHttpRequest();
			 if (xmlHttp == null) return;
			 
			 var query = createVATSOAPRequest(countryCode, vatNumber);
			 if (query == null) return;
		  
			busy = 1;
			var url = "http://ec.europa.eu/taxation_customs/vies/api/checkVatPort";
			
			try { 
				xmlHttp.open("POST", url, true);
			}
			catch (e)
			{
				xml = setCustomFault("Client.userException", "XMLHttpRequest.Error{ 'OPEN_FAILED' }", "Connectie met webservice niet mogelijk.");
				placeholder.innerHTML = "";
				callback(xml);
				return;			
			}
			
			xmlHttp.onreadystatechange = function()
			{
				if(xmlHttp.readyState==4)
				  {
					callback(xmlHttp.responseText);
					busy = 0;
				  }
			};
				//xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
			var sSOAPAction = "http://ec.europa.eu/taxation_customs/vies/api/checkVatPort#checkVat";

			xmlHttp.setRequestHeader("SOAPAction", sSOAPAction);
			xmlHttp.setRequestHeader("Content-Type", "text/xml");
				
			//alert("send query: " + query);
			xmlHttp.send(query);
		}
	}
  
	function createVATSOAPRequest(countryCode, vatNumber)
	{
		soapReq = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>";
		soapReq += "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:apachesoap=\"http://xml.apache.org/xml-soap\" xmlns:impl=\"urn:ec.europa.eu:taxud:vies:services:checkVat\" xmlns:intf=\"urn:ec.europa.eu:taxud:vies:services:checkVat\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:tns1=\"urn:ec.europa.eu:taxud:vies:services:checkVat:types\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:wsdlsoap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
		soapReq += "<SOAP-ENV:Body>";
		soapReq += "<mns1:checkVat xmlns:mns1=\"urn:ec.europa.eu:taxud:vies:services:checkVat\">";
		soapReq += "<countryCode>" + countryCode + "</countryCode>";
		soapReq += "<vatNumber>" + vatNumber + "</vatNumber>";
		soapReq += "</mns1:checkVat>";
		soapReq += "</SOAP-ENV:Body>";
		soapReq += "</SOAP-ENV:Envelope>";
		
		return soapReq;
	} 	

	 function createXMLDocumentFromString(text)
	{
		try //Internet Explorer
		  {
		  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		  xmlDoc.async="false";
		  xmlDoc.loadXML(text);
		  }  
		catch(e)
		  {
		  try // Firefox, Mozilla, Opera, etc.
			{
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(text,"text/xml");
			}
		  catch(e)
			{
			alert(e.message);
			return;
			}
		  }
		return xmlDoc;
	}
	
	function createXMLDocument()
	{
		var xmldoc = null;
		try
		{
			xmldoc = new ActiveXObject("Microsoft.XMLDOM");
			
		}
		catch(e)
		{
			try 
			{
				xmldoc = new DOMDocument();
			}
			catch(e)
			{
				alert(e.message);
				return;
			}
		}
		return xmldoc;
	}
	
	function createXMLHttpRequest(){
		var xmlHttp = null;
		try
		  {  // Firefox, Opera 8.0+, Safari  
			xmlHttp=new XMLHttpRequest();  
		  }
		catch (e)
		  {  // Internet Explorer  
		  try
			{    
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
			}
		  catch (e)
			{    
			try
			  {      
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			  }
			catch (e)
			  {      
				alert("Your browser does not support AJAX!");   
				//return false;
			  }    
			}   
		}
		return xmlHttp;
	}