﻿jQuery(function() {
    var p = jQuery("#formLoginPrensa").validate({
        submitHandler: function(form) {
            send.LoginPrensa();
        },
        errorLabelContainer: null
    });
    // show a simple loading indicator
    var loader = jQuery('<div id="loader">Enviando...</div>')
			    .css({ position: "fixed", top: "1em", textAlign: "center" })
			    .appendTo("body")
			    .hide();
    jQuery().ajaxStart(function() {
        var backItem = $("#BackAlertLay");
        backItem.show();
        loader.show();
    }).ajaxStop(function() {
        loader.hide();
        //p.resetForm();
    }).ajaxError(function(a, b, e) {
        throw e;
    });
});

var send = {
    LoginPrensa: function() {
        var f = $("#formLoginPrensa");
        var serializedForm = f.serialize();
        var UrlPage = "/Programas/";
        var itemController = 0;

        if (location.href.split("/Programas/").length > 1) {
            if (idNotaPrensa != "") {
                UrlPage = "/Programas/PrensaContenidos/" + idPrograma + "/" + idEdicion + "/" + idNotaPrensa;
            } else {
                UrlPage = "/Programas/Prensa/" + idPrograma + "/" + idEdicion;
            }
        }
        else {
            //UrlPage = "/Prensa/PrensaContenidos";
            if (idNotaPrensa != "") {
                UrlPage = "/Prensa/PrensaContenidos/" + idNotaPrensa;
            } else {
                UrlPage = "/Prensa";
            }
        }

        if ($("input[name$='remember']").get(0).checked) {
            $.cookie("UserEmail", $("input[name$='txtMail']").val());
        } else { $.cookie("UserEmail", null); }

        $.post("/Prensa/LoginUser", serializedForm, function(data) {
            if (data == 'true') { open(UrlPage, "_self"); }
            else { openWin.init(['alertKO', 'Login', 'El usuario no existe, la clave es incorrecta o no has activado tu cuenta', ""]); }
        });
        return false;

    }
}


$(document).ready(function() {

});