
//This whole file was added as part of CHG171008

// 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;
}


// 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
   }
}
 
// #################### Get Address to Combo ############################ 

// 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
	  //alert ("SPLGetFullAddressStep1.asp?postcode=" + escape(postcode));
      SPLxmlHttp.open("GET", "SPLGetFullAddressStep1.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
    }
  }
}

// #################### On Click on Combo ############################ 
 
// handles click on combo
function SPLAddressChange(AddressCombo)
{
		
       // 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", "SPLGetFullAddressStep2.asp?AddressID=" + AddressCombo.options[AddressCombo.selectedIndex].value, 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
         }
  	}
     
     document.getElementById("SPLSearchArea").innerHTML = "";
	
} 
 
 
function aTEST() 
{
	document.getElementById("SPLSearchArea").innerHTML = "HELLO3";
	document.getElementById("COMP").value = "1";
	document.getElementById("SPLSearchArea").innerHTML = "HELLO4";
}

function fAddAddressLine(sFullAddress, sLine) 
{
	var sAddressline 
	
	if (sLine.len > 0)  then 
		sFullAddress = sFullAddress + '<BR>' + sLine;
	return sFullAddress
}

function fAddAddressLineInput(sFullAddressInput, sLine) 
{
	var sAddressline 
	
	if (sLine.len > 0)  then 
		sFullAddressInput = sFullAddressInput + '<BR>' + sLine;
	return sFullAddressInput
}
 
 
 function myAlert(mytitle, mytext){
  html = "<html><head><title>" + myTitle +"</title></head>"
  +"<body bgcolor=thistle>" +mytext +"</body></html>"
  args = "width=200,height=100"
  nw = window.open('',"myAlert",args)
  nw.document.write(html)
  alert("This is standard alert box:\n"+ html)
}


// #################### Handle returned data ############################ 
function SPLhandleServerResponse()
{
  //##########################################################
  //### Delete this line on production server              ###
  //### This can be used for debuging to show XML returned ###
  //alert(SPLxmlHttp.responseText);
  //##########################################################

  // Test that XML contains valid Address, test for <line1>
  var Credits="",COMP="", LINE1="",LINE2="",LINE3="",TOWN="",COUNTY="",POSTCODE="",COUNTRY="",MultiLineAddress=""
  var XMLresponse = SPLxmlHttp.responseText;
  
  if (XMLresponse.indexOf("</select>")>0)
  {
		   XMLresponse = XMLresponse.replace('<p align="right" style="margin-top: 0; margin-bottom: 0"><font face="Arial" style="font-size: 6pt"><a style="text-decoration:none" href="http://www.postcodelite.com/" target="_blank"><font color="#808080">Powered by Postcode Lite</font></a></font></p>','');
		   
      //if contains a </select> then must be combo box
	  
      document.getElementById("SPLSearchArea").innerHTML = XMLresponse;
  }
  else
  {
      if (XMLresponse.indexOf("<line1>")==-1)
	  	{	
		   
      	  if(XMLresponse.indexOf("Cannot find Postcode")>0) 
		  {
		  	alert("Cannot find postocde");
		  }
		}
      else
      {
		  
	  //contains address information so write to fields
	  // read the message from the server
	  // error or no data so show message
	  
	  try
  {

	
	  var xmlResponse = SPLxmlHttp.responseXML;
	  var SPLFullAddress = new String();
	  var SPLFullAddressInput = new String();

	  // 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("organisation").item(0).firstChild) 
		{COMP = xmlRoot.getElementsByTagName("organisation").item(0).firstChild.data;
		SPLFullAddress = fAddAddressLine(SPLFullAddress, '<input type="text" id="ADCOMP" name="ADCOMP" value="'+COMP+'" class="formelement" onchange="javascript:js_copyfield(this.value, \'COMP\')" onKeyUp="javascript:js_copyfield(this.value, \'COMP\')">');
		SPLFullAddressInput = fAddAddressLineInput(SPLFullAddressInput, COMP);
		document.getElementById("COMP").value = COMP;}

	  if (xmlRoot.getElementsByTagName("line1").item(0).firstChild) 
		{LINE1 = xmlRoot.getElementsByTagName("line1").item(0).firstChild.data;
		SPLFullAddress = fAddAddressLine(SPLFullAddress, '<input type="text" id="ADLINE1" name="ADLINE1" value="'+LINE1+'" class="formelement" onchange="javascript:js_copyfield(this.value, \'LINE1\')" onKeyUp="javascript:js_copyfield(this.value, \'LINE1\')">');
		SPLFullAddressInput = fAddAddressLineInput(SPLFullAddressInput, LINE1);
		document.getElementById("LINE1").value = LINE1;}
 
	  if (xmlRoot.getElementsByTagName("line2").item(0).firstChild) 
		{LINE2 = xmlRoot.getElementsByTagName("line2").item(0).firstChild.data;
		SPLFullAddress = fAddAddressLine(SPLFullAddress, '<input type="text" id="ADLINE2" name="ADLINE2" value="'+LINE2+'" class="formelement" onchange="javascript:js_copyfield(this.value, \'LINE2\')" onKeyUp="javascript:js_copyfield(this.value, \'LINE2\')">');
		SPLFullAddressInput = fAddAddressLineInput(SPLFullAddressInput, LINE2);
		document.getElementById("LINE2").value = LINE2;}
 
	  if (xmlRoot.getElementsByTagName("line3").item(0).firstChild) 
		{LINE3 = xmlRoot.getElementsByTagName("line3").item(0).firstChild.data;
		SPLFullAddress = fAddAddressLine(SPLFullAddress, '<input type="text" id="ADLINE3" name="ADLINE3" value="'+LINE3+'" class="formelement" onchange="javascript:js_copyfield(this.value, \'LINE3\')" onKeyUp="javascript:js_copyfield(this.value, \'LINE3\')">');
		SPLFullAddressInput = fAddAddressLineInput(SPLFullAddressInput, LINE3);
		document.getElementById("LINE3").value = LINE3;}

	  if (xmlRoot.getElementsByTagName("town").item(0).firstChild) 	
		{TOWN = xmlRoot.getElementsByTagName("town").item(0).firstChild.data;
		SPLFullAddress = fAddAddressLine(SPLFullAddress, TOWN);
		SPLFullAddressInput = fAddAddressLineInput(SPLFullAddressInput, TOWN);
		document.getElementById("TOWN").value = TOWN;}

	  if (xmlRoot.getElementsByTagName("county").item(0).firstChild) 
		{COUNTY = xmlRoot.getElementsByTagName("county").item(0).firstChild.data;
		SPLFullAddress = fAddAddressLine(SPLFullAddress, COUNTY);
		SPLFullAddressInput = fAddAddressLineInput(SPLFullAddressInput, COUNTY);
		document.getElementById("COUNTY").value = COUNTY;}

	  if (xmlRoot.getElementsByTagName("postcode").item(0).firstChild) 
		{POSTCODE = xmlRoot.getElementsByTagName("postcode").item(0).firstChild.data;
		SPLFullAddress = fAddAddressLine(SPLFullAddress, POSTCODE);
		SPLFullAddressInput = fAddAddressLineInput(SPLFullAddressInput, POSTCODE);
		document.getElementById("POSTCODE").value = POSTCODE;}

	  if (xmlRoot.getElementsByTagName("country").item(0).firstChild) 
		{COUNTRY = xmlRoot.getElementsByTagName("country").item(0).firstChild.data;
		SPLFullAddress = fAddAddressLine(SPLFullAddress, COUNTRY);
		SPLFullAddressInput = fAddAddressLineInput(SPLFullAddressInput, COUNTRY);
		document.getElementById("COUNTRY").value = COUNTRY;}
		
		document.getElementById("SPLFullAddress").innerHTML = SPLFullAddress;	
		document.getElementById("FULLADDRESS").value = SPLFullAddressInput;	
		try
			{document.getElementById("addnewaddress").style.display = "block";}
		catch(err)
			{}
		
		 }
catch(err)
  {
  txt="The following error occured while processing the address.\n\n";
  txt+="Error description: " + err.description + "\n\n";
  txt+="Click OK to continue.\n\n";
  alert(txt);
  }
	 
	// Multi line
	if (COMP!="") {MultiLineAddress = MultiLineAddress  + COMP + "\n"}
	if (LINE1!="") {MultiLineAddress = MultiLineAddress + LINE1 + "\n"}
	if (LINE2!="") {MultiLineAddress = MultiLineAddress + LINE2 + "\n"}
	if (LINE3!="") {MultiLineAddress = MultiLineAddress + LINE3 + "\n"}
	if (TOWN!="") {MultiLineAddress = MultiLineAddress + TOWN + "\n"}
	if (COUNTY!="") {MultiLineAddress = MultiLineAddress + COUNTY + "\n"}
	if (POSTCODE!="") {MultiLineAddress = MultiLineAddress + POSTCODE + "\n"}
	if (COUNTRY!="") {MultiLineAddress = MultiLineAddress + COUNTRY + "\n"}
	document.getElementById("memo").value=MultiLineAddress;

      }
  }
}

 
