var account_code='ACTIN11111';
var license_code='RB21-DH89-JD36-DP29';
var machine_id='';

function pcaByPostcodeBegin()
	{
	var oRegForm = document.forms['register'];
	var postcode = oRegForm["Postcode"].value;
	var scriptTag = document.getElementById("pcaScriptTag");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";

	document.getElementById("divLoading").style.display = '';

	//Build the url
	strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
	strUrl += "&action=lookup";
	strUrl += "&type=by_postcode";
	strUrl += "&postcode=" + escape(postcode);
	strUrl += "&account_code=" + escape(account_code);
	strUrl += "&license_code=" + escape(license_code);
	strUrl += "&machine_id=" + escape(machine_id);
	strUrl += "&callback=pcaByPostcodeEnd";

	//Make the request
	if (scriptTag)
		{
		headTag.removeChild(scriptTag);
		}
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScriptTag";
	headTag.appendChild(scriptTag);
	}

function pcaByPostcodeEnd()
	{
	document.getElementById("divLoading").style.display = 'none';
	var oRegForm = document.forms['register'];

	//Test for an error
	if (pcaIsError)
		{
		//Show the error message
		document.getElementById("selectaddress").style.display = 'none';
		document.getElementById("btnFetch").style.display = 'none';
		alert(pcaErrorMessage);
		}
	else
		{
		//Check if there were any items found
		if (pcaRecordCount==0)
			{
			document.getElementById("selectaddress").style.display = 'none';
			document.getElementById("btnFetch").style.display = 'none';
			alert("Sorry, no matching items found. Please try another postcode.");
			}
		else
			{
			oRegForm["selectaddress"].style.display = '';
			oRegForm["btnFetch"].style.display = '';

			for (i = oRegForm["selectaddress"].options.length-1; i >= 0; i--)
				{
				oRegForm["selectaddress"].options[i] = null;
				}
			for (i = 0; i < pca_id.length; i++)
				{
				oRegForm["selectaddress"].options[oRegForm["selectaddress"].length] = new Option(pca_description[i], pca_id[i]);
				}
			}
		}
	}

function pcaFetchBegin()
	{
	var oRegForm = document.forms['register'];
	var address_id = oRegForm["selectaddress"].value;
	var scriptTag = document.getElementById("pcaScriptTag");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";

	//Build the url
	strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
	strUrl += "&action=fetch";
	strUrl += "&id=" + escape(address_id);
	strUrl += "&account_code=" + escape(account_code);
	strUrl += "&license_code=" + escape(license_code);
	strUrl += "&machine_id=" + escape(machine_id);
	strUrl += "&callback=pcaFetchEnd";

	//Make the request
	if (scriptTag)
		{
		headTag.removeChild(scriptTag);
		}
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScriptTag";
	headTag.appendChild(scriptTag);

	oRegForm["selectaddress"].style.display = 'none';
	oRegForm["btnFetch"].style.display = 'none';
	}

function pcaFetchEnd()
	{
	var oRegForm = document.forms['register'];
	//Test for an error
	if (pcaIsError)
		{
		//Show the error message
		alert(pcaErrorMessage);
		}
	else
		{
		//Check if there were any items found
		if (pcaRecordCount==0)
			{
			alert("Sorry, no matching items found");
			}
		else
			{
			oRegForm["Company"].value = '' + pca_organisation_name[0];
			var sAddress1 = '' + pca_line1[0];
			if (sAddress1 != '' &&
				pca_line2[0] != null &&
				pca_line2[0] != '')
				{
				sAddress1 += ', ';
				}
			sAddress1 += pca_line2[0];
			oRegForm["Address1"].value = sAddress1;

			var sAddress2 = '' + pca_line3[0];
			if (sAddress2 != '' &&
				pca_line4[0] != null &&
				pca_line4[0] != '')
				{
				sAddress2 += ', ';
				}
			sAddress2 += pca_line4[0];
			if (sAddress2 != '' &&
				pca_line5[0] != null &&
				pca_line5[0] != '')
				{
				sAddress2 += ', ';
				}
			sAddress2 += pca_line5[0];
			oRegForm["Address2"].value = sAddress2;
			oRegForm["City"].value = '' + pca_post_town[0];
			oRegForm["State"].value = '' + pca_county[0];
			oRegForm["Postcode"].value = '' + pca_postcode[0];
			}
		}
	}

