function pcaLookup(accountCode,licenseCode){
	postcode = document.getElementById('pcaPostcode').value;
	building = document.getElementById('pcaBuilding').value;
	pcaFastAddressBegin(postcode,building,"english","simple",accountCode,licenseCode,"","");
}

function pcaFastAddressBegin(postcode, building, language, style, account_code, license_code, machine_id, options)
   {
      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 += "&postcode=" + escape(postcode);
      strUrl += "&building=" + escape(building);
      strUrl += "&language=" + escape(language);
      strUrl += "&style=" + escape(style);
      strUrl += "&account_code=" + escape(account_code);
      strUrl += "&license_code=" + escape(license_code);
      strUrl += "&machine_id=" + escape(machine_id);
      strUrl += "&options=" + escape(options);
      strUrl += "&callback=pcaFastAddressEnd";

      //Make the request
      if (scriptTag) 
         {
            headTag.removeChild(scriptTag);
         }
      scriptTag = document.createElement("script");
      scriptTag.src = strUrl
      scriptTag.type = "text/javascript";
      scriptTag.id = "pcaScript";
      headTag.appendChild(scriptTag);
   }

function pcaFastAddressEnd()
   {
      //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 trBuilding = document.getElementById("pcaBuilding").parentNode.parentNode;
                  var tr = trBuilding.cloneNode(1);
                  tr.childNodes[1].innerHTML = "";

                  var code = '<select onchange="pcaSelect(this);"><option value="">Please select...</option>';
                  for(var i = 0; i < pca_line1.length; i++){
                  	code += '<option value="'+i+'">'+pcaFormat(i)+'</option>';
                  }
                  
                  var div = document.getElementById("pcaSelect");
                  div.innerHTML = code;

                  document.getElementById("pcaWrapper").innerHTML;
                  
                  
               }
         }
   }

function pcaFormat(index){
	var s = "";
	var vars = ["pca_organisation_name","pca_line1","pca_line2","pca_post_town","pca_county","pca_postcode"];
	for(var i = 0; i < vars.length; i++){
		var v = eval(vars[i]+"["+index+"]");
		if(v.length){
			s += (s.length?", ":"")+v;
		}
	}
	return s;
}

function pcaSelect(sel){
	var i = sel.value;
	if(pca_line1[i] != undefined){
		var a = document.getElementById("pcaAddressBlock");
		var inputs = document.getElementById("pcaAddressBlock").getElementsByTagName("input");
		inputs[0].value = pca_line1[i];
		inputs[1].value = pca_line2[i];
		inputs[2].value = pca_post_town[i];
		inputs[3].value = pca_county[i];
		document.getElementById("pcaPostcode").value = pca_postcode[i];
	}
	closePCA();
}
