var ajaxObject = null;
var refreshIntervalId = null;
var recursiveCall = false;

window.attachEvent('onload', function() { populateDropdownOnPageLoad(); });

function populateDropdownOnPageLoad() {
    var locationNodeId = 0;
    if (document.getElementById('hsform')) {
        var countryDDL = document.getElementById('hsform').hscountry;
        
        if (countryDDL.length > 0) {
            for (var i = 0; i < countryDDL.length; i++) {
                if (countryDDL.options[i].selected) {
                    locationNodeId = countryDDL.options[i].value;
                    break;
                }
            }
        }
		
        if (locationNodeId) {
            populateDropdown(locationNodeId);
        }
    }    
}

function populateDropdown(locationNodeId) {    
	if(locationNodeId > 0){
		sendAjaxAvailabilityRequest(locationNodeId);
	}
}

function sendAjaxAvailabilityRequest(locationNodeId){
	if (window.XMLHttpRequest){
		// W3C method
		ajaxObject = new XMLHttpRequest();
	}else if (window.ActiveXObject){
		// IE method
		ajaxObject = new ActiveXObject('Microsoft.XMLHTTP');
	}else{
		return;
	}

	ajaxObject.onreadystatechange = updateAvailability;

	var locationDDL = document.getElementById('hsform').location;
	var subLocationDDL = document.getElementById('hsform').sublocation;
	
	addOption(locationDDL, 'Loading...', 'li')
	selectedIndex = locationDDL.options.selectedIndex;

	for (var i = 0; i < locationDDL.length; i++){
		if(locationDDL.options[i].value == 'li'){
			locationDDL.options[i].selected = true;
		}
	}

	if(!recursiveCall){
		removeAllOptions(subLocationDDL);
	}
	addOption(subLocationDDL, 'Loading...', 'li')
			
	ajaxObject.open('GET', '/utility/hotelajaxdropdowns.aspx?locationid=' + locationNodeId, true);
	ajaxObject.send('');
}

function updateAvailability(){
	if (ajaxObject.readyState == 4){		
		pausecomp(500);
		var locations = ajaxObject.responseXML.getElementsByTagName('Location');	
		var newSearchId = ajaxObject.responseXML.firstChild.getAttribute('SearchLocationId');
		var searchCountryId = ajaxObject.responseXML.firstChild.getAttribute('HotelCountryId');	
		var locationDDL = document.getElementById('hsform').location;
		var subLocationDDL = document.getElementById('hsform').sublocation;
		
		if(selectedIndex > 0){locationDDL.options[selectedIndex].selected = true;}

		for (var i = 0; i < subLocationDDL.length; i++){
			if(subLocationDDL.options[i].value == 'li'){
				subLocationDDL.options[i] = null;
			}
		}
		for (var i = 0; i < locationDDL.length; i++){
			if(locationDDL.options[i].value == 'li'){
				locationDDL.options[i] = null;
			}
		}
		for (var i = 0; i < locationDDL.length; i++){
			if(locationDDL.options[i].value == 'li'){
				locationDDL.options[i] = null;
			}
		}
		
		if(document.getElementById('hscountry').value != document.getElementById('countrysearch').value && !recursiveCall){
			//COUNTRY CHANGED SO DELETE ALL OPTIONS IN BOTH SELECT BOXES
			removeAllOptions(locationDDL);
			removeAllOptions(subLocationDDL);
		}

		if(document.getElementById('hsform').location.options.length <= 1 && searchCountryId == 0){			
			document.getElementById('countrysearch').value = newSearchId;
			removeAllOptions(locationDDL);
			if(!recursiveCall){
				removeAllOptions(subLocationDDL);
			}
			var selectbox = document.getElementById('hsform').location;
			if(locations.length > 0){
				for (var i = 0; i < locations.length; i++){
					var locationName = locations[i].firstChild.lastChild.nodeValue;
					var locationNodeId = locations[i].firstChild.nextSibling.lastChild.nodeValue;					
					if(i == 0){addOption(selectbox, '<Please Select>', '');}
					addOption(selectbox, locationName, locationNodeId);
					if(locationDDL.options[i].value == newSearchId && !recursiveCall){
						locationDDL.options[i].selected = true;
					}else if(recursiveCall && locationDDL.options[i].value == recursiveCallId){
						locationDDL.options[i].selected = true;
					}
				}
				selectbox.disabled = false;
				if(searchCountryId != 0 || recursiveCall){					
					var countryDDL = document.getElementById('hsform').country;
					for (var i = 0; i < countryDDL.length; i++){
						if(countryDDL.options[i].value == searchCountryId || (recursiveCall && countryDDL.options[i].value == newSearchId)){
							countryDDL.options[i].selected = true;					
						}
					}
				}				
			}else{
				addOption(selectbox, '<Any>', '');
				addOption(document.getElementById('hsform').sublocation, '<Any>', '');
			}
		}else{
			removeAllOptions(document.getElementById('hsform').sublocation);
			var selectbox = document.getElementById('hsform').sublocation;
			var locationDDL = document.getElementById('hsform').location;
			if(locations.length > 0){				
				for (var i = 0; i < locations.length; i++){
					var locationName = locations[i].firstChild.lastChild.nodeValue;
					var locationNodeId = locations[i].firstChild.nextSibling.lastChild.nodeValue;				
					if(i == 0){addOption(selectbox, '<Please Select>', '');}
					addOption(selectbox, locationName, locationNodeId);					
					if(locationDDL.options.length > i && locationDDL.options[i].value == newSearchId){
						locationDDL.options[i].selected = true;					
					}
				}
				if(locationDDL.options.length == 0 && searchCountryId != 0){
					recursiveCall = true;
					recursiveCallId = newSearchId;
					populateDropdown(searchCountryId);
				}
				selectbox.disabled = false;									
			}else{
				addOption(selectbox, '<Any>', '');
			}
		}						
	}
}

function addOption(selectbox,text,value){
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);	
}

function removeAllOptions(selectbox){
	selectbox.innerHTML = "";
	selectbox.disabled = true;
}

function pausecomp(millis){
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); } 
	while(curDate-date < millis);
}

function changeHotelFormAction() {
    document.getElementById('hsform').action = "/hotels/getrates.aspx"
}
