﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />

function initDatePicker() {
    $('.datePicker').datePicker({ clickInput: true, createButton: true, displayClose: false });
    //propojit dva vybery datumu od a do tak, aby umoznovali logicky vyber mezi dvema hodnotami.
    //hodnota definovana v OpenTravelControls.HtmlControls.JQueryHtmlControls
    if (typeof combineStartAndEndDates != 'undefined' && combineStartAndEndDates) {
        $('#terminOd').bind(
		        'dpClosed',
		        function(e, selectedDates) {
		            var d = selectedDates[0];
		            if (d) {
		                d = new Date(d);
		                $('#terminDo').dpSetStartDate(d.addDays(1).asString());
		            }
		            else {
		                $('#terminDo').dpSetStartDate((new Date()).zeroTime().asString());
		            }
		        }
		    );

        $('#terminDo').bind(
		        'dpClosed',
		        function(e, selectedDates) {
		            var d = selectedDates[0];
		            if (d) {
		                d = new Date(d);
		                $('#terminOd').dpSetEndDate(d.addDays(-1).asString());
		            }
		            else {
		                var futureDate = new Date();
		                futureDate.setFullYear(2050, 0, 1);
		                $('#terminOd').dpSetEndDate(futureDate.asString());
		            }
		        }
	        );

    }
}



function fillCategory(categoryIDSelected) {
    var combo = document.getElementById("kategorie");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "téma: (libovolné)", "", "");

        for (var i = 0; i < categories[0].length; i++) {
            appendOptionLast(combo, categories[0][i].id, categories[0][i].name, categoryIDSelected, "indent" + categories[0][i].indent);
        }
    }
}

function fillCountry(categoryID, countryID) {
    var combo = document.getElementById("zeme");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "země: (libovolná)", "", "");
        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 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 refreshZeme() {
    var combo = document.getElementById("kategorie");
    if (combo != null) {
        fillCountry(combo.options[combo.selectedIndex].value);
    }
    refreshDestinace();
}

function refreshDestinace() {
    var combo = document.getElementById("zeme");
    if (combo != null) {
        fillDestination(combo.options[combo.selectedIndex].value);
    }
}

function lastMinuteRedirect(selectBox) {
    var selectedValue = selectBox.options[selectBox.selectedIndex].value;
    if (selectedValue == "") {
        location.href = "http://localhost:1002/last-minute.html";
    }
    else {
        location.href = "http://localhost:1002/last-minute/" + selectedValue + ".html";
    }
}

function getLocationsForCountry(countryID, locationsCombo) {
    $.ajax({
        type: "POST",
        url: "http://localhost:1002/services/locations.asmx/GetLocations",
        data: "{'countryID': '" + countryID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result, context) {
            var data = eval('(' + result.d + ')');
            $("#" + locationsCombo).html("");
            var combo = document.getElementById(locationsCombo);
            if (data.length == 0) {
                combo.disabled = true;
                appendOptionLast(combo, "", "destinace: (libovolná)", "", "");
            }
            else {
                combo.disabled = false;
                var selectedDestination = 0;
                if (typeof destinationSelected != "undefined") {
                    selectedDestination = destinationSelected;
                }
                appendOptionLast(combo, "", "destinace: (libovolná)", selectedDestination, "");
                for (var i = 0; i < data.length; i++) {
                    appendOptionLast(combo, data[i].ID, data[i].Name, selectedDestination, "");
                }
            }
        }
    });
}

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;

    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, 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];
        }
    }
}










// OK ---------------------------------------------------------------------------------------------
function refreshDestinace() {
    var combo = document.getElementById("kategorie");
    if (combo != null) {
        fillLocation(combo.options[combo.selectedIndex].value);
    }
}
function fillLocation(categoryID, locationID) {
    var combo = document.getElementById("destinace");
    if (combo != null) {
        $(combo).html("");
        appendOptionLast(combo, "", "-- destinace --", "");
        if (categoryID == "") categoryID = 0;
        if (typeof locations[categoryID] != "undefined") {
            for (var i = 0; i < locations[categoryID].length; i++) {
                appendOptionLast(combo, locations[categoryID][i].id, locations[categoryID][i].name, locationID);
            }
        }
    }
}
// OK ---------------------------------------------------------------------------------------------


