﻿$(document).ready(function() {
    // ASOCIAMOS FUNCION DE LOGIN AL EVENTO CLICK DEL BOTON
    $("#inputsubmit1").bind("click", function(e) {
        e.preventDefault();
        if (jQuery.trim($("#inputtext1").val()) == '' || jQuery.trim($("#inputtext2").val()) == '') {
            jAlert('Debe rellenar usuario y contraseña','Voucher');
        }
        else {
            ConfigFormLogin();
        }
    });

});

function ConfigFormLogin() {
    // ENVIA SOLICITUD
$.ajax({
    type: "POST",
    data: "{ _CodUsu: '" + $("#inputtext1").val() + "', _Passwd: '" + $("#inputtext2").val() + "'}",
    url: "Default.aspx/DoLogin",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    cache: false,
    beforeSend: function() { OnStart() },
    complete: function() { OnEnd() },
    success: function(msg) { OnOK(msg) },
    error: function(msg) { OnErr(msg); }
});
    $("[id$=txtuserlogin]").val($("#inputtext1").val());
}

function OnStart() {
    // DESHABILITA BOTON
    $("#inputsubmit1").attr("disabled", "disabled");
    // MUESTRA IMAGEN DE CONECTANDO
    $("#Sending").removeClass("oculto");
    $("#inputtext1").text("");

}

// AL FINALIZAR SOLICITUD
function OnEnd() {
    setTimeout(function() {
        $("#Sending").addClass("oculto");
        $("#logindialog img").css("display", "inline");
        $("#inputsubmit1").removeAttr("disabled");
        IsSessionesValida();

    }, 3000);

}

// ERROR EN EL METODO DE LOGIN
function OnErr(msg) {
    // alert(msg);
}

// OK METODO DE LOGIN
function OnOK(msg) {
    if (msg.d == "false") {
        jAlert('Nombre de Usuario o contraseña invalido', 'Voucher');
    } else {

    }

}


