
var arr_line1 = new Array();
var arr_line2 = new Array();
var arr_line3 = new Array();
var arr_line4 = new Array();
var arr_city = new Array();
var arr_county = new Array();
var arr_postcode = new Array();


function makeRequest(postcode, formobj, postcodeselectname, postcodeselectdivname, messages_div, loading_div, objLine1, objLine2, objLine3, objLine4, objCity, objCounty, pleaseselectmessage) 
{

	url = "/Commerce/Postcodes/getpostcodes.php?Postcode=" + postcode;
	postvar = "postcode=" + postcode;

	var http_request = false;

	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari, ...
   	  http_request = new XMLHttpRequest();
	  if (http_request.overrideMimeType) 
	  {
	      	http_request.overrideMimeType('text/xml');
               // See note below about this line
          }
   	} 
	else if (window.ActiveXObject) 
	{ // IE
   	  try 
	  {
	    http_request = new ActiveXObject("Msxml.XMLHTTP");
          } 
	  catch (e) 
	  {
	    try
	    {
	      http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
          }
        }

        if (!http_request) 
	{
	  alert('Giving up :( Cannot create an XMLHTTP instance');
          return false;
        }
	
	objLine1.value = '';
	objLine2.value = '';
	objLine3.value = '';
	objLine4.value = '';
	objCity.value = '';
	objCounty.value = '';
	
	
	var obj_postcodeselect = document.getElementById(postcodeselectname);
	var obj_postcodediv = document.getElementById(postcodeselectdivname);
	var obj_messagediv = document.getElementById(messages_div);
	var obj_loadingdiv = document.getElementById(loading_div);
	

	
	
	obj_loadingdiv.style.display = '';
	obj_messagediv.style.display = 'none';
	obj_postcodediv.style.display = 'none';
	
   http_request.onreadystatechange = function() { alertContents(http_request, obj_postcodeselect, obj_postcodediv, obj_messagediv, obj_loadingdiv, pleaseselectmessage); };
   http_request.open('GET', url, true );
	//http_request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        
   http_request.send(null);
	
	
}


function clearSelect(select_obj, pleaseselectmessage)
{

	while (select_obj.length > 0)
	{
	  select_obj.remove(0);
	}
	
	addToSelect(select_obj, pleaseselectmessage, '');
	
}


function addToSelect(obj_select, name, value)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = name;
  elOptNew.value = value;

  try 
  {
    obj_select.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) 
  {
    obj_select.add(elOptNew); // IE only
  }
}


function selectaddress(selectedid, objForm, objLine1, objLine2, objLine3, objLine4, objCity, objCounty, objPostcode, postcodediv)
{
		  

   	if (selectedid != '')
	{

			  
	
		objLine1.value = arr_line1[selectedid];
		objLine2.value = arr_line2[selectedid];
		objLine3.value = arr_line3[selectedid];
		objLine4.value = arr_line4[selectedid];
		objCity.value = arr_city[selectedid];
		objCounty.value = arr_county[selectedid];
		objPostcode.value = arr_postcode[selectedid];
	
		document.getElementById(postcodediv).style.display='none';
	}
}

var appendnext = 0;

function GetUnderlyingValueToArray(objTag, address_array, allowsharing)
{
	arraylen = address_array.length;
	
	getitemval = GetUnderlyingValue(objTag);
	
	if (getitemval != '')
	{
		if (appendnext == 1)
		{
			address_array[arraylen-1] = address_array[arraylen-1] + ' ' + getitemval;
		}
		else
		{
			address_array[arraylen] = getitemval;
		}
		
		if (allowsharing == 1)
		{
			appendnext = 1;
		}
		else
		{
			appendnext = 0;
		}
	}
	


}

function GetUnderlyingValue(objTag)
{

	if (objTag.childNodes.length > 0)
	{
		return objTag.firstChild.data;
	}
	else
	{
		return '';
	}


}


function alertContents(http_request, obj_postcodeselect, obj_postcodediv, obj_messagediv, obj_loadingdiv, pleaseselectmessage) 
{

	if (http_request.readyState != 4)
	{
	  obj_postcodediv.style.display='none';
	  obj_loadingdiv.style.display='';
	}

   if (http_request.readyState == 4)
	{
		obj_loadingdiv.style.display='none';
		if (http_request.status == 200)
		{

			response = http_request.responseXML.documentElement;
			
			a_status = GetUnderlyingValue(response.getElementsByTagName('Status')[0]);
			if (a_status == 0)
			{
				obj_postcodediv.style.display='';		
				clearSelect(obj_postcodeselect, pleaseselectmessage);
		
				addressheader = response.getElementsByTagName('Address');
				for (x = 0; x<addressheader.length; x++)
				{
					addressitem = addressheader[x];
					
					y= addressheader[x].childNodes;

					postcodeid = GetUnderlyingValue(addressitem.getElementsByTagName('Key')[0]);
					
					PostTown = GetUnderlyingValue(addressitem.getElementsByTagName('PostTown')[0]);
					PostCode = GetUnderlyingValue(addressitem.getElementsByTagName('Postcode')[0]);
					County = GetUnderlyingValue(addressitem.getElementsByTagName('County')[0]);
					
					address_array = new Array();
					
					AddressName = GetUnderlyingValue(addressitem.getElementsByTagName('DepartmentName')[0]);

					if (AddressName == '')
					{
					  AddressName = GetUnderlyingValue(addressitem.getElementsByTagName('OrganisationName')[0]);
					}
					else
					{
					  GetUnderlyingValueToArray(addressitem.getElementsByTagName('OrganisationName')[0], address_array, 0);
					}


					
					GetUnderlyingValueToArray(addressitem.getElementsByTagName('SubBuildingName')[0], address_array, 0);
					GetUnderlyingValueToArray(addressitem.getElementsByTagName('BuildingName')[0], address_array, 0);
					
					GetUnderlyingValueToArray(addressitem.getElementsByTagName('BuildingNumber')[0], address_array, 1);
					GetUnderlyingValueToArray(addressitem.getElementsByTagName('DependentThoroughfare')[0], address_array, 0);
					GetUnderlyingValueToArray(addressitem.getElementsByTagName('Thoroughfare')[0], address_array, 0);
					GetUnderlyingValueToArray(addressitem.getElementsByTagName('DoubleDependentLocality')[0], address_array, 0);
					GetUnderlyingValueToArray(addressitem.getElementsByTagName('DependentLocality')[0], address_array, 0);
					

					
					for (z = address_array.length; z < 3; z++)
					{
						address_array[z] = '';
					}
					
					line1 = '';
					line2 = '';
					line3 = '';
					//line4 = '';
					
					if (address_array.length <= 3)
					{
						line1 = address_array[0];
						line2 = address_array[1];
						line3 = address_array[2];
						//line4 = address_array[3];
					}
					
					if (address_array.length > 3)
					{
						//line4 = address_array[address_array.length - 1];
						line3 = address_array[address_array.length - 1];
						line2 = address_array[address_array.length - 2];
						line1 = '';
						for (z = 0; z < address_array.length - 2; z++)
						{
							if (line1 != '') 
							{
								line1 = line1 + ', '
							}
							line1 = line1 + address_array[z];
						}
					
					}
					
					arr_line1[postcodeid] = AddressName;
					arr_line2[postcodeid] = line1;
					arr_line3[postcodeid] = line2;
					arr_line4[postcodeid] = line3;
					arr_city[postcodeid] = PostTown;
					arr_county[postcodeid] = County;
					arr_postcode[postcodeid] = PostCode;
					
					
					addressline = addressitem.getElementsByTagName('AddressLine')[0].firstChild.data;
					
					
					addToSelect(obj_postcodeselect, addressline, postcodeid);
									
				}
			}
			else
			{
				obj_messagediv.style.display = '';
			}
		}
		else 
	   {
      	obj_messagediv.style.display = '';
      }
	}
}


//***** New functions to handle postcode lookup through backend message *****
function makeRequestMessage(postcode, nameno, sid, customerref, formobj, postcodeselectname, postcodeselectdivname, messages_div, loading_div, objLine1, objLine2, objLine3, objLine4, objCity, objCounty, pleaseselectmessage, objPostcode, objCountrySel) 
{

	//in the instance of a message being used, need to verify that a postcode and house name/number have been provided
	if (postcode==""){
		alert("please enter a postcode");
		return false;
	}

	if (nameno==""){
		alert("please enter a number/name for your address");
		return false;
	}

	url = "/Commerce/Postcodes/getpostcodes_msg.php?postcode=" + postcode + "&nameno=" + nameno + "&sid=" + sid + "&customerref=" + customerref;
	postvar = "postcode=" + postcode;
	namenovar = "nameno=" + nameno
	sidvar = "sid=" + sid
	custrefvar = "customerref=" + customerref

	var http_request = false;

	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari, ...
   	http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) 
		{
      	http_request.overrideMimeType('text/xml');
               // See note below about this line
      }
   } 
	else if (window.ActiveXObject) 
	{ // IE
   	try 
		{
		   http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } 
		catch (e) 
		{
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }

   if (!http_request) 
	{
		alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
   }
	
	objLine1.value = '';
	objLine2.value = '';
	objLine3.value = '';
	objLine4.value = '';
	objCity.value = '';
	objCounty.value = '';
	
	
	var obj_postcodeselect = document.getElementById(postcodeselectname);
	var obj_postcodediv = document.getElementById(postcodeselectdivname);
	var obj_messagediv = document.getElementById(messages_div);
	var obj_loadingdiv = document.getElementById(loading_div);
	
	obj_loadingdiv.style.display = '';
	obj_messagediv.style.display = 'none';
	
   http_request.onreadystatechange = function() { alertContentsMessage(http_request, obj_postcodeselect, obj_postcodediv, obj_messagediv, obj_loadingdiv, pleaseselectmessage, objLine1, objLine2, objLine3, objLine4, objCity, objCounty, objPostcode, objCountrySel); };
   http_request.open('GET', url, true );
	//http_request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        
   http_request.send(null);
	
	
}

function alertContentsMessage(http_request, obj_postcodeselect, obj_postcodediv, obj_messagediv, obj_loadingdiv, pleaseselectmessage, objLine1, objLine2, objLine3, objLine4, objCity, objCounty, objPostcode, objCountrySel) 
{

	if (http_request.readyState != 4)
	{
	  obj_loadingdiv.style.display='';
	}

   if (http_request.readyState == 4)
	{
		obj_loadingdiv.style.display='none';
		if (http_request.status == 200)
		{
			
			response = http_request.responseText;
			
			//split the string down
			var addressAry;
			
			addressAry = response.split("|");
			statuscode = addressAry[0];
			jsHomeno = addressAry[1];
			jsAdd1 = addressAry[2];
			jsAdd2 = addressAry[3];
			jsAdd3 = addressAry[4];
			jsCity = addressAry[5];
			jsCounty = addressAry[6];
			jsCountry = addressAry[7];
			jsPostcode = addressAry[8];
			
			//set back to the UK for now, as postcode search is only valid here.
			objCountrySel.value = 1;
			
			if(statuscode=="0"){
				objLine1.value = jsHomeno;
				objLine2.value = jsAdd1;
				objLine3.value = jsAdd2;
				objLine4.value = jsAdd3;
				objCity.value = jsCity;
				objCounty.value = jsCounty
				objPostcode.value = jsPostcode
			}else{	
				obj_messagediv.style.display = '';
			}
			
		}else{
      		obj_messagediv.style.display = '';
      	}
	}
}
