﻿var xmlHttp, xmlHttp2;

/*
var SRVID_txtpostcode 
var SRVID_lbaddress
have been defined server side.
*/
function leftTrim(sString) 
{
    while (sString.substring(0, 1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    return sString;
}

function loadPafDropDown()
{
	if( document.getElementById(SRVID_txtpostcode).value != null )
		SetPAFDropDownItems( document.getElementById(SRVID_txtpostcode).value );
	
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch (e)
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
	}
	return xmlHttp;
}

function SetPAFDropDownItems( searchStr )
{ 
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url="/ajax/GetPAFEntriesFromPostcode.asp";
	url=url+"?searchStr="+searchStr;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=postcodeChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function postcodeChanged() 
{ 
	if (xmlHttp.readyState==4)
	{
		document.getElementById(SRVID_lbaddress).options.length = 0
		//frmMain.sel_paf_addresses.options[frmMain.sel_paf_addresses.length] = new Option('Please select an address','');
						
		var xmlDoc=xmlHttp.responseXML.documentElement;
		
		var addrItems = xmlDoc.getElementsByTagName('Item');
		
		for( var i=0; i<addrItems.length; i++  )
		{
			var addrItem = addrItems[i];
			
			var strAddKey = addrItem.getElementsByTagName('Key')[0].firstChild.nodeValue;
			var strAddress = addrItem.getElementsByTagName('List')[0].firstChild.nodeValue;
			var strPostcode = addrItem.getElementsByTagName('Postcode')[0].firstChild.nodeValue;
			
            //strip the leading postcode from the address...
			strAddress = leftTrim(strAddress.replace(strPostcode + ' ', ""));

            // load the items into the list box
			document.getElementById(SRVID_lbaddress).options[document.getElementById(SRVID_lbaddress).length] = new Option(strAddress,strAddKey);
		}
		
		sel_paf_addresses_changed();
		set_address_pane();
	}
}

function sel_paf_addresses_changed()
{
	if( document.getElementById(SRVID_lbaddress).options.length > 0 )
	{
		if( document.getElementById(SRVID_lbaddress).selectedIndex > -1 )
		{
			var addressKey = document.getElementById(SRVID_lbaddress).options[document.getElementById(SRVID_lbaddress).selectedIndex].value;
			
			xmlHttp2=GetXmlHttpObject();
			
			if (xmlHttp2==null)
			{
				alert ("Your browser does not support AJAX!");
				return;
			}
			var url="/ajax/GetAddressFromKey.asp";
			url=url+"?searchStr="+ addressKey;
			url=url+"&sid="+Math.random();
			xmlHttp2.onreadystatechange=addressChanged;
			xmlHttp2.open("GET",url,true);
			xmlHttp2.send(null);
		}
	}
	else
	{	//wipe the hiddens
		document.getElementById("addr1").value = "";
		document.getElementById("addr2").value = "";
		document.getElementById("addr3").value = "";
		document.getElementById("town").value = "";
		document.getElementById("county").value = "";
		document.getElementById("postcode").value = "";
	}
}

function addressChanged()
{
	//set the hiddens so that when posted we get the correct values passed via the form
	if (xmlHttp2.readyState==4)
	{
		var xmlDoc=xmlHttp2.responseXML.documentElement;
		
		var addrItems = xmlDoc.getElementsByTagName('Item');
		
		//there should be only one...
		for( var i=0; i<addrItems.length; i++  )
		{
			var addrItem = addrItems[i];
			
			var strOrganisation = strProperty = strStreet = strLocality = strTown = strCounty = strOptionalCounty = dbpostcode = '';
			
			if( addrItem.getElementsByTagName('Organisation')[0].firstChild )
				strOrganisation = addrItem.getElementsByTagName('Organisation')[0].firstChild.nodeValue;
				
			if( addrItem.getElementsByTagName('Property')[0].firstChild)
				strProperty = addrItem.getElementsByTagName('Property')[0].firstChild.nodeValue;
				
			if( addrItem.getElementsByTagName('Street')[0].firstChild)
				strStreet = addrItem.getElementsByTagName('Street')[0].firstChild.nodeValue;
				
			if( addrItem.getElementsByTagName('Locality')[0].firstChild)
				strLocality = addrItem.getElementsByTagName('Locality')[0].firstChild.nodeValue;
			
			if( addrItem.getElementsByTagName('Town')[0].firstChild)
				strTown = addrItem.getElementsByTagName('Town')[0].firstChild.nodeValue;
			
			if( addrItem.getElementsByTagName('PostalCounty')[0].firstChild)
				strCounty = addrItem.getElementsByTagName('PostalCounty')[0].firstChild.nodeValue;
			
			if( addrItem.getElementsByTagName('OptionalCounty')[0].firstChild)
				strOptionalCounty = addrItem.getElementsByTagName('OptionalCounty')[0].firstChild.nodeValue;
			
			if( addrItem.getElementsByTagName('Postcode')[0].firstChild)
				dbpostcode = addrItem.getElementsByTagName('Postcode')[0].firstChild.nodeValue;			
			
			var dbaddressline1=dbaddressline2=dbaddressline3=dbtown=dbcounty='';
			
			if( strOrganisation.length > 0 )
			{
				dbaddressline1 = strOrganisation;
				dbaddressline2 = strStreet;
				dbaddressline3 = strLocality;
			}
			else
			{
				dbaddressline1 = strStreet;
				dbaddressline2 = strLocality;
			}
			
			dbtown = strTown;
			
			if( strCounty.length > 0 )
				dbcounty = strCounty;
			else
				dbcounty = strOptionalCounty;
			
			document.getElementById('addr1').value = dbaddressline1;
			document.getElementById('addr2').value = dbaddressline2;
			document.getElementById('addr3').value = dbaddressline3;
			document.getElementById('town').value = dbtown;
			document.getElementById('county').value = dbcounty;
			document.getElementById('postcode').value = dbpostcode;

			//alert(document.getElementById('addr1').value);
		}
		
		//set_address_pane();
	}
}

function set_address_pane( )
{
	return;
	
	var str = document.getElementById("alt_addr1").value;
	
	if( str.length > 0 )
		str += "<br/>";
	
	if( document.getElementById("sel_paf_addresses").selectedIndex > -1 )
	{
		

		str += document.getElementById("addr1").value + '<br/>';
		if( document.getElementById("addr2").value != '' )
			str+=document.getElementById("addr2").value + '<br/>';
		if( document.getElementById("addr3").value != '' )
			str+=document.getElementById("addr3").value + '<br/>';
		
		//rare ocasions where town is the county as well.
		if( document.getElementById("town").value != document.getElementById("county").value )
			str+=document.getElementById("town").value + '<br/>';
		str+=document.getElementById("county").value + '<br/>';
		str+=document.getElementById("postcode").value + '<br/>';
	
		document.getElementById("address_pane").innerHTML = str;
	}
}
		
	
