﻿$(document).ready(function() {
    IsSessionesValida();
    $('#txtrutcliente').autotab({ target: 'txtrutcliente', format: 'numeric' });
    $('#txtdigito_verificador').autotab({ target: 'txtdigito_verificador', format: 'alphanumeric', previous: 'txtrutcliente' });
    $('#txtclientevoucher').autotab({ format: 'text' });
    $('#txtnumdocumento').autotab({ target: 'txtnumdocumento', format: 'numeric' });
    $('#txtmontodocumento').autotab({ target: 'txtmontodocumento', format: 'numeric' });
    $("input:text, input:checkbox, input:radio, input:password, select").enter2tab();

});
function IsSessionesValida() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/IsSessionValida",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function(msg) {
            if (msg.d == '0') {
                $("#divmenuvoucher").hide();
                $("#divcrearvoucher").hide();
                $("#col-one").show();
                $("#col-two").hide();
            } else {
                var obj = $.evalJSON(msg.d);
                for (var k = 0; k < obj.length; k++) {
                    $("#txtvendedor").val(obj[k].nombreVendedor);
                    $("#slssucursal option:selected").text(obj[k].idSucursal);
                    $("#txtIdUsuario").val(obj[k].idUsers);
                    GetAllVoucher(obj[k].idUsers);
                    $("#txtestado").val('menu');
                    $("#col-two").show();
                    $("#col-one").hide();
                }
                if ($("#txtestado").val() == 'menu') {
                    $("#divmenuvoucher").show();
                    $("#divcrearvoucher").hide();
                    $("#col-one").hide();
                    $("#divconsultaVoucher").hide();
                    $("#col-two").show();
                } else if ($("#txtestado").val() == 'creando') {
                    $("#col-two").show();
                    $("#divcrearvoucher").show();
                    $("#divmenuvoucher").hide();
                    $("#col-one").hide();
                    $("#divconsultaVoucher").hide();
                } else if ($("#txtestado").val() == 'consulta') {
                    $("#divmenuvoucher").hide();
                    $("#divcrearvoucher").hide();
                    $("#col-one").hide();
                    $("#divconsultaVoucher").show();
                    $("#col-two").show();
                }


            }
        },
        error: function(msg) { OnErr(msg); }
    });
}




function Exit() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/ExitSistema",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function(msg) {
            window.location.href = "Default.aspx";
        },
        error: function(msg) { OnErr(msg); }
    });
}
jQuery.fn.reset = function() {
    $(this).each(function() { this.reset(); });
}
jQuery.fn.enter2tab = function() {
    this.keyup(function(e) {
        // get key pressed (charCode from Mozilla, Firefox and Opera / keyCode in IE)
        var key = e.charCode ? e.charCode : (e.keyCode ? e.keyCode : 0);
        if ((key == 13) && /^(input|select)$/i.test(this.nodeName)) {
            var next = this.tabIndex + 1;
        } else if ((key == 38) && /^(input|select)$/i.test(this.nodeName)) {
            var next = this.tabIndex - 1;
        }
        if (next == 0) {
            return false;
        }
        var keytop = 0;
        if (key == 38 && this.nodeName == "SELECT") {
            keytop = 0;
            var indx = this.selectedIndex;
            if (indx - 1 < 0) {
                keytop = 1;
            }
        } else if (key == 38) {
            keytop = 1;
        }
        if ((key == 13 || (keytop == 1)) && /^(input|select)$/i.test(this.nodeName)) {
            var list;
            list = $("select[@tabIndex='" + next + "'],input[@tabIndex='" + next + "']");
            list2 = $("select[@tabIndex='" + next + "'],textarea[@tabIndex='" + next + "']");
            if (list.size() > 0) {
                list.get(0).focus();
                //return false;
            } else if (list2.size() > 0) {
                list2.get(0).focus();
            }
        }
    });
    return this;
}

