// JavaScript by Casper Thun - October 2008
bSearchOneInProgress = false;
function clearAjaxSearchList(listElem) {
	if(listElem!=0){
		while(listElem.options.length > 0) {
			listElem.options[0] = null;
		}
		while(listElem.hasChildNodes()) {
			listElem.removeChild(listElem.firstChild);
		}
	}
	listElem.selectedIndex=0;
	listElem.options[0] = new Option('- Loading. Please wait... -','');
}

var FillSearch2Timeout;
function FillSearch2(frmName) {
	if(bSearchOneInProgress) {
		FillSearch2Timeout = setTimeout('FillSearch2(\''+frmName+'\')', 500);
	}else{
		clearTimeout(FillSearch2Timeout);
		FillSearch(frmName);
	}
}


function GetNode(object) {
	if(object) {
		return object.nodeValue;
	}
	return '';
}

function GetAttribute(object) {
	if(object) {
		return object.nodeValue;
	}
	return '';
}
var ClientDebug = false;
function FillSearch(frmName) {
	/* START CLIENT DEBUG */
	shortcut.add("Ctrl+Alt+Shift+1",function() {
		if(ClientDebug==false) {
			ClientDebug = true;
			alert('Debug turned on!');
		}else{
			ClientDebug = false;
			alert('Debug turned off!');
		}
	},{
		'type':'keydown',
		'propagate':true,
		'target':document
	});
	/* FALSE CLIENT DEBUG */
	bSearchOneInProgress = true;
	if(frmName) {
		var sFormName = frmName;
	}else{
		var sFormName = 'frmFlightHotel';
	}
	var oForm = document.forms[sFormName];
	if(oForm){
		if(sAjaxSearch_SelectedCountry) {
			for(x=0;x<oForm.country.options.length;x++) {
				if(oForm.country.options[x].value.toLowerCase()==sAjaxSearch_SelectedCountry.toLowerCase()) {
					oForm.country.selectedIndex=x;
				}
			}
			
		}
		ajaxSearchChange(sFormName,'City',oForm.country.value,true);
	}
}

var sAjaxSearchTimeout;
function ajaxSearchChange(sFormID, sLevel, sLevelUpCode, bOnLoad) {
	clearTimeout(sAjaxSearchTimeout);
	oForm = document.forms[sFormID];
	if(oForm) {
		//alert(sLevelUpCode);
		if(sLevel.toLowerCase()=='city') {
			oDDL = oForm.city;
			oForm.locationto.disabled=true;
			oForm.locationto.options[0] = new Option('- Please select -','');
			oForm.locationto.selectedIndex=0;
		}else{
			oDDL = oForm.locationto;
		}
		oDDL.disabled = true;
		clearAjaxSearchList(oDDL);
		if(!sLevelUpCode) {
			oDDL.disabled=true;
			oDDL.options[0] = new Option('- Please select -','');
			oDDL.selectedIndex=0;
		}else{
			//run shit and set timeout to 30ms to make it smooth in IE
			sAjaxSearchTimeout = setTimeout('ajaxSearchChangeProcess(\''+sFormID+'\',\''+sLevel+'\',\''+sLevelUpCode+'\','+bOnLoad+')',30);
		}
	}
}

var sAjaxSearch_SelectedLocation = '';
var sAjaxSearch_SelectedCity = '';
function ajaxSearchChangeProcess(sFormID, sLevel, sLevelUpCode, bOnLoad) {
	if(!bOnLoad) {
		bOnLoad = false;	
	}
	oForm = document.forms[sFormID];
	var oDDL;
	if(sLevel.toLowerCase()=='city') {
		//alert('city');
		//ShowHideRegion(true);
		oDDL = oForm.city;
		sSelectedValue = sAjaxSearch_SelectedCity;
	}else{
		oDDL = oForm.locationto;
		sSelectedValue = sAjaxSearch_SelectedLocation;
	}
	var timestamp = new Date();
	sURL = '/includes/ajaxsearchbox/lookup.asp?level='+sLevel+'&lucode='+sLevelUpCode+'&onload='+bOnLoad+'&country='+oForm.country.value+'&ts='+timestamp;
	oXML = getSyncXML(sURL);
	sShowRegion = GetAttribute(oXML.attributes.getNamedItem("showregion"));
	if(sLevel.toLowerCase()=='city') {
		if(sShowRegion.toLowerCase()=='true'){
			ShowHideRegion(true, sFormID);
		}else{
			ShowHideRegion(false, sFormID);
		}
		oDDL.options[0] = new Option('- All regions -','Anything');
	}else{
		oDDL.options[0] = new Option('- Please select -','');
	}
	oOptions = oXML.getElementsByTagName('option');
	
	iSelectedNum = 0;
	for(x=0;x<oOptions.length;x++) {
		//sName = GetNode(oOptions[x].childNodes.item(0));
		sName = GetAttribute(oOptions[x].attributes.getNamedItem("title"));
		sCode = GetAttribute(oOptions[x].attributes.getNamedItem("value"));
		sSelected = GetAttribute(oOptions[x].attributes.getNamedItem("selected"));
		if(((sSelectedValue.toLowerCase()==sCode.toLowerCase() || sSelectedValue.toLowerCase()==sName.toLowerCase()) && bOnLoad) || (sSelected=='selected' && !bOnLoad)){iSelectedNum=x+1}
		oDDL.options[x+1] = new Option(sName,sCode);
	}
	oDDL.disabled = false;
	if(oOptions.length==1) {
		oDDL.selectedIndex=1;
	}else{
		oDDL.selectedIndex=iSelectedNum
	}
	//if(oDDL.selectedIndex>0) {
		if(sLevel.toLowerCase()=='city') {
			ajaxSearchChange(sFormID, 'Location', oForm.city.value, bOnLoad)
			//findDestAirport(sFormID, oForm.city.value);
		}else if(sLevel.toLowerCase()=='location'){
			bSearchOneInProgress = false;	
		}
	//}
}

function checkFlightHotelSearchBox(oForm) {
	var e = '';
	if(oForm) {
		if(!oForm.locationto.value){e+='- Please select the resort you are going to\n'}
	}
	if(e) {
		alert('The following errors occurred:\n\n' + e);
		return false;
	}else{
		return true;	
	}
}
function ShowHideRegion(bol, sFormID) {
	o1 = document.getElementById('idRegionDDL'+sFormID.replace('frm',''));
	if(o1) {
		if(bol){
			o1.style.display='';
		}else{
			o1.style.display='none';
		}
	}
}