var _controls = new Object();

function _initFlightSearchFilters(result)
{
    _controls.fldDepCities = document.forms["filter"].idcitytype;
    _controls.fldGeos = document.forms["filter"].idgeo;
    _controls.fldDateFrom = document.forms["filter"].datefrom;
    _controls.fldDateTo = document.forms["filter"].dateto;
    _controls.cldrDateFrom = cldr1;
    _controls.cldrDateTo = cldr2;
    
    _controls.fldDateFrom.onchange = function() {
        if (__parseDate(_controls.fldDateFrom.value) == null) { alert('Введена некорректная дата'); _controls.fldDateFrom.value = _controls.fldDateFrom_prevValue; }
        else _controls.fldDateFrom_prevValue = _controls.fldDateFrom.value;

        var d1 = __parseDate(_controls.fldDateFrom.value);
        var d2 = __parseDate(_controls.fldDateTo.value);
        if (d1 >= d2 || d2 == null) {
            d2 = d1;
            d2.setDate(d1.getDate() + 1);
            _controls.fldDateTo.value = __formatDate(d2);
        }
    }
    
	_controls.fldDateTo.onchange = function() {
	    if (__parseDate(_controls.fldDateTo.value) == null) { alert('Введена некорректная дата'); _controls.fldDateTo.value = _controls.fldDateTo_prevValue; }
	    else _controls.fldDateTo_prevValue = _controls.fldDateTo.value;

	    var d1 = __parseDate(_controls.fldDateFrom.value);
	    var d2 = __parseDate(_controls.fldDateTo.value);
	    if (d1 >= d2 || d1 == null) {
	        d1 = d2;
	        d1.setDate(d2.getDate());
	        _controls.fldDateFrom.value = __formatDate(d1);
	    }
	}  
	  
    __registerEventHandler(_controls.fldDepCities,
                        "change",
                        function()
                        {
                            _loadDictionaries(Vko.Travel.BusinessLogic.Flights.FlightSearchActions.DepCityChanged);
                        });
                    
    __registerEventHandler(_controls.fldGeos,
                        "change",
                        function()
                        {
                            _loadDictionaries(Vko.Travel.BusinessLogic.Flights.FlightSearchActions.GeoChanged);
                        });
                        
    _loadDictionariesCallBack(result);
}

function _loadDictionaries(action) 
{
    var filters = _getFormFilters();
    var hasPrice = (typeof (Consider_HasPrice_SearchCondition) != "undefined") ? Consider_HasPrice_SearchCondition : false;
    
    Gpi.Vko.Search2WS.LoadFlightSearchDictionaries(filters, action, hasPrice, _loadDictionariesCallBack);
}

function _getFormFilters()
{
    var filters = new Vko.Travel.BusinessLogic.Flights.FlightSearchFilters();    
    filters.DepCityId = _controls.fldDepCities.value != null ? parseInt(_controls.fldDepCities.value) : null;
    filters.GeoId = _controls.fldGeos.value != null ? parseInt(_controls.fldGeos.value) : null;
    return filters;
}

function _loadDictionariesCallBack(result)
{
    if(result.DepCities != null)
        __bindComboBox(_controls.fldDepCities, result.DepCities, result.SearchFilters.DepCityId, false);
    if(result.Geos != null)
        __bindComboBox(_controls.fldGeos, result.Geos, result.SearchFilters.GeoId, false);
        
    if(result.ForthFlightDates != null)
    {
        _controls.cldrDateFrom.setActiveDates(result.ForthFlightDates);
        _controls.cldrDateTo.setActiveDates(result.ReturnFlightDates);
        
        var mindate = _getMinDate(result.ForthFlightDates);        
        _controls.fldDateFrom.value = __formatDate(mindate);        
        
        if(result.ReturnFlightDates.length > 0)
        {
            mindate = _getMinDate(result.ReturnFlightDates);           
        }
        
        _controls.fldDateTo.value = __formatDate(mindate);
    }
}

function _getMinDate(dates)
{
    if(dates.length > 0)
    {
        var minDate = dates[0];
        
        for(var i = 1; i < dates.length; i++)
        {
            if(dates[i] <  minDate)
                minDate = dates[i];
        }
        
        return minDate;
    }
    else
        return null;
}
    