
//Global XMLHTTP Request object
var XmlHttp;



//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}

function CountryListLoad()
{
	// URL to get countries
	var requestUrl = AjaxServerPageName + "?LoadCountries=1";
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleCountries;
		
		//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);		
	}
}

//Gets called when country combo box selection changes
function CountryListOnChange() 
{
	var countryList = document.getElementById("ctl00_mainContentPlaceHolder_countryList");

	//Getting the selected country from country combo box.
	var selectedCountry = countryList.options[countryList.selectedIndex].value;
	
	// URL to get states for a given country
	var requestUrl = AjaxServerPageName + "?SelectedCountry=" + encodeURIComponent(selectedCountry);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;
		
		//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 HandleCountries()
{
	// 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)
		{			
		    var imgLoading_Countries = document.getElementById("imgLoading_Countries");
            imgLoading_Countries.style.visibility = 'hidden';
            imgLoading_Countries.style.display = 'none';
            var countryList = document.getElementById("ctl00_mainContentPlaceHolder_countryList");
            countryList.disabled = ''
	        //Clears the location combo box contents.
	        for (var count = countryList.options.length-1; count >-1; count--)
	        {
		        countryList.options[count] = null;
	        }
	        //var locationNodes = cityNode.getElementsByTagName('ctl00$locations');
	        var countryNodes = XmlHttp.responseXML.documentElement.getElementsByTagName('country');
	        var textValue;
	        var optionItem;
	        //Add new locations list to the locations combo box.
	        //optionItem = new Option('All locations', '',  false, false);
	        //countryList.options[countryList.length] = optionItem;
	        for (var count = 0; count < countryNodes.length; count++)
	        {
   		        textValue = GetInnerText(countryNodes[count]);
   		        if (textValue != '')
   		        {
		        optionItem = new Option(textValue, textValue,  false, false);
		        countryList.options[countryList.length] = optionItem;
		        }
            }
            CountryListOnChange();
		}
		else
		{
			alert("There was a problem retrieving country list from the server." );
    	    var imgLoading_Countries = document.getElementById("imgLoading_Countries");
            imgLoading_Countries.style.visibility = 'hidden';
            imgLoading_Countries.style.display = 'none';
		}
	}
	else
        {
       	    var imgLoading_Countries = document.getElementById("imgLoading_Countries");
			imgLoading_Countries.style.visibility = 'visible';
            imgLoading_Countries.style.display = 'inline';
            var countryList = document.getElementById("ctl00_mainContentPlaceHolder_countryList");
		    //countryList.disabled = 'true';
            var cityList = document.getElementById("ctl00_mainContentPlaceHolder_cityList");
		    cityList.disabled = 'true';
		    var locationList = document.getElementById("ctl00_mainContentPlaceHolder_locationList");
		    locationList.disabled = 'true';
		    var optionItem;
	        //optionItem = new Option('Loading...', '',  false, false);
	        optionItem = new Option('Australia', 'Australia',  false, false);
	        countryList.options[countryList.length] = optionItem;
        }
}


//Called when response comes back from server
function HandleResponse()
{
	// 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)
		{			
			ClearAndSetCityListItems(XmlHttp.responseXML.documentElement);
		    var imgLoading_Cities = document.getElementById("imgLoading_Cities");
            imgLoading_Cities.style.visibility = 'hidden';
            imgLoading_Cities.style.display = 'none';
            var cityList = document.getElementById("ctl00_mainContentPlaceHolder_cityList");
		    cityList.disabled = '';
		    var locationList = document.getElementById("ctl00_mainContentPlaceHolder_locationList");
		    locationList.disabled = '';
            CityListOnChange();
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
	else
        {
       	    var imgLoading_Cities = document.getElementById("imgLoading_Cities");
			imgLoading_Cities.style.visibility = 'visible';
            imgLoading_Cities.style.display = 'inline';
            var cityList = document.getElementById("ctl00_mainContentPlaceHolder_cityList");
		    cityList.disabled = 'false';
		    var locationList = document.getElementById("ctl00_mainContentPlaceHolder_locationList");
		    locationList.disabled = 'false';
		    var optionItem;
		    optionItem = new Option('Loading...', '',  false, false);
	        cityList.options[cityList.length] = optionItem;
		    optionItem = new Option('Loading...', '',  false, false);
	        locationList.options[locationList.length] = optionItem;
        }
}



//Clears the contents of state combo box and adds the states of currently selected country
function ClearAndSetCityListItems(countryNode)
{
    var cityList = document.getElementById("ctl00_mainContentPlaceHolder_cityList");
	//Clears the state combo box contents.
	for (var count = cityList.options.length-1; count >-1; count--)
	{
		cityList.options[count] = null;
	}
    var locationList = document.getElementById("ctl00_mainContentPlaceHolder_locationList");
	//Clears the location combo box contents.
	for (var count = locationList.options.length-1; count >-1; count--)
	{
		locationList.options[count] = null;
	}
	
	var cityNodes = countryNode.getElementsByTagName('cities');
	var textValue;
	var optionItem;
	//Add new states list to the state combo box.
	for (var count = 0; count < cityNodes.length; count++)
	{
   		textValue = GetInnerText(cityNodes[count]);
   		//if (textvalue == '')
   		//{
   		//optionItem = new Option('All cities', '', false, false);
	    //locationList.options[locationList.length] = optionItem;
   		//}
        //else
        //{
		optionItem = new Option(textValue, textValue, false, false);
		cityList.options[cityList.length] = optionItem;
		//}
	}
}

//Returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}




//Gets called when city combo box selection changes
function CityListOnChange() 
{
	var cityList = document.getElementById("ctl00_mainContentPlaceHolder_cityList");

	//Getting the selected city from city combo box.
	var selectedCity = cityList.options[cityList.selectedIndex].value;
	
	// URL to get locations for a given city
	var requestUrl = AjaxServerPageName + "?SelectedCity=" + encodeURIComponent(selectedCity);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleLocations;
		
		//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 HandleLocations()
{
	// 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)
		{	
			ClearAndSetLocationListItems(XmlHttp.responseXML.documentElement);
		    var imgLoading_Locations = document.getElementById("imgLoading_Locations");
            imgLoading_Locations.style.visibility = 'hidden';
            imgLoading_Locations.style.display = 'none';
		    var locationList = document.getElementById("ctl00_mainContentPlaceHolder_locationList");
		    locationList.disabled = ''
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
	else
        {
       	    var imgLoading_Locations = document.getElementById("imgLoading_Locations");
			imgLoading_Locations.style.visibility = 'visible';
            imgLoading_Locations.style.display = 'inline';
		    var locationList = document.getElementById("ctl00_mainContentPlaceHolder_locationList");
		    locationList.disabled = 'true'
		    var optionItem;
		    optionItem = new Option('Loading...', '',  false, false);
	        locationList.options[locationList.length] = optionItem;

        }
}


//Clears the contents of locations combo box and adds the locations of currently selected city
function ClearAndSetLocationListItems(cityNode)
{
    var locationList = document.getElementById("ctl00_mainContentPlaceHolder_locationList");
	//Clears the location combo box contents.
	for (var count = locationList.options.length-1; count >-1; count--)
	{
		locationList.options[count] = null;
	}

	//var locationNodes = cityNode.getElementsByTagName('ctl00$locations');
	var locationNodes = cityNode.getElementsByTagName('locations');
	var textValue;
	var optionItem;
	//Add new locations list to the locations combo box.
	optionItem = new Option('All locations', '',  false, false);
	locationList.options[locationList.length] = optionItem;
	for (var count = 0; count < locationNodes.length; count++)
	{
   		textValue = GetInnerText(locationNodes[count]);
   		if (textValue != '')
   		{
		optionItem = new Option(textValue, textValue,  false, false);
		locationList.options[locationList.length] = optionItem;
		}
    }
}
