﻿// put here all the functions that need to be on the top of the page
// ****** main.js
function initMenu() {
    var menu = document.getElementById("menu");
    if (menu) {
        width = menu.offsetWidth;
        nod = menu.getElementsByTagName("span");
        width_e = Math.round((width - 2 * nod.length) / nod.length);
        for (var i = 0; i < nod.length; i++) {
            nod[i].style.width = width_e + "px";
        }
        if ((width_e * nod.length + nod.length * 2) > width)
            nod[0].style.width = width_e - ((width_e * nod.length + nod.length * 2) - width) + "px";
    }
}
if (window.addEventListener) {
    window.addEventListener("load", initMenu, false);
}
else if (window.attachEvent && !window.opera) {
    window.attachEvent("onload", initMenu);
}

function IsDefined(obj) {
    //alert(typeof(obj) != "undefined")
    return (obj != "undefined");
    //return (typeof(obj) != "undefined");
}

function IsTypeDefined(obj) {
    //alert(typeof(obj) != "undefined")
    //return (obj != "undefined");
    var result = (typeof (obj) != "undefined");

    //alert(result);

    return result;
}
// **** end of main.js



//********* search calender
///Inits controls properties i.e. calendars custom properties
function InitHotelsControls() {
    //alert('initcarsc')
    CalendarsIDs =
            {
                strPickerFromID: 'PickerFromHotels',
                strCalendarFromID: 'CalendarFromHotels',
                strPickerToID: 'PickerToHotels',
                strCalendarToID: 'CalendarToHotels',

                strDdlMonthFromID: 'ddlFromHotelsMonth',
                strDdlMonthToID: 'ddlToHotelsMonth'
            };
    //alert(1)
    //alert(CalendarsIDs.strCalendarFromID)
    if (IsDefined(typeof (PickerFromHotels)))
        SetCalendarProperties(PickerFromHotels, CalendarsIDs);
    //alert(2)
    if (IsDefined(typeof (CalendarFromHotels)))
        SetCalendarProperties(CalendarFromHotels, CalendarsIDs);
    if (IsDefined(typeof (PickerToHotels)))
        SetCalendarProperties(PickerToHotels, CalendarsIDs);
    if (IsDefined(typeof (CalendarToHotels)))
        SetCalendarProperties(CalendarToHotels, CalendarsIDs);

    //alert(CalendarFromCar.GetProperty('CalendarsIDs').strCalendarFromID);
}

function InitIsraelHotelsControls() {
    //alert('initcarsc')
    CalendarsIDs =
            {
                strPickerFromID: 'PickerFromIsraelHotels',
                strCalendarFromID: 'CalendarFromIsraelHotels',
                strPickerToID: 'PickerToIsraelHotels',
                strCalendarToID: 'CalendarToIsraelHotels',

                strDdlMonthFromID: 'ddlFromIsraelHotelsMonth',
                strDdlMonthToID: 'ddlToIsraelHotelsMonth'
            };

    if (IsDefined(typeof (PickerFromIsraelHotels)))
        SetCalendarProperties(PickerFromIsraelHotels, CalendarsIDs);
    if (IsDefined(typeof (CalendarFromIsraelHotels)))
        SetCalendarProperties(CalendarFromIsraelHotels, CalendarsIDs);
    if (IsDefined(typeof (PickerToIsraelHotels)))
        SetCalendarProperties(PickerToIsraelHotels, CalendarsIDs);
    if (IsDefined(typeof (CalendarToIsraelHotels)))
        SetCalendarProperties(CalendarToIsraelHotels, CalendarsIDs);

    //alert(CalendarFromCar.GetProperty('CalendarsIDs').strCalendarFromID);
}

function InitCarControls() {
    //alert('initcarsc')
    CalendarsIDs =
            {
                strPickerFromID: 'PickerFromCar',
                strCalendarFromID: 'CalendarFromCar',
                strPickerToID: 'PickerToCar',
                strCalendarToID: 'CalendarToCar',

                strDdlMonthFromID: 'ddlFromCarMonth',
                strDdlMonthToID: 'ddlToCarMonth',
                strLblDdlHoursFromClientID: "lblDdlHoursFromClientID",
                strLblDdlHoursToClientID: "lblDdlHoursToClientID"
            };

    //alert(CalendarsIDs.strLblDdlHoursFromClientID)
    if (IsDefined(typeof (PickerFromCar)))
        SetCalendarProperties(PickerFromCar, CalendarsIDs);
    if (IsDefined(typeof (CalendarFromCar)))
        SetCalendarProperties(CalendarFromCar, CalendarsIDs);
    if (IsDefined(typeof (PickerToCar)))
        SetCalendarProperties(PickerToCar, CalendarsIDs);
    if (IsDefined(typeof (CalendarToCar)))
        SetCalendarProperties(CalendarToCar, CalendarsIDs);

    //alert(CalendarFromCar.GetProperty('CalendarsIDs').strCalendarFromID);
}

function InitFlightControls() {
    CalendarsIDs =
            {
                strPickerFromID: 'PickerFromFlight',
                strCalendarFromID: 'CalendarFromFlight',
                strPickerToID: 'PickerToFlight',
                strCalendarToID: 'CalendarToFlight',
                strPickerSingleID: 'PickerSingle',
                strCalendarSingleID: 'CalendarSingle',
                strPickerFlight4ID: 'PickerFlight4',
                strCalendarFlight4ID: 'CalendarFlight4',
                strDdlMonthFromID: 'ddlFromFlightMonth',
                strDdlMonthToID: 'ddlToFlightMonth'
            };

    if (IsDefined(typeof (PickerFromFlight)))
        SetCalendarProperties(PickerFromFlight, CalendarsIDs);
    if (IsDefined(typeof (CalendarFromFlight)))
        SetCalendarProperties(CalendarFromFlight, CalendarsIDs);
    if (IsDefined(typeof (PickerToFlight)))
        SetCalendarProperties(PickerToFlight, CalendarsIDs);
    if (IsDefined(typeof (CalendarToFlight)))
        SetCalendarProperties(CalendarToFlight, CalendarsIDs);
    if (IsDefined(typeof (PickerSingle)))
        SetCalendarProperties(PickerSingle, CalendarsIDs);
    if (IsDefined(typeof (CalendarSingle)))
        SetCalendarProperties(CalendarSingle, CalendarsIDs);
    if (IsDefined(typeof (PickerFlight4)))
        SetCalendarProperties(PickerFlight4, CalendarsIDs);
    if (IsDefined(typeof (CalendarFlight4)))
        SetCalendarProperties(CalendarFlight4, CalendarsIDs);
}

function InitPackageControls() {
    //alert('initPackcontrols')
    CalendarsIDs =
                {
                    strPickerFromID: 'PickerFromPackage',
                    strCalendarFromID: 'CalendarFromPackage',
                    strPickerToID: 'PickerToPackage',
                    strCalendarToID: 'CalendarToPackage',

                    strDdlMonthFromID: 'ddlFromPackageMonth',
                    strDdlMonthToID: 'ddlToPackageMonth',
                    IsdoubleCalendar: true
                };

    //alert(CalendarsIDs.strCalendarFromID)
    if (IsDefined(typeof (PickerFromPackage)))
        SetCalendarProperties(PickerFromPackage, CalendarsIDs);
    if (IsDefined(typeof (CalendarFromPackage)))
        SetCalendarProperties(CalendarFromPackage, CalendarsIDs);
    if (IsDefined(typeof (PickerToPackage)))
        SetCalendarProperties(PickerToPackage, CalendarsIDs);
    if (IsDefined(typeof (CalendarToPackage)))
        SetCalendarProperties(CalendarToPackage, CalendarsIDs);

    //alert(CalendarFromCar.GetProperty('CalendarsIDs').strCalendarFromID);
}

function InitFlyAndDriveControls() {
    CalendarsIDs =
            {
                strPickerFromID: 'PickerFromFlyAndDrive',
                strCalendarFromID: 'CalendarFromFlyAndDrive',
                strPickerToID: 'PickerToFlyAndDrive',
                strCalendarToID: 'CalendarToFlyAndDrive',

                strDdlMonthFromID: 'ddlFromFlyAndDriveMonth',
                strDdlMonthToID: 'ddlToFlyAndDriveMonth',
                IsdoubleCalendar: true
            };

    //alert(CalendarsIDs.strCalendarFromID)
    if (IsDefined(typeof (PickerFromFlyAndDrive)))
        SetCalendarProperties(PickerFromFlyAndDrive, CalendarsIDs);
    if (IsDefined(typeof (CalendarFromFlyAndDrive)))
        SetCalendarProperties(CalendarFromFlyAndDrive, CalendarsIDs);
    if (IsDefined(typeof (PickerToFlyAndDrive)))
        SetCalendarProperties(PickerToFlyAndDrive, CalendarsIDs);
    if (IsDefined(typeof (CalendarToFlyAndDrive)))
        SetCalendarProperties(CalendarToFlyAndDrive, CalendarsIDs);
    //alert(CalendarFromCar.GetProperty('CalendarsIDs').strCalendarFromID);
}


///adds the CalendarsIDs object to objCalendar properties as a custom property
///CalendarsIDs holds all the relevant control ids for that calendar so
///the generic functions in SearchCalendars.js would know what calendars to change when objCalendar date is changed
function SetCalendarProperties(objCalendar, CalendarsIDs) {
    if (objCalendar)
        objCalendar.SetProperty('CalendarsIDs', CalendarsIDs);
}



///End Init Controls functions
//** end of search calendar


function initPopup() {
    var cal = $(".calendar a.btn-calendar, .calendar a.close");
    cal.click(function() {
        if ($(this).hasClass("btn-calendar")) {
            $(this).parents('div.calendar').addClass("o-calendar");
            var hold = $(this).parents('div.calendar').children(".holder");
            if (window.attachEvent && !window.opera) { hideSelectBoxes(hold[0]); }
        }
        else if ($(this).hasClass("close")) {
            $(this).parents('div.calendar').removeClass("o-calendar");
            var hold = $(this).parents('div.calendar').children(".holder");
            if (window.attachEvent && !window.opera) { showSelectBoxes(hold[0]); }
        }
        return false;
    });
    $('div.more-link span.more a').click(function() {//alert($(this).parents('div.more-link').eq(0).className)
        ///nnn
        $(this).parents('div.more-link').eq(0).toggleClass("open");
        return false;
    });
}

if (window.addEventListener) {
    window.addEventListener("load", initPopup, false);
}
else if (window.attachEvent && !window.opera) {
    window.attachEvent("onload", initPopup);
}

function createCookie(name,value,hours) {
    if (!window.addEventListener)
    {
        if (hours) {
            var date = new Date();
            date.setTime(date.getTime()+(hours*1000*60*60));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }
}

function readCookie(name) {
    if (!window.addEventListener)
    {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
}
return null;
}

function takeLastChoosen(cookieTextName, cookieValueName ,objCmb)
{
   var text =  readCookie(cookieTextName);
    var value = readCookie(cookieValueName);
    if (text != null  && text != "" && text!="הקלידו שם יעד" &&  text!= "הקלידו שם עיר בלבד" && value!=null)
    {                    
          setTimeout(function(){ 
                                objCmb.requestItems(value, false)}, 50);
          setTimeout(function(){
                                var itm = objCmb.findItemByValue(value);
                                if (itm != null)
                                {
                                  itm.select();
                                    // remove the class of the empty message;
                                    objCmb._inputDomElement.className = objCmb._inputDomElement.className.replace(/rcbEmptyMessage/g, "");
                                }
                                objCmb.raise_selectedIndexChanged(); },750);
    }   
    
}

function takeLastChoosenCountryPackages(cookieTextName, cookieValueName ,objCmb)
{
   var text =  readCookie(cookieTextName);
    var value = readCookie(cookieValueName);
    if (text != null  && text != "" && text!="הקלידו שם יעד"  && value!=null)
    {                    
          setTimeout(function(){ 
                                objCmb.requestItems(value, false)}, 100);
          setTimeout(function(){
                                var itm = objCmb.findItemByValue(value);
                                if (itm != null)
                                {
                                  itm.select();
                                    // remove the class of the empty message;
                                    objCmb._inputDomElement.className = objCmb._inputDomElement.className.replace(/rcbEmptyMessage/g, "");
                                }
                                objCmb.raise_selectedIndexChanged(); },1600);
    }   
    
}


function takeLastChoosenCityPackages(cookieTextName, cookieValueName ,objCmb)
{
   var text =  readCookie(cookieTextName);
    var value = readCookie(cookieValueName);
    if (text != null  && text != "" && text!="הקלידו שם יעד"  && value!=null)
    {                    
          setTimeout(function(){ 
                                objCmb.requestItems(value, false)}, 50);
          setTimeout(function(){
                                var itm = objCmb.findItemByValue(value);
                                if (itm != null)
                                {
                                  itm.select();
                                    // remove the class of the empty message;
                                    objCmb._inputDomElement.className = objCmb._inputDomElement.className.replace(/rcbEmptyMessage/g, "");
                                }
                                objCmb.raise_selectedIndexChanged(); },1100);
    }   
    
}


function takeLastChoosenDate(cookieValueName ,objCmb)
{
    var value = readCookie(cookieValueName);
    if (value != ""  && value!=null)
    {
        var date = new Date(value);
        objCmb.setSelectedDate(date);
    }    

}

function rememberFlightDestinations(CalFrom, CalTo, CalFrom2, CalTo2)
{
  createCookie('flight1ToText',objCmbFlight1ToCity.get_text(),6)
  createCookie('flight2FromText',objCmbFlight2FromCity.get_text(),6)
  createCookie('flight1ToValue',objCmbFlight1ToCity.get_value(),6)
  createCookie('flight2FromValue',objCmbFlight2FromCity.get_value(),6)
  createCookie('flight3ToText',objCmbFlight3ToCity.get_text(),6)
  createCookie('flight3FromText',objCmbFlight3FromCity.get_text(),6)
  createCookie('flight3ToValue',objCmbFlight3ToCity.get_value(),6)
  createCookie('flight3FromValue',objCmbFlight3FromCity.get_value(),6)
  createCookie('flight4ToText',objCmbFlight4ToCity.get_text(),6)
  createCookie('flight4FromText',objCmbFlight4FromCity.get_text(),6)
  createCookie('flight4ToValue',objCmbFlight4ToCity.get_value(),6)
  createCookie('flight4FromValue',objCmbFlight4FromCity.get_value(),6)
   if (CalFrom)
        createCookie('ChoosenFlightFromDate',CalFrom.getSelectedDate(),6)
 
    if (CalTo)
        createCookie('ChoosenFlightToDate',CalTo.getSelectedDate(),6)
   if (CalFrom2)
        createCookie('ChoosenFlight2FromDate',CalFrom2.getSelectedDate(),6)
 
    if (CalTo2)
        createCookie('ChoosenFlight2ToDate',CalTo2.getSelectedDate(),6)

 }


function rememberHotelDestinations(DestinationCombo,CalFrom, CalTo)
{
  if (DestinationCombo)
  {
    createCookie('ChoosenHotelCityText',DestinationCombo.get_text(),6)
    createCookie('ChoosenHotelCityValue',DestinationCombo.get_value(),6)
   }
        
   if (CalFrom)
        createCookie('ChoosenHotelFromDate',CalFrom.getSelectedDate(),6)
 
    if (CalTo)
        createCookie('ChoosenHotelToDate',CalTo.getSelectedDate(),6)

 }
 
 function rememberIsraelHotelDestinations(DestinationCombo, CalFrom, CalTo)
{
  if (DestinationCombo)
  {
    createCookie('ChoosenIsraelHotelCityText',DestinationCombo.get_text(),6)
    createCookie('ChoosenIsraelHotelCityValue',DestinationCombo.get_value(),6)
   }
      
   if (CalFrom)
        createCookie('ChoosenIsraelHotelFromDate',CalFrom.getSelectedDate(),6)
 
    if (CalTo)
        createCookie('ChoosenIsraelHotelToDate',CalTo.getSelectedDate(),6)
 }
 
function rememberPackagesDestinations(CountryCombo, CityCombo, CalFrom, CalTo)
{
  if (CountryCombo)
  {
    createCookie('ChoosenPackagesCountryText',CountryCombo.get_text(),6)
    createCookie('ChoosenPackagesCountryValue',CountryCombo.get_value(),6)
   }
   
  if (CityCombo)
  {
    createCookie('ChoosenPackagesCityText',CityCombo.get_text(),6)
    createCookie('ChoosenPackagesCityValue',CityCombo.get_value(),6)
   }
   
   if (CalFrom)
        createCookie('ChoosenPackagesFromDate',CalFrom.getSelectedDate(),6)
 
    if (CalTo)
        createCookie('ChoosenPackagesToDate',CalTo.getSelectedDate(),6)

   
   
 }