﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />

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 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);
    }
}

function refreshDestinace() {
    var combo = document.getElementById("zeme");
    if (combo != null) {
        fillDestination(combo.options[combo.selectedIndex].value);
    }
}

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 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 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, ',', ',', ' ') + " " + document.getElementById("currencySign").value);
}


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];
        }
    }
}


function checkMinLetters() {
    if ($("#fulltext").val().length < 3) {
        $("#fullTextError").text("vložte minimálně 3 znaky");
        return false;
    }
    else {
        $("#fullTextError").text("");
        return true;
    }
}

/*google maps*/
var map = null;
function showAddress(name, addr) {
    if (!GBrowserIsCompatible()) return;

    if (!map) {
        map = new GMap2(document.getElementById(name));
        map.addControl(new GSmallZoomControl());
    }
    geocoder = new GClientGeocoder();
    geocoder.getLocations(addr, callbackLocations);
}


function showPoint(name, lat, lng, acc) {
    if (!GBrowserIsCompatible()) return;

    if (!map) {
        map = new GMap2(document.getElementById(name));
        map.addControl(new GSmallZoomControl());
    }

    point = new GLatLng(lat / 3600, lng / 3600);
    map.setCenter(point, acc);
    marker = new GMarker(point);
    map.addOverlay(marker);
}



function callbackLocations(response) {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
    acc = place.AddressDetails.Accuracy;
    if (acc > 6) {
        map.setCenter(point, 15);
        marker = new GMarker(point);
        map.addOverlay(marker);
    } else if (acc > 4) {
        map.setCenter(point, 12);
    } else {
        map.setCenter(point, 11);
    }
}
// A function to create the marker and set up the event window
function addMarker(lat, lng, name, html) {
    // use a custom icon with letter A - Z
    var letter = String.fromCharCode("A".charCodeAt(0) + (gmarkers.length));
    var myIcon = new GIcon(G_DEFAULT_ICON, "http://www.google.com/mapfiles/marker" + letter + ".png");
    myIcon.printImage = "http://maps.google.com/mapfiles/marker" + letter + "ie.gif"
    myIcon.mozPrintImage = "http://maps.google.com/mapfiles/marker" + letter + "ff.gif"

    var pt = new GLatLng(lat / 3600, lng / 3600);
    if (gbounds) gbounds.extend(pt);

    var marker = new GMarker(pt, { icon: myIcon });
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });
    // save the info we need to use later for the side_bar
    if (gmarkers) gmarkers.push(marker)
    return marker;
}

function showMarker(i) {
    if (!gmarkers) return true;

    GEvent.trigger(gmarkers[i], "click");
    return false;
}
