var account_code='PALME11121';
var license_code='DZ26-NK41-HK98-JN56';
var machine_id='';
var label='';

function pcaByPostcodeBegin(l){
	
	label = l;
		
	var postcode = document.getElementById(label+"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)
	 {
		//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").style.display = 'none';
	  
	  //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
			   {
				  document.forms[0]["selectaddress"].style.display = '';
				  document.forms[0]["btnFetch"].style.display = '';
				  
				  for (i=document.forms[0]["selectaddress"].options.length-1; i>=0; i--){
					  document.forms[0]["selectaddress"].options[i] = null;
					}
				  for (i=0; i<pca_id.length; i++){
					document.forms[0]["selectaddress"].options[document.forms[0]["selectaddress"].length] = new Option(pca_description[i], pca_id[i]);
				  }
			   }
		 }
   }

function pcaFetchBegin()
   {
	  var address_id = document.forms[0]["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)
		 {
			//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.forms[0]["selectaddress"].style.display = 'none';
	  document.forms[0]["btnFetch"].style.display = 'none';
   }

function pcaFetchEnd(){
	//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]["companyName"].value = '' + pca_organisation_name[0];
			document.forms[0][label+"AddressLine1"].value = '' + pca_line1[0];
			document.forms[0][label+"AddressLine2"].value = '' + pca_line2[0];
			document.forms[0][label+"AddressLine3"].value = '' + pca_line3[0];
			//document.forms[0]["line4"].value = '' + pca_line4[0];
			//document.forms[0]["line5"].value = '' + pca_line5[0];
			document.forms[0][label+"TownCity"].value = '' + pca_post_town[0];
			document.forms[0][label+"County"].value = '' + pca_county[0];
			//document.forms[0]["postcode"].value = '' + pca_postcode[0];
			if (pca_line1[0] == ""){
				document.forms[0][label+"AddressLine1"].value = '' + pca_organisation_name[0];
			}
			var myselect=document.forms[0][label+"Country"];
			for (var i=0; i<myselect.options.length; i++){
				if (myselect.options[i].value == "United Kingdom"){
					myselect.options[i].selected=true;
					break
				}
			}
		}
	}
}
   
function pcaGeocodePostcodeBegin(postcode, building, accuracy)
   {
      var scriptTag = document.getElementById("pcaScript");
      var headTag = document.getElementsByTagName("head").item(0);
      var strUrl = "";

      //Build the url
      strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
      strUrl += "&action=geocode";
      strUrl += "&postcode=" + escape(postcode);
      strUrl += "&building=" + escape(building);
      strUrl += "&accuracy=" + escape(accuracy);
      strUrl += "&account_code=" + escape(account_code);
      strUrl += "&license_code=" + escape(license_code);
      strUrl += "&callback=pcaGeocodePostcodeEnd";

      //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 pcaGeocodePostcodeEnd()
   {
      //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
               {
                  //PUT YOUR CODE HERE
				  alert("location: "+pca_location+"\n"+
						"longitude: "+pca_longitude+"\n"+
						"latitude: "+pca_latitude+"\n"+
						"wgs84_longitude: "+pca_wgs84_longitude+"\n"+
						"wgs84_latitude: "+pca_wgs84_latitude);
               }
         }
   }
	
