function addOption(selectObject,optionText,optionValue) 
{
    var optionObject = new Option(optionText,optionValue)
    var optionRank = selectObject.options.length
    selectObject.options[optionRank]=optionObject
}

function removeAllObjects(selectObject)
{
	while (selectObject.options.length > 0)
	{
			selectObject.options[0] = null;
	} 
}


function CLocationAjax()
{
	this.PostCodeId;
	this.RegionId;
	this.CountryId;
	this.SuburbId;
	this.StateId;
	this.SmallRegionId;
	
	this.PostCodeSelect;
	this.RegionSelect;
	this.SuburbSelect;
	this.StateSelect;
	this.CountrySelect;


	this.SetPostCodeId = function(PostcodeId)
	{
		this.PostCodeId = PostcodeId
	}
	
	this.SetRegionId = function(RegionId)
	{
		this.RegionId = RegionId;
	}

	this.SetCountryId = function(CountryId)
	{
		this.CountryId = CountryId
	}

	this.SetSuburbId = function(SuburbId)
	{
		this.SuburbId = SuburbId;
	}

	this.SetStateId = function(StateId)
	{
		this.StateId = StateId
	}
	this.SetSmallRegionId = function(SmallRegionId)
	{
		this.SmallRegionId = SmallRegionId;
	}

	this.CountrySelect = function(select)
	{
		var value = select[select.selectedIndex].value;
		var url = 'GetStates.php?CountryId='+value;
		var AJAX = new CAJAX();

		AJAX.executeRequest('GET', url, AJAX.getToken(), 'includes/xml/');

		var oSelect = document.getElementById(this.StateId);
		removeAllObjects(oSelect);
		addOption(oSelect,'Loading',-1);
		oSelect.disabled = true;

		oLocation = this;
		AJAX.request.onreadystatechange = function ()
		{
			if (AJAX.getReadyState() == 'OK')
			{
				var oSelect = document.getElementById(oLocation.StateId);
				var states = AJAX.request.responseXML.getElementsByTagName('state');
				removeAllObjects(oSelect);
				addOption(oSelect,'Choose a State . . .', -1)
				for (i=0;i < states.length ;i++ )
				{
					addOption(oSelect,states[i].firstChild.nodeValue ,states[i].attributes[0].value);
				}

				oSelect.disabled = false;
				
			}
		}
		document.getElementById(this.RegionId).disabled = true;
		document.getElementById(this.SmallRegionId).disabled = true;
		document.getElementById(this.SuburbId).disabled = true;
		AJAX.request.send(null);
	}

	this.StateSelect = function(select)
	{
		var value = select[select.selectedIndex].value;
		var url = 'GetRegions.php?StateId='+value;
		var AJAX = new CAJAX();
		
		AJAX.executeRequest('GET', url, AJAX.getToken(), 'includes/xml/');

		var oSelect = document.getElementById(this.RegionId);
		oSelect.disabled = true;

		removeAllObjects(oSelect);
		addOption(oSelect,'Loading',-1);

		oLocation = this;
		AJAX.request.onreadystatechange = function ()
		{
			if (AJAX.getReadyState() == 'OK')
			{
				var oSelect = document.getElementById(oLocation.RegionId);
				var regions = AJAX.request.responseXML.getElementsByTagName('region');
				removeAllObjects(oSelect);
				addOption(oSelect,'Choose a Region . . .',-1)
				for (i=0;i < regions.length ;i++ )
				{
					addOption(oSelect,regions[i].firstChild.nodeValue,regions[i].attributes[0].value)
				}
				
				oSelect.disabled = false;
				
			}
		}
		document.getElementById(this.RegionId).disabled = true;
		document.getElementById(this.SmallRegionId).disabled = true;
		document.getElementById(this.SuburbId).disabled = true;
		AJAX.request.send(null);
	}

	this.RegionSelect = function(select)
	{
		var value = select[select.selectedIndex].value;
		var url = 'GetSmallRegions.php?RegionId='+value;
		var AJAX = new CAJAX();
		
		AJAX.executeRequest('GET', url, AJAX.getToken(), 'includes/xml/');

		var oSelect = document.getElementById(this.SmallRegionId);
		oSelect.disabled = true;

		removeAllObjects(oSelect);
		addOption(oSelect,'Loading',-1);

		oLocation = this;
		AJAX.request.onreadystatechange = function ()
		{
			if (AJAX.getReadyState() == 'OK')
			{
				var oSelect = document.getElementById(oLocation.SmallRegionId);
				
				removeAllObjects(oSelect);
				addOption(oSelect,'Choose a Sub Region . . .',-1);

				var smallRegions = AJAX.request.responseXML.getElementsByTagName('smallregion');
				for (i=0;i < smallRegions.length ;i++ )
				{
					addOption(oSelect,smallRegions[i].firstChild.nodeValue,smallRegions[i].attributes[0].value);
				}

				oSelect.disabled = false;
				
			}
		}
		document.getElementById(this.SmallRegionId).disabled = true;
		document.getElementById(this.SuburbId).disabled = true;
		AJAX.request.send(null);
	}


	this.CountrySelectAny = function(select)
	{
		var value = select[select.selectedIndex].value;

		if (value != '' && value != -1)
		{

			var url = 'GetStates.php?CountryId='+value;
			var AJAX = new CAJAX();

			AJAX.executeRequest('GET', url, AJAX.getToken(), 'includes/xml/');

			var oSelect = document.getElementById(this.StateId);
			removeAllObjects(oSelect);
			addOption(oSelect,'Loading',-1);
			oSelect.disabled = true;

			oLocation = this;
		AJAX.request.onreadystatechange = function ()
			{
				if (AJAX.getReadyState() == 'OK')
				{
					var oSelect = document.getElementById(oLocation.StateId);
					var states = AJAX.request.responseXML.getElementsByTagName('state');
					removeAllObjects(oSelect);
					addOption(oSelect,'Any State', -1)
					for (i=0;i < states.length ;i++ )
					{
						addOption(oSelect,states[i].firstChild.nodeValue ,states[i].attributes[0].value);
					}

					oSelect.disabled = false;
					
				}
			}
			document.getElementById(this.RegionId).disabled = true;
			document.getElementById(this.SmallRegionId).disabled = true;
			document.getElementById(this.SuburbId).disabled = true;
			AJAX.request.send(null);
		}
		else
		{
			var oSelect = document.getElementById(this.StateId);
			removeAllObjects(oSelect);
			addOption(oSelect,'Any State',-1);
			oSelect.disabled = true;


			oSelect = document.getElementById(this.RegionId);
			removeAllObjects(oSelect);
			addOption(oSelect,'Any Region',-1);
			oSelect.disabled = true;

			oSelect = document.getElementById(this.SmallRegionId);
			removeAllObjects(oSelect);
			addOption(oSelect,'Any Sub Region',-1);
			oSelect.disabled = true;

			oSelect = document.getElementById(this.SuburbId);
			removeAllObjects(oSelect);
			addOption(oSelect,'Any Suburb',-1);
			oSelect.disabled = true;

			

		}
	}

	this.StateSelectAny = function(select)
	{
		var value = select[select.selectedIndex].value;
		if (value != '' && value != -1)
		{
			var url = 'GetRegions.php?StateId='+value;
			var AJAX = new CAJAX();
			
			AJAX.executeRequest('GET', url, AJAX.getToken(), 'includes/xml/');

			var oSelect = document.getElementById(this.RegionId);
			oSelect.disabled = true;

			removeAllObjects(oSelect);
			addOption(oSelect,'Loading',-1);

			oLocation = this;
		AJAX.request.onreadystatechange = function ()
			{
				if (AJAX.getReadyState() == 'OK')
				{
					var oSelect = document.getElementById(oLocation.RegionId);
					var regions = AJAX.request.responseXML.getElementsByTagName('region');
					removeAllObjects(oSelect);
					addOption(oSelect,'Any Region',-1)
					for (i=0;i < regions.length ;i++ )
					{
						addOption(oSelect,regions[i].firstChild.nodeValue,regions[i].attributes[0].value)
					}
					
					oSelect.disabled = false;
					
				}
			}
			document.getElementById(this.RegionId).disabled = true;
			document.getElementById(this.SmallRegionId).disabled = true;
			document.getElementById(this.SuburbId).disabled = true;
			AJAX.request.send(null);
		}
		else
		{
			oSelect = document.getElementById(this.RegionId);
			removeAllObjects(oSelect);
			addOption(oSelect,'Any Region',-1);
			oSelect.disabled = true;

			oSelect = document.getElementById(this.SmallRegionId);
			removeAllObjects(oSelect);
			addOption(oSelect,'Any Sub Region',-1);
			oSelect.disabled = true;

			oSelect = document.getElementById(this.SuburbId);
			removeAllObjects(oSelect);
			addOption(oSelect,'Any Suburb',-1);
			oSelect.disabled = true;
		}
	}

	this.RegionSelectAny = function(select)
	{
		var value = select[select.selectedIndex].value;
		if (value != '' && value != -1)
		{

			var url = 'GetSmallRegions.php?RegionId='+value;
			var AJAX = new CAJAX();
			
			AJAX.executeRequest('GET', url, AJAX.getToken(), 'includes/xml/');

			var oSelect = document.getElementById(this.SmallRegionId);
			oSelect.disabled = true;

			removeAllObjects(oSelect);
			addOption(oSelect,'Loading',-1);

			oLocation = this;
		AJAX.request.onreadystatechange = function ()
			{
				if (AJAX.getReadyState() == 'OK')
				{
					var oSelect = document.getElementById(oLocation.SmallRegionId);
					
					removeAllObjects(oSelect);
					addOption(oSelect,'Any Sub Region',-1);

					var smallRegions = AJAX.request.responseXML.getElementsByTagName('smallregion');
					for (i=0;i < smallRegions.length ;i++ )
					{
						addOption(oSelect,smallRegions[i].firstChild.nodeValue,smallRegions[i].attributes[0].value);
					}

					oSelect.disabled = false;
					
				}
			}
			document.getElementById(this.SmallRegionId).disabled = true;
			document.getElementById(this.SuburbId).disabled = true;
			AJAX.request.send(null);
		}
		else
		{

			oSelect = document.getElementById(this.SmallRegionId);
			removeAllObjects(oSelect);
			addOption(oSelect,'Any Sub Region',-1);
			oSelect.disabled = true;

			oSelect = document.getElementById(this.SuburbId);
			removeAllObjects(oSelect);
			addOption(oSelect,'Any Suburb',-1);
			oSelect.disabled = true;
		}
	}

	this.RegionSelectToSuburb = function(select)
	{
		var value = select[select.selectedIndex].value;
		var url = 'GetSuburbs.php?RegionId='+value;
		var AJAX = new CAJAX();
		
		AJAX.executeRequest('GET', url, AJAX.getToken(), 'includes/xml/');

		var oSelect = document.getElementById(this.SuburbId);
		oSelect.disabled = true;

		removeAllObjects(oSelect);
		addOption(oSelect,'Loading',-1);



		oLocation = this;
		AJAX.request.onreadystatechange = function ()
		{
			if (AJAX.getReadyState() == 'OK')
			{
				var oSelect = document.getElementById(oLocation.SuburbId);
				var suburbs = AJAX.request.responseXML.getElementsByTagName('suburb');
				removeAllObjects(oSelect);
				addOption(oSelect,'**** ALL SUBURBS ****',-1);
				for (i=0;i < suburbs.length ;i++ )
				{
					addOption(oSelect,suburbs[i].firstChild.nodeValue,suburbs[i].attributes[0].value);
				}

				oSelect.disabled = false;
				
			}
		}
		document.getElementById(this.SuburbId).disabled = true;
		AJAX.request.send(null);
	}
	this.SmallRegionSelectAllSuburbs = function(select)
	{
		var value = select[select.selectedIndex].value;
		var url = 'GetSuburbs.php?SmallRegionId='+value;
		var AJAX = new CAJAX();
		
		AJAX.executeRequest('GET', url, AJAX.getToken(), 'includes/xml/');

		var oSelect = document.getElementById(this.SuburbId);
		oSelect.disabled = true;

		removeAllObjects(oSelect);
		addOption(oSelect,'Loading',-1);

		oLocation = this;
		AJAX.request.onreadystatechange = function ()
		{
			if (AJAX.getReadyState() == 'OK')
			{
				var oSelect = document.getElementById(oLocation.SuburbId);
				var suburbs = AJAX.request.responseXML.getElementsByTagName('suburb');
				removeAllObjects(oSelect);
				addOption(oSelect,'**** ALL SUBURBS ****',-1)
				for (i=0;i < suburbs.length ;i++ )
				{
					addOption(oSelect,suburbs[i].firstChild.nodeValue,suburbs[i].attributes[0].value);
				}

				oSelect.disabled = false;
				
			}
		}
		document.getElementById(this.SuburbId).disabled = true;
		AJAX.request.send(null);
	}

	this.SmallRegionSelect = function(select)
	{
		var value = select[select.selectedIndex].value;
		var url = 'GetSuburbs.php?SmallRegionId='+value;
		var AJAX = new CAJAX();
		
		AJAX.executeRequest('GET', url, AJAX.getToken(), 'includes/xml/');

		var oSelect = document.getElementById(this.SuburbId);
		oSelect.disabled = true;

		removeAllObjects(oSelect);
		addOption(oSelect,'Loading',-1);

		oLocation = this;
		AJAX.request.onreadystatechange = function ()
		{
			if (AJAX.getReadyState() == 'OK')
			{
				var oSelect = document.getElementById(oLocation.SuburbId);
				var suburbs = AJAX.request.responseXML.getElementsByTagName('suburb');
				removeAllObjects(oSelect);
				addOption(oSelect,'Choose a Suburb . . .',-1)
				for (i=0;i < suburbs.length ;i++ )
				{
					addOption(oSelect,suburbs[i].firstChild.nodeValue,suburbs[i].attributes[0].value);
				}
				oSelect.disabled = false;
			}
		}
		document.getElementById(this.SuburbId).disabled = true;
		AJAX.request.send(null);
	}

	this.PostCodeSelect = function(select)
	{
		var value = select[select.selectedIndex].value;
		var url = 'GetSuburbs.php?PostCodeId='+value;
		var AJAX = new CAJAX();
		
		AJAX.executeRequest('GET', url, AJAX.getToken(), 'includes/xml/');

		var oSelect = document.getElementById(this.SuburbId);
		oSelect.disabled = true;

		removeAllObjects(oSelect);
		addOption(oSelect,'Loading',-1);

		oLocation = this;
		AJAX.request.onreadystatechange = function ()
		{
			if (AJAX.getReadyState() == 'OK')
			{
				var oSelect = document.getElementById(oLocation.SuburbId);
				var suburbs = AJAX.request.responseXML.getElementsByTagName('suburb');
				removeAllObjects(oSelect);
				addOption(oSelect,'Choose a Suburb . . .',-1)
				for (i=0;i < suburbs.length ;i++ )
				{
					addOption(oSelect,suburbs[i].firstChild.nodeValue,suburbs[i].attributes[0].value);
				}

				oSelect.disabled = false;
				
			}
		}
		AJAX.request.send(null);
	}

	this.GetPostCode = function(suburbSelect)
	{
		var value = suburbSelect[suburbSelect.selectedIndex].value;
		var url = 'GetPostCodes.php?SuburbId='+value;
		var AJAX = new CAJAX();

		AJAX.executeRequest('GET', url, AJAX.getToken(), 'includes/xml/');

		AJAX.request.onreadystatechange = function()
		{
			if (AJAX.getReadyState() == 'OK')
			{
				var oInput = document.getElementById(oLocation.PostCodeId);
				var postcode = AJAX.request.responseXML.getElementsByTagName('postcode');
				oInput.value= postcode[0].firstChild.nodeValue;
			}
		}
		AJAX.request.send(null);
	}

}

var oLocation = new CLocationAjax();

