document.write('<script type="text/javascript" src="Scripts/pca.js"></script>'); 
		
		function pcaByPostcodeBegin()
		   {
			  var postcode = document.getElementsByName("postcode")[0].value;
			  //var building = document.getElementsByName("suburb")[0].value;
		      var scriptTag = document.getElementById("pcaScriptTag");
		      var headTag = document.getElementsByTagName("head").item(0);
		      var strUrl = "";
			  
			  if (document.getElementById("address_section")) {
			  	document.getElementById("address_section").style.display = '';
			  }

			  document.getElementById("divLoading").style.display = '';
		      
		      //Build the url
		      strUrl = "https://services.postcodeanywhere.co.uk/inline.aspx?";
		      strUrl += "&action=lookup";
		      strUrl += "&type=by_postcode";
		      //strUrl += "&building=" + escape(building);
		      strUrl += "&postcode=" + escape(postcode);
		      strUrl += "&account_code=" + escape(account_code);
		      strUrl += "&license_code=" + escape(license_code);
		      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);

		      document.getElementById("selectaddress").style.display = '';
		      document.getElementById("btnFetch").style.display = '';

		   }

		function pcaByPostcodeEnd()
		   {
			  document.getElementById("divLoading").style.display = 'none';
			  
		      //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. Please try another postcode.");
		               }
		            else
		               {
						  document.getElementsByName("selectaddress")[0].style.display = '';
						  document.getElementsByName("btnFetch")[0].style.display = '';
						  
						  for (i=document.getElementsByName("selectaddress")[0].options.length-1; i>=0; i--){
							  document.getElementsByName("selectaddress")[0].options[i] = null;
							}
						  for (i=0; i<pca_id.length; i++){
		                    document.getElementsByName("selectaddress")[0].options[document.getElementsByName("selectaddress")[0].length] = new Option(pca_description[i], pca_id[i]);
		                  }
		               }
		         }
		   }
		
		function pcaFetchBegin()
		   {
			  var address_id = document.getElementsByName("selectaddress")[0].value;
		      var scriptTag = document.getElementById("pcaScriptTag");
		      var headTag = document.getElementsByTagName("head").item(0);
		      var strUrl = "";

		      //Build the url
		      strUrl = "https://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 += "&style=raw";
		      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.getElementsByName("selectaddress")[0].style.display = 'none';
		      document.getElementsByName("btnFetch")[0].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
		               {
						  //alert('pca_organisation_name[0]: ' + pca_organisation_name[0] + '\n' + 'pca_building_number[0]: ' + pca_building_number[0] + '\n' + 'pca_building_name[0]: ' + pca_building_name[0] + '\n' + 'pca_line1[0]: ' + pca_line1[0] + '\n' + 'pca_line2[0]: ' + pca_line2[0]);
						  var building_number = '' + pca_building_number[0];
						  var building_name = '' + pca_building_name[0];
						  var line1 = '' + pca_line1[0];
						  var line2 = '' + pca_line2[0];
						  if (building_number == '' && building_name != '') {
							var pat = new RegExp('^[0-9]+[a-zA-Z]*');
							building_number = pat.exec(building_name);
							if (building_number == null) {
								building_number = building_name;
							}
						  }
						  if (building_number != '' && line1 != '') {
							var pat = new RegExp('^' + building_number + ' ');
							line1 = line1.replace(pat, '');
							var pat = new RegExp('^' + building_number + '');
							line1 = line1.replace(pat, '');
						  }
						  if (building_number != '' && line2 != '') {
							var pat = new RegExp('^' + building_number + ' ');
							line2 = line2.replace(pat, '');
							var pat = new RegExp('^' + building_number + '');
							line2 = line2.replace(pat, '');
						  }
						  if (line1 == '') {
							line1 = line2;
							line2 = '';
						  }
						  if (line2 != '') {
							line1 += ', ' + line2;
						  }
						  document.getElementsByName("company")[0].value = '' + pca_organisation_name[0];
						  document.getElementsByName("street_address")[0].value = '' + line1;
						  //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.getElementsByName("city")[0].value = '' + pca_post_town[0];
						  document.getElementsByName("state")[0].value = '' + pca_county[0];
						  document.getElementsByName("postcode")[0].value = '' + pca_postcode[0];
						  /*if (pca_building_flat[0]!=''){
							document.forms[0]["flat"].value = '' + pca_building_flat[0];
							}
						  else{
							document.forms[0]["flat"].value = '' + pca_sub_building_name[0];
							}*/
						  document.getElementsByName("suburb")[0].value = '' + building_number;
						  //document.forms[0]["building_name"].value = '' + pca_building_name[0];
						  //document.forms[0]["street1"].value = '' + pca_dependent_thoroughfare_name[0] + ' ' + pca_dependent_thoroughfare_descriptor[0];
						  //document.forms[0]["street2"].value = '' + pca_thoroughfare_name[0] + ' ' + pca_thoroughfare_descriptor[0];
						  //document.forms[0]["dd_locality"].value = '' + pca_double_dependent_locality[0];
						  //document.forms[0]["d_locality"].value = '' + pca_dependent_locality[0];
		               }
		         }
		   }