﻿function searchSchoolDelayed() {
    var e = $("#Search");
    if (e.val().length > 0) {
        $("#searchresults").html("Søger...");

        $.getJSON(searchUrl.replace('_query_', e.val()), function(data) {

            if (data.schools.length == 0) {
                $("#searchresults").html("Ingen skoler<br /><span style='font-size: 0.8em'>(tjek venligst din indtastning eller søg på færre ord/bogstaver)</span>");
            } else {
                html = '';
                for (var i = 0; i < data.schools.length && i < 16; i++) {
                    html += '<a href="' + schoolUrl.replace('_name_', data.schools[i].EasyUrl) + '">' + data.schools[i].Name + '</a><br />';
                }

                if (data.length > 14) {
                    html += 'Kun de første 15 skoler bliver vist. Søg mere specifikt.<br />';
                }

                html += '<br /><a href="' + soegUrl.replace('_query_', e.val()) + '">Se resultaterne på et kort</a>';

                $("#searchresults").html(html);
            }

            if (typeof gotSchoolsToMap == 'function') { gotSchoolsToMap(data); }
        });
    } else {
        $("#searchresults").html("");
    }
}

var t;
function searchSchool(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    // Only search if there is something to search for.
    if ($("#Search").val().length > 2)
    {
        if (charCode == 13) {
            searchSchoolDelayed();
        } else {
            // Disable search when some keys are pressed.
            if ((charCode != 9) && (charCode > 20 || charCode < 16) && (charCode != 27) && (charCode > 38 || charCode < 33) && (charCode > 93 || charCode < 91) && (charCode > 185 || charCode < 112)) {
                // Delay search until the user stops typing.
                $("#searchresults").html("Søgning startes...");
                clearTimeout(t);
                t = setTimeout("searchSchoolDelayed()", 800);
            }
        }
    } else {
        $("#searchresults").html("<em>Skriv minimum tre tegn.</em>");
    }
}

function disableEnter(evt) { return evt.keyCode != 13; }

function getMunicipalities(e) {
    if (e.options[e.selectedIndex].value != -1) {
        // $('#Municipalities').val('Henter...');
        $.getJSON(municipalitiesUrl.replace('_id_', e.options[e.selectedIndex].value), function(data) {

            var e = document.getElementById("Municipalities");
            e.options.length = 0;
            e.options[0] = new Option("- Vælg kommune -", -1);

            for (var i = 0; i < data.municipalities.length; i++) {
                e.options[i + 1] = new Option(data.municipalities[i].Name, data.municipalities[i].Id);
            }

            if (typeof gotMunicipalitiesToMap == 'function') { gotMunicipalitiesToMap(data); }
        });
    }
}

function getSchools(e) {
    if (e.options[e.selectedIndex].value != -1) {
        $.getJSON(schoolsUrl.replace('_id_', e.options[e.selectedIndex].value), function(data) {

            var e = document.getElementById("Schools");
            e.options.length = 0;
            e.options[0] = new Option("- Vælg skole -", -1);

            for (var i = 0; i < data.schools.length; i++) {
                e.options[i + 1] = new Option(data.schools[i].Name, data.schools[i].EasyUrl);
            }

            if (typeof gotSchoolsToMap == 'function') { gotSchoolsToMap(data); }
        });
    }
}

function selectSchool(e) {
    if (e.options[e.selectedIndex].value != -1) {
        location.href = schoolUrl.replace('_name_', e.options[e.selectedIndex].value);
    }
}