﻿$("#estimaParada").focus();

$("#estimaParada").keypress(
    function(e) {
        if (e.keyCode == 13) {
            validaCodigo();
            return false;
        }
    }
);

openHelp = function(lan) {
    var width = 600;
    var height = 310;
    var left = (screen.width - width) / 2;
    var top = (screen.height - height) / 2;
    window.open('estima/help_' + lan + '.html', null, 'resizable=0,left=' + left + ',top=' + top + ',width=' + width + ',height=' + height);
}

obtenMunicipios = function() {

    $("#estimaResult").css("display", "none"); 
    $("#Pan_ConsultaPorCodigo").css("display", "none");
    $("#Pan_BuscarParada").css("display", "block");

    var listBox = document.getElementById("LB_Municipios");

    listBox.addItem = function(value, text) {
        var op = document.createElement("option");
        op.value = value;
        op.text = text;
        this.options.add(op);
    }
    listBox.options.length = 0;
    listBox.addItem(0, strings.selec);

    listBox.onchange = function() {
        $("#estimaResult").css("display", "none");
        var munId = parseInt(this.value);
        if (munId != 0) {
            obtenParadas(munId);
        }
    }

    $.ajax({
        type: "POST",
        url: "/WS_Lbw/Service.asmx/Municipios",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {

            if (msg.d) msg = msg.d; // framework 3.5, IIS7

            for (var i = 0; i < msg.length; i++) {
                var municipio = msg[i];
                listBox.addItem(municipio.id, municipio.des);
            }

        },
        error: function(result) {
            //alert(result.status + ' ' + result.statusText);
        }
    });

}

obtenParadas = function(munId) {

    var listBox = document.getElementById("LB_Paradas");

    listBox.addItem = function(value, text) {
        var op = document.createElement("option");
        op.value = value;
        op.text = text;
        this.options.add(op);
    }
    listBox.options.length = 0;
    listBox.addItem(0, strings.selec);

    listBox.onchange = function() {
        var parCod = parseInt(this.value);
        if (parCod != 0) {
            obtenEstimacion(parCod, 0, $("#estimaResult"));
        }
    }

    $.ajax({
        type: "POST",
        url: "/WS_Lbw/Service.asmx/ParadasMunicipio",
        data: "{munId:" + munId + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {

            if (msg.d) msg = msg.d; // framework 3.5, IIS7

            for (var i = 0; i < msg.length; i++) {
                var parada = msg[i];
                var des = parada.cod + " - " + parada.des;
                listBox.addItem(parada.cod, des);
            }

        },
        error: function(result) {
            //alert(result.status + ' ' + result.statusText);
        }
    });

}

validaCodigo = function() {
    var parText = $("#estimaParada").val();
    if (parText == '') {
        $("#estimaResult").css("display", "none");
    }
    else {
        var parCod = parseInt(parText);
        if (isNaN(parCod)) {
            $("#estimaResult").css("display", "block");
            $("#estimaResult").html("<b>" + parText + "</b>" + strings.parNoValid);
        }
        else {
            obtenEstimacion(parCod, 0, $("#estimaResult"));
        }
    }
}
