﻿var __waitStatus = 0;
function __setWaitStatus()
{
	__waitStatus++;
	if( __waitStatus==1 )
	{
		document.body.style.cursor = "wait";
		__setSearchFormEnabled(false);
	}
}
function __clearWaitStatus()
{
	if( __waitStatus > 0 ) __waitStatus--;
	if( __waitStatus==0 )
	{
		document.body.style.cursor = "auto";
		__setSearchFormEnabled(true);
	}
}
function __setStatusText( str )
{
	//document.getElementById("__ajax_status").innerHTML = str;
}

function __failedCallback(error)
{
    __setStatusText("Ошибка: "+error.get_message());
	//alert("Ошибка: "+error.get_message());
	__clearWaitStatus();
}


var __data = new Object();
function __initPacketPricesFilters( dicts ) 
{
	try
	{
		__data.availmode = document.forms["header_search_frm"].availmode;
		//__data.availmode_available = document.forms["header_search_frm"].availmode_available;
		//__data.availmode_online = document.forms["header_search_frm"].availmode_online;
		//__data.availmode_all = document.forms["header_search_frm"].availmode_all;
		__data.depcity = document.forms["header_search_frm"].depcity;
		__data.transtype = document.forms["header_search_frm"].transtype;
		__data.country = document.forms["header_search_frm"].country;
		__data.datefrom = document.forms["header_search_frm"].datefrom;
		__data.dateto = document.forms["header_search_frm"].dateto;
		//__data.tourtype = document.forms["header_search_frm"].tourtype;
		//__data.resort = document.forms["header_search_frm"].resort;
		__data.spo = document.forms["header_search_frm"].spo;
		//__data.nights_tab = document.getElementById("nights_tab");
		//__data.hotelcategories_tab = document.getElementById("hotelcategories_tab");
		//__data.foodtypes_tab = document.getElementById("foodtypes_tab");
		//__data.hotels_tab = document.getElementById("hotels_tab");
		//__data.hotelservices_tab = document.getElementById("hotelservices_tab");

		//__data.hid_nights = document.forms["header_search_frm"].hid_nights;
		//__data.hid_hotelcategories = document.forms["header_search_frm"].hid_hotelcategories;
		//__data.hid_foodtypes = document.forms["header_search_frm"].hid_foodtypes;
		//__data.hid_hotels = document.forms["header_search_frm"].hid_hotels;
		//__data.hid_hotelservices = document.forms["header_search_frm"].hid_hotelservices;
		//__data.hid_parms = document.forms["header_search_frm"].hid_parms;
		

		//__data.availmode_available.onclick = function() { __loadDictionariesStart( Gpi.Vko.Search.PacketPricesAction2.Init ); };
		//__data.availmode_online.onclick = function() { __loadDictionariesStart( Gpi.Vko.Search.PacketPricesAction2.Init ); };
		//__data.availmode_all.onclick = function() { __loadDictionariesStart( Gpi.Vko.Search.PacketPricesAction2.Init ); };
		__data.depcity.onchange = function() {  __loadDictionariesStart( Gpi.Vko.Search.PacketPricesAction2.DepCityChanged ); }
		__data.transtype.onchange = function() {  __loadDictionariesStart( Gpi.Vko.Search.PacketPricesAction2.TransportTypeChanged ); }
		__data.country.onchange = function() {  __loadDictionariesStart( Gpi.Vko.Search.PacketPricesAction2.CountryChanged ); }
		__data.datefrom.onchange = function() 
			{ 
				if( __parseDate(__data.datefrom.value)==null ) { alert('Введена некорректная дата'); __data.datefrom.value = __data.datefrom_prevValue; } 
				else __data.datefrom_prevValue = __data.datefrom.value;

				__data.dateto.value = __data.datefrom.value;
				__loadDictionariesStart( Gpi.Vko.Search.PacketPricesAction2.DatesChanged ); 
			}
		__data.dateto.onchange = function() 
			{ 
				if( __parseDate(__data.dateto.value)==null ) { alert('Введена некорректная дата'); __data.dateto.value = __data.dateto_prevValue; } 
				else __data.dateto_prevValue = __data.dateto.value;

				var d1 = __parseDate(__data.datefrom.value);
				var d2 = __parseDate(__data.dateto.value);
				if( d1>d2 ) __data.datefrom.value = __data.dateto.value;
				__loadDictionariesStart( Gpi.Vko.Search.PacketPricesAction2.DatesChanged );
			}
		//__data.tourtype.onchange = function() { __highlightInstr('instr_search_or_continue'); __loadDictionariesStart( Gpi.Vko.Search.PacketPricesAction2.TourTypeChanged ); }
		//__data.resort.onchange = function() { __highlightInstr('instr_nights'); __loadDictionariesStart( Gpi.Vko.Search.PacketPricesAction2.ResortChanged ); }
		__data.spo.onchange = function() { __loadDictionariesStart( Gpi.Vko.Search.PacketPricesAction2.SpoChanged ); }

		//__data.nights_onchange = function() { __highlightInstr('instr_hotelcategories'); }
		//__data.hotelcategories_onchange = function() { __highlightInstr('instr_foodtypes'); }
		//__data.foodtypes_onchange = function() { __highlightInstr('instr_hotels'); }
		//__data.hotels_onchange = function() { __highlightInstr('instr_parms'); }
		//document.forms["header_search_frm"].adults.onchange = function() { __highlightInstr('instr_hotelservices'); }
		//document.forms["header_search_frm"].childs.onchange = function() { __highlightInstr('instr_hotelservices'); }
		//document.forms["header_search_frm"].maxprice.onchange = function() { __highlightInstr('instr_hotelservices'); }
		//document.forms["header_search_frm"].currency.onchange = function() { __highlightInstr('instr_hotelservices'); }
		//document.forms["header_search_frm"].sort.onchange = function() { __highlightInstr('instr_hotelservices'); }
		//__data.hotelservices_onchange = function() { __highlightInstr('instr_search'); }

		__loadDictionariesCallback( dicts );
	}
	catch(e)
	{
		alert(e.message);
	}
}

function __setSearchFormEnabled( bEnabled )
{
	//__data.availmode_available.disabled = !bEnabled;
	//__data.availmode_online.disabled = !bEnabled;
	//__data.availmode_all.disabled = !bEnabled;
	__data.depcity.disabled = !bEnabled;
	//__data.transtype.disabled = !bEnabled;
	__data.country.disabled = !bEnabled;
	__data.datefrom.disabled = !bEnabled;
	__data.dateto.disabled = !bEnabled;
	//__data.tourtype.disabled = !bEnabled;
	//__data.resort.disabled = !bEnabled;
	__data.spo.disabled = !bEnabled;
}

function __loadDictionariesStart( action, loadSingleDictName  )
{
	try
	{
		var filters = __getPacketPricesSearchFilters();
		var parms = __getPacketPricesDictionaryParms( loadSingleDictName );

		__setWaitStatus();
		__setStatusText( "Идёт загрузка справочников..." );

		Gpi.Vko.SearchWS.LoadPacketPricesDictionaries( filters, action, parms, __loadDictionariesCallback, __failedCallback, void 0 );
	}
	catch(e)
	{
		alert(e.message);
	}
}

function __getPacketPricesSearchFilters()
{
	var f = new Vko.Pricing.PacketPricesSearchQuery();

	f.AvailabilityMode = __getComboBoxValue( __data.availmode );
	f.DepCityID = __getComboBoxValue( __data.depcity );
	f.TransportTypeID = __getComboBoxValue( __data.transtype );
	f.CountryID = __getComboBoxValue( __data.country );
	f.DateFrom = __parseDate( __data.datefrom.value );
	f.DateTo = __parseDate( __data.dateto.value );
	//f.TourTypeID = __getComboBoxValue( __data.tourtype );
	//f.ResortID = __getComboBoxValue( __data.resort );
	f.SpoCodeFilter = __getSpoComboBoxValue( __data.spo );

	/*if( __data.hid_nights.value=="1" ) f.Nights = __getIds( __data.nights );
	if( __data.hid_hotelcategories.value=="1" ) f.HotelCategoryIDs = __getIds( __data.hotelcategories );
	if( __data.hid_foodtypes.value=="1" ) f.FoodTypeIDs = __getIds( __data.foodtypes );
	if( __data.hid_hotels.value=="1" ) f.HotelIDs = __getIds( __data.hotels );
	if( __data.hid_hotelservices.value=="1" ) f.HotelServiceIDs = __getIds( __data.hotelservices );

	if( __data.hid_parms.value=="1" ) 
	{
	}*/
	
	return f;
}

function __getSpoComboBoxValue( ctl )
{
	if( ctl.value!="" )
	{
		var parts = ctl.value.split('\t');
		var res = new Vko.Pricing.SpoCodeFilter();
		res.TourOperatorID = parseInt( parts[0] );
		res.SpoCode = parts[1];
		return res;
	}
	else
	{
		return  null;
	}
}

function __getPacketPricesDictionaryParms( loadSingleDictName )
{
	var parms = new Gpi.Vko.Search.PacketPricesDictionaryParms();

	if( loadSingleDictName )
	{
		parms[loadSingleDictName] = true;
	}
	else
	{
		/*parms.LoadNights = __data.hid_nights.value=="1";
		parms.LoadHotelCategories = __data.hid_hotelcategories.value=="1";
		parms.LoadFoodTypes = __data.hid_foodtypes.value=="1";
		parms.LoadHotels = __data.hid_hotels.value=="1";
		parms.LoadHotelServices = __data.hid_hotelservices.value=="1";*/
	}
	return parms;
}
function __cancelLoadDictionaries()
{
	__setSearchFormEnabled(true);
}

function __loadDictionariesCallback( res )
{
	try
	{
		/*if( res.SearchFilters.AvailabilityMode == Vko.Pricing.AvailabilityModes.Available )
		{
			__data.availmode_available.checked = true;
		}
		else if( res.SearchFilters.AvailabilityMode == Vko.Pricing.AvailabilityModes.Online ) 
		{
			__data.availmode_online.checked = true;
		}
		else 
		{
			__data.availmode_all.checked = true;
		}*/

		if( res.DepCities!=null )
		{
			__bindComboBox( __data.depcity, res.DepCities, res.SearchFilters.DepCityID );
		}
		/*if( res.TransportTypes!=null )
		{
			__bindComboBox( __data.transtype, res.TransportTypes, res.SearchFilters.TransportTypeID );
		}*/
		if( res.Countries!=null )
		{
			__bindComboBox( __data.country, res.Countries, res.SearchFilters.CountryID );
		}
		__data.datefrom_prevValue = __data.datefrom.value = __formatDate( res.SearchFilters.DateFrom );
		__data.dateto_prevValue = __data.dateto.value = __formatDate( res.SearchFilters.DateTo );
		
		if(res.Dates != null)
		{
		    calendar1.setActiveDates(res.Dates);
		    calendar2.setActiveDates(res.Dates);
		}
		/*if( res.TourTypes!=null )
		{
			__bindComboBox( __data.tourtype, res.TourTypes, res.SearchFilters.TourTypeID, true );
		}
		if( res.Resorts!=null )
		{
			__bindComboBoxGeo( __data.resort, res.Resorts, res.SearchFilters.ResortID, true );
		}*/
		if( res.SpoCodes!=null )
		{
			for( var i=0; i<res.SpoCodes.length; ++i )
			{
				res.SpoCodes[i].ID = ""+res.SpoCodes[i].TourOperatorID+"\t"+res.SpoCodes[i].SpoCode;
			}
			var selectedId = res.SearchFilters.SpoCodeFilter!=null ? ""+res.SearchFilters.SpoCodeFilter.TourOperatorID+"\t"+res.SearchFilters.SpoCodeFilter.SpoCode : null;
			__bindComboBox( __data.spo, res.SpoCodes, selectedId, true );
		}
/*		if( res.Nights!=null )
		{
			__data.nights = __bindCheckBoxList( "nights", __data.nights_tab, res.Nights, res.SearchFilters.Nights, __data.nights_onchange );
		}
		if( res.HotelCategories!=null )
		{
			__data.hotelcategories = __bindCheckBoxList( "hotelcategories", __data.hotelcategories_tab, res.HotelCategories, res.SearchFilters.HotelCategoryIDs, __data.hotelcategories_onchange );
		}
		if( res.FoodTypes!=null )
		{
			__data.foodtypes = __bindCheckBoxList(  "foodtypes", __data.foodtypes_tab, res.FoodTypes, res.SearchFilters.FoodTypeIDs, __data.foodtypes_onchange );
		}
		if( res.Hotels!=null )
		{
			__data.hotels = __bindCheckBoxList( "hotels", __data.hotels_tab, res.Hotels, res.SearchFilters.HotelIDs, __data.hotels_onchange );
		}
		if( res.HotelServices!=null )
		{
			__data.hotelservices = __bindCheckBoxHotelServices( "hotelservices", __data.hotelservices_tab, 4, res.HotelServices, res.SearchFilters.HotelServiceIDs, __data.hotelservices_onchange );
		}*/
	}
	catch(e)
	{
		alert( e.message );
	}
	__clearWaitStatus();
	__setStatusText( "" );
}

