﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />

function fillCategory(categoryIDSelected) {
    var combo = document.getElementById("kategorie");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "oblast: (libovolná)", "", "");

        for (var i = 0; i < categories[583].length; i++) {
            appendOptionLast(combo, categories[583][i].id, categories[583][i].name, categoryIDSelected, "");
        }
    }
}

function fillShips(shipIDSelected) {
    var combo = document.getElementById("lode");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "loď: (libovolná)", "", "");

        for (var i = 0; i < ships[584].length; i++) {
            appendOptionLast(combo, ships[584][i].id, ships[584][i].name, shipIDSelected, "indent" + ships[584][i].indent);
        }
    }
}

function fillCountry(categoryID, shipID, countryID) {
    var combo = document.getElementById("zeme");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "země: (libovolná)", "", "");
        if (categoryID == "") categoryID = 583;
        if (shipID == "") shipID = 584;
        if (typeof countries[categoryID] != "undefined") {
            for (var i = 0; i < countries[categoryID].length; i++) {
                for (var j = 0; j < countries[shipID].length; j++) {
                    if (countries[categoryID][i].id == countries[shipID][j].id)
                        appendOptionLast(combo, countries[shipID][j].id, countries[shipID][j].name, countryID, "");
                }
            }
        }
    }
}

function fillDestination(countryID, destinationID) {
    //    var combo = document.getElementById("destinace");
    //    if (combo != null) {
    //        $(combo).html("");
    //        appendOptionLast(combo, "", "destinace: (libovolná)", "", "");
    //        if (typeof destinations[countryID] != "undefined") {
    //            for (var i = 0; i < destinations[countryID].length; i++) {
    //                appendOptionLast(combo, destinations[countryID][i].id, destinations[countryID][i].name, destinationID, "");
    //            }
    //            combo.disabled = false;
    //        }
    //        else {
    //            combo.disabled = true;
    //        }
    //    }
}

function chgChild(sel) {
    childrens = parseInt(sel.options[sel.selectedIndex].text);

    var vek = document.getElementById('vek');
    if (childrens <= 0) {
        vek.style.display = 'none';
        return;
    }

    vek.style.display = 'inline';
    for (i = 1; i <= 10; i++)
        document.getElementById('ctl00_cph_plavba1_TourInfoControl_vek' + i).style.display = (i > childrens) ? 'none' : 'inline';
}


function refreshZeme() {
    var combo = document.getElementById("kategorie");
    var shipCombo = document.getElementById("lode");
    if (combo != null && combo != shipCombo) {
        fillCountry(combo.options[combo.selectedIndex].value, shipCombo.options[shipCombo.selectedIndex].value, "");
    }
    refreshDestinace();
}

function refreshDestinace() {
    var combo = document.getElementById("zeme");
    if (combo != null) {
        fillDestination(combo.options[combo.selectedIndex].value);
    }
}



function zobrazitDetailPlavby() {
    var icon = $("#detailPlavbyImg");

    if (icon.attr('src').indexOf("plus") != -1) {
        $("#detailPlavby").show();
        icon.attr('src', icon.attr('src').replace("plus", "minus"));
    }
    else {
        $("#detailPlavby").hide();
        icon.attr('src', icon.attr('src').replace("minus", "plus"));
    }
}

function zobrazitCeny(typCeny) {
    var icon = $("#typCeny" + typCeny);

    if (icon.attr('src').indexOf("plus") != -1) {
        $("." + typCeny).show();
        icon.attr('src', icon.attr('src').replace("plus", "minus"));
    }
    else {
        $("." + typCeny).hide();
        icon.attr('src', icon.attr('src').replace("minus", "plus"));
    }

}

function calculateTotalPrice(reservationCountCss, reservationPriceCss) {
    var reservationCounts = $("select." + reservationCountCss);
    var reservationPrices = $("span." + reservationPriceCss);
    var totalPrice = 0;

    for (var i = 0; i < reservationCounts.length; i++) {
        var reservationCount = reservationCounts[i].options[reservationCounts[i].selectedIndex].value;
        if (reservationCount != "") {
            var price = parseInt($(reservationPrices[i]).text().replace(/\s|\u00a0/g, ''))
            if (!isNaN(price))
                totalPrice += parseInt(reservationCount) * price;
        }
    }
    $("#TotalPriceDiv").text(formatNumber(totalPrice, ',', ',', ' ') + " Kč");
}

function getPrices(elem, termID, currency, bank, rateType) {
    var element = $(elem);
    if(element.hasClass("selected"))
        return;
        
    element.siblings().removeClass("selected");
    element.addClass("selected");
    
    $.cookie('currency', currency, { path: '/', expires: 60 });
    $.ajax({
        type: "GET",
        url: "/shared/sharedFolder/getPrices.ashx",
        data: "termID=" + termID + "&currency=" + currency + "&bank=" + bank + "&rateType=" + rateType,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result, context) {
            var currencySign = currency == "" ? " Kč" : " €";
            for (i = 0; i < result.length; i++) {
                var price = formatNumber(result[i].price, ',', ',', ' ');
                var tr = $("#td" + result[i].id);
                var notAvaliable = $("#td" + result[i].id + " span span");
                var lm = $("#td" + result[i].id + " span");

                if (lm.text() != "" && lm.attr("title").indexOf("Last Minute sleva") != -1) {
                    lm.text(price + currencySign);
                    lm.attr("title", "Last Minute sleva, původní cena: " + formatNumber(result[i].priceCatalog, ',', ',', ' ') + currencySign + ".");
                }
                else if (notAvaliable.text() != "")
                    notAvaliable.text(price + currencySign);
                else
                    tr.text(price + currencySign);                   
            }
        }
    });
}

function setPersonCountCookie() {
    var dospelych = document.getElementById("ctl00_cph_plavba1_TourInfoControl_pocetdospelych").value;
    var deti = document.getElementById("ctl00_cph_plavba1_TourInfoControl_deti").value;
    var veky = "";
    for (i = 1; i <= 10; i++) {
        veky += document.getElementById("ctl00_cph_plavba1_TourInfoControl_vek" + i).value + ","; 
    }
    $.cookie('personCount', dospelych + "|" + deti + "|" + veky, { path: '/', expires: 60 });
}

function setPersonCountCombos() {
    if ($.cookie('personCount') != null) {
        var values = $.cookie('personCount').split('|');
        if (values[0] != "")
            document.getElementById("ctl00_cph_plavba1_TourInfoControl_pocetdospelych").selectedIndex = values[0] - 1;
        if (values[1] != "") {
            document.getElementById("ctl00_cph_plavba1_TourInfoControl_deti").selectedIndex = values[1];
            if (values[2] != "") {
                var veky = values[2].split(',');
                var counter = 0;
                for (i = 0; i <= 9; i++) {
                    if (veky[i] != "") {
                        if (counter++ >= parseInt(values[1])) break;
                        var vekCombo = document.getElementById("ctl00_cph_plavba1_TourInfoControl_vek" + (i + 1));
                        vekCombo.selectedIndex = parseInt(veky[i]) + 1;
                        vekCombo.style.display = "block";
                    }
                }
            }
        }
    }
}


function addMoreOptions(combo, startValue) {
    for (i = startValue; i < startValue + 10; i++)
        appendOptionLast(combo, i, i, "", "");
}

function formatNumber(nStr, inD, outD, sep) {
    nStr += '';
    var dpos = nStr.indexOf(inD);
    var nStrEnd = '';
    if (dpos != -1) {
        nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
        nStr = nStr.substring(0, dpos);
    }
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(nStr)) {
        nStr = nStr.replace(rgx, '$1' + sep + '$2');
    }
    return nStr + nStrEnd;
}


function appendOptionLast(select, value, text, selectedDestination, cssClass) {
    var optionNew = document.createElement('option');
    optionNew.text = text;
    optionNew.value = value;
    optionNew.setAttribute('class', cssClass);

    try {
        select.add(optionNew, null); // standards compliant; doesn't work in IE
    }
    catch (ex) {
        select.add(optionNew); // IE only
    }

    if (selectedDestination == value)
        optionNew.selected = true;
}

function queryString(searchKey) {
    querystrings = window.location.search.substring(1);
    var strings = querystrings.split("&");
    for (i = 0; i < strings.length; i++) {
        keyValue = strings[i].split("=");
        if (keyValue[0] == searchKey) {
            return keyValue[1];
        }
    }
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


