function AddressFind(postcode, building)
{
  var scriptTag = document.getElementById("ScriptTag");
  var headTag = document.getElementsByTagName("head").item(0);
  var strUrl = "";

  // Build the url
  strUrl = document.location.protocol + "//services.postcodeanywhere.co.uk/inline.aspx?";
  strUrl += "&action=fetch";
  strUrl += "&postcode=" + escape(postcode);
  strUrl += "&building=" + escape(building);
  strUrl += "&language=english";
  strUrl += "&style=simple";
  strUrl += "&account_code=JELLY11116"
  strUrl += "&license_code=UT78-MR58-GT41-BY49";
  strUrl += "&callback=AddressProcess";

  // Clear up any previous attempts
  if (scriptTag)
    headTag.removeChild(scriptTag);

  scriptTag = document.createElement("script");
  scriptTag.src = strUrl;
  scriptTag.type = "text/javascript";
  scriptTag.id = "ScriptTag";
  headTag.appendChild(scriptTag);

  return false;
}

function AddressProcess()
{
  // Error?
  if (pcaIsError)
    alert(pcaErrorMessage);
  else
  {
    // No Addresses Found
    if (pcaRecordCount == 0)
      alert("Sorry, no matching items found");
    else
    {
      if (document.getElementById("name"))
        document.getElementById("name").value=pca_organisation_name[0];

      document.getElementById("address1").value=pca_line1[0];
      document.getElementById("address2").value=pca_line2[0];
      document.getElementById("address3").value=pca_post_town[0];
      document.getElementById("county").value=pca_county[0];
      document.getElementById("postcode").value=pca_postcode[0];

      // Enable Editables
      changeExtra(false);
    }
  }
}

function changeExtra(state)
{
  var theDiv = document.getElementById('AddressSection');

  if (theDiv)
    changeInputs(theDiv, state);
}

function changeInputs(theDiv, state)
{
  changeInputState(theDiv.getElementsByTagName("input"), state);
  changeInputState(theDiv.getElementsByTagName("select"), state);
  changeInputState(theDiv.getElementsByTagName("textarea"), state);
}

function changeInputState(theChildren, state)
{
  for (var i = 0; i < theChildren.length; i++)
  {
    if (theChildren[i].parentNode.id != 'Control')
      theChildren[i].disabled = state;
  }
}