/*------------------------------------------
|
|  Address Lookup For use with : CRCWeb 
|			 (c) CCL 2006
|
|	web : http://www.companioncomputers.co.uk
|  mail : Andrew@compan.net / km0ti0n@gmail.com
|  date : 06/02/2006
| 
--------------------------------------------*/

function XAPAddressLookup(){}
var _this = XAPAddressLookup.prototype = new XAPDataList();
_this.constructor	= XAPAddressLookup();
_this.ClassName		= "XAPAddressLookup";
_this.XAP			= new XAP();
_this.Events		= [];
_this.address1		= "";
_this.address2		= "";	
_this.address3		= "";
_this.town			= "";
_this.county		= "";
_this.postcode		= "";
_this.aAddress		= [];

_this.Init = function()
{
	var aSel = document.getElementsByTagName("select"); for( var i=0;i<aSel.length; i++ ){ aSel[i].style.visibility = "hidden"; }
	var self = this; 
	window.status = "Looking up Address for Postcode : " + this.cPostCode.toUpperCase();
	this.aAddress = [];
	/* Render ListArea */
	this.ePane = this.NewObj("div", { id:"AddressList" }, this.ParentElement);
	this.NewObj( "p", {}, this.ePane, "Please wait, retriving list of houses names and address information for the postcode : " );
	this.NewObj( "p", {}, this.ePane, this.cPostCode.toUpperCase() );
	this.NewObj( "img", {src:"/images/loading.gif"}, this.ePane  );
	var oCords = FindXY( document.getElementById("Table1") );
	this.ePane.style.top	= (oCords.y + 89 ) + document.body.scrollTop + "px";
	this.ePane.style.left	= (oCords.x + 147) + "px";
	this.XAP = new XAP();
	if(!this.XAP){ alert("Please upgrade your browser.\nThe technique used on this page is over 5 years old, and as such is common practice.") }
	var fnXAPResult  = function(oXML){ self.RenderList(oXML); }
	this.XAP.connect("/ws/pclookup.asmx/getPCDetails", "POST", "cPostCode=" + this.cPostCode, fnXAPResult );
	
}

_this.RenderList = function(oXml)
{
	window.status = "Looking up Address for Postcode : FOUND";

	this.ePane.innerHTML = "";
	try{ this.address1	= oXml.responseXML.documentElement.selectSingleNode("//address1/text()").nodeValue;		}catch(e){}
	try{ this.address2	= oXml.responseXML.documentElement.selectSingleNode("//address2/text()").nodeValue;		}catch(e){}
	try{ this.address3	= oXml.responseXML.documentElement.selectSingleNode("//address3/text()").nodeValue;		}catch(e){}
	try{ this.town		= oXml.responseXML.documentElement.selectSingleNode("//town/text()").nodeValue;			}catch(e){}
	try{ this.county	= oXml.responseXML.documentElement.selectSingleNode("//county/text()").nodeValue;		}catch(e){}
	try{ this.postcode	= oXml.responseXML.documentElement.selectSingleNode("//post_code/text()").nodeValue;	}catch(e){}
		
	this.DataSource = oXml.responseXML.documentElement.selectNodes("//address");
	this.DataBind();

}

_this.DataBind = function()
{
	
	window.status = "Looking up Address for Postcode : RENDERING";
	var self = this;
	this.ePane.onselectstart = function(){ return false; }
	for( var i = 0; i < this.DataSource.length; i++  )
	{
		try
		{
			//Build Address			
			var cBuilding = ""; try{ cBuilding = this.DataSource[i].selectSingleNode("building/text()").nodeValue }catch(e){}
			var cSubBuild = ""; try{ cSubBuild = this.DataSource[i].selectSingleNode("subbuild/text()").nodeValue }catch(e){}
			var cNumber   = ""; try{ cNumber   = this.DataSource[i].selectSingleNode("number/text()").nodeValue   }catch(e){}
			var cOrganise = ""; try{ cOrganise = this.DataSource[i].selectSingleNode("organise/text()").nodeValue }catch(e){}
			
			var cAddress = "";
			
			if		( cSubBuild.trim() != "" )	{ cAddress += cSubBuild + ", "; }
			if		( cBuilding.trim() != "" )	{ cAddress += cBuilding;		}
			else if	( cNumber.trim()   != "" )  { cAddress += cNumber;			}
			
			if( cAddress.trim() != "" ){ this.aAddress[this.aAddress.length] = cAddress; }
			
		}catch(e){}
	}
	
	this.aAddress.sort();
	for( var i = 0; i < this.aAddress.length; i++  )
	{
		var Text = Id = this.aAddress[i];
		if(this.address2 != ""){	Text += ", " + this.address2; }
		var oItem				= new XAPListItem(Text, Id);
		oItem.Parent			= this;
		oItem.ParentElement		= this.ePane;
		this.Controls.Add( oItem );
		oItem.oItem.oldclick = oItem.oItem.onclick;
		oItem.oItem.onclick = function(){ this.oldclick(); self.PopulateAddress(); }
	}
	window.status = "Please Select an Addrees.";
	
	if( this.aAddress.length == 0 ){ alert("Sorry unable to find Address for the Postcode.\n\nPlease check the postcode you entered\nor fill in the address fields manually."); this.ePane.parentNode.removeChild( this.ePane ); }
}

_this.PopulateAddress = function()
{

	this.ePane.parentNode.removeChild( this.ePane );
	//alert(this.oSelectedItem.cItemID)
	
	document.getElementById(cAddress1).value	= this.oSelectedItem.cItemID;
	document.getElementById(cAddress2).value	= this.address2;
	document.getElementById(cAddress3).value	= this.address3;
	document.getElementById(cTown).value		= this.town ;
	document.getElementById(cCounty).value	= this.county;
	document.getElementById(cPostCode).value	= this.postcode;
	//this.eForm.country.value	= "UK";

	var aSel = document.getElementsByTagName("select"); for( var i=0;i<aSel.length; i++ ){ aSel[i].style.visibility = ""; }
}

/*----------------------------------------------
|LookupAddress()  
----------------------------------------------*/
var oAddress;
function LookupAddress(eForm, e)
{
	var evt = e || event; 
	evt.src = e.target || e.srcElement;
	
	var cPostCode =  document.getElementById(window.cPostCode).value;
	if( /[A-Za-z]{1,2}\d{1,2}[A-Za-z]? \d[A-Za-z]{2}/.test( cPostCode ) )
	{
		oAddress = new XAPAddressLookup();
		oAddress.eForm = eForm;
		oAddress.evt = evt;
		oAddress.Parent = window;
		oAddress.ParentElement = document.body;
		oAddress.cPostCode = cPostCode
		oAddress.Init();
	}
	else
	{
		alert("Please use a valid UK Postal Code")
	}
}