//Gets called when Region combo box selection changes
function RegionByLocationOnChange() 
{
	
	//Getting the selected region from region combo box.
	var regionID = document.getElementById("ddlRegionByLocationClientID").value;
	var regionList = document.getElementById(regionID);
	var selectedRegCode = regionList.options[regionList.selectedIndex].value;
	
	// URL to get states for a given country
	var requestUrl = "/searchapt/updateCity.aspx?regcode=" + encodeURIComponent(selectedRegCode);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = UpdateCity;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}


//Called when response comes back from server
function UpdateCity()
{
		
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		
		if(XmlHttp.status == 200)
		{		
			//alert(XmlHttp.responseText);
			document.getElementById("txtCity").innerHTML=XmlHttp.responseText;
			SetCurrentCity();
			CityOnChange();
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

function getQueryVariable(variable) 
{ 
	var query = window.location.search.substring(1); 
	var vars = query.split("&"); 
	for (var i=0;i<vars.length;i++) 
	{ 
		var pair = vars[i].split("="); 
		if (pair[0] == variable) { 
				return pair[1]; 
		} 
	} 
} 

function SetCurrentCity()
{
	var curr_city = getQueryVariable("city");
	if (curr_city != undefined)
	{
		var strspace=" "
		var strnewcity=curr_city.replace(/%20/g,strspace);
		var cityList = document.getElementById("ddlCity");
		for (i=0; i<cityList.length; i++)
		{
				if (cityList.options[i].value == strnewcity) 
				{
						cityList.options[i].selected = true;
				}
		}
	}
}

//Gets called when City combo box selection changes
function CityOnChange() 
{
	//Getting the selected region from region combo box.
	var regionID = document.getElementById("ddlRegionByLocationClientID").value;
	var regionList = document.getElementById(regionID);
	var selectedRegCode = regionList.options[regionList.selectedIndex].value;

	//Getting the selected city from city combo box.
	var cityList = document.getElementById("ddlCity");
	var selectedCity = cityList.options[cityList.selectedIndex].value;
	
	// URL to get states for a given country
	var requestUrl = "/searchapt/updateCommunity.aspx?regcode=" + encodeURIComponent(selectedRegCode) + "&city=" + encodeURIComponent(selectedCity);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = UpdateCommunity;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

function SetCurrentCommunity()
{
	var curr_community = getQueryVariable("communitycode");
	if (curr_community != undefined)
	{
		var communityList = document.getElementById("ddlCommunity");
		for (i=0; i<communityList.length; i++)
		{
				if (communityList.options[i].value == curr_community) 
				{
						communityList.options[i].selected = true;
				}
		}
	}
}

//Called when response comes back from server
function UpdateCommunity()
{
		
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		
		if(XmlHttp.status == 200)
		{		
			//alert(XmlHttp.responseText);
			document.getElementById("txtCommunity").innerHTML=XmlHttp.responseText;
			SetCurrentCommunity();
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}
