// JavaScript Document

var req;
var sFormType;
var seldate;


function loadXMLDoc3(url,sType) {
    // branch for native XMLHttpRequest object
    if(sType==1) {
		var processChange = processCityChange3;
	}else if(sType==2){
		var processChange = processLocationChange3;
	}else if(sType==3){
		var processChange = processCityChange3;
	}

	if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
		req.onreadystatechange = processChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function clearList3(listElem) {
	//alert(listElem);
	var startTime = new Date() ;
	if(listElem!=0){
		
		//listElem.options.length = 0;
		while(listElem.options.length > 0) {
		listElem.options[0] = null;
		listElem.disabled="disabled";
		}
		while(listElem.hasChildNodes()) {
		listElem.removeChild(listElem.firstChild);
		}
		listElem.options[0] = new Option('Please wait... Loading...','-');
		listElem.options[1] = new Option('- Choose -','-');
		//alert(listElem.name);
		if(listElem.name == 'lstDestinationCities' && listElem.value == '-') {
			//document.getElementById("lstDestinationCities").options[0] = new Option('- Choose -','-');
		}
	}
		var endTime = new Date() ;
		//alert((endTime - startTime) / 1000 + ' seconds to clear list');
}

function changeDestination3(sForm,sType,sSelect) {
		sFormType = sForm;
		seldate = 0;
		lstCountries = document.getElementById("lstDestinationCountries"+sFormType);
		lstUSStates = document.getElementById("lstDestinationUSStates"+sFormType);
		lstCities = document.getElementById("lstDestinationCities"+sFormType);
		//lstLocations = document.getElementById("lstDestinationLocations"+sFormType);
		if(lstCountries.value=='US') {
			//div_switch("divUSStates" + sFormType, true, true);
			lstUSStates.disabled = "";
			if(document.getElementById("divState")) {
				oStateDiv = document.getElementById("divState");
				oStateDiv.style.display = "block";
			}
			if(lstUSStates.value!='-') {
				lstCities.disabled = "";
			}else{
				clearList3(lstCities);
				lstCities.options[0] = new Option('- Choose -','-');
			}
		}else{
			
			if(document.getElementById("divState")) {
				oStateDiv = document.getElementById("divState");
				oStateDiv.style.display = "none";
			}
			lstUSStates.disabled = "disabled";
		}
		
		// build url
		if(lstCountries.value=='US' && sType!=2) {
			if(lstUSStates.value=='-') {
				var sURL = "";
				clearList3(lstCities);
				lstCities.options[0] = new Option('- Choose -','-');
			}else{
				var sURL = "/DestinationLookup/DestinationLookup.aspx?typeid=3&countrycode=us&statecode=" + lstUSStates.value;
			}
		}else{
			var sURL = "/DestinationLookup/DestinationLookup.aspx?typeid="+sType+"&countrycode=" + lstCountries.value + "&citycode=" + lstCities.value;
		}
		//alert(sURL);
		
		if(sURL!='') {
			setTimeout("loadXMLDoc3('"+sURL+"','"+sType+"')", 100);
		}
		if(sType==1) {
		//lstLocations.options[0] = null;
		}
}




function processCityChange3() {	
	if (req.readyState == 4)
	{
		if (req.status == 200)
		{
			xmldoc = req.responseXML.documentElement;
			lstDestinationCountries = document.getElementById("lstDestinationCountries"+sFormType);
			lstDestinationUSStates = document.getElementById("lstDestinationUSStates"+sFormType);
			lstDestinationCities = document.getElementById("lstDestinationCities"+sFormType);
			//lstDestinationLocations = document.getElementById("lstDestinationLocations"+sFormType);
			clearList3(lstDestinationCities);
			nodes = xmldoc.getElementsByTagName('City');
			lstDestinationCities.options[2] = new Option('Destination not listed', 'freetext');
			lstDestinationCities.options[2].style.fontWeight = 'bold';
			lstDestinationCities.options[2].style.backgroundColor = '#F954BA';
			lstDestinationCities.options[2].style.color = '#FFFFFF';
			for(var i = 0; i < nodes.length; i++) {
				if (nodes.length != 0) {	
					
							
					//add new option group if needed
					var GroupName = nodes.item(i).attributes.getNamedItem("RegionName").nodeValue;
					if(PreGroupName!=GroupName){
						var RegionName = document.createElement('optgroup');
						RegionName.label = GroupName;
						if(GroupName!='zzz') {
							lstDestinationCities.appendChild(RegionName);
						}else if(i>0) {
							RegionName.label = 'Others';
							lstDestinationCities.appendChild(RegionName);
						}
					}
					var PreGroupName = GroupName;
					sName = nodes.item(i).attributes.getNamedItem("Name").nodeValue;
					sCode = nodes.item(i).attributes.getNamedItem("Code").nodeValue;
					lstDestinationCities.options[lstDestinationCities.options.length] = new Option(sName, sCode);
					
				}
			}
			
			if(lstDestinationCountries.value == '-'){
				lstDestinationCities.disabled = "disabled";
				lstDestinationCities.options[0] = new Option('- Choose -','-');
			}else if(lstDestinationCountries.value == 'US' && lstDestinationUSStates.value == '-') {
				lstDestinationCities.disabled = "disabled";
				lstDestinationCities.options[0] = new Option('- Choose -','-');
			}else{
				lstDestinationCities.disabled = "";
			}
			//lstDestinationLocations.disabled = "disabled";
			// remove loading option
			lstDestinationCities.options[0] = null;
			
			
			if(nodes.length==1) {
				//alert(lstDestinationCities.options[1].value);
				lstDestinationCities.options[1].selected = true;
				//changeDestination3(sFormType,2,'Locations');
			}
		}
		else
		{
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}



function processLocationChange3() {	
	if (req.readyState == 4)
	{
	lstDepAirport = document.getElementById("lstDepAirport"+sFormType);
		if (req.status == 200)
		{	
			xmldoc = req.responseXML.documentElement;
			//alert(req.responseXML);
			lstDestinationCountries = document.getElementById("lstDestinationCountries"+sFormType);
			lstDestinationCities = document.getElementById("lstDestinationCities"+sFormType);
			//lstDestinationLocations = document.getElementById("lstDestinationLocations"+sFormType);
			//add temp loading
			//lstDestinationLocations.options[0] = new Option('Please wait... Loading', '');
			// first - choose -
			//lstDestinationLocations.options[1] = new Option('- Choose -','-');
			nodes = xmldoc.getElementsByTagName('Location');

			
			
			var doLine = 0;
			
			if(lstDestinationCities.value!='-') {
				
				
				for(var i = 0; i < nodes.length; i++) {
					if (nodes.length != 0) {
						sName = nodes.item(i).attributes.getNamedItem("Name").nodeValue;
						sCode = nodes.item(i).attributes.getNamedItem("Code").nodeValue;
						var Popularity = nodes.item(i).attributes.getNamedItem("Popularity").nodeValue;
						
						
						if(Popularity!='0') {
							if(i==0) {
								//add top destinations option group
								var TopLabel = document.createElement('optgroup');
								TopLabel.label = 'Top destinations';
								//lstDestinationLocations.appendChild(TopLabel);
								var doLine = 1;
							}
							//lstDestinationLocations.options[lstDestinationLocations.options.length] = new Option(sName, sCode);
						}else{
							if(doLine==1) {
								var lineGroup = document.createElement('optgroup');
								lineGroup.label = "------------------------------------------------------";
								//lstDestinationLocations.appendChild(lineGroup);
								var doLine = 0;
							}
							//add new option group if needed
							var GroupName = nodes.item(i).attributes.getNamedItem("LocationGroup").nodeValue;
							if(PreGroupName!=GroupName){
								var LocationGroup = document.createElement('optgroup');
								//LocationGroup.label = GroupName;
								//lstDestinationLocations.appendChild(LocationGroup);
							}
							var PreGroupName = GroupName;
							//lstDestinationLocations.options[lstDestinationLocations.options.length] = new Option(sName, sCode);
						}
					}
				}
			
			}else{
				//clearList3(lstDestinationLocations);
			}
				
				
			if(lstDestinationCities.value == '-'){
				//lstDestinationLocations.disabled = "disabled";
			}else{
				//lstDestinationLocations.disabled = "";
			}
			// remove loading option
			//lstDestinationLocations.options[0] = null;
			//addDurationBYO3(sFormType);
		}
		else
		{
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function showFreetext() {
	document.getElementById("freetextSearch").style.display = (document.getElementById("lstDestinationCities").options[document.getElementById("lstDestinationCities").selectedIndex].value == 'freetext') ? "block" : "none";
}