function postcode_lookup(){

	var thePostcode = document.getElementById("fmPostcode");
	var thePostcodeValue = document.getElementById("fmPostcode").value;
	var theTR = document.getElementById("trPostcodeLookup");
	var theTD = document.getElementById("tdPostcodeLookup");

	ajax_postcode = create_ajax();
	
	ajax_postcode.onreadystatechange=function(){
		if (ajax_postcode.readyState==4 && ajax_postcode.status==200){
			logIt();	// Function comes from remote included file: http://ws.fastantobas.co.uk/postcodelog/logpcfunction.js
			response = ajax_postcode.responseText;
			// If no addresses were found, then display alert asking user to input manually
			if (response.indexOf("Postcode Not Found") > -1){
				alert("Your postcode could not be found - please try again.");
				thePostcode.value = "";
				thePostcode.focus();
			}
			// Otherwise show the addresses and force country to be UK
			else{
				// Show the address table row
				if (ie6 || ie7)
					theTR.style.display = "block";
				else
					theTR.style.display = "table-row";
				// Put the select option created in the AJAX page into the cell in the row
				theTD.innerHTML = response;
			}
		}
	}

	var post_vars = "postcode="+thePostcode.value;
	
	run_ajax(ajax_postcode,"POST","/_ajax/postcode_lookup.asp",post_vars);
	
}

function select_address(postkey){
	
	var theTR = document.getElementById("trPostcodeLookup");
	var theTD = document.getElementById("tdPostcodeLookup");

	ajax_address = create_ajax();
	
	ajax_address.onreadystatechange=function(){
		if (ajax_address.readyState==4 && ajax_address.status==200){
			// Hide the address select option row and remove the select option from the cell inside it
			theTD.innerHTML = '';
			theTR.style.display = "none";
			// Run the fill_address() function created in the AJAX page to populate the address fields
			eval(ajax_address.responseText);
		}
	}

	var post_vars = "postkey="+postkey;
	
	run_ajax(ajax_address,"POST","/_ajax/address_lookup.asp",post_vars);
	
}

function fill_address(organisation,property,street,locality,town,county,postcode,grideast,gridnorth){
	
	var theAddress1 = document.getElementById("fmAddress1");
	var theAddress2 = document.getElementById("fmAddress2");
	var theAddress3 = document.getElementById("fmAddress3");
	var theTown = document.getElementById("fmTown");
	var theCounty = document.getElementById("fmCounty");
	var thePostcode = document.getElementById("fmPostcode");
	var theGridEast = document.getElementById("fmGridEast");
	var theGridNorth = document.getElementById("fmGridNorth");
	
	var strAddress1 = "";
	var strAddress2 = "";
	var strAddress3 = "";
	var strTown = "";
	var strCounty = "";
	var strPostcode = "";
	var strGridEast = "";
	var strGridNorth = "";
	
	if (organisation != "") strAddress1 = organisation;
	if (property != "")	strAddress1 = property;
	if (organisation == "" && property == "")
		strAddress1 = street
	else
		strAddress2 = street
	if (locality != ""){
		if (strAddress2 == "") strAddress2 = locality;
		else
			strAddress3 = locality;
	}
	strTown = town;
	strCounty = county
	strPostcode = postcode;
	strGridEast = grideast;
	strGridNorth = gridnorth;
	
	theAddress1.value = strAddress1;
	theAddress2.value = strAddress2;
	theAddress3.value = strAddress3;
	theTown.value = strTown;
	theCounty.value = strCounty;
	thePostcode.value = strPostcode;
	theGridEast.value = strGridEast;
	theGridNorth.value = strGridNorth;
	
}
