
var lstHotelCountry = null, lstHotelCity = null,lstHotelZone=null, lstServiceLevel = null,lstHotels=null;
var txtFromDate = null, txtNightsCount = null, txtToDate = null, lstRoomsNumber = null;
var ABHSE_HotelAreaTitle = "";
var ABHSE_HotelCityTitle = "";
var ABHSE_HotelZoneTitle = "";
var ABHSE_HotelTitle = "";
var ISE_SE_TotalRows = 5; //hold number of rows (rooms number)
var ISE_SE_TotalCells = 4; //hold number of children in 1 room

function ABHSE_InitPage()
{

	ABHSE_LoadPageVars();
	ABHSE_LoadCitiesByCountry(ListBox_GetValue(lstHotelCountry)); //load cities by country
	ABHSE_LoadZonesByCity(ListBox_GetValue(lstHotelCity)); //load zones by city
	ABHSE_LoadHotelsByZone(ListBox_GetValue(lstHotelZone)); //load zones by city
	if(ABHSE_gilboa_area != "") //if area name was selected
	{
		ListBox_SetItemsSelected(lstHotelCity, ABHSE_gilboa_area);
	}
	if(ABHSE_zone != "") //if zone or zone_group was selected
	{
		ListBox_SetItemsSelected(lstHotelZone, ABHSE_zone);
	}
	else if(ABHSE_zone_group != "")
	{
		ListBox_SetItemsSelected(lstHotelZone, ABHSE_zone_group);
	}
	
	if(ABHSE_hotel != "")
	{
		ListBox_SetItemsSelected(lstHotels, ABHSE_hotel);
	}
	ABHSE_RoomsNumber_Change();
	ABHSE_LoadUserLastSearchValues();
	ABHSE_SetSEDisplay(!ABHSE_collapsed_search_engine);
}

function ABHSE_Search(includeHotel)
{
	if(!ABHSE_IsAbleToSearch()) return false;
	var sURL;
	//sURL = ABHSE_RunSEOnUrl + "?" + ABHSE_CreateSearchObject();
	if(ABHSE_RunSEOnUrl.indexOf("?") > -1)
	{
		sURL = ABHSE_RunSEOnUrl + "&" + ABHSE_CreateSearchObject(includeHotel);
	}
	else
	{
		sURL = ABHSE_RunSEOnUrl + "?" + ABHSE_CreateSearchObject(includeHotel);
	}	
	location.href = sURL;
	return true;
}

function ABHSE_LoadUserLastSearchValues()
{
	if(ABHSE_Load_FromDate != "")
	{
		TextBox_SetText(txtFromDate, ABHSE_Load_FromDate);
		if(ABHSE_Load_NightsCount != "") TextBox_SetText(txtNightsCount, ABHSE_Load_NightsCount);
		if(ABHSE_Load_ToDate != "") TextBox_SetText(txtToDate, ABHSE_Load_ToDate);
	}
	if(ABHSE_Load_RoomsNumber != "")	
	{
		ListBox_SetItemsSelected(lstRoomsNumber, ABHSE_Load_RoomsNumber);
	}
	if(ABHSE_Load_RoomsData != "")
	{
		var arr = ABHSE_Load_RoomsData.split(";");
		for(var i=0;i<arr.length;i++)
		{
			if(arr[i] == "") break;
			var arr2 = arr[i].split("_");
			var adults = arr2[0];
			var children = arr2[1];
			var age1 = arr2[2];
			var age2 = arr2[3];
			var age3 = arr2[4];
			var age4 = arr2[5];
			
			if(adults != "") 
			{
				ListBox_SetItemsSelected(document.all("lstAdultsNumAB_" + (i+1)), adults);
			}
			if(children != "")
			{
				ListBox_SetItemsSelected(document.all("lstChildNumAB_" + (i+1)), children);
				if(age1 != "" && age1 != undefined) ListBox_SetItemsSelected(document.all("lstChildAge1_" + (i+1)), age1)
				if(age2 != "" && age2 != undefined) ListBox_SetItemsSelected(document.all("lstChildAge2_" + (i+1)), age2)
				if(age3 != "" && age3 != undefined) ListBox_SetItemsSelected(document.all("lstChildAge3_" + (i+1)), age3)
				if(age4 != "" && age4 != undefined) ListBox_SetItemsSelected(document.all("lstChildAge4_" + (i+1)), age4)
			}
		}
	}
}

function ABHSE_SetSEDisplay(bShow)
{
	document.all.SE_VisibleOption_PlusAB.style.display	= !bShow?"inline":"none";
	document.all.SE_VisibleOption_MinusAB.style.display	= bShow?"inline":"none";
	document.all.ABHSE_TR1.style.display = bShow?"":"none";
	document.all.ABHSE_TR2.style.display = bShow?"":"none";
	document.all.ABHSE_TD1.style.display = bShow?"":"none";
}

function ABHSE_CreateSearchObject(includeHotel)
{
	var s = "";
	var obj = {};
	obj.HotelCountry = ListBox_GetValue(lstHotelCountry);
	s+= "HotelCountry=" + obj.HotelCountry
	
	obj.MarkTravelCountryCode = ListBox_GetAttribute(lstHotelCountry, "MarkTravelCountryCode");
	s+= "&MarkTravelCountryCode=" + obj.MarkTravelCountryCode
	
	obj.HotelBedsCountryCode = ListBox_GetAttribute(lstHotelCountry, "HotelBedsCountryCode");
	s+= "&HotelBedsCountryCode=" + obj.HotelBedsCountryCode
	
	obj.HotelArea = ListBox_GetValue(lstHotelCity);
	s+= "&HotelArea=" + obj.HotelArea
	
	obj.MarkTravelDestCode = ListBox_GetAttribute(lstHotelCity, "MarkTravelDestCode");
	s+= "&MarkTravelDestCode=" + obj.MarkTravelDestCode
	
	obj.HotelBedsDestCode = ListBox_GetAttribute(lstHotelCity, "HotelBedsDestCode");
	s+= "&HotelBedsDestCode=" + obj.HotelBedsDestCode	
	
	obj.ServiceLevel = ListBox_GetValue(lstServiceLevel);
	s+= "&ServiceLevel=" + obj.ServiceLevel	
	
	if(lstHotelZone.options.selectedIndex > 0)
	{
		obj.HotelZone = ListBox_GetValue(lstHotelZone);
		obj.HotelZoneLabel = trim(ListBox_GetText(lstHotelZone));
		
		if(ListBox_GetAttribute(lstHotelZone, "ZoneCode")=="0")
			s+= "&HotelGroupZone=" + obj.HotelZone
		else
			s+= "&HotelZone=" + obj.HotelZone
		s+= "&HotelZoneLabel=" + obj.HotelZoneLabel	;
	}
	
	if(includeHotel && lstHotels.options.selectedIndex > 0)
	{
		obj.Hotel = ListBox_GetValue(lstHotels);
		s+= "&Hotel=" + obj.Hotel
		obj.HotelName = ListBox_GetText(lstHotels).replace(/'/g,"`");
		s+= "&HotelName=" + obj.HotelName
	}
		
	//obj.MarkTravelServiceCode = ListBox_GetAttribute(lstServiceLevel, "MarkTravelServiceCode");
	//s+= "&MarkTravelServiceCode=" + obj.MarkTravelServiceCode
	
	//obj.HotelBedsServiceCode = ListBox_GetAttribute(lstServiceLevel, "HotelBedsServiceCode");
	//s+= "&HotelBedsServiceCode=" + obj.HotelBedsServiceCode

	obj.FromDate = txtFromDate.value;
	s+= "&FromDate=" + obj.FromDate
	
	obj.NightsCount = txtNightsCount.value;
	s+= "&NightsCount=" + obj.NightsCount
	
	obj.ToDate = txtToDate.value;
	s+= "&ToDate=" + obj.ToDate

	obj.rooms = {}
	//s+= "\n"
	
	obj.rooms.Count = ListBox_GetValue(lstRoomsNumber);
	s+= "&RoomsNumber=" + obj.rooms.Count
	
	obj.rooms.Data = []
	
	s+= "&RoomsData="
	//ABHSE_Load_RoomsData = "[adults]_[children]_[age1]_[age2]_[age3]_[age4];";
	for(var i = 1, iCount = obj.rooms.Count; i <= iCount; i++)
	{
		obj.rooms.Data[i] = {}

		obj.rooms.Data[i].AdultsCount = ListBox_GetValue(document.all("lstAdultsNumAB_" + i));
		//s+= "\nRoom " + i + " AdultsCount=" + obj.rooms.Data[i].AdultsCount
		s+= obj.rooms.Data[i].AdultsCount
		
		obj.rooms.Data[i].ChildsCount = ListBox_GetValue(document.all("lstChildNumAB_" + i));
		s+= "_" + obj.rooms.Data[i].ChildsCount
		
		obj.rooms.Data[i].ChildsAge = [];

		for(var j = 1, jCount = obj.rooms.Data[i].ChildsCount; j <= jCount; j++)
		{
			obj.rooms.Data[i].ChildsAge[j] = ListBox_GetValue(document.all("lstChildAge" + j + "_" + i));
			//s+= "\nRoom " + i + " Child " + j + " Age=" + obj.rooms.Data[i].ChildsAge[j]
			s+= "_" + obj.rooms.Data[i].ChildsAge[j]
		}
		
		s+= ";"
	}
	
	//return obj;	
	return s;
}

function ABHSE_IsAbleToSearch()
{
	if(ListBox_GetValue(lstHotelCountry) == "")
	{
		alert(ABHSE_HotelCountryErrMsg);
		lstHotelCountry.focus();
		return false;
	}
	if(ListBox_GetValue(lstHotelCity) == "")
	{
		alert(ABHSE_HotelAreaErrMsg);
		lstHotelCity.focus();
		return false;
	}
	if(txtFromDate.value == "")
	{
		alert(ABHSE_SelectFromDate);
		document.all.cmdSelectFromDate.click();
		return false;
	}	
	if(txtToDate.value == "")
	{
		alert(ABHSE_SelectToDate);
		document.all.cmdSelectToDate.click();
		return false;
	}	
	return true;
}

function ABHSE_LoadPageVars()
{
	lstHotelCountry = document.all.lstHotelCountryAB;
	lstHotelCity = document.all.lstHotelCityAB;
	lstHotelZone = document.all.lstHotelZoneAB;
	lstHotels = document.all.lstHotelsAB;
	lstServiceLevel = document.all.lstServiceLevelAB;
	txtFromDate = document.all.txtFromDateAB;
	txtNightsCount = document.all.txtNightsCountAB;
	txtToDate = document.all.txtToDateAB;
	lstRoomsNumber = document.all.lstRoomsNumberAB;
	ABHSE_HotelAreaTitle = CLDIC.ABHSE_HotelAreaTitle;
	ABHSE_HotelCityTitle = CLDIC.ABHSE_HotelCityTitle;
	ABHSE_HotelZoneTitle = CLDIC.ABHSE_HotelZoneTitle;
	ABHSE_HotelTitle = CLDIC.ABHSE_HotelTitle;
}

function ABHSE_LoadLevelByHotel(sID)
{
	if(sID=="") return;
	var o=lstServiceLevel;
	ListBox_SetItemsSelected(o, sID)
}
function ABHSE_DisableLevel(sID)
{
	var o=lstServiceLevel;
	if(sID=="")
	{
		o.disabled=false;
	}
	else
	{
		
		o.selectedIndex=0;
		o.disabled=true;
	}
}

var ABHSE_XMLCitiesData = {};
function ABHSE_LoadCitiesByCountry(sID)
{
	
	var o = lstHotelCity;
	var oZone = lstHotelZone;
	var oHotels = lstHotels;
	
	oZone.length=1;
	oZone[0].text = ABHSE_HotelZoneTitle;
	oZone[0].value = "";
	oZone[0].selected=true;
	oZone.disabled=true;

	oHotels.length=1;
	oHotels[0].text = ABHSE_HotelTitle;
	oHotels[0].value = "";
	oHotels[0].selected=true;
	oHotels.disabled=true;

	lstServiceLevel.disabled=false;
	if(sID == "")
	{
		o.length=1;
		//o[0].text = ABHSE_HotelAreaTitle;
		o[0].text = ABHSE_HotelCityTitle;
		o[0].value = "";
		o[0].selected=true;
		o.disabled=true;
	}
	else
	{
		var bFound;
		var xml = null;
		
		var xml = ABHSE_XMLCitiesData[sID];
		
		if(xml != undefined)
		{
			bFound = true;
		}
		else
		{
			xml = new ActiveXObject("Microsoft.XMLDOM");	
			xml.async = false;
			bFound = xml.load(CLVAR.ABROADDESTINATIONS_BYCOUNTRY_URL + "?CountryCode=" + sID);
		}
		
		if(!bFound)	return;
		else	ABHSE_XMLCitiesData[sID] = xml;
		
		var placeNodes = xml.selectNodes("//row");
		
		o.length=placeNodes.length+1;
		//o[0].text = ABHSE_HotelAreaTitle;
		o[0].text = ABHSE_HotelCityTitle;
		o[0].value = "";
		o[0].selected=true;
		
		for(var i = 1; i < placeNodes.length + 1; i++)
		{
			var e = placeNodes.nextNode();
			o[i].text=e.getAttribute("HebrewDestDesc");
			o[i].value=e.getAttribute("DestinationCode");
			o[i].MarkTravelDestCode=e.getAttribute("MarkTravelDestCode");
			o[i].HotelBedsDestCode=e.getAttribute("HotelBedsDestCode");
		}
		o.disabled=false;
	}
}

var ABHSE_XMLZonesData = {};
function ABHSE_LoadZonesByCity(sID)
{

	var o = lstHotelZone;
	var oHotels = lstHotels;
	
	oHotels.length=1;
	oHotels[0].text = ABHSE_HotelTitle;
	oHotels[0].value = "";
	oHotels[0].selected=true;
	oHotels.disabled=true;
	
	lstServiceLevel.disabled=false;
	
	if(sID == "")
	{
		o.length=1;
		o[0].text = ABHSE_HotelZoneTitle;
		o[0].value = "";
		o[0].selected=true;
		o.disabled=true;
	}
	else
	{
		var bFound;
		var xml = null;
		
		var xml = ABHSE_XMLZonesData[sID];
		
		if(xml != undefined)
		{
			bFound = true;
		}
		else
		{
			xml = new ActiveXObject("Microsoft.XMLDOM");	
			xml.async = false;
			bFound = xml.load(CLVAR.ABROADZONES_BYCITY_URL + "?CityCode=" + sID);
		}
		
		if(!bFound)	return;
		else	ABHSE_XMLZonesData[sID] = xml;
		
		var placeNodes = xml.selectNodes("//row");
		
		o.length=placeNodes.length+1;
		//o[0].text = ABHSE_HotelAreaTitle;
		o[0].text = ABHSE_HotelZoneTitle;
		o[0].value = "";
		o[0].selected=true;
		
		for(var i = 1; i < placeNodes.length + 1; i++)
		{
			var e = placeNodes.nextNode();
			if(e.getAttribute("ZoneCode")!="0")
				o[i].text="   "+e.getAttribute("Label");
			else	
				o[i].text=e.getAttribute("Label");
			o[i].value=e.getAttribute("HotelBedsCode");
			o[i].ZoneGroupCode=e.getAttribute("ZoneGroupCode");
			o[i].ZoneCode=e.getAttribute("ZoneCode");
			if(e.getAttribute("ZoneCode")=="0")
				o[i].className="zone_group_option";
			else
				o[i].className="zone_option";
		}
		if (placeNodes.length==0)
			lstHotelZone.disabled=true;
		else
			lstHotelZone.disabled=false

	}
}

///////////////////////////////////

var ABHSE_XMLHotelsData = {};
function ABHSE_LoadHotelsByZone(sID)
{

	var o = lstHotels;
	var zoneCode = ListBox_GetAttribute(lstHotelZone, "ZoneCode");
	var zoneGroupCode = ListBox_GetAttribute(lstHotelZone, "ZoneGroupCode");
	lstServiceLevel.disabled=false;
	
	var key=zoneGroupCode+'_'+zoneCode;
	if(sID == "")
	{
		o.length=1;
		o[0].text = ABHSE_HotelTitle;
		o[0].value = "";
		o[0].selected=true;
	}
	else
	{
	
		var bFound;
		var xml = null;
		
		var xml = ABHSE_XMLHotelsData[key];
		
		if(xml != undefined)
		{
			bFound = true;
		}
		else
		{
			xml = new ActiveXObject("Microsoft.XMLDOM");	
			xml.async = false;
			
			bFound = xml.load(CLVAR.ABROADHOTELS_BYZONE_URL + "?ZoneCode=" + zoneCode + "&ZoneGroupCode=" + zoneGroupCode);
			
		}
		if(!bFound)	return;
		else	ABHSE_XMLHotelsData[key] = xml;
		
		var placeNodes = xml.selectNodes("//row");
		
		o.length=placeNodes.length+1;
		//o[0].text = ABHSE_HotelAreaTitle;
		o[0].text = ABHSE_HotelTitle;
		o[0].value = "";
		o[0].selected=true;
		
		for(var i = 1; i < placeNodes.length + 1; i++)
		{
			var e = placeNodes.nextNode();
			o[i].text=e.getAttribute("Label");
			o[i].value=e.getAttribute("HotelBedsCode");
			o[i].ZoneGroupCode=e.getAttribute("ZoneGroupCode");
			o[i].ZoneCode=e.getAttribute("ZoneCode");
//			o[i].className="zone_option";
		}
		if (placeNodes.length==0)
		{
			lstHotels.disabled=true;
		}
		else
		{
			lstHotels.disabled=false
		}

	}
}

//////////////////////////////////
function ABHSE_IsValidNightNumber()
{
	var s = txtNightsCount.value;
	if(isNaN(s))
	{
		txtNightsCount.value = "";
		txtNightsCount.focus();
		return false;
	}

	if(s * 1 < 1)
	{
		txtNightsCount.value = "";
		txtNightsCount.focus();
		return false;
	}
	return true;
}

function ABHSE_SynDateControls(sKey)
{
	var sNights, sDate, sFromDate;
	sFromDate = txtFromDate.value;
	sNights   = txtNightsCount.value;
	sToDate   = txtToDate.value;
	switch (sKey)
	{
		case "NightsCount":
			if(sNights == "")	sToDate = "";
			else				sToDate = D_AddDaysToDateString(sFromDate, sNights * 1)
			break;
			
		case "ToDate":
			if(sToDate=="")	sNights = "";
			else				sNights = D_GetDaysDiff(sFromDate, sToDate);
			break;
	}
	txtNightsCount.value = sNights;
	txtToDate.value = sToDate;
	return true;
}

function ABHSE_IsAbleToSetToDate(o)
{
	if(txtFromDate.value == "")
	{
		o.value = "";
		o.blur();
		document.all.cmdSelectFromDate.click();
		event.returnValue = false;
		event.cancelBubble = true;
		return false;
	}
	return true;
}

function ABHSE_SelectDate(bIsFromDate)
{
	if(bIsFromDate == false)
	{
		if(!ABHSE_IsAbleToSetToDate(txtToDate)) return false;
	}
	
	if(bIsFromDate){
		TextBox_SetText(txtToDate, "");
	}

	var sDate = ABHSE_GetDateFromCalendar(bIsFromDate);
	if(sDate=="") return false;
	if(bIsFromDate)
	{
		TextBox_SetText(txtFromDate, sDate);
		if(txtToDate.value != "")
		{
			if(D_GetDaysDiff(txtFromDate.value, txtToDate.value) < 1) TextBox_SetText(txtToDate, "");
		}
	}
	else
	{
		TextBox_SetText(txtToDate, sDate);
	}		
	return true;
}

function ABHSE_GetDateFromCalendar(bIsFromDate)
{
	var sFromDate = ""; //user able to select days from this date only
	var sDefaultDate = ""; //will be selected as default
	if(bIsFromDate)
	{
		sFromDate = D_AddDaysToDateString(D_GetTodayDateString(), 1);
		sDefaultDate = txtFromDate.value;
		if(sDefaultDate == "") {sDefaultDate = sFromDate}
	}
	else
	{
		sFromDate = txtFromDate.value;
		sFromDate = D_AddDaysToDateString(sFromDate, 1)
		sDefaultDate = txtToDate.value;
	}
	qString = "";
	qString += "&IsFrom=" + (bIsFromDate ? "1" : "");
	qString += "&DefaultDate=" + sDefaultDate;
	qString += "&FromDate=" + sFromDate;
	//qString += "&rand=" + Math.random();
	
	var sURL = CLVAR.ABROAD_SE_CALENDAR_URL + qString;	
	//window.open(sURL);
	sDate = ShowModalDialog(sURL, "", 250, 300).toString();
	return sDate;
}

// modified by Ofir 27/04/2006
var maxCurrentChildren = [];
maxCurrentChildren[0] = 0;
maxCurrentChildren[1] = 0;
maxCurrentChildren[2] = 0;
maxCurrentChildren[3] = 0;
maxCurrentChildren[4] = 0;
function getMaxValueInArr(arr)
{
	return Math.max.apply({},arr);
	
}
function ABHSE_ChildNum_Change(iRowPos)
{
	var iChildCount = ListBox_GetValue(document.all("lstChildNumAB_" + iRowPos));
	maxCurrentChildren[iRowPos-1]=iChildCount;
	var mx = getMaxValueInArr(maxCurrentChildren);
	for(var i = 1; i <= ISE_SE_TotalCells; i++)
	{
		var oDiv = document.all("row_" + iRowPos + "_ChildAgeAB" + i);	
		if(i <= iChildCount)
		{
			oDiv.style.display = "inline";
			
		}
		else
		{
			oDiv.style.display = "none";
		}
		// set child age titles
		if (i<=mx)
		{
			document.all("ChildAgeTitleAB" + i).style.display = "inline";
		}
		else
		{
			document.all("ChildAgeTitleAB" + i).style.display = "none";
		}
	}
	
}

function ABHSE_RoomsNumber_Change()
{
	var iRoomsNumber = ListBox_GetValue(lstRoomsNumber);
	for(var i=1;i<=ISE_SE_TotalRows;i++)
	{
		var oTR = document.all("row_" + i + "_TRAB");
		if(i <= iRoomsNumber)	oTR.style.display = "";
		else					oTR.style.display = "none";
	}
}