﻿function at_updateValuesFromMemory() {
    var searchFormSelect = $('search-form-select');
    if (searchFormSelect) {
        _at_fillWithBrandByValue(searchFormSelect);
        changeSearchForm();
    }

    var form_to_update = $('frmCompactSearch');
    if (!form_to_update)
        return; // nothing to do
        
    var COOKIE = 1;
    var PARAM = 2;
    var PARAM_AND_COOKIE = 3;        
    _at_fillWithBrandByValue(form_to_update.departAirport);
    _at_fillWithBrandByValue($('CTY1_ID'),true,PARAM_AND_COOKIE);
    if(typeof multiSelect != "undefined") { //only on full search
        getMultiple(this,'CTY1');
    }
    _at_fillWithBrandByValue($('CTY2_ID'),true,PARAM_AND_COOKIE);
    if(typeof multiSelect != "undefined") { //only on full search
        Reset('CTY2', false);buildGeogLists('CTY2','','','','');
    }
    _at_fillWithBrandByValue($('CTY3_ID'),true,PARAM_AND_COOKIE);
    _at_fillWithBrandByValue(form_to_update.departureMonthYear);
    _at_fillWithBrandByValue(form_to_update.departureDay);
    _at_fillWithBrandByValue(form_to_update.departureMonthYearEnd);
    _at_fillWithBrandByValue(form_to_update.departureDayEnd);
    _at_fillWithBrandByValue(form_to_update.duration);
    _at_fillWithBrandByValue($('dur3'),true);
    _at_fillWithBrandByValue($('dur7'),true);
    _at_fillWithBrandByValue($('dur14'),true);
    _at_fillWithBrandByValue($('dur4-6'),true);
    _at_fillWithBrandByValue($('dur10-11'),true);
    _at_fillWithBrandByValue($('dur14+'),true);
    _at_fillWithBrandByValue($('star-rating-1-2'),true);
    _at_fillWithBrandByValue($('star-rating-3'),true);
    _at_fillWithBrandByValue($('star-rating-4'),true);
    _at_fillWithBrandByValue($('star-rating-5'),true);
    _at_fillWithBrandByValue(form_to_update.accommodation);
    _at_fillWithBrandByValue($('env-rating-1'),true);
    _at_fillWithBrandByValue($('env-rating-2'),true);
    _at_fillWithBrandByValue($('env-rating-3'),true);
    _at_fillWithBrandByValue($('productgroup-option-1'),true);
    _at_fillWithBrandByValue($('productgroup-option-2'),true);
    _at_fillWithBrandByValue($('productgroup-option-3'),true);
    _at_fillWithBrandByValue($('productgroup-option-4'),true);
    _at_fillWithBrandByValue($('activity-option-1'),true);
    _at_fillWithBrandByValue($('activity-option-2'),true);
    _at_fillWithBrandByValue($('activity-option-3'),true);
    _at_fillWithBrandByValue($('activity-option-4'),true);
    _at_fillWithBrandByValue($('accom-option-1'),true);
    _at_fillWithBrandByValue($('accom-option-2'),true);
    _at_fillWithBrandByValue($('accom-option-3'),true);
    _at_fillWithBrandByValue($('accom-option-4'),true);
    _at_fillWithBrandByValue($('accom-option-5'),true);
    _at_fillWithBrandByValue($('accom-option-6'),true);
    _at_fillWithBrandByValue($('accom-option-7'),true);
    _at_fillWithBrandByValue($('accom-option-8'),true);
    _at_fillWithBrandByValue($('accom-option-9'),true);
    _at_fillWithBrandByValue($('accom-option-10'),true);
    _at_fillWithBrandByValue($('accom-option-11'),true);
    _at_fillWithBrandByValue($('accom-option-12'),true);
    _at_fillWithBrandByValue(form_to_update.rooms);
    oSearchController.setRooms();
    for (var i = 0; i < oSearch.Rooms.countRooms(); i++) {
        oSearchController.fetchAdultsFromCookie(i);
        oSearchController.fetchChildrenFromCookie(i);
    }
}

function at_updateFlightValuesFromMemory() {
    var form_to_update = $('frmCompactSearch');
    var searchFormSelect = $('search-form-select');
    if (searchFormSelect) {
        _at_fillWithBrandByValue(searchFormSelect);
        changeSearchForm();
    }

    var COOKIE = 1;
    var PARAM = 2;
    var PARAM_AND_COOKIE = 3;        
    _at_fillWithBrandByValue(form_to_update.departAirport);
    _at_fillWithBrandByValue($('CTY1_ID'),true,PARAM_AND_COOKIE);
    if(typeof multiSelect != "undefined") { //only on full search
        getMultiple(this,'CTY1');
    }
    _at_fillWithBrandByValue($('CTY2_ID'),true,PARAM_AND_COOKIE);
    if(typeof multiSelect != "undefined") { //only on full search
        Reset('CTY2', false);buildGeogLists('CTY2','','','','');
    }
    _at_fillWithBrandByValue($('CTY3_ID'),true,PARAM_AND_COOKIE);
    _at_fillWithBrandByValue(form_to_update.departureMonthYear);
    _at_fillWithBrandByValue(form_to_update.departureDay);
    _at_fillWithBrandByValue(form_to_update.departureMonthYearEnd);
    _at_fillWithBrandByValue(form_to_update.departureDayEnd);
    _at_fillWithBrandByValue(form_to_update.duration);
    _at_fillWithBrandByValue(form_to_update.infants);
    _at_fillWithBrandByValue($('r-0-adults'));
    _at_fillWithBrandByValue($('r-0-children'));
        
}

function at_reset_form() {
    var form_to_update = $('frmCompactSearch');
    if (!form_to_update)
        return; // nothing to do
    _at_set_selected(form_to_update.departAirport,0);
    _at_set_selected(form_to_update.CTY1_ID,0);
    _at_set_selected(form_to_update.CTY2_ID,0);
    _at_set_selected(form_to_update.CTY3_ID,0);
    _at_set_selected(form_to_update.departureMonthYear,0);
    _at_set_selected(form_to_update.departureDay,0);
    _at_set_selected(form_to_update.departureMonthYearEnd,0);
    _at_set_selected(form_to_update.departureDayEnd,0);
    _at_set_selected(form_to_update.duration,0);
    _at_set_checked($('dur3'),false);
    _at_set_checked($('dur7'),true);
    _at_set_checked($('dur14'),false);
    _at_set_checked($('dur4-6'),false);
    _at_set_checked($('dur10-11'),false);
    _at_set_checked($('dur14+'),false);
    _at_set_checked($('star-rating-1-2'),false);
    _at_set_checked($('star-rating-3'),false);
    _at_set_checked($('star-rating-4'),false);
    _at_set_checked($('star-rating-5'),false);
    _at_set_selected(form_to_update.accommodation,0);
    _at_set_checked($('env-rating-1'),false);
    _at_set_checked($('env-rating-2'),false);
    _at_set_checked($('env-rating-3'),false);
    _at_set_checked($('productgroup-option-1'),false);
    _at_set_checked($('productgroup-option-2'),false);
    _at_set_checked($('productgroup-option-3'),false);
    _at_set_checked($('productgroup-option-4'),false);
    _at_set_checked($('activity-option-1'),false);
    _at_set_checked($('activity-option-2'),false);
    _at_set_checked($('activity-option-3'),false);
    _at_set_checked($('activity-option-4'),false);
    _at_set_checked($('accom-option-1'),false);
    _at_set_checked($('accom-option-2'),false);
    _at_set_checked($('accom-option-3'),false);
    _at_set_checked($('accom-option-4'),false);
    _at_set_checked($('accom-option-5'),false);
    _at_set_checked($('accom-option-6'),false);
    _at_set_checked($('accom-option-7'),false);
    _at_set_checked($('accom-option-8'),false);
    _at_set_checked($('accom-option-9'),false);
    _at_set_checked($('accom-option-10'),false);
    _at_set_checked($('accom-option-11'),false);
    _at_set_checked($('accom-option-12'),false);
    _at_set_selected(form_to_update.rooms,0);
    oSearchController.setRooms();
    _at_set_selected($('r-0-adults'),1);
    var cookie_name = brand + 'r-0-adults';
    _at_removeCookie(cookie_name)
    _at_set_selected($('r-0-children'),0);
    oSearchController.setAdults('0');
    oSearchController.setChildren('0');
}

function at_reset_flight_form() {
    var form_to_update = $('frmCompactSearch');
    if (!form_to_update)
        return; // nothing to do
    _at_set_selected(form_to_update.departAirport,0);
    _at_set_selected(form_to_update.CTY1_ID,0);
    _at_set_selected(form_to_update.departureMonthYear,0);
    _at_set_selected(form_to_update.departureDay,0);
    _at_set_selected(form_to_update.departureMonthYearEnd,0);
    _at_set_selected(form_to_update.departureDayEnd,0);
    _at_set_selected(form_to_update.duration,0);
    _at_set_selected($('r-0-adults'),1);
    var cookie_name = brand + 'r-0-adults';
    _at_removeCookie(cookie_name)
    _at_set_selected($('children'),0);
    _at_set_selected($('infants'),0);
}



// Used for IE6 related bug regarding CS00008 - if selects are dynamically created inside display: none area, IE6 doesn't want to reveal their content afterwards
function at_rebuildRoomsForIE6() {
    if(oSearchController && oSearch && document.frmCompactSearch && document.frmCompactSearch.rooms) {
        oSearchController.rebuildRooms();
        for (var i = 0; i < oSearch.Rooms.countRooms(); i++) {
            oSearchController.fetchAdultsFromCookie(i);
            oSearchController.fetchChildrenFromCookie(i);
        }
    }
}


// used from calendar 
function at_saveDateFields() {
    oSearchController.setChildAgeDisplay();
    var form_to_update = $('frmCompactSearch');
    _at_saveWithBrandByValue(form_to_update.departureDay);    
    _at_saveWithBrandByValue(form_to_update.departureMonthYear);
    _at_saveWithBrandByValue(form_to_update.departureMonthYearEnd);
    _at_saveWithBrandByValue(form_to_update.departureDayEnd);
}

// setCookie: function (sName, sValue, oExpires, sPath, sDomain, bSecure) {
// getCookie: function (sName) {

function _at_setCookie(name, value) {

    var escapedValue = "#"+escape(value)+"#";

    if (escapedValue == "#null#") {
        _at_removeCookie(name);
        return;
    }

    // cookie syntax : memory=/key1/#escape(value1)#/key2/#escape(value2)#; PATH=/

    var masterCookie = oCookie.getCookie("frmmry");

    var key = "/"+name+"/";

    if (masterCookie == null) {
        masterCookie = key + escapedValue;
    } else {
        var ind = masterCookie.indexOf(key);
        if (ind != -1) {
            var endInd = masterCookie.indexOf("/",ind + key.length);
            if (endInd != -1) {
                masterCookie = masterCookie.substring(0,ind) + key + escapedValue + masterCookie.substring(endInd);
            } else {
                masterCookie = masterCookie.substring(0,ind) + key + escapedValue;
            }
        } else {
            masterCookie = masterCookie + key + escapedValue;
        }
    }

    oCookie.setCookie("frmmry",masterCookie);
}

function _at_removeCookie(name) {
    var masterCookie = oCookie.getCookie("frmmry");

    var key = "/"+name+"/";

    if (masterCookie != null) {
        var ind = masterCookie.indexOf(key);
        if (ind != -1) {
            var endInd = masterCookie.indexOf("/",ind + key.length);
            if (endInd != -1) {
                masterCookie = masterCookie.substring(0,ind) + masterCookie.substring(endInd);
            } else {
                masterCookie = masterCookie.substring(0,ind);
            }
            oCookie.setCookie("frmmry",masterCookie);
        }
    }
}

function _at_getCookie(name) {
    // cookie syntax : memory=/key1/#escape(value1)#/key2/#escape(value2)#; PATH=/

    var masterCookie = oCookie.getCookie("frmmry");

    if (masterCookie != null) {
        var key = "/"+name+"/";
        var ind = masterCookie.indexOf(key);
        if (ind != -1) {
            var startInd = ind + key.length;
            var endInd = masterCookie.indexOf("/",startInd);
            var result = null;
            if (endInd != -1) {
                return unescape(masterCookie.substring(startInd+1,endInd-1));
            } else {
                return unescape(masterCookie.substring(startInd+1,masterCookie.length-1));
            }
        }
    }
    return null;
}

function _at_saveWithBrandByValue(element,useId) {
    if (!element)   
        return;
    if (typeof(element) == "string") {
        element = document.getElementById(element);
        if (!element)   
            return;
    }
    var val = null;
    if ((element.type == "text") || (element.type == "password") || (element.type == "textarea") || (element.type == "radio") || (element.type == "hidden")) {
        val = element.value;
    } else if (element.type == "select-one") {
        if(element.selectedIndex != -1)
            val = element.options[element.selectedIndex].value
    } else if (element.type == "select-multiple") {
        if(element.selectedIndex != -1) {
            val = element.options[element.selectedIndex].value
            for(var i = element.selectedIndex + 1 ; i < element.options.length ; i++)
                if(element.options[i].selected)
                    val += "_" + element.options[i].value;
        }
    } else if (element.type == "checkbox") {
        val = element.checked;
    } else if (typeof(element) == "object" && element.length > 0) {
        var elements = element;
        for ( var i = 0 ; i < elements.length ; i++) {
            element = elements[i];
            if(element && element.type == "radio" && element.checked)
                val = element.value;
        }
    }
    var cookie_name;
    if (useId) {
        cookie_name=brand+element.id;
    } else {
        cookie_name=brand+element.name;
    }
    _at_setCookie(cookie_name,val);
}

// for custom form elements
// methods : 1 (default) = only cookie, 2 = only http request query parameter, 3 = both
function _at_fillWithBrandByValue(element,useId,method) {
    if(!element)
        return false;
    if (typeof(element) == "string") {
        element = document.getElementById(element);
        if (!element)   
            return;
    }
    var cookie_name;
    var param_name;
    if (useId) {
        cookie_name=brand+element.id;
        param_name=element.id;
    } else {
        cookie_name=brand+element.name;
        param_name=element.name;
    }
    var val;
    if (method && method >= 2)
        val = _at_getParam(param_name);
    if (!val && (!method || method != 2))
        val = _at_getCookie(cookie_name);
    if(!val)
        return false;
    if (element.type == "select-one" || element.type == "select-multiple") {
        var values = val.split("_");
        element.selectedIndex = -1;
        for(var l=0;l < values.length ; l++) {
            for(var k=0;k<element.options.length;k++)
                if (element.options[k].value == values[l]) {
                    element.options[k].selected=true;
                    if(element.type == "select-one")
                        break;
                }
        }
        if(element.selectedIndex == -1) {
            element.selectedIndex = 0;
        } 
    } else if (element.type == "checkbox") {
        if (val != "true") {
            element.checked = false;
        } else {
            element.checked = true;
        }
    } else if ((element.type == "text") || (element.type == "password") || (element.type == "textarea") || (element.type == "radio") || (element.type == "hidden")) {
        element.value = val;
    } else {
        return false;
    }
    
    $(element).fireEvent('change', $(element));
    if(element.onchange) {
        element.onchange();
    }
}

function _at_set_selected(select, index) {
    if(select && (select.selectedIndex != index || select.type == "select-multiple")) {
        select.selectedIndex = index;
        if(select.onclick)
            select.onclick();
        if(select.onchange)
            select.onchange();
    }
}

function _at_set_checked(checkbox, checked) {
    if(checkbox && checkbox.checked != checked) {
        checkbox.checked = checked;
        if(checkbox.onclick)
            checkbox.onclick();
        if(checkbox.onchange)
            checkbox.onchange();
    }
}
function _at_set_text(text, value) {
    if(text && text.value != value) {
        text.value = value;
        if(text.onclick)
            text.onclick();
        if(text.onchange)
            text.onchange();
    }
}

function _at_getParam(name) {
    var queryString = location.search;
    if(!queryString)
        return null;
    var queryString = queryString.substring(1,queryString.length);
    if(queryString.length>0) {
        if(queryString.search(name) == -1)
            return null;
        var arrayPairs = queryString.split("&");
        for(var i=0;i<arrayPairs.length;i++){
            var pair = arrayPairs[i].split("=");
            if (pair[0] == name)
                return pair[1]
        }
    }
    return null;
}



