function getXMLHttpRequestObject() {

	// Initialize the object:
	var ajax = false;

	// Choose object type based upon what's supported:
	if (window.XMLHttpRequest) {
	
		// IE 7, Mozilla, Safari, Firefox, Opera, most browsers:
		ajax = new XMLHttpRequest();
		
	} else if (window.ActiveXObject) { // Older IE browsers
	
		// Create type Msxml2.XMLHTTP, if possible:
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) { // Create the older type instead:
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { }
		}
		
	} // End of main IF-ELSE IF.
	
	// Return the value:
	return ajax;

} // End of getXMLHttpRequestObject() function.


FAjax = getXMLHttpRequestObject();

function JsFnAjaxAddressLookup()
{
	var aPostcodeLookup = document.getElementById('contactpostcode').value;
	var aPropertyLookup = document.getElementById('HouseNumber').value;
	
	if (aPostcodeLookup.length > 3)
	{
		FAjax.abort();
				
		// Open the connection:
		FAjax.open('get', FFileDirectory + 'getaddress_ajax.php?action=faddr&contactpostcode=' + encodeURIComponent(aPostcodeLookup)+'&contactproperty='+encodeURIComponent(aPropertyLookup));
					
		// Function that handles the response:
		FAjax.onreadystatechange = JsFnAjaxAddressLookupHandleResponse; 
		
		// Send the request:
		FAjax.send(null);
	}
}

function JsFnAjaxAddressLookupHandleResponse()
{
	if (FAjax.readyState == 4)
	{
		if ((FAjax.status == 200) || (FAjax.status == 304))
		{
			var aXMLData = FAjax.responseXML;
							
			var aAddressStreet = aXMLData.getElementsByTagName('Street');
			var aAddressLocality = aXMLData.getElementsByTagName('Locality');
			var aAddressTown = aXMLData.getElementsByTagName('Town');
			var aAddressCounty = aXMLData.getElementsByTagName('County');
			var aAddressPostcode = aXMLData.getElementsByTagName('Postcode');
			
			var aErrorMessage = aXMLData.getElementsByTagName('ERROR');
			
			var street = aAddressStreet[0].firstChild;
			var locality = aAddressLocality[0].firstChild;
			var town = aAddressTown[0].firstChild;
			var county = aAddressCounty[0].firstChild;
			var postcode = aAddressPostcode[0].firstChild;
												
			var addressStreet = document.getElementById('contactaddressline1');
			var addressLocality = document.getElementById('contactaddressline2');
			var addressTown = document.getElementById('contactaddressline3');
			var addressCounty = document.getElementById('contactaddressline4');
			var addressPostcode = document.getElementById('contactpostcode');
			var addressErrorDisplay = document.getElementById('addressError');
						
			if(aErrorMessage[0].firstChild != null && aErrorMessage[0].firstChild.nodeValue != " "){addressErrorDisplay.innerHTML = aErrorMessage[0].firstChild.nodeValue; addressErrorDisplay.style.display = ''}else{addressErrorDisplay.style.display = "none"}
			if(street != null){addressStreet.value = street.nodeValue;} else {addressStreet.value = ''}
			if(locality != null){addressLocality.value = locality.nodeValue;} else {addressLocality.value = ''}
			if(town != null){addressTown.value = town.nodeValue;} else {addressTown.value = ''}
			if(county != null){addressCounty.value = county.nodeValue;} else {addressCounty.value = ''}
			if(postcode != null){addressPostcode.value = postcode.nodeValue;} else {addressPostcode.value = ''}		
			
			document.getElementById('foundaddressdetails').style.display='';
			document.getElementById('proceedbutton').style.display = '';
		}
	}
}