var account_code='WARRA11111';
var license_code='WR77-UZ17-DD94-PF22';
var machine_id='';
var formno = 1;
		
function pcaByPostcodeBegin(form)
   {
	  formno = form
	  var postcode = document.forms[0]["postcode_search1"].value;
	  if (2 == form ) {
		var postcode = document.forms[0]["postcode_search2"].value;
	  }
	  
	  var scriptTag = document.getElementById("pcaScriptTag");
	  var headTag = document.getElementsByTagName("head").item(0);
	  var strUrl = "";
	  
	  if(1 == form ) {
		document.getElementById("divLoading1").style.display = '';
	  } else {
		document.getElementById("divLoading2").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()
   {
	  if( 1 == formno) {
	  
	  
	  document.getElementById("divLoading1").style.display = 'none';
	  
	  //Test for an error
	  if (pcaIsError)
		 {
			//Show the error message
			document.getElementById("selectaddress1").style.display = 'none';
			document.getElementById("btnFetch1").style.display = 'none';
			alert(pcaErrorMessage);
		 }
	  else
		 {
			//Check if there were any items found
			if (pcaRecordCount==0)
			   {
				  document.getElementById("selectaddress1").style.display = 'none';
				  document.getElementById("btnFetch1").style.display = 'none';
				  alert("Sorry, no matching items found. Please try another postcode.");
			   }
			else
			   {
				  document.forms[0]["selectaddress1"].style.display = '';
				  document.forms[0]["btnFetch1"].style.display = '';
				  
				  for (i=document.forms[0]["selectaddress1"].options.length-1; i>=0; i--){
					  document.forms[0]["selectaddress1"].options[i] = null;
					}
				  for (i=0; i<pca_id.length; i++){
					document.forms[0]["selectaddress1"].options[document.forms[0]["selectaddress1"].length] = new Option(pca_description[i], pca_id[i]);
				  }
			   }
		 }
	  
	  } else {
	  
	  
	  document.getElementById("divLoading2").style.display = 'none';
	  
	  //Test for an error
	  if (pcaIsError)
		 {
			//Show the error message
			document.getElementById("selectaddress2").style.display = 'none';
			document.getElementById("btnFetch2").style.display = 'none';
			alert(pcaErrorMessage);
		 }
	  else
		 {
			//Check if there were any items found
			if (pcaRecordCount==0)
			   {
				  document.getElementById("selectaddress2").style.display = 'none';
				  document.getElementById("btnFetch2").style.display = 'none';
				  alert("Sorry, no matching items found. Please try another postcode.");
			   }
			else
			   {
				  document.forms[0]["selectaddress2"].style.display = '';
				  document.forms[0]["btnFetch2"].style.display = '';
				  
				  for (i=document.forms[0]["selectaddress2"].options.length-1; i>=0; i--){
					  document.forms[0]["selectaddress2"].options[i] = null;
					}
				  for (i=0; i<pca_id.length; i++){
					document.forms[0]["selectaddress2"].options[document.forms[0]["selectaddress2"].length] = new Option(pca_description[i], pca_id[i]);
				  }
			   }
		 }
	  
	  }
   }

function pcaFetchBegin()
   {
	  var address_id = document.forms[0]["selectaddress1"].value;
	  if(2 == formno) {
		var address_id = document.forms[0]["selectaddress2"].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);
	  
	  if (1 == formno) {
		document.forms[0]["selectaddress1"].style.display = 'none';
		document.forms[0]["btnFetch1"].style.display = 'none';
	  } else {
		document.forms[0]["selectaddress2"].style.display = 'none';
		document.forms[0]["btnFetch2"].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
			   {
				  if (1 == formno) {
					  document.forms[0]["owner_address1"].value = '' + pca_organisation_name[0] + ' , '+ pca_line1[0];
					  document.forms[0]["owner_address2"].value = '' + pca_line2[0] + ', ' + pca_line3[0];
					  document.forms[0]["owner_city"].value = '' + pca_post_town[0];
					  document.forms[0]["owncode"].value = '' + pca_postcode[0];
					} else {
					  document.forms[0]["garage_name"].value = '' + pca_organisation_name[0];
					  document.forms[0]["garage_address1"].value = '' + pca_line1[0];
					  document.forms[0]["garage_address2"].value = '' + pca_line2[0] + ', ' + pca_line3[0];
					  document.forms[0]["garage_city"].value = '' + pca_post_town[0];
					  document.forms[0]["garage_post"].value = '' + pca_postcode[0];
					}
			   }
		 }
   }