
//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();
	}
}

//Gets called when country combo box selection changes
function destinationChanged(destination) 
{
	//Getting the selected country from country combo box.

	//alert('destination: ' + destination);	
	if (destination != '0') {
		// URL to get states for a given country
		var requestUrl = AjaxServerPageName + "?x=" + encodeURIComponent(destination);
                        document.getElementById("validateDestinationGateway").style.display='none';
		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);		
		}
	}
	else { 
		var resortList = document.getElementById("ctl00_cph1_Inc_iq1_resort");
		for (var count = resortList.options.length-1; count >-1; count--)
		{
			resortList.options[count] = null;
		}

		var textValue; 
		var optionItem;
		//Add new states list to the state combo box.

		optionItemSelect = new Option(' - Select Destination First -', '0',  false, false);
		resortList.options[0] = optionItemSelect;
		resortList.selected = 0;

		resortList.disabled = true;
	}
}


//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)
		{			
//			test = XmlHttp.responseXML.documentElement
//			alert(test.tagName)
			ClearAndSetResortListItems(XmlHttp.responseXML.documentElement);
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

//Clears the contents of state combo box and adds the states of currently selected country
function ClearAndSetResortListItems(destinationNode)
{
    var resortList = document.getElementById("ctl00_cph1_Inc_iq1_resort");
	//Clears the resort combo box contents.
	for (var count = resortList.options.length-1; count >-1; count--)
	{
		resortList.options[count] = null;
	}

	var resortNodes = destinationNode.getElementsByTagName('resort');
	var textValue; 
	var optionItem;
	//Add new states list to the state combo box.

	optionItemSelect = new Option(' - Select Resort (Optional) -', '0',  false, false);
	resortList.options[0] = optionItemSelect;
	resortList.selected = 0;

	for (var count = 0; count < resortNodes.length; count++)
	{
   		textValue = GetInnerText(resortNodes[count]);
		selectValue = resortNodes[count].getAttribute("value");
		optionItem = new Option( textValue, selectValue,  false, false);
		resortList.options[resortList.length] = optionItem;
	}
	resortList.disabled = false;
}

//Returns the node text value 
function GetInnerText (node)
{
	//alert('node: ' + node.textContent) 
	return (node.textContent || node.innerText || node.text) ;
}









