//to include this library the following variables should be initialized:
//sLoading, sAllLocations

//index of last selected city
var lastCity = -1

//initialise search engine
function initSearchEng() {
	frm = document.searchEng
	datesFrm = frm

	//initialise rollover effects
	if (typeof(initRollOver) != "undefined") initRollOver()

	//initialise travel dates (also extracts cookie with defaults)
	populateDates()

	sourceCtl = ""
	targetCtl = ""
	arrLoading = new Array("", sLoading)
	arrData = new Array()
	arrData["city"] = new Array()
	arrData["suburb"] = new Array()
	defCountry = objCookie.country
	defCity = objCookie.city
	defSuburb = objCookie.suburb

	setValue(frm.country, defCountry)
	countryChange(frm.country)
}


//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
}


//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
}


//handle country-change event
function countryChange(ctl) {
	sourceCtl = ctl
	targetCtl = frm.city

	ctlChange("SBGetSearchEngCitiesLang")
}


//handle city-change event
function cityChange(ctl) {
	if (getText(ctl).substr(0, 3) == "---") {  //do not allow to select the divider
		ctl.selectedIndex = lastCity
	} else {
		//syncronise selections in city and specCity
		if (frm.specCity) frm.specCity.selectedIndex = ctl.selectedIndex

		//set the specials frame source
		if (window.frames.spec) window.frames.spec.location.replace("/CitySpecials.asp?city=" + escape(getValue(ctl)))

		//invoke remote scripting
		sourceCtl = ctl
		targetCtl = frm.suburb

		//note last selected city
		lastCity = ctl.selectedIndex

		ctlChange("SBGetSearchEngSuburbsLang")
	}
}


//handle specials-city-change event
function specCityChange(ctl) {
	if (getText(ctl).substr(0, 3) == "---") {  //do not allow to select the divider
		ctl.selectedIndex = lastCity
	} else {
		frm.city.selectedIndex = ctl.selectedIndex  //syncronise selections in city and specCity
		cityChange(frm.city)  //invoke city-change event
	}
}


//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.specCity)

		//display "loading..." in the controls that will be repopulated
		populate(targetCtl, arrLoading)

		if (targetCtl == frm.city) {
			populate(frm.suburb, arrLoading)
			if (frm.specCity) populate(frm.specCity, arrLoading)
		}

		//get remote data
		objRS.getData(func, getValue(sourceCtl))
	} else
		rsCallBack(selArray)
}


//remote scripting callback function
function rsCallBack(data) {
	//prevent error in N6 when front 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.specCity)
	objRS.busy = false

	if (targetCtl == frm.city) {
		if (frm.specCity) populate(frm.specCity, data)
		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) {
		startNum = 1
		selBox[0] = new Option(sAllLocations, "")
	}

	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() {	

	var frm = document.searchEng
	var button
	
	//validations
	if (objRS.busy) return

	if (!(frm.star3.checked || frm.star4.checked || frm.star5.checked)) {
		if (typeof (SearchEng_SelectStar) == "undefined")
			SearchEng_SelectStar = "Please select at least one Star Rating."
			
		alert(SearchEng_SelectStar)
		frm.star5.focus()
		return
	}				

	//save selected country, city, suburb, in and out dates to a cookie
	setSelCookie(getValue(frm.country), getValue(frm.city), getValue(frm.suburb), getVal(frm.inDate), getVal(frm.outDate))

	if (!document.layers) {
			
		if (document.all) {
			button = document.all.sn_h2
		} else {
			button = document.getElementById("sn_h2")
		}
			
		if (button.style.display == "") {
			//validate hotel name
			if (typeof (SearchEng_HotelName) == "undefined")
				SearchEng_HotelName = "Hotel Name cannot contain any of the following illegal characters:\n\n%"
				
			if (invalidChars(frm.hotelName, "", "%", SearchEng_HotelName)) return
			
			if (charCheck(frm.hotelName, langMsg)) return
			
			if (trim(frm.hotelName.value).length < 3) {
				if (typeof (SearchEng_NameLength) == "undefined")
					SearchEng_NameLength = "Hotel Name should be at least 3 characters long."
					
				alert(SearchEng_NameLength)
				frm.hotelName.focus()
				return
			}
			
			frm.action = frm.action + '?hotelName=' + frm.hotelName.value			
		} else {
			frm.hotelName.value = ''
		}
	}

	//submit form
	frm.submit()
	
}


//redirection to hotel page from specials page
function redir(hotelID) {
	//save selected country, city, suburb, in and out dates to a cookie
	setSelCookie(getValue(frm.country), getValue(frm.city), getValue(frm.suburb), getVal(frm.inDate), getVal(frm.outDate))

	location = "/Hotel.asp?HotelID=" + hotelID + "&inDate=" + getVal(frm.inDate) + "&outDate=" + getVal(frm.outDate)
}


//bookmark the site
function SetBookmark(url, title) {
	document.all.bookmark.src = "/Common/Bookmark.asp"
	window.external.AddFavorite(url, title)
}

//prosess key-press event in hotel name text-box
function keyPressed(frm, pressedKeyCode) {
	if (pressedKeyCode == 13) submitForm()
}

//Swap 'Search by Destination' with 'Search by Hotel Name'
function SwapSearch(idFrom, idTo) {

	var i, elDispFrom, elVisFrom, elDispTo, elVisTo, tdStyleMain, tdStyletHotel, HrefMain, HrefHotel

	for (i=0; i<12; i++) {
		with (document) {
			if (document.all) {
				elDispFrom = all(idFrom + "_h" + i)
				elVisFrom = all(idFrom + "_c" + i)
				elDispTo = all(idTo + "_h" + i)
				elVisTo = all(idTo + "_c" + i)
			} else {
				elDispFrom = getElementById(idFrom + "_h" + i)
				elVisFrom = getElementById(idFrom + "_c" + i)
				elDispTo = getElementById(idTo + "_h" + i)
				elVisTo = getElementById(idTo + "_c" + i)
			}
		}

		if (elDispFrom) elDispFrom.style.display = "none"
		if (elVisFrom) elVisFrom.style.visibility = "hidden"
		if (elDispTo) elDispTo.style.display = ""
		if (elVisTo) elVisTo.style.visibility = ""		
	}
	
	if (document.all) {
		tdStyleMain = document.all.tdMain
		tdStyletHotel = document.all.tdHotel
		HrefMain = document.all.hrefMain
		HrefHotel = document.all.hrefHotel
	} else {
		tdStyleMain = document.getElementById("tdMain")
		tdStyletHotel = document.getElementById("tdHotel")
		HrefMain = document.getElementById("hrefMain")
		HrefHotel = document.getElementById("hrefHotel")
	}	
	
	//set focus to hotelname
	var frm = document.searchEng

	if (idTo == "sn") {
		frm.hotelName.focus()
		tdStyletHotel.className = "tab-act"
		tdStyleMain.className =  "tab-pass"
		HrefHotel.className = "tab-lnk-pass"
		HrefMain.className = "tab-lnk-act"
	} else {
		frm.inDate.focus()
		tdStyleMain.className = "tab-act"
		tdStyletHotel.className = "tab-pass"
		HrefMain.className = "tab-lnk-pass"
		HrefHotel.className = "tab-lnk-act"
	}
}