//to include this library the following variables should be initialized:
//sLoading, sAllLocations

var lastCity = 1  //index of last selected city

//initialise search engine
function initSearchEng() {

	frm = document.forma2

	//initialise dates
	tDates = new TravDates(frm.inYear, frm.inMonth, frm.inDay, frm.SetInDay, frm.outYear, frm.outMonth, frm.outDay, frm.SetOutDay, "lenStay", false, null)
}


//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 (searchBy == null) searchBy = frm.searchByName.value

	if (searchBy == "Name") {
		//validate hotel name
		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(frm.hotelName, "", "%", Common_Scripts_SearchEngine_HotelName)) return
		
		//if (charCheck(frm.hotelName, langMsg)) return
		
		if (trim(frm.hotelName.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)
			frm.hotelName.focus()
			return
		}
	} 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.cat1.checked || frm.cat4.checked || frm.cat6.checked || frm.cat8.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.cat1.focus()
			return
		}
	
		//save selected country, city and suburb to cookie
		document.cookie = 
		escape("|SearchEng|") + "=" +
		escape("|" + getValue(frm.perifereia) + "|" + getValue(frm.nomos) + "|") +
		";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)
}

//bookmark the site
function SetBookmark(url, title) {
	document.all.bookmark.src = "/Common/Bookmark.asp"
	window.external.AddFavorite(url, title)
}
//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 = ""
		}
	}

	//set focus to hotelname
	var frm = document.searchEngine	
	
	if (idTo == "sn") {
		tblDestination.style.display = "block"
		tblMap.style.display = "none"
		
		tdStyletHotel.className = "tab-act"
		tdStyleMain.className =  "tab-pass"
		tdMap.className = "tab-pass"
		
		hrefMain.className = "a-h-sub"
		hrefMap.className = "a-h-sub"
		hrefHotel.className = "a-h-sub-hovered"
		
		frm.searchBy.value = "Name"
		frm.hotelName.focus()
	} else if (idTo == "sd") {	
		tblDestination.style.display = "block"
		tblMap.style.display = "none"
		
		tdStyleMain.className = "tab-act"
		tdStyletHotel.className = "tab-pass"
		tdMap.className = "tab-pass"
		
		hrefMain.className = "a-h-sub-hovered"
		hrefMap.className = "a-h-sub"
		hrefHotel.className = "a-h-sub"
		
		frm.searchBy.value = "Criteria"
	} else if (idTo == "sm") {
		if (imgMap) imgMap.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")
}
