﻿
var iParamDaysDelta = 6;
var tt = false;
var strAirportIdKey = "AirportId";
var strCityIdKey = "CityId";
var strCountryIdKey = "CountryId";
//var strRemoveFlightText = '<%= Consts.FLIGHTS_TEXT_REMOVE_FLIGHT %>';
//var strAddFlightText = '<%= Consts.FLIGHTS_TEXT_ADD_FLIGHT %>';
//alert(strRemoveFlightText);
///global comboboxes variables
var objCmbFlight1FromCity = null;
var objCmbFlight1ToCity = null;
var objCmbFlight2FromCity = null;
var objCmbFlight2ToCity = null;
var objCmbFlight3FromCity = null;
var objCmbFlight3ToCity = null;
var objCmbFlight4FromCity = null;
var objCmbFlight4ToCity = null;

///global hiddens variables
var objHdnFlight1FromCity = null;
var objHdnFlight1FromAirport = null;
var objHdnFlight1ToCity = null;

//must keep the new Flight 1 To SelectedIndex because it is not reflected in the combo object
var objHdnFlight1ToNewSelectedIndex = null;

var objHdnFlight1ToCountry = null;
var objHdnFlight1ToAirport = null;
var objHdnFlight2FromCity = null;
var objHdnFlight2FromAirport = null;
var objHdnFlight2ToCity = null;
var objHdnFlight2ToAirport = null;
var objHdnFlight3FromCity = null;
var objHdnFlight3FromAirport = null;
var objHdnFlight3ToCity = null;
var objHdnFlight3ToAirport = null;
var objHdnFlight4FromCity = null;
var objHdnFlight4FromAirport = null;
var objHdnFlight4ToCity = null;
var objHdnFlight4ToAirport = null;

///holds the last typed (i.e. what user clicked) in the from 1 airport to be used by the request items method
///of the flight 2 to airport
var lastTypedFlight1FromValue = '';
///holds the last typed (i.e. what user clicked) in the to 1 airport to be used by the request items method
///of the flight 2 from airport
var lastTypedFlight1ToValue = '';




///fill airports
function Flight1FromCityComboBox_ItemsRequesting(sender, e) {
    //alert('itreq')
    //alert(sender.get_text());
    if (sender.get_text().length < 2) {
        //return false if the number of the typed characters is less than 3
        //alert('<3')
        e.set_cancel(true);
    }
    else {
        //alert('fetch');
        e.set_cancel(false);
    }
}

function keyp(sender, e) {

}

function ccc(sender, e) {
}

function OnFocusHandler(sender, e)  
{  
    var inputElement = sender.get_inputDomElement();  
    inputElement.className = inputElement.className.replace(/rcbEmptyMessage/g, "");
    setTimeout(function() { inputElement.select() }, 200);
}  

/// Start combo boxes events

///Flight 1
///fill the From airport, To airport
function Flight1FromCityComboBox_SelectedIndexChanged(sender, e) {//alert('indch')
    //sender.get_attributes().setAttribute('ddd', 'hhh');//	undefined	User-defined Type
    ///get the airport id attribute from the selected item and put it in a hidden
    var objFlight2ToCityComboBox = objCmbFlight2ToCity; //$find("<%= Flight2ToCityComboBox.ClientID %>");

    ///set hiddens to ne used in search results. extra data is stored in each item attributes
    SetHiddenFieldByItemAttribute(objHdnFlight1FromAirport, e, strAirportIdKey);
    SetHiddenFieldByItemAttribute(objHdnFlight1FromCity, e, strCityIdKey);
    SetHiddenFieldByItemAttribute(objHdnFlight2ToAirport, e, strAirportIdKey);
    SetHiddenFieldByItemAttribute(objHdnFlight2ToCity, e, strCityIdKey);

    ///request the items of the opposite flight and mark the correct one
    ///lastTypedFlight1FromValue - i.e 'lon'.
    objFlight2ToCityComboBox.requestItems(lastTypedFlight1FromValue, false)
    objFlight2ToCityComboBox.set_text(e.get_item().get_text());
    objFlight2ToCityComboBox.set_value(e.get_item().get_value());
    
}

function Flight1ToCityComboBox_SelectedIndexChanged(sender, e) {
    //debugger
    //alert('1toch')
    ///get the airport id attribute from the selected item and put it in a hidden
    var objFlight2FromCityComboBox = objCmbFlight2FromCity; //$find("<%= Flight2FromCityComboBox.ClientID %>");

    ///set hiddens to ne used in search results. extra data is stored in each item attributes
    SetHiddenFieldByItemAttribute(objHdnFlight1ToAirport, e, strAirportIdKey);
    SetHiddenFieldByItemAttribute(objHdnFlight1ToCity, e, strCityIdKey);

    objHdnFlight1ToNewSelectedIndex = 0;

    ///set the objHdnFlight1ToNewSelectedIndex for all cases different from the case when nothig is selected yet
    ///used in dp Search.ascx
    if (sender._text.length != 3) {
        for (var y = 0; y < sender.get_items().get_count(); y++) {
            if (sender.get_items().getItem(y)._text == e._item._text) {
                objHdnFlight1ToNewSelectedIndex = y;
            }
        }
    }
    
    SetHiddenFieldByItemAttribute(objHdnFlight1ToCountry, e, strCountryIdKey);
    SetHiddenFieldByItemAttribute(objHdnFlight2FromAirport, e, strAirportIdKey);
    SetHiddenFieldByItemAttribute(objHdnFlight2FromCity, e, strCityIdKey);

    ///request the items of the opposite flight and mark the correct one
    ///first set the text of the combo to be the last typed value of the flight 1 to combo
    ///so the requesrtitems method will have an argument. i.e 'lon'.
    if (objFlight2FromCityComboBox) {

        objFlight2FromCityComboBox.set_text(lastTypedFlight1ToValue);
        objFlight2FromCityComboBox.requestItems(lastTypedFlight1ToValue, false); //alert(lastTypedFlight1ToValue);
    
        //setTimeout("", 3000);

        //objFlight2FromCityComboBox.findItemByValue('0');
        objFlight2FromCityComboBox.set_text(e.get_item().get_text()); //alert(e.get_item().get_text());
        objFlight2FromCityComboBox.set_value(e.get_item().get_value()); //alert(e.get_item().get_value());
        //alert(objFlight2FromCityComboBox.get_text());

        var inputElement = objFlight2FromCityComboBox.get_inputDomElement();
        inputElement.className = inputElement.className.replace(/rcbEmptyMessage/g, "");
    }
    
    ///DP
    ///when the combobox is in dynamic package if the combobox calling this method has
    ///a property indicating it is a dynamic search then the car and hotel searches should be filled 
    ///automaticaly - relevat only for dp homepage
    //debugger
    if (typeof (sender.get_attributes().getAttribute('FromDP')) != 'undefined') {
        if (sender.get_attributes().getAttribute('FromDP').toLowerCase() == 'true') {
            var dtFlight1From = null;
            var dtFlight1To = null;

            if (PickerFromFlight && PickerToFlight) { 
                dtFlight1From = PickerFromFlight.getSelectedDate();
                dtFlight1To = PickerToFlight.getSelectedDate();
            }
            
            ///if we are not in hotel service details page - set hotel default values
            if ((typeof (hotelDealDetailsHotelCityID) == "undefined")) {
                //alert('fromdp')
                if (typeof (ClearHotelCombo) == 'function')
                    ClearHotelCombo();
                    
                SetHotelDefaultValues();
                SetHotelDefaultDates(dtFlight1From, dtFlight1To);
            }

            ///if we are not in car service details page - set car default values
            if ((typeof (carDealDetailsFromStationCityID) == "undefined")) {
                //alert('fromdp2')
                if (typeof (ClearCarCombo) == 'function')
                    ClearCarCombo();

                SetCarDefaultValues();
                SetCarDefaultDates(dtFlight1From, dtFlight1To);
            }
        }
    }
}

function Flight1ToCityComboBox_ItemsRequested(sender, e)
{
    ///if we are in dp search page then select the first item to trigger selectedindexchanged event 
    ///so car and hotel values will be filled automatically
    if (typeof (pageLoadedHandlerDynamicPackageSearch) == 'function') {
        if (sender.get_items().getItem(0)) {
            sender.get_items().getItem(0).select();
            ///set the forecolor of the combobox back to black after it was used to hold the cityid.
            sender._inputDomElement.style.color = 'black';

            var spnDPFlightSearchMessage = $get('spnDPFlightSearchMessage');

            if (spnDPFlightSearchMessage) {
                spnDPFlightSearchMessage.style.display = 'none';
            }
        }
        else {
            if (typeof (SetDPFlightSearchMessage) == 'function')
                SetDPFlightSearchMessage('לא נמצאה טיסה ליעד זה');
//            var spnDPFlightSearchMessage = $get('spnDPFlightSearchMessage');

//            if (spnDPFlightSearchMessage) {
//                spnDPFlightSearchMessage.style.display = 'block';
//                spnDPFlightSearchMessage.innerHTML = 'לא נמצאה טיסה ליעד זה';
//            }
        }
    }
}

///flight 2
function Flight2FromCityComboBox_SelectedIndexChanged(sender, e) {
    ///get the airport id attribute from the selected item and put it in a hidden
    SetHiddenFieldByItemAttribute(objHdnFlight2FromAirport, e, strAirportIdKey);
    SetHiddenFieldByItemAttribute(objHdnFlight2FromCity, e, strCityIdKey);
}

function Flight2ToCityComboBox_SelectedIndexChanged(sender, e) {
    var objFlight2ToCityComboBox = objCmbFlight2ToCity; //$find("<%= Flight2ToCityComboBox.ClientID %>");
    //sender.get_attributes().setAttribute('ddd', 'hhh');//	undefined	User-defined Type
    ///get the airport id attribute from the selected item and put it in a hidden
    SetHiddenFieldByItemAttribute(objHdnFlight2ToAirport, e, strAirportIdKey);
    SetHiddenFieldByItemAttribute(objHdnFlight2ToCity, e, strCityIdKey);


    //alert(objHdnFlight2ToAirport.value)
    objFlight2ToCityComboBox.set_text(e.get_item().get_text());
    objFlight2ToCityComboBox.set_value(e.get_item().get_value());
}

function Flight3FromCityComboBox_SelectedIndexChanged(sender, e) {
    SetHiddenFieldByItemAttribute(objHdnFlight3FromAirport, e, strAirportIdKey);
    SetHiddenFieldByItemAttribute(objHdnFlight3FromCity, e, strCityIdKey);
}

function Flight3ToCityComboBox_SelectedIndexChanged(sender, e) {
    SetHiddenFieldByItemAttribute(objHdnFlight3ToAirport, e, strAirportIdKey);
    SetHiddenFieldByItemAttribute(objHdnFlight3ToCity, e, strCityIdKey);
}

function Flight4FromCityComboBox_SelectedIndexChanged(sender, e) {
    SetHiddenFieldByItemAttribute(objHdnFlight4FromAirport, e, strAirportIdKey);
    SetHiddenFieldByItemAttribute(objHdnFlight4FromCity, e, strCityIdKey);
}

function Flight4ToCityComboBox_SelectedIndexChanged(sender, e) {
    SetHiddenFieldByItemAttribute(objHdnFlight4ToAirport, e, strAirportIdKey);
    SetHiddenFieldByItemAttribute(objHdnFlight4ToCity, e, strCityIdKey);
}


/// End combobox events

///sets the value of hidden field with the value of the p_strAttributeName of p_e item
function SetHiddenFieldByItemAttribute(p_objHidden, p_e, p_strAttributeName) {
    if (p_objHidden)
        p_objHidden.value = p_e.get_item().get_attributes().getAttribute(p_strAttributeName);
}

///returns true if strFlightFromCityValue or strFlightToCityValue are empty
function FlightIsNotValid(strFlightFromCityValue, strFlightToCityValue) {
    return (strFlightFromCityValue == '' || strFlightToCityValue == '' || strFlightFromCityValue == 'הקלידו שם עיר בלבד' || strFlightToCityValue =='הקלידו שם עיר בלבד');
}

///returns true if the a specific link of "show flight" inner text equals a constant
function FlightIsVisible(iFlightIndex) {
    var result = false;
    
    if ($get('lnkShowFlight' + iFlightIndex))
        result = ($get('lnkShowFlight' + iFlightIndex).innerHTML == strRemoveFlightText);

    return result;
}

///clears specific flight controls
function ClearFlightControls(iFlightIndex) {
    if (eval("objCmbFlight" + iFlightIndex + "FromCity"))
        eval("objCmbFlight" + iFlightIndex + "FromCity.clearItems()");
    if (eval("objCmbFlight" + iFlightIndex + "ToCity"))
        eval("objCmbFlight" + iFlightIndex + "ToCity.clearItems()");
    if (eval("objCmbFlight" + iFlightIndex + "FromCity"))
        eval("objCmbFlight" + iFlightIndex + "FromCity.set_text('')");
    if (eval("objCmbFlight" + iFlightIndex + "ToCity"))
        eval("objCmbFlight" + iFlightIndex + "ToCity.set_text('')");
}

function lnkShowFlight3_Click() {//debugger
    var lnkShowFlight3 = $get('lnkShowFlight3');

    ///replace the add flight link text to "remove flight" or vice versa
    ToggleText(lnkShowFlight3, strAddFlightText, strRemoveFlightText);

    ///show flight 4 "add flight" link
    //if ($get('<%= rdoMutipleDestinations.ClientID %>').checked)
    Toggle('innerFlight3');
    Toggle('divFlight4');

    ///if user hide flight 3 - cleare its values and flight 4 values
    if (!FlightIsVisible(3)) {
        ClearFlightControls(3);
        ClearFlightControls(4);
    }


}

function lnkShowFlight4_Click() {
    var lnkShowFlight4 = $get('lnkShowFlight4');

    ToggleText(lnkShowFlight4, strAddFlightText, strRemoveFlightText);

    Toggle('innerFlight4');

    if (!FlightIsVisible(4))
        ClearFlightControls(4);
}


function ggg(sender, e) 
{

}


// Begin - Div popup for common destinations
function ShowPopupCommonDestinations(sender, e)
{
    showd('hoverwrap');
}

function showd(element)
{
    var el = document.getElementById(element);
    el.style.display = "block";
}
function closeContentd(element)
{
    var el = document.getElementById(element);
    el.style.display = "none";
}
// End - Div popup for common destinations


///nu
function SetDefaultComboBoxItem(objComboBox, strValue) {
    objComboBox._inputDomElement.value = strValue;
    objComboBox._inputDomElement.click();
    //$find("<%= Flight1FromCityComboBox.ClientID %>").click();
    objComboBox._selectFirstMatch();
    //set_selectedIndex()	set_selectedIndex(_f6)	Variant
    //_getAllItems()	_getAllItems()	Variant
    //_inputDomElement	{object}	DispHTMLInputElement
    //_dropDownElement	{object}	DispHTMLDivElement
    objComboBox.hideDropDown();
}

