﻿/// <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());
		                if (typeof tourDays != 'undefined') {
                            $('#terminDo').val(d.addDays(tourDays-2).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 calculateTotalPrice(reservationCountCss, reservationPriceCss) {
    var reservationCounts = $("input." + 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++) {
        if (affectLTPrices[i].selectedIndex > 0 && affectLTPrices[i].selectedIndex < 11)
            LTCount += parseInt(affectLTPrices[i].selectedIndex);
        else {
            var inputValue = document.getElementById(affectLTPrices[i].id.replace("service", "serviceTxt")).value;
            if (inputValue != "")
                LTCount += parseInt(inputValue);
        }
    }
    if (LTPrices.length > 0 && LTPrices[0].disabled) {
        for (var i = 0; i < LTPrices.length; i++) {
            if (LTCount < 10) {
                LTPrices[i].selectedIndex = LTCount;
                document.getElementById(LTPrices[i].id.replace("service", "serviceTxt")).value = LTCount;
            }
            else {
                var inputValue = document.getElementById(LTPrices[i].id.replace("service", "serviceTxt"));
                LTPrices[i].style.display = "none";
                inputValue.style.display = "block";
                inputValue.disabled = true;
                LTPrices[i].selectedIndex = 10;
                inputValue.value = LTCount;
            }
        }
    }

    for (var i = 0; i < reservationCounts.length; i++) {
        var reservationCount = reservationCounts[i].value;
        if (reservationCount != "") {
            if (reservationCount > 10) {
                document.getElementById(reservationCounts[i].id.replace("Txt", "")).style.display = "none";
                reservationCounts[i].style.display = "block";
            }
            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 comboVisibility(comboValue, serviceID) {
    if (comboValue.value == 'vice') {
        comboValue.style.display = 'none';
        var inputField = document.getElementById('serviceTxt' + serviceID)
        inputField.style.display = 'block';
        inputField.value = "";
        inputField.focus();
    }
}

function numbersonly(e) {
    var unicode = e.charCode ? e.charCode : e.keyCode
    if (unicode != 8 && unicode != 9 && unicode != 12 && unicode != 27 && unicode != 37 && unicode != 39 && unicode != 46) {
        if (unicode < 48 || unicode > 57)
            return false
    }
}


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;
}


