﻿var COOKIE_TRIP_SEARCH         = "TripSearch"
var COOKIE_TRIP_TYPE = "TripType"
var COOKIE_TRIP_COUNTRY = "TripCountry"
var COOKIE_TRIP_CONTINENT = "TripContinent"
var COOKIE_TRIP_AGE = "TripAge"
var COOKIE_TRIP_DATE = "TripDate"
var COOKIE_TRIP_DATE_RANGE = "TripDateRange"

var COOKIE_TRIP_EXPIRE_DAYS    = "1"


function initControl(_baseurl){

        ClearSelectControl("ddlCategory");
        ClearSelectControl("ddlContinent");
        if(document.getElementById("ddlPersonType"))
        {
        ClearSelectControl("ddlPersonType");
        loadPersonType();
        }
        ClearSelectControl("ddlDateFlexibility");
        
        loadCategories();
        
        
        loadContinents();
        loadDateFlex();
       
         SetTripsOnLoadValuesFromCookie(_baseurl) ;
         loadCountries(document.getElementById("ddlContinent").value);
       
}

function loadCategories()
{
           InsertOptionsToSelectControl("ddlCategory", "", "0", "- הכל -")
        InsertOptionsToSelectControl("ddlCategory", "", "1", "הנוסע העצמאי")
        InsertOptionsToSelectControl("ddlCategory", "", "2", "בת / בר מצווה")
        InsertOptionsToSelectControl("ddlCategory", "", "3", "ג'פים / אקסטרים")
        InsertOptionsToSelectControl("ddlCategory", "", "4", "יוקרה")
 	 InsertOptionsToSelectControl("ddlCategory", "", "5", "מסלולי ערים")
        InsertOptionsToSelectControl("ddlCategory", "", "6", "משפחות")
        InsertOptionsToSelectControl("ddlCategory", "", "7", "נשים")
        InsertOptionsToSelectControl("ddlCategory", "", "8", "ספא ובריאות")
  InsertOptionsToSelectControl("ddlCategory", "", "9", "ספארי / טבע")
        InsertOptionsToSelectControl("ddlCategory", "", "10", "צעירים / סטודנטים")
        InsertOptionsToSelectControl("ddlCategory", "", "11", "קהל דתי")
  InsertOptionsToSelectControl("ddlCategory", "", "40", "טיולים לקהל הרחב")
}

function loadPersonType()
{
        InsertOptionsToSelectControl("ddlPersonType", "", "0", "- הכל -")
        InsertOptionsToSelectControl("ddlPersonType", "", "2", "נוער 17-18")
        InsertOptionsToSelectControl("ddlPersonType", "", "1", "סטודנטים 20-35")
        InsertOptionsToSelectControl("ddlPersonType", "", "15", "איסתא 'פלוס' (לקהל הרחב)")
}
function loadContinents()
{
        InsertOptionsToSelectControl("ddlContinent", "", "0", "- הכל -")
InsertOptionsToSelectControl("ddlContinent", "", "1", "אירופה")        
InsertOptionsToSelectControl("ddlContinent", "", "2", "אפריקה")
        InsertOptionsToSelectControl("ddlContinent", "", "3", "אסיה")
        InsertOptionsToSelectControl("ddlContinent", "", "4", "אמריקה")
        

}

function loadDateFlex()
{
  var MAX_RANGE_TO_SEARCH = 90;
         var MIN_RANGE_TO_SEARCH = 0;
         var DEF_VALUE_OF_DATE_RANGE = 30;
          for (i=MIN_RANGE_TO_SEARCH; i <= MAX_RANGE_TO_SEARCH;i+=15)
          {
            InsertOptionsToSelectControl("ddlDateFlexibility", "", i, i + " יום")
          }
          
          document.getElementById("ddlDateFlexibility").value = DEF_VALUE_OF_DATE_RANGE;
}

function loadCountries(_continent)
{

    ClearSelectControl("ddlCountry");
    //name;code;continent
    var arrCountries = new Array("איטליה;99;1","אנגליה;61;1","אוסטריה;13;1","אזרבייג`ן;14;1","אנדורה;5;1","אירלנד;97;1","ארה``ב;212;4","ארגנטינה;9;4","בולגריה;31;1","בלגיה;20;1","בוליביה;25;4","ברזיל;28;4","גרמניה;76;1","דנמרק;54;1","הולנד;141;1","הודו;93;3","הונג קונג;90;3","ויאטנם;219;3","ונצואלה;218;4","מקסיקו;130;4","מרוקו;134;2","נורווגיה;152;1","סין;42;3","סיני;59;2","ספרד;183;1","סלובניה;178;1","נפאל;140;3","פולין;162;1","פרו;160;4","קנדה;36;2","קמבודיה;34;3","קירגיזסטן;108;3","קניה;105;2","קרואטיה;50;1","רומניה;167;1","רוסיה;168;1","צ`ילה;41;4","צרפת;70;1","שבדיה;193;1","שוויץ;194;1","תורכיה;205;3","תאילנד;199;3");
    var _value = new String();
    
    if (_continent=="0")  // on first load
    {
        InsertOptionsToSelectControl("ddlCountry", "", "0" ,"- הכל -" )
    }
    
    for (i=0 ;i<arrCountries.length;i++)
    {
         _value =  arrCountries[i].toString();
        if (_continent!='undefined' && _continent!="0") // when select continents had changed
        {
            if (_continent == _value.split(";")[2])
            {
                InsertOptionsToSelectControl("ddlCountry", "",_value.split(";")[1] , _value.split(";")[0] )
            }
        }
        else // on first load
        {
             InsertOptionsToSelectControl("ddlCountry", "",_value.split(";")[1] , _value.split(";")[0] )
        }
    }
    if (document.getElementById("ddlCountry").childNodes.length>0)
    {
        document.getElementById("ddlCountry").selectedIndex=0;
    }
    else
    {
         InsertOptionsToSelectControl("ddlCountry", "", "-1" ,"- בחר -" )
    }
}


function SetTripsOnLoadValuesFromCookie()
{
    
     if (readCookie(COOKIE_TRIP_TYPE) == null)
            document.getElementById("ddlCategory").options[0].selected = true;
        else
            document.getElementById("ddlCategory").value = readCookie(COOKIE_TRIP_TYPE);
            
     if (readCookie(COOKIE_TRIP_CONTINENT) == null)
    document.getElementById("ddlContinent").options[0].selected = true;
    else
    document.getElementById("ddlContinent").value = readCookie(COOKIE_TRIP_CONTINENT);
    if (document.getElementById("ddlPersonType"))
    {
     if (readCookie(COOKIE_TRIP_AGE) == null)
    document.getElementById("ddlPersonType").options[0].selected = true;
    else
    document.getElementById("ddlPersonType").value = readCookie(COOKIE_TRIP_AGE);
    }
      if (readCookie(COOKIE_TRIP_COUNTRY) == null)
    document.getElementById("ddlCountry").options[0].selected = true;
    else
    document.getElementById("ddlCountry").value = readCookie(COOKIE_TRIP_COUNTRY);
    
     if (readCookie(COOKIE_TRIP_DATE_RANGE) != null)
    document.getElementById("ddlDateFlexibility").value = readCookie(COOKIE_TRIP_DATE_RANGE);
    
   if (readCookie(COOKIE_TRIP_DATE) == null)
   {
        var tmpDate = new Date();
        document.getElementById("fdtTripsFrom").value = tmpDate.getDate() + "/" + parseInt(tmpDate.getMonth() + 1).toString() + "/" + tmpDate.getFullYear();
    }
    else
    document.getElementById("fdtTripsFrom").value = readCookie(COOKIE_TRIP_DATE);
}

 function readCookie(name) {
	    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 eraseCookie(name) {
    createCookie(name,"",-1);
}

function createCookie(name,value,days) {
    if (days) {
	    var date = new Date();
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function SetTripsCookies(){
     
     
    var COOKIE_TRIP_TYPE = "TripType"
    var COOKIE_TRIP_COUNTRY = "TripCountry"
    var COOKIE_TRIP_CONTINENT = "TripContinent"
    var COOKIE_TRIP_AGE = "TripAge"
    var COOKIE_TRIP_DATE = "TripDate"
    var COOKIE_TRIP_DATE_RANGE = "TripDateRange"


    eraseCookie(COOKIE_TRIP_TYPE);
    createCookie(COOKIE_TRIP_TYPE, document.getElementById("ddlCategory").value, COOKIE_TRIP_EXPIRE_DAYS);

    eraseCookie(COOKIE_TRIP_COUNTRY);
    createCookie(COOKIE_TRIP_COUNTRY, document.getElementById("ddlCountry").value, COOKIE_TRIP_EXPIRE_DAYS);
    
    eraseCookie(COOKIE_TRIP_CONTINENT);
    createCookie(COOKIE_TRIP_CONTINENT, document.getElementById("ddlContinent").value, COOKIE_TRIP_EXPIRE_DAYS);
    if (document.getElementById("ddlPersonType"))
    {
    eraseCookie(COOKIE_TRIP_AGE);
    createCookie(COOKIE_TRIP_AGE, document.getElementById("ddlPersonType").value, COOKIE_TRIP_EXPIRE_DAYS);
    }
    eraseCookie(COOKIE_TRIP_DATE);
    createCookie(COOKIE_TRIP_DATE, document.getElementById("fdtTripsFrom").value, COOKIE_TRIP_EXPIRE_DAYS);
    
    eraseCookie(COOKIE_TRIP_DATE_RANGE);
    createCookie(COOKIE_TRIP_DATE_RANGE, document.getElementById("ddlDateFlexibility").value, COOKIE_TRIP_EXPIRE_DAYS);

 	}
  function ValidateControl() {
        var departuredate = document.getElementById("fdtTripsFrom").value
        if (departuredate == "")
        {
            alert("נא למלא תאריך יציאה ")
            return false;
        }
        return true;
    }
    
function GoToTripResults(_BaseUrl){

    var _isValid = ValidateControl();
    if (_isValid){
            SetTripsCookies();
            var _redirectUrl = _BaseUrl + "resources/services/loading_search.aspx?page=../../trips/results.aspx&msg=1&dest=" + document.getElementById("ddlCountry").value + "&dcontid=" + document.getElementById("ddlContinent").value + "&ddate=" + document.getElementById("fdtTripsFrom").value + "&maxrdays=" + document.getElementById("ddlDateFlexibility").value + "&minrdays=0" + "&numd=0"
            if (document.getElementById("ddlCategory"))
            {
           _redirectUrl = _redirectUrl +  "&ttype=" +document.getElementById("ddlCategory").value;
            }
            else
            { _redirectUrl = _redirectUrl + "&ttype=0"; }
            
            _redirectUrl = _redirectUrl + "&numn=" + document.getElementById("ddlPersonType").value;
            window.location.href = _redirectUrl;
    }    
}
