// JavaScript Document

function addressSearch()
{
	this.addresses = new Array();
	this.orgVal = 0;
	
    // define object methods
    this.getAddressList = getAddressList;
	this.confirmAddress = confirmAddress;
	this.showSelection = showSelection;
}

function getAddressList(sAddressLine1, sPostcode)
{
	document.forms[0].paf_line1.value = '';
	document.forms[0].paf_line2.value = '';
	document.forms[0].paf_line3.value = '';
	document.forms[0].paf_town.value = '';
	document.forms[0].paf_county.value = '';
	document.forms[0].paf_postcode.value = '';
	
	sUrl = '/scripts/addresssearch.cfm';
	sUrl += '?address1=' + sAddressLine1 + '&postcode=' + sPostcode;
	HTTPRequest(sUrl, null, 'GET', false, 'addressValidationAjaxCallback');
}

function addressValidationAjaxCallback()
{
	// request is the name of the XMLHTTP object instance
	// defined within ajax.js
	if (request.readyState == 4)
	{

		if (window.ActiveXObject)
		{
			var oXml=new ActiveXObject("Microsoft.XMLDOM");
			oXml.async="false";
			oXml.loadXML(request.responseText);
		}
		// code for Mozilla, Firefox, Opera, etc.
		else
		{
			var parser=new DOMParser();
			var oXml=parser.parseFromString(request.responseText,"text/xml");
		}		
		// convert the string to an XML object
		//var oXml = (new DOMParser()).parseFromString(request.responseText, "text/xml");


		var root = oXml.getElementsByTagName("postcode")[0];
		var orgs = root.getElementsByTagName('Organisations');
		oAddressSearch.orgVal = 1//orgs[0].childNodes[0].nodeValue;
		var addresses = root.getElementsByTagName('IDX');
		for(i = 0; i < addresses.length; i++)
		{
			oAddressSearch.addresses[oAddressSearch.addresses.length] = new Object();
			for(j = 0; j < addresses[i].childNodes.length; j++)
			{
				
				if(addresses[i].childNodes[j].tagName != undefined)
				{
					sValue = (addresses[i].childNodes[j].childNodes.length > 0) ? addresses[i].childNodes[j].firstChild.nodeValue : '';
					oAddressSearch.addresses[oAddressSearch.addresses.length-1][addresses[i].childNodes[j].tagName] = sValue;
				}
			}
		}
		oAddressSearch.showSelection();
	}
}


function confirmAddress(event)
{
	oElement = (event == undefined) ? window.event.srcElement : event.target;
	while(oElement.tagName != 'A')
	{
		oElement = oElement.parentNode;	
	}
	aIDSplit = oElement.id.split('_');
	iID = aIDSplit[1];
	document.forms[0].paf_line1.value = oAddressSearch.addresses[iID].line1;
	document.forms[0].paf_line2.value = oAddressSearch.addresses[iID].line2;
	document.forms[0].paf_line3.value = '';
	document.forms[0].paf_town.value = oAddressSearch.addresses[iID].town;
	document.forms[0].paf_county.value = oAddressSearch.addresses[iID].county;
	document.forms[0].paf_postcode.value = oAddressSearch.addresses[iID].postcode;
	// Only submit form if captcha value is 5 chars long (all captcha words are 5 letters long, so anything else and its definitely incorrect)
	if (document.getElementById('strCaptcha').value.length == 5) 
	{ submitForm(true,true,true); } 
}

function showSelection()
{
	oDiv = document.getElementById('addresslist');
	
	for(i = 0; i < this.addresses.length; i++)
	{
		oAddressLink = document.createElement("a");
		oAddressLink.id = 'address_'+i;
		oAddressLink.setAttribute('href', '#null');
		oAddressLink.onclick = confirmAddress;

		sAddressString = this.addresses[i].line1;
		sAddressString += ((this.addresses[i].line2.length) ? ', ' + this.addresses[i].line2 : '');
		//sAddressString += ((this.addresses[i].address3.length) ? ', ' + this.addresses[i].line3 : '');
		sAddressString += ((this.addresses[i].town.length) ? ', ' + this.addresses[i].town + '<br>' : '');
		sAddressString += ((this.addresses[i].county.length) ? this.addresses[i].county + '<br>' : '');
		sAddressString += this.addresses[i].postcode;
		
		oAddressLink.innerHTML = sAddressString;

		oDiv.appendChild(oAddressLink);
	}
	if (oAddressSearch.orgVal == 1) { sSearchAgainMsg = 'not showing commercial properties - click here to search again' }
	else if (this.addresses.length == 0) { sSearchAgainMsg = 'sorry, no addresses found - click here to search again' } 
	else { sSearchAgainMsg = 'address not listed? - click here to search again' }
	document.getElementById('link_addresssearchagain').removeChild(document.getElementById('link_addresssearchagain').firstChild)
	document.getElementById('link_addresssearchagain').appendChild(document.createTextNode(sSearchAgainMsg));
	document.getElementById('regform').style.display = 'none';
	document.getElementById('selectaddress').style.display = 'block';
	document.getElementById('selectcaptcha').style.display = 'block';
}