var roomNumber = "";
var selectedLocations = [];

function saveHotelSearchParamsToCookies()
{
	//TODO add additional parameters.
	var cookie = '';
	cookie += prepareCookieItem('locationText', getElementById("locationText").value);
	cookie += prepareCookieItem('location', getElementById("location").value);
	cookie += prepareCookieItem('checkInDay', getElementById("checkInDay").value);

	cookie += prepareCookieItem('checkInMonthYear', getElementById("checkInMonthYear").value);
	cookie += prepareCookieItem('nightNumber', getElementById("nightNumber").value);
	var numberOfRooms = getElementById("roomNumber").value;
	cookie += prepareCookieItem('roomNumber', numberOfRooms);
	if(getElementById("hotelName"))
	{
		cookie += prepareCookieItem('hotelName', getElementById("hotelName").value);
	}
	cookie += prepareCookieItem('hotelRating', getElementById("hotelRating").value);
	for(var i = 0; i < numberOfRooms; i++)
	{
		var roomTypeID = "roomTypes[" + i + "]";
		cookie += prepareCookieItem(roomTypeID, getElementById(roomTypeID).value);
	}

	setCookie('HotelSearchForm', cookie);
}

function displayRoomsTypes(roomsNumber, maxRoomsNumber, isOnInit, maxGuestsNumber)
{
	for(var i = 0; i < maxRoomsNumber; i++)
	{
		var roomRow = getElementById("roomRow" + i);
		if(i < roomsNumber)
		{
			roomRow.style.display="";
		}
		else
		{
			roomRow.style.display="none";
		}
		setGuestsCountForRoom(i, maxGuestsNumber);
		getElementById("roomTypes[" + i + "]").value = roomTypes[i];
		checkRoomType(i, true);
	}
}

function setGuestsCountForRoom(index, maxGuestsNumber)
{
	var roomType = roomTypes[index];
	var guestsNumberSelect = getElementById("guestCount["+index+"]");

	for(var i = 0; i < maxGuestsNumber; i++)
	{
		if(allowedRoomTypesMap[i+1] != null)
		{
			for(var j = 0; j < allowedRoomTypesMap[i+1].length; j++)
			{
				if(allowedRoomTypesMap[i+1][j].key == roomType)
				{
					guestsNumberSelect.value = i+1;

					return;
				}
			}
		}
	}
}

function checkRoomType(index, isOnInit)
{
	var guestsCount = getElementById("guestCount[" + index + "]").value;
	var roomTypeLableSpan = getElementById("roomTypeLable" + index);

	var allowedRoomTypes = allowedRoomTypesMap[guestsCount];

	if(allowedRoomTypes.length > 1)
	{
		getElementById("roomTypesBlock"+index).innerHTML="<select name='roomTypes["+index+"]' tabindex='" + (9 + 2 * index) + "' id='roomTypes["+index+"]' />";
		var roomTypeSelect = getElementById("roomTypes[" + index + "]");
		roomTypeSelect.options.length = 0;
		for(var i = 0; i < allowedRoomTypes.length; i++)
		{
			roomTypeSelect.options.length++;
			roomTypeSelect.options[i] = new Option (allowedRoomTypes[i].value, allowedRoomTypes[i].key, false, false);
			if(isOnInit && allowedRoomTypes[i].key == roomTypes[index])
			{
				roomTypeSelect.options[i].selected = true;
			}
		}
	}
	else
	{
		getElementById("roomTypesBlock"+index).innerHTML="<input type='hidden' name='roomTypes["+index+"]' value='' id='roomTypes["+index+"]'>" + allowedRoomTypes[0].value;

		getElementById("roomTypes[" + index + "]").value=allowedRoomTypes[0].key;
	}
}


function changeCheckOutDate()
{
	var day = parseInt(getElementById("checkInDay").value) + parseInt(getElementById("nightNumber").value);
	var monthAndYear = getElementById("checkInMonthYear").value;
	var month = monthAndYear.substr(0, monthAndYear.indexOf("-"));
	var year = monthAndYear.substring(monthAndYear.indexOf("-") + 1, monthAndYear.length);
	var dt = new Date(month + '/' + day + '/' + year);
	var result = dt.format("j") + "&nbsp;" + dt.format("M") + "&nbsp;" + dt.format("Y");
	getElementById("hotelCheckOutDate").innerHTML = result;
	changeDateOfWeekByDateControlsValues(day, month, year, "checkOutDayOfWeek");
}

function customValidateLocation(locationControlId, validationOnEmptyRequired)
{
	return validateLocation(locationControlId, validationOnEmptyRequired, LOCATION_CODE_LENGTH, locationMaxLengthMessages, locationInvalidMessages, locationRequiredMessages);
}

function calendarHotelCheckInInputFunc (sources) {
	var today = new Date();
	var bookingBuffer = (sources.bookingBuffer) ? sources.bookingBuffer : 0;
	tFirstAvailDate = today.add(Date.DAY, bookingBuffer);
	tLastAvailDate = today.add(Date.MONTH, 11);

	this.availableDateInterval = {from:{day: tFirstAvailDate.getDate(), month: tFirstAvailDate.getMonth(), year: tFirstAvailDate.getFullYear()}, to:{day: tLastAvailDate.getDate(), month: tLastAvailDate.getMonth(), year: tLastAvailDate.getFullYear()}};
	this.validDateInterval = {from:{day: tFirstAvailDate.getDate(), month: tFirstAvailDate.getMonth(), year: tFirstAvailDate.getFullYear()}, to:{day: tLastAvailDate.getDate(), month: tLastAvailDate.getMonth(), year: tLastAvailDate.getFullYear()}};

	var day = Ext.get(sources.idDaysField).getValue();
	var month = 0;
	var year = 0;
	var dt = Date.parseDate("1-" + Ext.get(sources.idMonthYearField).getValue(), Date.patterns.DayMonthYear);
	if(dt) {
		month = dt.getMonth();
		year = dt.getFullYear();
	}

	with(this){
		iDate.day = cDate.day = day;
		iDate.month = cDate.month = month;
		iDate.year = cDate.year = year;
	}
}

function loadHotelSearchParamsFromCookies()
{
	var cookie = getCookie("HotelSearchForm");
	if (cookie == null)
	{
		return;
	};

	var locationText = getCookieItem("locationText", cookie);

	if (StringUtils.isEmpty(locationText))
	{
		return;
	}
	getElementById("location").value = staUnescape(getCookieItem("location", cookie));

	var hotelName = staUnescape(getCookieItem("hotelName", cookie));

	getElementById("locationText").value = selectedLocations["locationText"] = staUnescape(locationText);
	getElementById("checkInDay").value = staUnescape(getCookieItem("checkInDay", cookie));
	getElementById("checkInMonthYear").value = staUnescape(getCookieItem("checkInMonthYear", cookie));
	getElementById("nightNumber").value = staUnescape(getCookieItem("nightNumber", cookie));
	getElementById("hotelRating").value = staUnescape(getCookieItem("hotelRating", cookie));


	if(getElementById("hotelName"))
	{
		getElementById("hotelName").value = hotelName;
	}

	roomNumber = getCookieItem("roomNumber", cookie);

	if (roomNumber == "")
	{
		return;
	}

	getElementById("roomNumber").value = roomNumber;

	for(var i = 0; i < roomNumber; i++)
	{
		var roomType = staUnescape(getCookieItem("roomTypes[" + i + "]", cookie));
		roomTypes[i] = roomType;
	}

	changeCheckOutDate();
}

function changeRefinSearchDateControl()
{
	correctionDaysList('checkInDay', getElementById('checkInMonthYear').value, Date.patterns.MonthYear);
	changeDateOfWeek('checkInDay', 'checkInMonthYear');
	changeCheckOutDate();
	_validateDateControls('checkInDay','checkInMonthYear',null,'checkInMonthYearBlock',dateRange);
}

function getDateErrorMessage()
{
	return dateInvalidMessages['range'];
}

function getDateErrorMessageKey()
{
	return dateInvalidMessageKeys['range'];
}

function checkHotelSearchForm()
{
	var result = customValidateLocation('location', true) &&	_validateDateControls('checkInDay', 'checkInMonthYear', null, 'checkInMonthYearBlock',dateRange);
	setTimeout("isValidationRequired = true;", 250);
	return result;
}

function submitHotelSearchForm(obj)
{
	isValidationRequired = false;
	if(checkHotelSearchForm() && controls.checkAndLock())
	{
		objectSwapping.swap(obj);
		saveHotelSearchParamsToCookies();
		document.forms['HotelSearchForm'].submit();
	}
}

function initSelectedLocations()
{
	selectedLocations["locationText"] = Ext.get("locationText").dom.value;
}

function initSearchDate()
{
	correctionDaysList('checkInDay', getElementById('checkInMonthYear').value, Date.patterns.MonthYear);
	changeDateOfWeek('checkInDay', 'checkInMonthYear');
	changeCheckOutDate();
}