
var g_currentItnTmp = ""; // itinerary template to be displayed if the list of locations is nu
var lastSailingID = "";
var lastItnColor = "#000099";

/*
*
*
*/
function setDefaultSelection(cb, defSel) {
    if (cb != null && cb.options != null) {
        for (var i = 0; i < cb.options.length; i++) {
            var op = cb.options[i];
            if (defSel == op.value) {
                cb.options[i].selected = true;
                break;
            }
        }
    }
}

/*
*
*
*/
function SetDefault(cb, cookieName) {
    var cookieSel = getCookie(cookieName);
    for (var i = 0; i < cb.length; i++) {
        var oOption = cb.options[i];
        if (oOption.value == cookieSel) {
            oOption.selected = true;
        }
    }
}

/*
*
*
*/
function MM_openWindow(theURL, winName, features) { //v2.0
    if (currSailingID != '' && currSailingID != null && currSailingID != '-1') {
        theURL += "&SailingID=" + currSailingID;
    }
    features = features + ",width=780";
    MM_openBrWindow(theURL, winName, features);
}

function MM_openWindowTemp(theURL, winName, features) { //v2.0
    features = features + ",width=780";
    MM_openBrWindow(theURL, winName, features);
}

/*
*
*
*/
function MM_findObj(n, d) { //v4.01
    var p, i, x;
    if (!d) d = document;
    if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document;
        n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all)
        x = d.all[n];

    for (i = 0; !x && i < d.forms.length; i++)
        x = d.forms[i][n];

    for (i = 0; !x && d.layers && i < d.layers.length; i++)
        x = MM_findObj(n, d.layers[i].document);

    if (!x && d.getElementById) x = d.getElementById(n);
    return x;
}

/*
*
*
*/
function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr;
    for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) {
        x.src = x.oSrc;
    }
}

/////////////////////////////////////////////////////////////////////////

var cookieDays = 90;

/////////////////////////////////////////////////////////////////////////

function BuildSearchReferrer(callFromDeals) {
    if ((callFromDeals != null) ||
		(parent.frames != null && parent.frames["mainFrame"] != null)) {
        try {
            return escape(parent.document.location);
        }
        catch (e) {
            return escape(document.location);
        }
    } else {
        var url = SendState("cruiseresultspage.aspx", 1, 0, true);
        return escape(url);
    }
}

function TransferToABE(cbeUrl, SailingDate, sailingId, skinId, vendorId, shipId, sz, withAir, is55, mil, zp1, zp2, pc, callFromDeals, csURL, PIN, lid, Phone, Home, branch, mode, ppOffer, currencyCode, ManualGroups, hasBonus) {
    childFrame = window.parent.setSailingInfo(vendorId, shipId, SailingDate, sailingId, pc, is55, mil, zp1, zp2);
}
/*
*
*/
function BookCruise(cbeUrl, sailingID, skinID, vndID, sz, withAir, is55, mil, zp1, zp2, pc, callFromDeals, csURL, PIN, lid, Phone, Home, branch, Resstate, mode, ppOffer, currencyCode, ManualGroups, hasBonus, transitCode) {
    var resCity = getElement('ResCityDropDown');
    var depCity = getElement('DepCityDropDown');
    var adult = getElement('AdultDropDown');
    var child = getElement('ChildrenDropDown');
    var infant = getElement('InfantDropDown');
    var senior = getElement('SeniorDropDown');   
    var vid = getElement('LVID');
    var iid = getElement('LIID');
    var sid = getElement('LSID');    
            
    //"withAir" and "mode" will have values for templates which has 'cruise only' and 'with Air' buttons in details page
    //"withAir" and "mode" will be empty for templates which has only 1 SELECT button in details page
    if (depCity != null && depCity.value != "" && depCity.value != null && withAir == 'True' && mode == 'a') {
        setCookie("TravTechDeparture", depCity.value, cookieDays);
    }
    else if (depCity != null && depCity.value != "" && depCity.value != null && withAir == '' && mode == '') {
        setCookie("TravTechDeparture", depCity.value, cookieDays);
        withAir = 'True';
        mode = 'a';
    }
    else if (withAir == '' && mode == '') {
        withAir = 'False';
        mode = 'c';
    }

    var depName = "";
    if (depCity != null && depCity.value != "" && depCity.value != null && withAir == 'True' && mode == 'a') {
        for (var idx = 1; idx < depCity.options.length; idx++) {
            if (depCity.options[idx].selected) {
                depName = depCity.options[idx].innerHTML;
            }
        }
    }
    if (withAir == 'True' && mode == 'a' && depCity != null && depCity.value == "") {
        if (getElement('PleaseSelectAirFromLabel') != null) {
            getElement('PleaseSelectAirFromLabel').style.display = "block";
        }
        if (getElement('PleaseSelectAirFrom') != null) {
            getElement('PleaseSelectAirFrom').style.display = "block";
        }
    }
    else {
        var strURL = cbeUrl;

        if (sailingID != null && sailingID != '')
            strURL += "?sailingid=" + sailingID;

        if (skinID != null && skinID != '') {
            if (skinID.length == 1) {
                skinID = "00" + skinID;
            }
            else if (skinID.length == 2) {
                skinID = "0" + skinID;
            }
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?skin=" + skinID;
            else
                strURL += "&skin=" + skinID;
        }
        if (transitCode != null && transitCode != '') {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?TransitCode=" + transitCode;
            else
                strURL += "&TransitCode=" + transitCode;
        }

        strURL += "&SearchReferrer=" + BuildSearchReferrer(callFromDeals); //escape(document.location);
        if (depCity != null && depCity.value != '' && depCity.value != null && mode == 'a' && withAir == 'True') {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?dep=" + depCity.value;
            else
                strURL += "&dep=" + depCity.value;
        }
        if (depName != null && depName != '' && mode == 'a' && withAir == 'True') {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?depname=" + escape(depName);
            else
                strURL += "&depname=" + escape(depName);
        }

        if (Resstate != null && Resstate != '') {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?res=" + escape(Resstate);
            else
                strURL += "&res=" + escape(Resstate);
        }
        if (adult != null && adult.value != "" && adult.value != null) {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?ad=" + adult.value;
            else
                strURL += "&ad=" + adult.value;
        }
        if (child != null && child.value != "" && child.value != null) {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?ch=" + child.value;
            else
                strURL += "&ch=" + child.value;
        }
        if (infant != null && infant.value != "" && infant.value != null) {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?inf=" + infant.value;
            else
                strURL += "&inf=" + infant.value;
        }
        if (senior != null && senior.value != "" && senior.value != null) {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?se=" + senior.value;
            else
                strURL += "&se=" + senior.value;
        }
        if (sz != null && sz != "") {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?sz=" + escape(sz);
            else
                strURL += "&sz=" + escape(sz);
        }
        if (withAir != null && withAir != "") {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?air=" + withAir;
            else
                strURL += "&air=" + withAir;
        }
        if (PIN != null && PIN != "") {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?PIN=" + PIN;
            else
                strURL += "&PIN=" + PIN;
        }
        if (Phone != null && Phone != "") {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?Phone=" + Phone;
            else
                strURL += "&Phone=" + Phone;
        }
        if (branch != null && branch != "") {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?branch=" + branch;
            else
                strURL += "&branch=" + branch;
        }
        if (lid != null && lid != "") {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?LID=" + lid;
            else
                strURL += "&LID=" + lid;
        }

        if (mode != null && mode != "") {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?mode=" + mode;
            else
                strURL += "&mode=" + mode;
        }

        if ('true' == withAir || 'True' == withAir) {
            strURL += "&FromSailingsList=True";
        }

        if (!callFromDeals) {
            if (is55 != null && is55 != "") {
                var arrURL = strURL.split('?');
                if (arrURL.length == 1)
                    strURL += "?s55=" + is55;
                else
                    strURL += "&s55=" + is55;
            }

            if (mil != null && mil != "") {
                var arrURL = strURL.split('?');
                if (arrURL.length == 1)
                    strURL += "?mil=" + mil;
                else
                    strURL += "&mil=" + mil;
            }

            if (ppOffer == "true") {
                var arrURL = strURL.split('?');
                if (arrURL.length == 1)
                    strURL += "?PP=y";
                else
                    strURL += "&PP=y";
            }

            if (zp1 != null && zp1 != "") {
                var arrURL = strURL.split('?');
                if (arrURL.length == 1)
                    strURL += "?ST1=" + zp1;
                else
                    strURL += "&ST1=" + zp1;
            }

            if (pc != null && pc != "") {
                var arrURL = strURL.split('?');
                if (arrURL.length == 1)
                    strURL += "?PC=" + pc;
                else
                    strURL += "&PC=" + pc;
            }
        }
        else {
            var hotDealsSeniors = getElement('HotDealsSeniors');
            var hotDealsMilitary = getElement('HotDealsMilitary');

            if (hotDealsSeniors != null && hotDealsSeniors.checked != "") {
                var arrURL = strURL.split('?');
                if (arrURL.length == 1)
                    strURL += "?s55=" + hotDealsSeniors.checked;
                else
                    strURL += "&s55=" + hotDealsSeniors.checked;
            }

            if (hotDealsMilitary != null && hotDealsMilitary.checked != "") {
                var arrURL = strURL.split('?');
                if (arrURL.length == 1)
                    strURL += "?mil=" + hotDealsMilitary.checked;
                else
                    strURL += "&mil=" + hotDealsMilitary.checked;
            }
        }

        if (callFromDeals != null && callFromDeals != '') {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?callFromDeals=" + callFromDeals;
            else
                strURL += "&callFromDeals=" + callFromDeals;
        }
        if (ManualGroups != null && ManualGroups != '') {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?arates=" + ManualGroups;
            else
                strURL += "&arates=" + ManualGroups;
        }
        if (hasBonus != null && hasBonus != '') {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?Bonus=" + hasBonus;
            else
                strURL += "&Bonus=" + hasBonus;
        }
        if (Home != null && Home != "") {
            var arrURL = strURL.split('?');
            if (arrURL.length == 1)
                strURL += "?Home=" + Home;
            else
                strURL += "&Home=" + Home;
        }

        try {
            parent.document.location = strURL;
        }
        catch (e) {
            document.location = strURL;
        }
    }
}


function LoadPax(strDefAdult, strDefChild, maxPax, minPax) {
    var adult = getElement('AdultDropDown');
    var children = getElement('ChildrenDropDown');
    var infant = getElement('InfantDropDown');

    var defAdult = parseInt(strDefAdult);
    var defChild = parseInt(strDefChild);

    if (parseInt(minPax) > 0 && parseInt(maxPax) > 0) {
        FillPaxDropDown(adult, maxPax, minPax); // fill adult drop down starting from defAdult
        setDefaultSelection(adult, 2);

        FillPaxDropDown(children, maxPax - 1, 0);

        FillPaxDropDown(infant, maxPax - 1, 0);
    }
    else {
        getElement('AdultDropDown').disabled = true;
        getElement('InfantDropDown').disabled = true;
        getElement('ChildrenDropDown').disabled = true;
    }
}

function LoadPaxOffline(minPax, maxPax) {
    var adult = document.getElementById('AdultDropDown');
    FillPaxDropDown(adult, maxPax, minPax);
    setDefaultSelection(adult, 2);
}

function OnPaxChange(MaxPax, ChangedControl) {
    var adult = getElement('AdultDropDown');
    var children = getElement('ChildrenDropDown');
    var infant = getElement('InfantDropDown');

    var selAdult = parseInt(adult.value);
    var selChild = children == null ? 0 : parseInt(children.value);
    var selInfant = infant == null ? 0 : parseInt(infant.value);
    var total = selAdult + selChild + selInfant;

    if (total > MaxPax) {
        var totalOtherPax = 0;
        switch (ChangedControl) {
            case 'AdultDropDown':
                totalOtherPax = selChild + selInfant;
                break;
            case 'ChildrenDropDown':
                totalOtherPax = selAdult + selInfant;
                break;
            case 'InfantDropDown':
                totalOtherPax = selAdult + selChild;
                break;
        }

        alert('Maximum ' + MaxPax + ' passengers are allowed on this cruise including adults, children and infants.');
        setDefaultSelection(getElement(ChangedControl), MaxPax - totalOtherPax);
    }
}


function FillPaxDropDown(cb, difPax, index) {
    if (cb == null)
        return;

    for (var i = index; i <= difPax; i++) {
        var oOption = new Option(i, i);
        cb.options[cb.length] = oOption;
    }
}

function showHideDepAir(selectorId, controlId) {
    if (getElement(selectorId).checked == true) {
        getElement(controlId).style.display = "block";
    }
    else {
        getElement(controlId).style.display = "none";
    }
}
