// JavaScript Document

function ShowSearchInSiteOptions() {

	SearchByDestinationPanel = document.getElementById('search_by_destination_options');
	SearchByDestinationTitle = document.getElementById('search_by_destination_dim_title');

	SearchInSitePanel = document.getElementById('search_in_site_options');
	SearchInSiteTitle = document.getElementById('search_in_site_dim_title');

	if(SearchInSitePanel && SearchByDestinationPanel) {

		SearchInSitePanel.style.display = "block";
		SearchByDestinationTitle.style.display = "block";

		SearchInSiteTitle.style.display = "none";
		SearchByDestinationPanel.style.display = "none";

		document.searchInSiteForm.s_keyword.focus();

	}

	return false;

}

function ShowSearchByDestinationOptions() {

	SearchByDestinationPanel = document.getElementById('search_by_destination_options');
	SearchByDestinationTitle = document.getElementById('search_by_destination_dim_title');

	SearchInSitePanel = document.getElementById('search_in_site_options');
	SearchInSiteTitle = document.getElementById('search_in_site_dim_title');

	if(SearchInSitePanel && SearchByDestinationPanel) {

		SearchInSitePanel.style.display = "none";
		SearchByDestinationTitle.style.display = "none";

		SearchInSiteTitle.style.display = "block";
		SearchByDestinationPanel.style.display = "block";

		document.searchDestinationForm.d_keyword.focus();

	}

	return false;

}

function SearchDateSelected() {

	CalendarDD = document.getElementById('search_calendar_dd');

	if(CalendarDD) {

		if(	CalendarDD.IgnoreDateSelect) {
			CalendarDD.IgnoreDateSelect = false;
			return;
		}

		CalendarDD.style.display = "none";

		if(CalendarDD.LastRanForGroup == "search")
			CalendarForm = document.searchDestinationForm;
		else {

			CalendarForm = document.establishmentAvailabilityForm;

			RoomResults 	  = document.getElementById('divRoomsResults');
			UpdateRoomResults = document.getElementById('divUpdateRoomsResults');

			if(RoomResults && UpdateRoomResults) {
				UpdateRoomResults.style.display = "block";
				RoomResults.style.display = "none";
			}

		}

		if(CalendarDD.LastRanForCheck == "CheckIn") {
			CalendarForm.check_in.value = SearchCalendar.selection.print("%d/%m/%Y", "");

			// We need to make sure CheckOut is not earlier now than CheckIn, In which case, we should increase
			CheckInDate = Calendar.parseDate(CalendarForm.check_in.value, false);
			CheckOutDate = Calendar.parseDate(CalendarForm.check_out.value, false);

			if(CheckInDate < CheckOutDate);
				// we don't need to do anything about this
			else {
				// We need to make CheckOut one day later than CheckIn
				CalendarDate = new Date(CheckInDate.getTime() + 86400000 * SearchCalendar.DefaultCheckOutIncrease);
				CalendarForm.check_out.value = Calendar.printDate(CalendarDate, "%d/%m/%Y");
			}

			DDLabel = document.getElementById(CalendarDD.LastRanForGroup + '_check_in_label');

			if(DDLabel)
				DDLabel.innerHTML = CalendarForm.check_in.value;

		}
		else {

			CalendarForm.check_out.value = SearchCalendar.selection.print("%d/%m/%Y", "");

			DDLabel = document.getElementById(CalendarDD.LastRanForGroup + '_check_out_label');

			if(DDLabel)
				DDLabel.innerHTML = CalendarForm.check_out.value;

		}

	}

	return false;
}


function ShowSearchCalendarDD(RunForGroup, RunForCheck) {

	CalendarDD = document.getElementById('search_calendar_dd');

	if(!CalendarDD)
		return;

	CalendarDD.LastRanForGroup = RunForGroup;
	CalendarDD.LastRanForCheck = RunForCheck;

	if(CalendarDD.LastRanForGroup == "search")
		CalendarForm = document.searchDestinationForm;
	else
		CalendarForm = document.establishmentAvailabilityForm;

	if(CalendarDD.LastRanForCheck == "CheckIn") {

		// We should set the MinDate for the calendar to Today
		CalendarDate = Calendar.printDate(new Date(), "%d/%m/%Y");
		SearchCalendar.args.min = Calendar.parseDate(CalendarDate, false);
		SearchCalendar.refresh();

		// And now we set the SelectedDate for the calendar
		CalendarDate = CalendarForm.check_in.value;
		CalendarDate = Calendar.parseDate(CalendarDate, false);
		CalendarDate = Calendar.dateToInt(CalendarDate);

		CalendarDD.IgnoreDateSelect = true;

		SearchCalendar.selection.set(CalendarDate);

		InputFix = document.getElementById(CalendarDD.LastRanForGroup + '_checkin_holder');

	}
	else {

		// We should set the MinDate for the calendar to CheckIn
		CalendarDate = CalendarForm.check_in.value;
		CalendarDate = Calendar.parseDate(CalendarDate, false);
		CalendarDate = new Date(CalendarDate.getTime() + 86400000 * SearchCalendar.DefaultCheckOutIncrease);
		SearchCalendar.args.min = CalendarDate;
		SearchCalendar.refresh();

		// And now we set the SelectedDate for the calendar
		CalendarDate = CalendarForm.check_out.value;
		CalendarDate = Calendar.parseDate(CalendarDate, false);
		CalendarDate = Calendar.dateToInt(CalendarDate);

		CalendarDD.IgnoreDateSelect = true;

		SearchCalendar.selection.set(CalendarDate);

		InputFix = document.getElementById(CalendarDD.LastRanForGroup + '_checkout_holder');

	}
	
	if(!InputFix)
		return false;

	fixPos = findPos(InputFix);

	CalendarDD.style.left = fixPos[0] + "px";
	CalendarDD.style.top  = fixPos[1] + 26 + "px";

	CalendarDD.style.display = "block";

	return false;

}

function HideSearchCalendarDD() {
}
function HideSearchCalendarDD() {
	document.SearchCalendarDDHideTimer = window.setTimeout("DelayedHideSearchCalendarDD()", 500);
}

function DelayedHideSearchCalendarDD() {
	SearchCalendarDD = document.getElementById('search_calendar_dd');

	if(SearchCalendarDD)
		SearchCalendarDD.style.display = "none";

	return false;

}

function CancelHideSearchCalendarDD() {
	window.clearTimeout(document.SearchCalendarDDHideTimer);
}

function InputBoxGotFocus(InputBox, DefaultText) {

	if(InputBox.value == DefaultText) {
		InputBox.value = "";
		InputBox.className = "search";
	}

}

function InputBoxLostFocus(InputBox, DefaultText) {

	if(InputBox.value == "") {

		InputBox.value = DefaultText;
		InputBox.className = "search_dim";

	}

}

function SubmitSearchDestinationForm() {

	CheckSearchDestinationForm();

	document.searchDestinationForm.submit();

}

function SubmitSearchInSiteForm() {

	CheckSearchInSiteForm();

	document.searchInSiteForm.submit();

}

function CheckSearchDestinationForm() {

	if(document.searchDestinationForm.d_keyword.className == "search_dim")
		document.searchDestinationForm.d_keyword.value = "";

}

function CheckSearchInSiteForm() {

	if(document.searchInSiteForm.s_keyword.className == "search_dim")
		document.searchInSiteForm.s_keyword.value = "";

}

function ToggleRoomDetailsView(DivName) {

	DivObj = document.getElementById(DivName);
	
	if(DivObj)
		if(DivObj.style.display == "block")
			DivObj.style.display = "none";
		else
			DivObj.style.display = "block";
	
	return false;
}

function CreateCalendar(checkInAdditionalDays, checkOutAdditionalDays) {
	var SearchCalendar = Calendar.setup({
											cont: "search_calendar_dd_holder",
											bottomBar: false,
											onSelect: SearchDateSelected
										});

	SearchCalendar.DefaultCheckInIncrease = checkInAdditionalDays;
	SearchCalendar.DefaultCheckOutIncrease = checkOutAdditionalDays;
	return SearchCalendar;
}