// holds an instance of XMLHttpRequest
var SPLxmlHttp = SPLcreateXmlHttpRequestObject();

// creates an XMLHttpRequest instance
function SPLcreateXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
                                    'MSXML2.XMLHTTP.5.0',
                                    'MSXML2.XMLHTTP.4.0',
                                    'MSXML2.XMLHTTP.3.0',
                                    'MSXML2.XMLHTTP',
                                    'Microsoft.XMLHTTP');
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// read a file from the server
function SPLGetAddressData(postcode)
{
  // only continue if xmlHttp isn't void
  if (SPLxmlHttp)
  {
    // try to connect to the server
    try
    {
      // initiate reading a file from the server
      SPLxmlHttp.open("GET", "SPLGetStreetAddress.asp?postcode=" + escape(postcode), true);
      SPLxmlHttp.onreadystatechange = SPLhandleRequestStateChange;
      SPLxmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      //alert("Can't connect to server:\n" + e.toString());
      //Suppress error, since can be cause by two calls at once
    }
  }
}

// function called when the state of the HTTP request changes
function SPLhandleRequestStateChange() 
{
  try
  {
	  // when readyState is 4, we are ready to read the server response
	  if (SPLxmlHttp.readyState == 4) 
	  {
	    // continue only if HTTP status is "OK"
	    if (SPLxmlHttp.status == 200) 
	    {
	      try
	      {
		// do something with the response from the server
		SPLhandleServerResponse();
	      }
	      catch(e)
	      {
		// display error message
		alert("Error reading the response: " + e.toString());
	      }
	    } 
	    else
	    {
	      // display status message
	      alert("There was a problem retrieving the data:\n" + 
		    SPLxmlHttp.statusText);
	    }
	  }
   }
   catch(e)
   {
   // ignore
   }
}
 
 
// handles the address response received from the server
function SPLhandleServerResponse()
{
  //##########################################################
  //### Delete this line on production server              ###
  //### This can be used for debuging to show XML returned ###
  // alert(SPLxmlHttp.responseText);
  //##########################################################
  
  var Credits="",LINE1="",LINE2="",LINE3="",TOWN="",COUNTY="",POSTCODE="",COUNTRY=""
  
  // Test that XML contains valid Address, test for <line1>
  var XMLtest = SPLxmlHttp.responseText;
  if (XMLtest.indexOf("<line1>")>=0)
  {
	  // read the message from the server
	  var xmlResponse = SPLxmlHttp.responseXML;

	  // obtain the XML's document element
	  xmlRoot = xmlResponse.documentElement;
  
	  if (xmlRoot.getElementsByTagName("credits_display_text").item(0).firstChild) 
		{Credits = xmlRoot.getElementsByTagName("credits_display_text").item(0).firstChild.data ;}

	  if (xmlRoot.getElementsByTagName("line1").item(0).firstChild) 
		{LINE1 = xmlRoot.getElementsByTagName("line1").item(0).firstChild.data;}

	  if (xmlRoot.getElementsByTagName("line2").item(0).firstChild) 
		{LINE2 = xmlRoot.getElementsByTagName("line2").item(0).firstChild.data;}

	  if (xmlRoot.getElementsByTagName("line3").item(0).firstChild) 
		{LINE3 = xmlRoot.getElementsByTagName("line3").item(0).firstChild.data;}

	  if (xmlRoot.getElementsByTagName("town").item(0).firstChild) 	
		{TOWN = xmlRoot.getElementsByTagName("town").item(0).firstChild.data;}

	  if (xmlRoot.getElementsByTagName("county").item(0).firstChild) 
		{COUNTY = xmlRoot.getElementsByTagName("county").item(0).firstChild.data;}

	  if (xmlRoot.getElementsByTagName("postcode").item(0).firstChild) 
		{POSTCODE = xmlRoot.getElementsByTagName("postcode").item(0).firstChild.data;}

	  if (xmlRoot.getElementsByTagName("country").item(0).firstChild) 
		{COUNTRY = xmlRoot.getElementsByTagName("country").item(0).firstChild.data;}
		
	  document.getElementById("postcode").value=POSTCODE;
  }

if(COUNTY=="Northern Ireland"){
	COUNTY="";
	COUNTRY="Northern Ireland";
}
if(
	 TOWN=="Isle Of Man"){
	COUNTY="Isle Of Man";
	COUNTRY="United Kingdom";
}
if(
	 TOWN=="Guernsey"){
	COUNTY="Channel Islands";
	COUNTRY="United Kingdom (Channel Islands)";
}
if(
	 TOWN=="Jersey"){
	COUNTY="Channel Islands";
	COUNTRY="United Kingdom (Channel Islands)";
}
if( COUNTY=="London")
	 TOWN="London";


//convert Country
		if(COUNTRY=="United Kingdom")COUNTRY="7"
if(COUNTRY=="Albania")COUNTRY="8"
if(COUNTRY=="Andorra")COUNTRY="9"
if(COUNTRY=="Antigua and Barbuda")COUNTRY="10"
if(COUNTRY=="Australia")COUNTRY="11"
if(COUNTRY=="Austria")COUNTRY="12"
if(COUNTRY=="Bahamas")COUNTRY="13"
if(COUNTRY=="Belarus")COUNTRY="14"
if(COUNTRY=="Belgium")COUNTRY="15"
if(COUNTRY=="Bermuda")COUNTRY="16"
if(COUNTRY=="BFPO (British Forces)")COUNTRY="17"
if(COUNTRY=="Bosnia and Herzegovina")COUNTRY="18"
if(COUNTRY=="Bulgaria")COUNTRY="19"
if(COUNTRY=="Canada")COUNTRY="20"
if(COUNTRY=="Cayman Islands")COUNTRY="21"
if(COUNTRY=="Christmas Island")COUNTRY="22"
if(COUNTRY=="Cook Islands")COUNTRY="23"
if(COUNTRY=="Croatia")COUNTRY="24"
if(COUNTRY=="Cyprus")COUNTRY="25"
if(COUNTRY=="Czech Republic")COUNTRY="26"
if(COUNTRY=="Denmark")COUNTRY="27"
if(COUNTRY=="England")COUNTRY="28"
if(COUNTRY=="Estonia")COUNTRY="29"
if(COUNTRY=="Falkland Islands")COUNTRY="30"
if(COUNTRY=="Faroe Islands")COUNTRY="31"
if(COUNTRY=="Finland")COUNTRY="32"
if(COUNTRY=="France")COUNTRY="33"
if(COUNTRY=="Georgia")COUNTRY="34"
if(COUNTRY=="Germany")COUNTRY="35"
if(COUNTRY=="Gibraltar")COUNTRY="36"
if(COUNTRY=="Greece")COUNTRY="37"
if(COUNTRY=="Guernsey")COUNTRY="38"
if(COUNTRY=="Hong Kong")COUNTRY="39"
if(COUNTRY=="Hungary")COUNTRY="40"
if(COUNTRY=="Iceland")COUNTRY="41"
if(COUNTRY=="India")COUNTRY="42"
if(COUNTRY=="Ireland (Republic of)")COUNTRY="43"
if(COUNTRY=="Israel")COUNTRY="44"
if(COUNTRY=="Italy")COUNTRY="45"
if(COUNTRY=="Jamaica")COUNTRY="46"
if(COUNTRY=="Japan")COUNTRY="47"
if(COUNTRY=="Jersey")COUNTRY="48"
if(COUNTRY=="Kosovo")COUNTRY="49"
if(COUNTRY=="Latvia")COUNTRY="50"
if(COUNTRY=="Liechtenstein")COUNTRY="51"
if(COUNTRY=="Lithuania")COUNTRY="52"
if(COUNTRY=="Luxembourg")COUNTRY="53"
if(COUNTRY=="Macedonia")COUNTRY="54"
if(COUNTRY=="Maldives")COUNTRY="55"
if(COUNTRY=="Malta")COUNTRY="56"
if(COUNTRY=="Monaco")COUNTRY="57"
if(COUNTRY=="Montenegro")COUNTRY="58"
if(COUNTRY=="Netherlands")COUNTRY="59"
if(COUNTRY=="New Zealand")COUNTRY="60"
if(COUNTRY=="Northern Ireland")COUNTRY="61"
if(COUNTRY=="Norway")COUNTRY="62"
if(COUNTRY=="Poland")COUNTRY="63"
if(COUNTRY=="Portugal")COUNTRY="64"
if(COUNTRY=="Romania")COUNTRY="65"
if(COUNTRY=="Russia")COUNTRY="66"
if(COUNTRY=="Saint Helena")COUNTRY="67"
if(COUNTRY=="San Marino")COUNTRY="68"
if(COUNTRY=="Scotland")COUNTRY="69"
if(COUNTRY=="Serbia")COUNTRY="70"
if(COUNTRY=="Slovakia")COUNTRY="71"
if(COUNTRY=="Slovenia")COUNTRY="72"
if(COUNTRY=="South Africa")COUNTRY="73"
if(COUNTRY=="Spain")COUNTRY="74"
if(COUNTRY=="Sweden")COUNTRY="75"
if(COUNTRY=="Switzerland")COUNTRY="76"
if(COUNTRY=="Ukraine")COUNTRY="77"
if(COUNTRY=="United Arab Emirates")COUNTRY="78"
if(COUNTRY=="United Kingdom")COUNTRY="79"
if(COUNTRY=="United States of America")COUNTRY="80"
if(COUNTRY=="Vatican City")COUNTRY="81"
if(COUNTRY=="Wales")COUNTRY="82"
if(COUNTRY=="United Kingdom (Channel Islands)")COUNTRY="83"

	
	
  //Write back even if not found, so clears address lines
  document.getElementById("del1").value=LINE1;
  document.getElementById("del2").value=LINE2;
  document.getElementById("del3").value=LINE3;
  document.getElementById("town").value=TOWN;
  document.getElementById("district").value=COUNTY;
  document.getElementById("country").value=COUNTRY;
	
showLayer("addtable1");
	
}

