﻿var COOKIE_TRIP_SEARCH  = "TripSearch"
var COOKIE_TRIP_TYPE    = "TripType"
var COOKIE_TRIP_COUNTRY = "TripCountry"
var COOKIE_TRIP_CONTINENT = "TripContinent"
var COOKIE_TRIP_DATE    = "TripDate"
var COOKIE_TRIP_DATE_RANGE = "TripDateRange"
var COOKIE_TRIP_DAYS = "TripDays"

var COOKIE_TRIP_EXPIRE_DAYS = "1"

var _urlSeVals;

function initTControl(_baseurl) {
    ClearSelectControl("ddlCategory");
    ClearSelectControl("ddlContinent");
    if(document.getElementById("ddlDays"))
    {
    ClearSelectControl("ddlDays");
    loadDays();
    }
    ClearSelectControl("ddlDateFlexibility");
    loadDateFlex();
    loadCategories();
    loadContinents();
    SetTripsOnLoadValuesFromCookie();
    _urlSeVals = _baseurl + "resources/destinations/trips.aspx";

   GetDdlValues("");
   
}

function ContinueLoading()
{
    var _vals, _currVal;
    if (document.getElementById("hidCategoryValues")!=null) {
        _vals= document.getElementById("hidCategoryValues").value.split(";");
        for (i=0;i<_vals.length;i++)
        {
            _currVal = _vals[i].split("|");
            if (_currVal.length==2) {
                InsertOptionsToSelectControl("ddlCategory", "", _currVal[1], _currVal[0]);
            }
        }
        if (typeof _triptype != 'undefined' && _triptype !='')
                document.getElementById("ddlCategory").value = _triptype
                else
                {
                    if (readCookie(COOKIE_TRIP_TYPE) == null) 
                    document.getElementById("ddlCategory").options[0].selected = true;
                    else
                    document.getElementById("ddlCategory").value = readCookie(COOKIE_TRIP_TYPE);
                }
                   
    }

    if (document.getElementById("hidRegionValues") != null) {
    
        _vals= document.getElementById("hidRegionValues").value.split(";");
        for (i=0;i<_vals.length;i++)
        {
            _currVal = _vals[i].split("|");
            if (_currVal.length==2) {
                InsertOptionsToSelectControl("ddlContinent", "", _currVal[1], _currVal[0]);
            }
        }

        if (typeof _worldarea != 'undefined') {
            if (_worldarea != "") {
                document.getElementById("ddlContinent").value = _worldarea;
            }
        } else {
            if (readCookie(COOKIE_TRIP_CONTINENT) == null)
                document.getElementById("ddlContinent").options[0].selected = true;

            else
                document.getElementById("ddlContinent").value = readCookie(COOKIE_TRIP_CONTINENT);

        }
        GetDdlValues(document.getElementById("ddlContinent").value);
            
    }
}

function CountriesLoading()
{
    clearCountries();
    if (document.getElementById("hidCountryValues")!=null) {
        _vals= document.getElementById("hidCountryValues").value.split(";");
        for (i=0;i<_vals.length;i++)
        {
            _currVal = _vals[i].split("|");
            if (_currVal.length==2) {
                InsertOptionsToSelectControl("ddlCountry", "", _currVal[1], _currVal[0]);
            }
        }
        if (readCookie(COOKIE_TRIP_COUNTRY) == null)
            document.getElementById("ddlCountry").options[0].selected = true;
        else
            document.getElementById("ddlCountry").value = readCookie(COOKIE_TRIP_COUNTRY);  
    }
}

function loadCategories()
{
    ClearSelectControl("ddlCategory");
    InsertOptionsToSelectControl("ddlCategory", "", "0", "- הכל -");
}

function loadDays()
{
    InsertOptionsToSelectControl("ddlDays", "", "0", "- הכל -")
    var MAX_RANGE_TO_SEARCH = 7;
    var MIN_RANGE_TO_SEARCH = 4;
    for (i=MIN_RANGE_TO_SEARCH; i <= MAX_RANGE_TO_SEARCH;i++)
    {
        InsertOptionsToSelectControl("ddlDays", "", i, i)
    }
}

function loadDateFlex()
{
    var MAX_RANGE_TO_SEARCH = 90;
    var MIN_RANGE_TO_SEARCH = 0;
    var DEF_VALUE_OF_DATE_RANGE = 15;
    for (i=MIN_RANGE_TO_SEARCH; i <= MAX_RANGE_TO_SEARCH;i+=5)
    {
        InsertOptionsToSelectControl("ddlDateFlexibility", "", i, i + " יום")
    }
          
    document.getElementById("ddlDateFlexibility").value = DEF_VALUE_OF_DATE_RANGE;
}

function loadContinents()
{
    ClearSelectControl("ddlContinent");
    InsertOptionsToSelectControl("ddlContinent", "", "0", "- הכל -");
}


function clearCountries()
{
    ClearSelectControl("ddlCountry");
    InsertOptionsToSelectControl("ddlCountry", "", "0" ,"- הכל -" );
}

function changeContinent(continentid)
{
    eraseCookie(COOKIE_TRIP_COUNTRY);
    GetDdlValues(continentid);
}

function SetTripsOnLoadValuesFromCookie() {
    
        
    if (readCookie(COOKIE_TRIP_DATE_RANGE) != null)
        document.getElementById("ddlDateFlexibility").value = readCookie(COOKIE_TRIP_DATE_RANGE);

    if (readCookie(COOKIE_TRIP_DAYS) != null && document.getElementById("ddlDays") != null)
        document.getElementById("ddlDays").value = readCookie(COOKIE_TRIP_DAYS);
    
    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 SetTripsResultsCookies(_typeVal, _daysVal, _fromVal, _flexVal, _countryVal, _contVal) {
    eraseCookie(COOKIE_TRIP_TYPE);
    createCookie(COOKIE_TRIP_TYPE, _typeVal, COOKIE_TRIP_EXPIRE_DAYS);
    if (_daysVal) {
        eraseCookie(COOKIE_TRIP_DAYS);
        createCookie(COOKIE_TRIP_DAYS, _daysVal, COOKIE_TRIP_EXPIRE_DAYS);
    }
    eraseCookie(COOKIE_TRIP_DATE);
    createCookie(COOKIE_TRIP_DATE, _fromVal, COOKIE_TRIP_EXPIRE_DAYS);
    if (_flexVal) {
        eraseCookie(COOKIE_TRIP_DATE_RANGE);
        createCookie(COOKIE_TRIP_DATE_RANGE, _flexVal, COOKIE_TRIP_EXPIRE_DAYS);
    }
    eraseCookie(COOKIE_TRIP_COUNTRY);
    createCookie(COOKIE_TRIP_COUNTRY, _countryVal, COOKIE_TRIP_EXPIRE_DAYS);

    eraseCookie(COOKIE_TRIP_CONTINENT);
    createCookie(COOKIE_TRIP_CONTINENT, _contVal, COOKIE_TRIP_EXPIRE_DAYS);
}

function SetTripsCookies(){
    eraseCookie(COOKIE_TRIP_TYPE);
    createCookie(COOKIE_TRIP_TYPE, document.getElementById("ddlCategory").value, COOKIE_TRIP_EXPIRE_DAYS);
    if (document.getElementById("ddlDays"))
    {
    eraseCookie(COOKIE_TRIP_DAYS);
    createCookie(COOKIE_TRIP_DAYS, document.getElementById("ddlDays").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);

    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);
}
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 _numd = 0;
    if (document.getElementById("ddlDays"))
        _numd = document.getElementById("ddlDays").value;
        
    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=" + _numd + "&numn=0" + "&ttype=" + document.getElementById("ddlCategory").value;
    window.location.href = _redirectUrl;
    }    
}

var reqVal;
function ProcessValues()
{
    if (reqVal.readyState == 4)
    {
    // only if "OK"
        if (reqVal.status == 200)
        {
            document.getElementById("divDdlValues").innerHTML = reqVal.responseText;
            ContinueLoading();
        }
    }
}

function ProcessCountryValues()
{
    if (reqVal.readyState == 4)
    {
    // only if "OK"
        if (reqVal.status == 200)
        {
            document.getElementById("divDdlValues").innerHTML = reqVal.responseText;
            CountriesLoading();
        }
    }
}

function GetDdlValues(_continentid)
{
    InitializeValues();
    if(reqVal!=null)
    {
        if (_continentid == "")
            reqVal.onreadystatechange = ProcessValues;
        else
            reqVal.onreadystatechange = ProcessCountryValues;
        reqVal.open("GET", _urlSeVals + "?continentid=" + _continentid, true);
        reqVal.send(null);
    }
}

function InitializeValues()
{
    try
    {
        reqVal=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            reqVal=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc)
        {
            reqVal=null;
        }
    }

    if(!reqVal&&typeof XMLHttpRequest!="undefined")
    {
        reqVal= new
        XMLHttpRequest();
    }
}
