
// declare a global  XMLHTTP Request object
var XmlHttpObj;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}

// called from onChange or onClick event of the continent dropdown list
function CountryListOnChange() 
{
	document.main.cityList.disabled = true;
	var countryList = document.getElementById("countryList");

	var selectedCountry = countryList.options[countryList.selectedIndex].value;
    var requestUrl;
	var myURL = document.URL;
	var pairs = myURL.split("/");
	var domain_name;
	domain_name = pairs[2];
	requestUrl = "http://www.greatgolf.com/xml.asp?filter1=" + encodeURIComponent(selectedCountry);
    
	CreateXmlHttpObj();

	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandler;
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);		
		setTimeout('document.main.cityList.disabled = false;', 2000);
	}

}

function StateChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateCityList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the country dropdown list
function PopulateCityList(cityNode)
{
	document.main.cityList.disabled=true;
	var cityList = document.getElementById("cityList");
	// clear the country list 
	for (var count = cityList.options.length-1; count >-1; count--)
	{
		cityList.options[count] = null;
	}
	
	var cityNodes = cityNode.getElementsByTagName('city');
	var idValue ;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < cityNodes.length; count++)
	{
   		textValue = GetInnerText(cityNodes[count]);
		idValue = cityNodes[count].getAttribute("id");
		//alert(textValue);
//		alert(textValue);
		optionItem = new Option( textValue, idValue,  false, false);
		cityList.options[cityList.length] = optionItem;
	}
//	document.main.cityList.disabled=false;
}

// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}


function CityListOnChange() 
{
    var cityList = document.getElementById("cityList");
	var countryListQ = document.getElementById("countryList");
    var country_selected = countryListQ.options[countryListQ.selectedIndex].value;
    var selectedCity = cityList.options[cityList.selectedIndex].value;
    var requestUrl;
	var myURL = document.URL;
	var pairs = myURL.split("/");
	var domain_name;
	domain_name = pairs[2];
	requestUrl = "http://www.greatgolf.com/xml.asp?filter4=" + encodeURIComponent(selectedCity) + "&active_country=" + country_selected;
    //alert(requestUrl)
	CreateXmlHttpObj();
	
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandler2;
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);		
	}
}


function StateChangeHandler2()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateLocationList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the country dropdown list
function PopulateLocationList(locationNode)
{

	var HotelList = document.getElementById("HotelList");
	// clear the country list 


for (var count = HotelList.options.length-1; count >-1; count--)
	{
		HotelList.options[count] = null;
	}

	var locationNodes = locationNode.getElementsByTagName('hotel');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < locationNodes.length; count++)
	{
   		textValue = GetInnerText(locationNodes[count]);

	idValue = locationNodes[count].getAttribute("id");

		optionItem = new Option( textValue, idValue,  false, false);
	
	HotelList.options[HotelList.length] = optionItem;
	}
			
}

function OnCountryLoad() 
{
    var countryList = document.getElementById("countryList");
    var requestUrl;
	var myURL = document.URL;
	var pairs = myURL.split("/");
	var domain_name;
	domain_name = pairs[2];
	requestUrl = "http://www.greatgolf.com/xml.asp?filter5=" + encodeURIComponent("Hotel");
	
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandler3;
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);		
	}
}

function PopulateCountryList(countryNode)
{

	var countryList = document.getElementById("countryList");


for (var count = countryList.options.length-1; count >-1; count--)
	{
		countryList.options[count] = null;
		cityList.options[count] = null;
		HotelList.options[count] = null;
	}


for (var count = countryList.options.length-1; count >-1; count--)
	{
		HotelList.options[count] = null;
	}

	var countryNodes = countryNode.getElementsByTagName('country');
	var idValue ;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < countryNodes.length; count++)
	{
   		textValue = GetInnerText(countryNodes[count]);
		idValue = countryNodes[count].getAttribute("id");
		//alert(textValue);
		
		optionItem = new Option( textValue, idValue,  false, false);
		countryList.options[countryList.length] = optionItem;
	}
}

function StateChangeHandler3()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateCountryList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}
