
var DepartureCityComboBox;
var DestinationCountryComboBox;
var CurrencyComboBox;
var MealsListBox;
var StarsListBox;
var HotelsListBox;
var DestinationCitiesListBox;
var TourTypesComboBox;

function ChangeFirstDate()
{
	var fe = document.getElementById("DateFromTextBox");
	var se = document.getElementById("DateToTextBox");
	var fdate = fe.value;
	var sdate = se.value;
	var fd = Date.fromFormatString(fdate);
	var sd = Date.fromFormatString(sdate);
	if (fd > sd) se.value = fe.value;
}

function onloadSearchForm()
{
	ToggleWaitBox("show");
	var screenWidth = 640, screenHeight = 480;
	if (parseInt(navigator.appVersion) > 3)
	{
		screenWidth = screen.availWidth;
		screenHeight = screen.availHeight;
		
		if ((resultsPageMaximized == "true") || (resultsPageMaximized == true))
		{
			resultsPageWidth = screenWidth;
			resultsPageHeight = screenHeight;
		}
	}
	
	TourTypeComboBox = new ComboBox("TourTypeComboBox", document.getElementById("TourTypeComboBoxLabel"));
	TourTypeComboBox.onChangeSelected = OnSelectTourType;
	DepartureCityComboBox = new ComboBox("DepartureCityComboBox", document.getElementById("DepartureCityComboBoxLabel"));
	DepartureCityComboBox.onChangeSelected = OnSelectDepartureCity;
	DestinationCountryComboBox = new ComboBox("DestinationCountryComboBox", document.getElementById("DestinationCountryComboBoxLabel"));
	DestinationCountryComboBox.onChangeSelected = OnSelectDestinationCountry;
	CurrencyComboBox = new ComboBox("CurrencyComboBox", document.getElementById("CurrencyComboBoxLabel"));
	if ((HideCurrenciesFilter == "false") || (HideCurrenciesFilter == false))
	{
		var documentElementRef = document.getElementById("currency");
		if (documentElementRef != null)
		{
			documentElementRef.style.display = "inline";
		}
	}
	MealsListBox = new ListBox("MealsListBox", document.getElementById("MealsListBoxLabel"));
	StarsListBox = new ListBox("StarsListBox", document.getElementById("StarsListBoxLabel"));
	StarsListBox.onChangeSelected = OnSelectStar;
	HotelsListBox = new ListBox("HotelsListBox", document.getElementById("HotelsListBoxLabel"));
	DestinationCitiesListBox = new ListBox("DestinationCitiesListBox", document.getElementById("DestinationCitiesListBoxLabel"));
	DestinationCitiesListBox.onChangeSelected = OnSelectDestinationCity;

	dictionary = CreateDictionaryFromQueryString();
	if (dictionary.departureCity_sm != null) {
	      selectedDepartureCityId = strToInt(dictionary.departureCity_sm);
	}
	if (dictionary.destinationCountry_sm != null) {
	      selectedDestinationCountryId = strToInt(dictionary.destinationCountry_sm);
	}
	if (dictionary.meals_sm != null) {
		selectedMealsIds = strToIntArray(dictionary.meals_sm);
	}
	if (dictionary.stars_sm != null) {
		selectedStarIds = strToIntArray(dictionary.stars_sm);
	}
	if (dictionary.destinationCities_sm != null) {
		selectedDestinationCitiesIds = strToIntArray(dictionary.destinationCities_sm);
	}
	if (dictionary.hotels_sm != null) {
		selectedHotelIds = strToIntArray(dictionary.hotels_sm);
	}
	//----
	if (dictionary.checkinDateFrom_sm != null) {
		BeginDate = Date.fromFormatString(dictionary.checkinDateFrom_sm);
	}
	if (dictionary.checkinDateTo_sm != null) {
		EndDate = Date.fromFormatString(dictionary.checkinDateTo_sm);
	}
	if (dictionary.costFrom_sm != null) {
	      BeginCost = strToInt(dictionary.costFrom_sm);
	}
	if (dictionary.costTo_sm != null) {
	      EndCost = strToInt(dictionary.costTo_sm);
	}
	if (dictionary.nightsFrom_sm != null) {
	      BeginNights = strToInt(dictionary.nightsFrom_sm);
	}
	if (dictionary.nightsTo_sm != null) {
	      EndNights = strToInt(dictionary.nightsTo_sm);
	}
	if (dictionary.adultsCount_sm != null) {
	      AdultCount = strToInt(dictionary.adultsCount_sm);
	}
	if (dictionary.childrenCount_sm != null) {
	      ChildCount = strToInt(dictionary.childrenCount_sm);
	}
	
	SetupCalendar("DateFromTextBox", "DateFromImage");
	SetupCalendar("DateToTextBox", "DateToImage");

	document.getElementById("DateFromTextBox").value = BeginDate.toFormatString();
	document.getElementById("DateToTextBox").value = EndDate.toFormatString();
	setValuetoField("AdultTextBox", AdultCount);
	setValuetoField("ChildTextBox", ChildCount);
	setValuetoField("PriceFromTextBox", BeginCost);
	setValuetoField("PriceToTextBox", EndCost);
	setValuetoField("NightsFromTextBox", BeginNights);
	setValuetoField("NightsToTextBox", EndNights);

	//LoadOperatorsByDepartureCityAndDestination(DefaultDepartureCity, true, 9, LoadOperators_callback);
	//LoadHotelsEx(3, [], [], LoadHotelsEx_callback);
	
	LoadTourTypes(loadTourTypes_callback);
	LoadMeals(LoadMeals_callback);
	LoadStars(LoadStars_callback);
	LoadCurrencies(Currencies, LoadCurrencies_callback);

	switch(interrelationshipsOfDepartureCitiesAndDestinationCountries)
	{
		case "NoInterrelationships":
			LoadDepartureCities(selectedTourTypeId, LoadDepartureCities_callback);
			LoadDestinationCountries(selectedTourTypeId, LoadDestinationCountries_callback);
			break;    
		case "DestinationCountriesDependsOnDepartureCities":
			LoadDepartureCities(selectedTourTypeId, LoadDepartureCities_callback);
			LoadDestinationCountriesByDepartureCity(selectedDepartureCityId, selectedTourTypeId, LoadDestinationCountries_callback)
			break;
		default:
			LoadDepartureCities(selectedTourTypeId, LoadDepartureCities_callback);
			LoadDestinationCountries(selectedTourTypeId, LoadDestinationCountries_callback);
	}
	
	LoadDestinationCities(selectedDestinationCountryId, LoadDestinationCities_callback);
	if (selectedDestinationCitiesIds.length > 0) {
		LoadHotels(selectedDestinationCountryId, selectedDestinationCitiesIds, selectedStarIds, LoadHotels_callback);
	}

	ToggleWaitBox("hide");
	resizeFrameEvent();
}

function OpenResults()
{
	var date1 = Date.fromFormatString(document.getElementById("DateFromTextBox").value);
	var date2 = Date.fromFormatString(document.getElementById("DateToTextBox").value);
	var dd = new Date(0).addDays(DateRange);
	if (date2-date1 >= dd) 
	{
		alert("Вы задали слишком большой диапазон дат.\nМакс. интервал дней "+DateRange);
		return;
	}

	var object = AJS.formContents(document.forms["search"]);
	var queryString = CreateQueryString(object);
	queryString += "&identHotels_sm=" + hideToursWithIdentHotels + "";
	queryString += "&windowWidth_sm=" + resultsPageWidth + "";
	queryString += "&windowHeight_sm=" + resultsPageHeight + "";
	windowOpen(resultPage + queryString, resultsPageWidth, resultsPageHeight, openResultsPageInNewWindow);
}
