		var account_code='ACUTE11112';
		var license_code='NX89-TX99-GZ21-PK84';
		var machine_id='';
		
		function pcaByPostcodeBillingBegin()
		   {
			  var postcode = document.getElementById("billing_postcode_search").value;
		      var scriptTag = document.getElementById("pcaScriptTag");
		      var headTag = document.getElementsByTagName("head").item(0);
		      var strUrl = "";
		      
		  	  $("#billing_divLoading").show();
			  
		      //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=pcaByPostcodeBillingEnd";
		      
		      //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 pcaByPostcodeBillingEnd()
		   {  
			  //Test for an error
		      if (pcaIsError)
		         {
					 // finished processing so hide loading graphic
					 $("#billing_divLoading").hide();
						  
					// hide option to choose from list of addresses
		            $("#formSelect").slideUp("fast");
					
					if (pcaErrorMessage == "Please supply a complete postcode")
					{
						// override this error message
						pcaErrorMessage = "Sorry, this postcode is not recognised - please check the spelling and enter a complete postcode.\n\nNOTE:  Postcode lookup will only work for UK addresses.  If you have an overseas address or have problems, you will need to enter your address details manually";	
					}

					//Show the returned error message
					alert(pcaErrorMessage);
				}
		      	else
		         {
		            //Check if there were any items found
		            if (pcaRecordCount==0)
		               { 
						 // finished processing so hide loading graphic
						  $("#billing_divLoading").hide();

		                  // hide selection controls
						  $("#formSelect").slideUp("fast");

		                  alert("Sorry, no matching addresses found.\n\nNOTE:  Postcode lookup will only work for UK addresses.  If you have an overseas address, you will need to enter your address details manually.");
		               }
		            else
		               {		  
						  for (i=document.getElementById("billing_selectaddress").options.length-1; i>=0; i--){
							  document.getElementById("billing_selectaddress").options[i] = null;
							}
						  for (i=0; i<pca_id.length; i++){
		                    document.getElementById("billing_selectaddress").options[document.getElementById("billing_selectaddress").length] = new Option(pca_description[i], pca_id[i]);
		                  }

						  // finished processing so hide loading graphic
						  $("#billing_divLoading").hide();

						  // now show the controls
						  $("#formSelect").slideDown("fast");

					  }
		         }
		   }
		
		function pcaFetchBillingBegin()
		   {
			 // show waiting graphic
			 $("#billing_divLoading").show();
			 
			 var address_id = document.getElementById("billing_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=pcaFetchBillingEnd";

		      //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);
		      
		     // hide option to choose from list of addresses
		     $("#formSelect").slideUp("fast");
		   }

		function pcaFetchBillingEnd()
		{
		      //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]["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];
						
						// append company, address 1 and address 2 onto single address line due to limited address fields
						var company = false;
						var address_1 = false;
						var address_2 = false;
						
						if ($.trim(pca_organisation_name[0]) != "")
						{
							company = true;
							document.getElementById("address_1").value = "" + pca_organisation_name[0];
						}
						if ($.trim(pca_line1[0]) != "")
						{
							address_1 = true;
							if (company)
							{
								document.getElementById("address_1").value = document.getElementById("address_1").value + ", " + pca_line1[0];
							}
							else
							{
								document.getElementById("address_1").value = pca_line1[0];
							}
						}
						if ($.trim(pca_line2[0]) != "")
						{
							address_2 = true;
							if (company || address_1)
							{
								document.getElementById("address_1").value = document.getElementById("address_1").value + ", " + pca_line2[0];
							}
							else
							{
								document.getElementById("address_1").value = pca_line2[0];
							}
						}
						
						document.getElementById("address_2").value = "" + pca_post_town[0];
						document.getElementById("address_3").value = "" + pca_county[0];
						document.getElementById("address_4").value = "" + pca_postcode[0];
					}
		         }
				 $("#billing_divLoading").hide();
		   }
