/**
*	Address Functions
*/



function ToggleAddress(strValue, Address2ID, Address3ID, TownID, CountyID, ButtonID)
{
	if(strValue == 'United Kingdom' || strValue == 'Northern Ireland' || strValue == 'Channel Islands')
	{
		document.getElementById(Address2ID+'_Row').style.display  = 'none';
		document.getElementById(Address3ID+'_Row').style.display  = 'none';
		document.getElementById(TownID+'_Row').style.display      = 'none';
		document.getElementById(CountyID+'_Row').style.display    = 'none';
		document.getElementById(ButtonID+'_Button').style.display = 'block';
	}
	else
	{
		document.getElementById(Address2ID+'_Row').style.display  = strRowShow;
		document.getElementById(Address3ID+'_Row').style.display  = strRowShow;
		document.getElementById(TownID+'_Row').style.display      = strRowShow;
		document.getElementById(CountyID+'_Row').style.display    = strRowShow;
		document.getElementById(ButtonID+'_Button').style.display = 'none';
	}
}


/*
*	Issue an AJAX request to get the available destination for the chosen country
*/
function PostcodeLookup(StreetID, PostcodeID, Address2ID, Address3ID, TownID, CountyID)
{
	var resXMLHTTP;
	var strStreet   = document.getElementById(StreetID).value;
	var strPostcode = document.getElementById(PostcodeID).value;
	
	objMenu                = document.getElementById(PostcodeID + '_Menu');
	objMenu.options.length = 0;
	
	resXMLHTTP = GetXMLHttpObject();
	if(resXMLHTTP == null)
	{
		alert("Your browser does not support XMLHTTP!");
		return;
	}
	
	resXMLHTTP.onreadystatechange=function()
	{
		if(resXMLHTTP.readyState==4)
		{
			var xmlDoc = resXMLHTTP.responseXML.documentElement;

			if(xmlDoc)
			{
				arrResults = xmlDoc.getElementsByTagName('searchResult');
				
				if(arrResults)
				{

					if(arrResults.length > 0)
					{
						for(intLoop=0; intLoop< arrResults.length; intLoop++)
						{
							objResult = arrResults[intLoop];
	
							strDisplay = objResult.getElementsByTagName("display")[0].childNodes[0].nodeValue;
							intID      = objResult.getElementsByTagName("resultId")[0].childNodes[0].nodeValue;
	
							objMenu.options[objMenu.options.length] = new Option(strDisplay,strDisplay);
						}
					
						document.getElementById(PostcodeID + '_Fetching').style.display='none';
						document.getElementById(PostcodeID + '_Results').style.display='block';
						document.getElementById(PostcodeID + '_NoResults').style.display='none';
					}
					else
					{
						document.getElementById(PostcodeID + '_Fetching').style.display='none';
						document.getElementById(PostcodeID + '_Results').style.display='none';
						document.getElementById(PostcodeID + '_NoResults').style.display='block';

						/**
						*	Show all address divs
						*/
						document.getElementById(Address2ID + '_Row').style.display = strRowShow;
						document.getElementById(Address3ID + '_Row').style.display = strRowShow;
						document.getElementById(TownID + '_Row').style.display     = strRowShow;
						document.getElementById(CountyID + '_Row').style.display   = strRowShow;
					}
				}
				else
				{
					document.getElementById(PostcodeID + '_Fetching').style.display='none';
					document.getElementById(PostcodeID + '_Results').style.display='none';
					document.getElementById(PostcodeID + '_NoResults').style.display='block';

					/**
					*	Show all address divs
					*/
					document.getElementById(Address2ID + '_Row').style.display = strRowShow;
					document.getElementById(Address3ID + '_Row').style.display = strRowShow;
					document.getElementById(TownID + '_Row').style.display     = strRowShow;
					document.getElementById(CountyID + '_Row').style.display   = strRowShow;
				}
			}
		}
	}

	strURL = "/cgi-bin/postcode.cgi?postcode="+escape(strPostcode)+'&street='+escape(strStreet);
	resXMLHTTP.open("GET", strURL, true);
	resXMLHTTP.send(null);
	
	/**
	*	Show 'fetching' div
	*/
	document.getElementById(PostcodeID + '_Fetching').style.display='block';
	document.getElementById(PostcodeID + '_Results').style.display='none';
	document.getElementById(PostcodeID + '_NoResults').style.display='none';
}



function PostcodeSelect(strAddress, PostcodeID, Address1ID, Address2ID, Address3ID, TownID, CountyID)
{
	var arrAddress = strAddress.split(', ');
	
	switch (arrAddress.length)
	{
		case 2:
			strAddress1    = arrAddress[0];
			strAddress2    = '';
			strAddress3    = '';
			strTown        = '';
			strCounty      = arrAddress[1];
		break;

		case 3:
			strAddress1    = arrAddress[0];
			strAddress2    = '';
			strAddress3    = '';
			strTown        = arrAddress[1];
			strCounty      = arrAddress[2];
		break;

		case 4:
			strAddress1    = arrAddress[0];
			strAddress2    = arrAddress[1];
			strAddress3    = '';
			strTown        = arrAddress[2];
			strCounty      = arrAddress[3];
		break;

		case 5:
			strAddress1    = arrAddress[0];
			strAddress2    = arrAddress[1];
			strAddress3    = arrAddress[2];
			strTown        = arrAddress[3];
			strCounty      = arrAddress[4];
		break;

		case 6:
			strAddress1    = arrAddress[0];
			strAddress2    = arrAddress[1]+', '+arrAddress[2];
			strAddress3    = arrAddress[3];
			strTown        = arrAddress[4];
			strCounty      = arrAddress[5];
		break;

		case 7:
			strAddress1    = arrAddress[0];
			strAddress2    = arrAddress[1]+', '+arrAddress[2];
			strAddress3    = arrAddress[3]+', '+arrAddress[4];
			strTown        = arrAddress[5];
			strCounty      = arrAddress[6];
		break;
	}


	document.getElementById(Address1ID).value    = strAddress1;
	document.getElementById(Address2ID).value    = strAddress2;
	document.getElementById(Address3ID).value    = strAddress3;
	document.getElementById(TownID).value        = strTown;
	document.getElementById(CountyID).value      = strCounty;
	
	/**
	*	Hide all Lookup related divs
	*/
	document.getElementById(PostcodeID + '_Fetching').style.display  = 'none';
	document.getElementById(PostcodeID + '_Results').style.display   = 'none';
	document.getElementById(PostcodeID + '_NoResults').style.display = 'none';
	document.getElementById(PostcodeID + '_Button').style.display    = 'none';

	/**
	*	Show all address divs
	*/
	document.getElementById(Address2ID + '_Row').style.display = strRowShow;
	document.getElementById(Address3ID + '_Row').style.display = strRowShow;
	document.getElementById(TownID + '_Row').style.display     = strRowShow;
	document.getElementById(CountyID + '_Row').style.display   = strRowShow;
}



function GetXMLHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	
	
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}