
var account_code='inter11240';
var license_code='cj93-cu25-et67-ja32';
var machine_id='';
var g_itemID='';
function pcaByPostcodeBegin(postcodeSearch,itemID)
   {
	  
	  if(g_itemID != '') return;
	  
	  g_itemID = itemID;
	  var postcode = document.getElementById(postcodeSearch).value;
	  var scriptTag = document.getElementById("pcaScriptTag");
	  var headTag = document.getElementsByTagName("head").item(0);
	  var strUrl = "";
	  
	  document.getElementById("divLoading"+itemID).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)
		 {
			
			//The following 2 lines perform the same function and should be interchangeable
			headTag.removeChild(scriptTag);
			//scriptTag.parentNode.removeChild(scriptTag);
		 }
	  scriptTag = document.createElement("script");
	  scriptTag.src = strUrl
	  scriptTag.type = "text/javascript";
	  scriptTag.id = "pcaScriptTag";
	  headTag.appendChild(scriptTag);
	  
	  
	  
   }

function pcaByPostcodeEnd()
   {

	  
	  document.getElementById("divLoading"+g_itemID).style.display = 'none';

	  //Test for an error
	  if (pcaIsError)
		 {
			//Show the error message
			document.getElementById("selectaddress"+g_itemID).style.display = 'none';
			document.getElementById("btnFetch"+g_itemID).style.display = 'none';
			document.getElementById("SelAddress"+g_itemID).style.display = 'none';
			
			
			alert(pcaErrorMessage);
		 }
	  else
		 {
			//Check if there were any items found
			if (pcaRecordCount==0)
			   {
				  document.getElementById("selectaddress"+g_itemID).style.display = 'none';
				  document.getElementById("btnFetch"+g_itemID).style.display = 'none';
				  document.getElementById("SelAddress"+g_itemID).style.display = 'none';
				  alert("Sorry, no matching items found. Please try another postcode.");
			   }
			else
			   {

				  document.getElementById("selectaddress"+g_itemID).style.display = '';
				  document.getElementById("btnFetch"+g_itemID).style.display = '';
				  document.getElementById("SelAddress"+g_itemID).style.display = '';
				  
				  for (i=document.getElementById("selectaddress"+g_itemID).options.length-1; i>=0; i--){
					  document.getElementById("selectaddress"+g_itemID).options[i] = null;
					}
				  for (i=0; i<pca_id.length; i++){
					document.getElementById("selectaddress"+g_itemID).options[document.getElementById("selectaddress"+g_itemID).length] = new Option(pca_description[i], pca_id[i]);
				  }
			   }
		 }
		 
	 //free the globle variable
	  g_itemID='';
   }

var g_callBack = null;
function pcaFetchBegin(itemID,callBack)
   {
	  
	  if(g_callBack != null) return;
	  
	  g_callBack = callBack;
	  
	  var address_id = document.getElementById("selectaddress"+itemID).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)
		 {
			//The following 2 lines perform the same function and should be interchangeable
			headTag.removeChild(scriptTag);
			//scriptTag.parentNode.removeChild(scriptTag);
		 }
	  scriptTag = document.createElement("script");
	  scriptTag.src = strUrl
	  scriptTag.type = "text/javascript";
	  scriptTag.id = "pcaScriptTag";
	  headTag.appendChild(scriptTag);
	  
	  document.getElementById("selectaddress"+itemID).style.display = 'none';
	  document.getElementById("btnFetch"+itemID).style.display = 'none';
	  document.getElementById("SelAddress"+itemID).style.display = 'none';
	  
	  
   }



function pcaFetchEnd()
   {
	  
	  var scriptTag = document.getElementById("pcaScriptTag");
	  
	  //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
			   {
				  
				  var fullAddress = new Array();
				  fullAddress[0]='' + pca_line1[0];
				  fullAddress[1]='' + pca_line2[0];
				  fullAddress[2]='' + pca_post_town[0];
				  fullAddress[3]='' + pca_county[0];
				  fullAddress[4]='' + pca_postcode[0];
				  fullAddress[5]='UK' ;
				  
				  
				  g_callBack(fullAddress);
				  
				  /*
				  document.forms[0]["company"].value = '' + pca_organisation_name[0];
				  document.forms[0]["line1"].value = '' + pca_line1[0];
				  document.forms[0]["line2"].value = '' + pca_line2[0];
				  document.forms[0]["line3"].value = '' + pca_line3[0];
				  document.forms[0]["line4"].value = '' + pca_line4[0];
				  document.forms[0]["line5"].value = '' + pca_line5[0];
				  document.forms[0]["town"].value = '' + pca_post_town[0];
				  document.forms[0]["county"].value = '' + pca_county[0];
				  document.forms[0]["postcode"].value = '' + pca_postcode[0];
				  */
			   }
		 }
		 g_callBack = null;
   }

function pcaFetchEnda()
   {
	  
	  var scriptTag = document.getElementById("pcaScriptTag");
	  
	  //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
			   {
				  document.forms[0]["company"].value = '' + pca_organisation_name[0];
				  document.forms[0]["line1"].value = '' + pca_line1[0];
				  document.forms[0]["line2"].value = '' + pca_line2[0];
				  document.forms[0]["line3"].value = '' + pca_line3[0];
				  document.forms[0]["line4"].value = '' + pca_line4[0];
				  document.forms[0]["line5"].value = '' + pca_line5[0];
				  document.forms[0]["town"].value = '' + pca_post_town[0];
				  document.forms[0]["county"].value = '' + pca_county[0];
				  document.forms[0]["postcode"].value = '' + pca_postcode[0];
			   }
		 }
   }
