var account_code='virgi11111';
var license_code='AX24-HZ15-CG93-ZP91';
var machine_id='';

function pcaByPostcodeBegin() {
   var postcode = document.getElementById('ITEM_ATT646_0').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=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() {
  
     //Test for an error
     if (pcaIsError)
        {
           //Show the error message
           document.getElementById("address_dropdown").style.display = 'none';
           alert(pcaErrorMessage);
        }
     else
        {
           //Check if there were any items found
           if (pcaRecordCount==0)
              {
                 jQuery("#address_search_row").hide();
                 alert("Sorry, no matching items found. Please try another postcode.");
              }
           else
              {
			  jQuery("#address_search_row").show();
			  
			  for (i=document.getElementById("address_dropdown").options.length-1; i>=0; i--){
				  document.getElementById("address_dropdown").options[i] = null;
				}
			  for (i=0; i<pca_id.length; i++){
                   document.getElementById("address_dropdown").options[document.getElementById("address_dropdown").length] = new Option(pca_description[i], pca_id[i]);
                 }
              }
        }
}

function pcaFetchBegin() {
	  var address_id = document.getElementById("address_dropdown").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);
      
      jQuery("#address_search_row").hide();
   }

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
               {
				  var company = '';
				  if(pca_organisation_name[0] != '') company = '' + pca_organisation_name[0] + ", ";
				  document.getElementById("ITEM_ATT647_0").value = '' + company + pca_line1[0];
				  document.getElementById("ITEM_ATT648_0").value = '' + pca_line2[0];
				  document.getElementById("ITEM_ATT649_0").value = '' + pca_post_town[0];
				  document.getElementById("ITEM_ATT650_0").value = '' + pca_county[0];
				  document.getElementById("ITEM_ATT646_0").value = '' + pca_postcode[0];
               }
         }
}
