﻿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", "", "3", "טיולים לקהל הרחב")
        InsertOptionsToSelectControl("ddlCategory", "", "10", "טיולי צעירים וסטודנטים")
        InsertOptionsToSelectControl("ddlCategory", "", "4", "טיולי קונטיקי")
        InsertOptionsToSelectControl("ddlCategory", "", "9", "טיולי ג'יפים ושטח")
}

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", "", "6", "אירופה")
        InsertOptionsToSelectControl("ddlContinent", "", "8", "אוסטרליה")
        InsertOptionsToSelectControl("ddlContinent", "", "9", "אסיה")
}

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("איטליה;13;6","אנגליה;12;6","אוסטריה;26;6","אזרבייג`ן;131;6","אנדורה;25;6","אירלנד;60;6","אסטוניה;133;6","ארה``ב;34;2","ארגנטינה;71;2","בולגריה;29;6", "בוסניה;51;6","בלגיה;68;6","בוליביה;77;2","ברזיל;54;2","גאורגיה;165;6","גרוזיה;132;6","גרמניה;15;6","דנמרק;30;6","דרום אפריקה;103;1","הולנד;16;6","הודו;33;9","הונג קונג;73;9","ויאטנם;99;9","ונצואלה;72;2","טנזניה;150;1","ליטא;145;6","לטביה;140;6","מונטנגרו;46;6","מקסיקו;41;2","מרוקו;144;1","נורווגיה;100;6","סין;42;9","סיני;114;1","סקוטלנד;59;6","ספרד;9;6","סלובניה;63;6","סרי לנקה;102;9","נפאל;95;9","פולין;98;6","פרו;76;2","צ`ילה;89;2","צרפת;14;6","קובה;85;2","קנדה;69;2","קמבודיה;107;9","קירגיזסטן;148;9","קניה;35;1","קרואטיה;27;6","רומניה;44;6","רוסיה;134;6","שבדיה;47;6","שוויץ;17;6","תאילנד;31;9","תורכיה;10;9");
    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,_sp){

    var _isValid = ValidateControl();
    if (_isValid){
            SetTripsCookies();
            var _redirectUrl = _BaseUrl + "resources/services/loading_search.aspx?page=../../trips/results.aspx&sp=" + _sp + "&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("ddlPersonType"))
            {
           _redirectUrl = _redirectUrl +  "&ptype=" +document.getElementById("ddlPersonType").value;
            }
            else
            {_redirectUrl = _redirectUrl + "&ptype=0";}
            
            _redirectUrl = _redirectUrl + "&catid=" + document.getElementById("ddlCategory").value
            window.location.href = _redirectUrl;
    }    
}