var account_code='theha11121';
var license_code='GD19-CU97-GX63-UM22';
var machine_id='';

function pcaByPostcodeBegin()
{
      var postcode = document.getElementById('address-postcode').value; //["frm_postcode"]
      var scriptTag = document.getElementById("pcaScriptTag");
      var headTag = document.getElementsByTagName("head").item(0);
      var strUrl = "";
      var theForm = document.getElementById('contact-us');
      
      document.getElementById("divLoading").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)
         {
            //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()
   {
        var theForm = document.getElementById("contact-us");
      document.getElementById("divLoading").style.display = 'none';
      
      //Test for an error
      if (pcaIsError)
         {
            //Show the error message
            document.getElementById("chooseAddress").style.display = 'none';
            document.getElementById("btnFetch").style.display = 'none';
            alert(pcaErrorMessage);
         }
      else
         {
            //Check if there were any items found
            if (pcaRecordCount===0)
               {
                  document.getElementById("chooseAddress").style.display = 'none';
                  document.getElementById("btnFetch").style.display = 'none';
                  alert("Sorry, no matching items found. Please try another postcode.");
               }
            else
               {
                  // BB 20081009 Ñ slight tweak to allow postcode re-entry
                  document.getElementById("chooseAddress").style.display = '';
                  document.getElementById("btnFetch").style.display = '';
                  // theForm["selectaddress"].style.display = '';
                  
                  for (i=theForm["selectaddress"].options.length-1; i>=0; i--){
                      theForm["selectaddress"].options[i] = null;
                    }
                  for (i=0; i<pca_id.length; i++){
                    theForm["selectaddress"].options[theForm["selectaddress"].length] = new Option(pca_description[i], pca_id[i]);
                  }
               }
         }
   }

function pcaFetchBegin()
   {
    var theForm = document.getElementById('contact-us');
      var address_id = theForm["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=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);

      // BB 20081009 Ñ slight tweak to allow postcode re-entry
      document.getElementById("chooseAddress").style.display = 'none';
      theForm["btnFetch"].style.display = 'none';
      // theForm["selectaddress"].style.display = 'none';
   }

function pcaFetchEnd()
{
    var theForm = document.getElementById("contact-us");
    //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
           {
              //theForm["company"].value = '' + pca_organisation_name[0];
              theForm["frm_house"].value = '' + pca_line1[0];
              theForm["frm_street"].value = '' + pca_line2[0];
              //theForm["frm_city"].value = '' + pca_line3[0];
              theForm["frm_county"].value = '' + pca_line4[0];
              //theForm["line5"].value = '' + pca_line5[0];
              theForm["frm_city"].value = '' + pca_post_town[0];
              for( i = 0; i < theForm["frm_country"].length; i++){
                var ctry = theForm["frm_country"].options[i].value;
                if( ctry === "United Kingdom" ){
                    theForm["frm_country"].options[i].selected = "true";
                }
              }
              YAHOO.hmg.findaclinic.handleSubmit();
              //theForm["frm_country"].value = '' + pca_country[0];
              //theForm["frm_postcode"].value = '' + pca_postcode[0];
           }
     }
}

// Find nearest single clinic YUI
YAHOO.namespace("YAHOO.hmg.findaclinic");
YAHOO.hmg.findaclinic.handleSubmit = function(ev) {
    // Stop the event
    //YAHOO.util.Event.stopEvent(ev);
    // Define callbacks
    var successHandler = function(o) {
        //YAHOO.hmg.findaclinic.topNode.innerHTML = o.responseText;
        //YAHOO.hmg.findaclinic.init();
      dropDown = document.getElementById("preferred-clinic");
      for( i = 0; i < dropDown.length; i++){
            var clinic = dropDown.options[i].value;
            if( clinic === o.responseText ){
                  dropDown.options[i].selected = "true";
            }
      }
      dropDown.enabled=true;
      var element = document.getElementById( 'address-div' );
        //alert(element.style.display);
		if(element.style.display === "none"){
		  element.style.display =  "block";
	  }
    };
    var failureHandler = function(o) {
        alert("Unable to select your nearest clinic.");
        //YAHOO.hmg.findaclinic.form.enable();
        dropDown.enabled=true;
        var element = document.getElementById( 'address-div' );
        //alert(element.style.display);
		if(element.style.display === "none"){
		  element.style.display =  "block";
		}
    }
    var callback = {
        success: successHandler,
        failure: failureHandler
    };
    // Prepare the request
    //YAHOO.util.Connect.setForm(YAHOO.hmg.findaclinic.form);
    // Do request
    post_code = document.getElementById("address-postcode").value;
    YAHOO.util.Connect.asyncRequest("POST", location.href, callback, "ajax_request=1&post_code=" + post_code);
    // Disable the form
    document.getElementById("preferred-clinic").disable();

}

//initLayers = function(){
      //toggleLayer( 'address-div' );
      //toggleLayer( 'findPostCodeElements' );
//}
//YAHOO.util.Event.onDOMReady( initLayers );
