var PAFAccountCode = "THECE11111";
var PAFLicenseKey = "KX56-CE91-FN89-EA72";
// REQUEST
function pcaByPostcodeBegin(postcode)
{
	var scriptTag = document.getElementById("pcaScript");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";

	//Build the url
	var strUrl = (("https:" == document.location.protocol) ? "https://" : "http://");
	strUrl += "services.postcodeanywhere.co.uk/inline.aspx?";
	strUrl += "&action=lookup";
	strUrl += "&type=by_postcode";
	strUrl += "&postcode=" + escape(postcode);
	strUrl += "&account_code=" + PAFAccountCode;
	strUrl += "&license_code=" + PAFLicenseKey;
	strUrl += "&machine_id=" + "";
	strUrl += "&callback=pcaByPostcodeEnd";

	//Make the request
	if (scriptTag) {
		try{
				headTag.removeChild(scriptTag);
		}
		catch (e){}
	}
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScript";
	headTag.appendChild(scriptTag);
}

function pcaByPostcodeEnd()
{
	//Test for an error
	if (pcaIsError) {
		//Show the error message
		if (pcaErrorMessage == "Please supply a complete postcode") {
			pcaErrorMessage = 'Please enter a complete post code or enter address manually.';
		}
		alert(pcaErrorMessage);
	} else {
		//Check if there were any items found
		if (pcaRecordCount==0) {
			alert("Sorry, no matching items found");
		} else {
			document.getElementById('selectPostcode').options[0] = new Option('Manual Enter Address',0);
			for (i=1;i<pcaRecordCount+1;i++ )
			{
				document.getElementById('selectPostcode').options[i] = new Option(pca_description[i],pca_id[i]);
			}	
		}
	}
}


function pcaFetchAddressBegin(id)
	{
		var scriptTag = document.getElementById("pcaScript");
		var headTag = document.getElementsByTagName("head").item(0);
		var strUrl = "";

		//Build the url
		var strUrl = (("https:" == document.location.protocol) ? "https://" : "http://");
		strUrl += "services.postcodeanywhere.co.uk/inline.aspx?";
		strUrl += "&action=fetch";
		strUrl += "&id=" + escape(id);
		strUrl += "&language=" + escape('english');
		strUrl += "&style=" + escape('simple');
		strUrl += "&account_code=" + PAFAccountCode;
		strUrl += "&license_code=" + PAFLicenseKey;
		strUrl += "&machine_id=" + "";
		strUrl += "&options=" + "";
		strUrl += "&callback=pcaFetchAddressEnd";

		//Make the request
		if (scriptTag) 
		 {
			try
			  {
				  headTag.removeChild(scriptTag);
			  }
			catch (e)
			  {
				  //Ignore
			  }
		 }
		scriptTag = document.createElement("script");
		scriptTag.src = strUrl
		scriptTag.type = "text/javascript";
		scriptTag.id = "pcaScript";
		headTag.appendChild(scriptTag);
	}

function pcaFetchAddressEnd()
{
  
  //Test for an error
  if (pcaIsError)
	 {
		//Show the error message
		if (pcaErrorMessage == "Please supply a complete postcode")
		{
			pcaErrorMessage = 'Please enter a complete post code or enter address manually.';
		}
		alert(pcaErrorMessage);
	 }
  else
	 {
		//Check if there were any items found
		if (pcaRecordCount==0)
		   {
			  alert("Sorry, no matching items found");
		   }
		else
		   {
				// populate form with details from postcode anywhere
//				if (pca_organisation_name[0]=='') {
//					document.getElementById("contactQuestionAddress1").value=pca_line1[0];
//				} else {
//					document.getElementById("contactQuestionCompanyName").value=pca_organisation_name[0] + ' - ' + pca_line1[0];
//				}
				if (document.getElementById('contactQuestionCompanyName')) {document.getElementById("contactQuestionCompanyName").value=pca_organisation_name[0];}
				if (document.getElementById('contactQuestionAddress1')) {document.getElementById("contactQuestionAddress1").value=pca_line1[0];}
				if (document.getElementById('contactQuestionAddress2')) {document.getElementById("contactQuestionAddress2").value=pca_line2[0];}
				if (document.getElementById('contactQuestionAddress3')) {document.getElementById("contactQuestionAddress3").value=pca_line3[0];}
				if (document.getElementById('contactQuestionCity')) {document.getElementById("contactQuestionCity").value=pca_post_town[0];}
				if (document.getElementById('contactQuestionCounty')) {document.getElementById("contactQuestionCounty").value=pca_county[0];}
				if (document.getElementById('contactQuestionPostcode')) {document.getElementById("contactQuestionPostcode").value=pca_postcode[0];}
				if (document.getElementById('contactQuestionCountry')) {document.getElementById("contactQuestionCountry").value='UK';}
		   }
	 }
}

function showAddressTownCounty() {
	var browserName=navigator.appName; 
	if (browserName=="Microsoft Internet Explorer")
	{
				if (document.getElementById('divAddress1')) {	document.getElementById('divAddress1').style.display='block'; }
				if (document.getElementById('divAddress2')) {	document.getElementById('divAddress2').style.display='block'; }
				if (document.getElementById('divAddress3')) {	document.getElementById('divAddress3').style.display='block'; }
				if (document.getElementById('divTown')) {	document.getElementById('divTown').style.display='block'; }
				if (document.getElementById('divCounty')) {	document.getElementById('divCounty').style.display='block'; }
	} else	{
				if (document.getElementById('divAddress1')) {	document.getElementById('divAddress1').style.display='table-row'; }
				if (document.getElementById('divAddress2')) {	document.getElementById('divAddress2').style.display='table-row'; }
				if (document.getElementById('divAddress3')) {	document.getElementById('divAddress3').style.display='table-row'; }
				if (document.getElementById('divTown')) {	document.getElementById('divTown').style.display='table-row'; }
				if (document.getElementById('divCounty')) {	document.getElementById('divCounty').style.display='table-row'; }
	}
	if (document.getElementById('divManualAddressLink')) {	document.getElementById('divManualAddressLink').style.display='none'; }
}

function getPostcodeDetails() {
	document.getElementById('showResults').style.display='block'; 
	document.getElementById('selectPostcode').style.height = 80;
	pcaByPostcodeBegin(document.getElementById('contactQuestionPostcode').value);
}

function getPostcodeDetails() {
	document.getElementById('showResults').style.display='block'; 
	document.getElementById('selectPostcode').style.height = 80;
	pcaByPostcodeBegin(document.getElementById('contactQuestionPostcode').value);
}

function populateForm()
{
	// get address
	if (document.getElementById('selectPostcode').options[document.getElementById('selectPostcode').selectedIndex].value==0){
		if (document.getElementById('contactQuestionCompanyName')) { document.getElementById("contactQuestionCompanyName").value=''; }
		if (document.getElementById('contactQuestionAddress1')) { document.getElementById("contactQuestionAddress1").value=''; }
		if (document.getElementById('contactQuestionAddress2')) { document.getElementById("contactQuestionAddress2").value=''; }
		if (document.getElementById('contactQuestionAddress3')) { document.getElementById("contactQuestionAddress3").value=''; }
		if (document.getElementById('contactQuestionCity')) { document.getElementById("contactQuestionCity").value=''; }
		if (document.getElementById('contactQuestionCounty')) { document.getElementById("contactQuestionCounty").value=''; }
		if (document.getElementById('contactQuestionPostcode')) { document.getElementById("contactQuestionPostcode").value=''; }
	} else {
		pcaFetchAddressBegin(document.getElementById('selectPostcode').options[document.getElementById('selectPostcode').selectedIndex].value); 	
	}
	// hide results
	document.getElementById('showResults').style.display='none'; 
	// clear results
	document.getElementById('selectPostcode').options.length = 0;
	// add in blank result
	document.getElementById('selectPostcode').options[0] = new Option('Manual Enter Address',0);
	document.getElementById('selectPostcode').style.height = 0;
	showAddressTownCounty()
}
