var lastCity = -1 //index of last selected city
var prevSel = "tabDest" //variable to hold the tab name

function getPageObject(sObjID) {
	if (document.all)
		return document.all[sObjID]
	else
		return document.getElementById(sObjID)
}

//initialise search engine
function initSearchEng(frm) {
	//frm = getPageObject("searchEngine")
	//initialise dates
	tDates = new TravDates(frm.inYear, frm.inMonth, frm.inDay, frm.SetInDay, frm.outYear, frm.outMonth, frm.outDay, frm.SetOutDay, "lenStay", false, null)

	sourceCtl = ""
	targetCtl = ""
	arrLoading = new Array("", sLoading) //"loading..."
	arrData = new Array()
	arrData["city"] = new Array()
	arrData["suburb"] = new Array()



}


//set select box text
function setText(ctl, newText) {
	for (var i=0; i<ctl.length; i++) {
		if (ctl[i].text == newText) {
			ctl[i].selected = true
			ctl.selectedIndex = i
			return true
		}
	}
	return false
}


//set select box value

function setValue(ctl, newValue) {
	for (var i=0; i<ctl.length; i++) {
		if (ctl[i].value == newValue) {
			ctl[i].selected = true
			ctl.selectedIndex = i
			return true
		}
	}

	return false
}


//get select box's text
function getText(selBox) {
	return selBox[selBox.selectedIndex].text
}


//get select box's value
function getValue(selBox) {
	return selBox[selBox.selectedIndex].value
}


//handle country-change event
function countryChange(ctl) {
	//repopulate cities
	sourceCtl = ctl
	targetCtl = frm.city

	ctlChange("HCSearchEngineGetCitiesLang")
}


//handle city-change event
function cityChange(ctl) {
	if (getText(ctl).substr(1, 3) == "---"){
		ctl.selectedIndex = lastCity
	} else {
		sourceCtl = ctl
		targetCtl = frm.suburb
		
		lastCity = ctl.selectedIndex

		ctlChange("HCSearchEngineGetSuburbsLang")
	}
}


//handle control-change event
function ctlChange(func) {
	var selArray = arrData[targetCtl.name][getValue(sourceCtl)]

	if (typeof(selArray) == "undefined") {
		//lock form controls
		objRS.busy = true
		setAccess(false, frm.country, frm.city, frm.suburb, frm.sb)

		//display "loading..." in the controls that will be repopulated
		populate(targetCtl, arrLoading)
		if (targetCtl == frm.city) populate(frm.suburb, arrLoading)

		//get remote data
		objRS.getData(func, disabled, affID, getValue(sourceCtl))
	} else
		rsCallBack(selArray)
}


//remote scripting callback function
function rsCallBack(data) {
	//this line prevents error in ie4 when page accessed via "back" button
	if (typeof(arrData) == "undefined") return

	//save downloaded data to data array
	arrData[targetCtl.name][getValue(sourceCtl)] = data

	//populate target select box
	populate(targetCtl, data)

	//unlock form controls
	setAccess(true, frm.country, frm.city, frm.suburb, frm.sb)
	objRS.busy = false

	if (targetCtl == frm.city) cityChange(frm.city)
}


//populate select box with data
function populate(selBox, data) {	
	var startNum = 0
	selBox.length = 0

	if (selBox.name == "suburb" && data[0] != sLoading) {
		selBox[0] = new Option(sAllLocations, "")
		startNum = 1
	}

	for (var i=0; i<data.length; i+=2)
		selBox[i/2 + startNum] = new Option(data[i+1], data[i])

	selBox.selectedIndex = 0

	//set default and clear it after first use
	if (selBox.name == "city") {
		if (setValue(selBox, defCity)) defCity = ""
	} else {
		if (setValue(selBox, defSuburb)) defSuburb = ""
	}
}


//set access to the passed controls
function setAccess(access, ctls) {
	var ctl

	if (document.layers) {
		for (var i=1; i<arguments.length; i++) {
			ctl = arguments[i]
			if (typeof(ctl) != "object") continue

			if (access)
				ctl.onFocus = ""
			else {
				ctl.onFocus = ctl.blur
				ctl.blur()
			}
		}
	} else {
		for (var i=1; i<arguments.length; i++) {
			ctl = arguments[i]
			if (typeof(ctl) != "object") continue
			ctl.disabled = !access
		}
	}
}


//submit the form
function submitForm(frm, searchBy) {

	if (typeof(blnDisplayFlg) != "undefined"){
		if (searchBy == "") searchBy = frm.searchBy.value
	} else {
		if (searchBy == null) searchBy = frm.searchBy.value
	}

	if (searchBy == "Name") {
		//validate hotel name
		var frmTmpHotelName

		if (typeof(frm.dummyhotelName) != "undefined")
			frmTmpHotelName = frm.dummyhotelName
		else
			frmTmpHotelName = frm.hotelName

		if (typeof (Common_Scripts_SearchEngine_HotelName) == "undefined")
			Common_Scripts_SearchEngine_HotelName = "Hotel Name cannot contain any of the following illegal characters:\n\n%"

		if (invalidChars(frmTmpHotelName, "", "%", Common_Scripts_SearchEngine_HotelName)) return

		if (typeof(frm.dummyhotelName) != "undefined") {
			if (charCheck(frmTmpHotelName, langMsg, "convToASCII")) return
		} else {
			if (charCheck(frmTmpHotelName, langMsg)) return
		}

		if (trim(frmTmpHotelName.value).length < 3) {
			if (typeof (Common_Scripts_SearchEngine_HotelLength) == "undefined")
				Common_Scripts_SearchEngine_HotelLength = "Hotel Name should be at least 3 characters long."

			alert(Common_Scripts_SearchEngine_HotelLength)
			frmTmpHotelName.focus()
			return
		}

		if (typeof(frm.dummyhotelName) != "undefined")
			frm.hotelName.value = ToASCII(frm.dummyhotelName)

	} else {
		//do not submit the form if remote scripting object is busy
		if (objRS.busy) return

		//check that at least one star rating is selected
		if (!(frm.star3.checked || frm.star4.checked || frm.star5.checked)) {
			if (typeof (Common_Scripts_SearchEngine_SelectStar) == "undefined")
				Common_Scripts_SearchEngine_SelectStar = "Please select at least one Star Rating."

			alert(Common_Scripts_SearchEngine_SelectStar)
			frm.star5.focus()
			return
		}
	}

	//save selected country, city and suburb to cookie
	document.cookie = 
		escape("|SearchEng|") + "=" +
		escape("|" + getValue(frm.country) + "|" + getValue(frm.city) + "|" + getValue(frm.suburb) + "|") +
		";path=/"

	//check dates and submit the form
	if (tDates.Validate()) {
		frm.searchBy.value = searchBy
		frm.submit()
	}
}


//calendar callback function
function SetCalDate(year, month, day, inOut) {
	tDates.SetDate(year, month, day, inOut)
}


//Swap 'Search by Destination' with 'Search by Hotel Name' with 'Search by Maps'
function SwapSearch(idFrom, idTo) {

	var i, elDispFrom, elVisFrom, elDispTo, elVisTo, tblDestination, tblMap, imgMap, tdMap, hrefMain, hrefMap, hrefHotel

	if (document.all) {
		with (document) {
			tdStyleMain = all.tdMain
			tdStyletHotel = all.tdHotel
			tblDestination = all.tblDestination
			tblMap = all.tblMap
			imgMap = all.imgMap
			tdMap = all.tdMap
			hrefMain = all.hrefMain
			hrefMap = all.hrefMap
			hrefHotel = all.hrefHotel
		}
	} else {
		with (document) {
			tdStyleMain = getElementById("tdMain")
			tdStyletHotel = getElementById("tdHotel")
			tblDestination = getElementById("tblDestination")
			tblMap = getElementById("tblMap")
			imgMap = getElementById("imgMap")
			tdMap = getElementById("tdMap")
			hrefMain = getElementById("hrefMain")
			hrefMap = getElementById("hrefMap")
			hrefHotel = getElementById("hrefHotel")
		}
	}

	if ((idTo == "sn") || (idTo == "sd")) {
		for (i=0; i<21; i++) {
			with (document) {
				if (document.all) {
					elDispFrom = all(idFrom + "_d" + i)
					elVisFrom = all(idFrom + "_v" + i)
					elDispTo = all(idTo + "_d" + i)
					elVisTo = all(idTo + "_v" + i)
				} else {
					elDispFrom = getElementById(idFrom + "_d" + i)
					elVisFrom = getElementById(idFrom + "_v" + i)
					elDispTo = getElementById(idTo + "_d" + i)
					elVisTo = getElementById(idTo + "_v" + i)
				}
			}

			if (elDispFrom) elDispFrom.style.display = "none"
			if (elVisFrom) elVisFrom.style.visibility = "hidden"
			if (elDispTo) elDispTo.style.display = ""
			if (elVisTo) elVisTo.style.visibility = ""

			if (typeof(blnDisplayFlg) != "undefined") {
				var sR = "#3#9#14#2#4#5#6#10#11#15#18#"

				if (((idFrom == "sd") && (sR.indexOf("#"+i+"#") != -1)) || (idTo != "sn")) {
					if (elDispFrom) elDispFrom.style.display = "none"
					if (elVisFrom) elVisFrom.style.visibility = "visible"
					if (elDispTo) elDispTo.style.display = ""
					if (elVisTo) elVisTo.style.visibility = ""
				}

				if ((i == 3) || (i == 9) || (i == 14)) {
					if (idFrom == "sd") elVisFrom.disabled = true;
					if (idTo == "sd") elVisTo.disabled = false;
				}
			}
		}
	}

	//set focus to hotelname
	frm = getPageObject("searchEngine")

	if (idTo == "sn") {
		if (typeof(blnDisplayFlg) != "undefined") {
			var but = document.getElementById('sform-b')
			but.style.display = "block"
			if (prevSel == "tabDest") {
				var ele = document.getElementById('seltab')
				ele.setAttribute('id','tabDest')
			}
			if (prevSel == "tabMap") {
				var ele3 = document.getElementById('seltab')
				ele3.setAttribute('id','tabMap')
			}
			if (prevSel != "tabHotel") {
				var ele1 = document.getElementById('tabHotel')
				ele1.setAttribute('id','seltab')
				prevSel = "tabHotel"
			}
		}

		tblDestination.style.display = "block"
		tblMap.style.display = "none"

		tdStyletHotel.className = "tab-act"
		tdStyleMain.className = "tab-pass"
		if (tdMap) tdMap.className = "tab-pass"

		hrefMain.className = "a-h-sub"
		if (tdMap) hrefMap.className = "a-h-sub"
		hrefHotel.className = "a-h-sub-hovered"

		frm.searchBy.value = "Name"

		if (typeof(frm.dummyhotelName) != "undefined")
			frm.dummyhotelName.focus() 
		else
			frm.hotelName.focus()

	} else if (idTo == "sd") {
		if (typeof(blnDisplayFlg) != "undefined") {
			var but = document.getElementById("sform-b")
			but.style.display = "block"
			if (prevSel == "tabMap") {
				var ele3 = document.getElementById('seltab')
				ele3.setAttribute('id','tabMap')
			}
			if (prevSel == "tabHotel") {
				var ele1 = document.getElementById('seltab')
				ele1.setAttribute('id','tabHotel')
			}
			if (prevSel != "tabDest") {
				var ele = document.getElementById("tabDest")
				ele.setAttribute('id','seltab')
				prevSel = "tabDest"
			}
		}

		tblDestination.style.display = "block"
		tblMap.style.display = "none"

		tdStyleMain.className = "tab-act"
		tdStyletHotel.className = "tab-pass"
		if (tdMap) tdMap.className = "tab-pass"

		hrefMain.className = "a-h-sub-hovered"
		if (tdMap) hrefMap.className = "a-h-sub"
		hrefHotel.className = "a-h-sub"

		frm.searchBy.value = "Criteria"

	} else if (idTo == "sm") {
		if (typeof(blnDisplayFlg) != "undefined") {
			var but = document.getElementById('sform-b')
			but.style.display ="none"
			if (prevSel == "tabHotel") {
				var ele3 = document.getElementById('seltab')
				ele3.setAttribute('id','tabHotel')
			}
			if (prevSel == "tabDest") {
				var ele1 = document.getElementById('seltab')
				ele1.setAttribute('id','tabDest')
			}
			if (prevSel != "tabMap") {
				var ele = document.getElementById("tabMap")
				ele.setAttribute('id','seltab')
				prevSel = "tabMap"
			}
		}

		var map = document.getElementById('imgMap')
		map.src = "/common/images/world.gif"

		tblMap.style.display = "block"
		tblDestination.style.display = "none"

		tdMap.className = "tab-act"
		tdStyleMain.className = "tab-pass"
		tdStyletHotel.className = "tab-pass"

		hrefMain.className = "a-h-sub"
		hrefMap.className = "a-h-sub-hovered"
		hrefHotel.className = "a-h-sub"
		
	}
}

//prosess key-press event in hotel name text-box
function keyPressed(frm, pressedKeyCode) {
	if (pressedKeyCode == 13) submitForm(frm, "Name")
}
