﻿var account_code='ECOMM11116';
var license_code='XF51-BB32-TJ69-BH96';
var machine_id='';
var btntosearch = '';
var selectforresults = '';
var btnforretrieve = '';
var t1 = '';
var t2 = '';
var t3 = '';
var t4 = '';
var t5 = '';
var t6 = '';
var isPCSelectionBoxShowing = false;

function pcaByPostcodeBegin(postcode, btnpressed)
   {
      isPCSelectionBoxShowing = false;
	  var postcode = postcode;
      var scriptTag = document.getElementById("pcaScriptTag");
      var headTag = document.getElementsByTagName("head").item(0);
      var strUrl = "";
      btntosearch = btnpressed;
           
      //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(btnpressed)
   { 
      //Test for an error
      if (pcaIsError)
         {
            //Show the error message
            alert(pcaErrorMessage);
         }
      else
         {
            //Check if there were any items found
            if (pcaRecordCount==0)
               {
                  document.getElementById(selectforresults).style.display = 'none';
				  document.getElementById(btnforretrieve).style.display = 'none';
				  pfjs_addpreparetocheckout();
                  alert("Sorry, no matching items found. Please enter your address manually.");
               }
            else
               {
				  document.forms[0][selectforresults].style.display = '';
				  document.forms[0][btnforretrieve].style.display = '';
				  
				  isPCSelectionBoxShowing = true;
				  
				  for (i=document.forms[0][selectforresults].options.length-1; i>=0; i--){
					  document.forms[0][selectforresults].options[i] = null;
					}
				  for (i=0; i<pca_id.length; i++){
                    document.forms[0][selectforresults].options[document.forms[0][selectforresults].length] = new Option(pca_description[i], pca_id[i]);
                  }
               }
         }
        document.getElementById(btntosearch).value = "Search >>";
   }

function pcaFetchBegin()
   {
      isPCSelectionBoxShowing = false;
	  var address_id = document.forms[0][selectforresults].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][selectforresults].style.display = 'none';
      document.forms[0][btnforretrieve].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][t1].value = (pca_organisation_name[0] != '') ? '' + pca_organisation_name[0] + ', ' + pca_line1[0] : pca_line1[0];
				  document.forms[0][t2].value = '' + pca_line2[0] + pca_line3[0] + pca_line4[0] + pca_line5[0];
				  document.forms[0][t3].value = '' + pca_post_town[0];
				  document.forms[0][t4].value = '' + pca_county[0];
				  document.forms[0][t5].value = '' + pca_postcode[0];
				  document.forms[0][t6].value = '' + 'UK';
               }
         }
   }

