﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />

function calculateTotalPrice(reservationCountCss, reservationPriceCss) {
    var reservationCounts = $("select." + reservationCountCss);
    var reservationPrices = $("span." + reservationPriceCss);
    var totalPrice = 0;

    var affectLTPrices = $("td.AffectLT select." + reservationCountCss);
    var LTPrices = $("td.LT select." + reservationCountCss);
    var LTCount = 0;

    for (var i = 0; i < affectLTPrices.length; i++) {
        LTCount += parseInt(affectLTPrices[i].selectedIndex);
    }
    if (LTPrices.length > 0 && LTPrices[0].disabled) {
        for (var i = 0; i < LTPrices.length; i++) {
            LTPrices[i].selectedIndex = LTCount;
        }
    }

    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 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) {
    var optionNew = document.createElement('option');
    optionNew.text = text;
    optionNew.value = value;

    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 fillTransport(transportID) {
    var combo = document.getElementById("doprava");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "-- doprava: --", "");
        
        for (var i = 0; i < transports[999].length; i++) {
            appendOptionLast(combo, transports[999][i].id, transports[999][i].name, transportID);
        }
    }
}
function fillCategory(transportID, categoryID) {
    var combo = document.getElementById("kategorie");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "-- kategorie: --", "");
        if (transportID == "") transportID = 999;
        if (typeof categories[transportID] != "undefined") {
            for (var i = 0; i < categories[transportID].length; i++) {
                appendOptionLast(combo, categories[transportID][i].id, categories[transportID][i].name, categoryID);
            }
        }
    }
}
function fillCountry(categoryID, countryID) {
    var combo = document.getElementById("zeme");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "-- země: --", "");
        if (categoryID == "") categoryID = 0;
        if (typeof countries[categoryID] != "undefined") {
            for (var i = 0; i < countries[categoryID].length; i++) {
                appendOptionLast(combo, countries[categoryID][i].id, countries[categoryID][i].name, countryID);
            }
        }
    }
}
function fillCountry2(transportIDcategoryID, countryID) {
    var combo = document.getElementById("zeme");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "-- země: --", "");
        if (transportIDcategoryID == "" || transportIDcategoryID == "-") transportIDcategoryID = "999-0";
        if (typeof countries[transportIDcategoryID] != "undefined") {
            for (var i = 0; i < countries[transportIDcategoryID].length; i++) {
                appendOptionLast(combo, countries[transportIDcategoryID][i].id, countries[transportIDcategoryID][i].name, countryID);
            }
        }
    }
}




function refreshKategorie() {
    var combo = document.getElementById("doprava");
    if (combo != null) {
        fillCategory(combo.options[combo.selectedIndex].value);
    }
    refreshZeme();
}
function refreshZeme() {
    /*
    var combo = document.getElementById("kategorie");
    if (combo != null) {
        fillCountry(combo.options[combo.selectedIndex].value);
    }
    */
    var combo_doprava = document.getElementById("doprava");
    var combo_kategorie = document.getElementById("kategorie");
    if ((combo_doprava != null) && (combo_kategorie != null)) {
        var doprava = "999";
        var kategorie = "0";
        if (combo_doprava.options[combo_doprava.selectedIndex].value != null && combo_doprava.options[combo_doprava.selectedIndex].value != "")
            doprava = combo_doprava.options[combo_doprava.selectedIndex].value;
        if (combo_kategorie.options[combo_kategorie.selectedIndex].value != null && combo_kategorie.options[combo_kategorie.selectedIndex].value != "")
            kategorie = combo_kategorie.options[combo_kategorie.selectedIndex].value;
        
        fillCountry2(doprava + '-' + kategorie);
    }
}
