﻿/*
    Control js de soporte al control ctrlNewsLetterNoRegistrados en el cliente
*/
Type.registerNamespace("GUI");
GUI.ctrlNewsLetterNoRegistrados = function(element) { GUI.ctrlNewsLetterNoRegistrados.initializeBase(this, [element]); }
GUI.ctrlNewsLetterNoRegistrados.prototype = {

    initialize: function () { GUI.ctrlNewsLetterNoRegistrados.callBaseMethod(this, 'initialize'); },
    dispose: function () { GUI.ctrlNewsLetterNoRegistrados.callBaseMethod(this, 'dispose'); },

    _ROOT: function () { if ($get('_ROOT_ctrlNewsLetterNoRegistrados') != null) { return $get('_ROOT_ctrlNewsLetterNoRegistrados').value; } else { return ''; } },

    Enviar: function () {

        var campoMail = $get(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_txtMail');
        var campoAvisoLegal = $get(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_CKBAvisoLegal');
        var campoAvisoLegalErr = $get(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_lbErrorCbAvisoLegal');
        var errorAL = false;
        GUI.FrmValidador.prototype.LimpiarErrores();

        if (campoMail.value.trim() == '')
            GUI.FrmValidador.prototype.RegistrarError('Introduce un e-mail', campoMail, GUI.ColorNotificacion.prototype.KO);
        else if (!GUI.FrmValidador.prototype.EsMailCorrecto(campoMail.value.trim()))
            GUI.FrmValidador.prototype.RegistrarError('Introduce un e-mail correcto', campoMail, GUI.ColorNotificacion.prototype.KO);

        campoAvisoLegalErr.style.display = 'none';
        if (!campoAvisoLegal.checked) { errorAL = true; campoAvisoLegalErr.style.display = 'block'; }

        if (!GUI.FrmValidador.prototype.TieneErrores() && !errorAL) {
            //Llamamos al webservice de suscripción
            CBL.wsAJAX.freeSubscriptions(campoMail.value.trim(), '1', '1', GUI.ctrlNewsLetterNoRegistrados.prototype.EnviadoCallback, GUI.ctrlNewsLetterNoRegistrados.prototype.EnviadoFailCallback);
        } else
            GUI.FrmValidador.prototype.VisualizaErrores();

    },

    AbrirAlertas: function () {
        try {
            _ActionPostLogin = GUI.AccionesPostLogin.prototype.AbrirAlerta;

            //Si no se ha cargado la clase de login significa que ya esta logeado el usuario, asi que verificamos si existe la alerta y abrimos el popup que toque (alerta nueva o repetida)
            if (_LOGEADO) {
                GUI.ctrlIntroAlerta.prototype.nuevaAlerta(false);
            }
            else {
                GUI.ctrlLogin.prototype.comprobarUsuario();
            }
        }
        catch (err) {
            window.open('http://www.pisos.com/introduciralerta.aspx')
        }
    },

    Mostrar: function (NameLayer) {
        var capaFormulario = $get(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_divFormulario');
        var divFormularioYaSuscrito = $get(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_divFormularioYaSuscrito');
        var divFormularioError = $get(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_divFormularioError');
        var divFormularioEnviado = $get(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_divFormularioEnviado');
        capaFormulario.style.display = 'none';
        divFormularioYaSuscrito.style.display = 'none';
        divFormularioError.style.display = 'none';
        divFormularioEnviado.style.display = 'none';
        $get(NameLayer).style.display = 'block';
    },

    Inicializar: function () {
        GUI.ctrlNewsLetterNoRegistrados.prototype.Mostrar(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_divFormulario');
        $get(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_txtMail').value = '';
        $get(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_txtMail').focus();
        $get(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_CKBAvisoLegal').checked = false;
    },

    EnviadoFailCallback: function (error) {
        GUI.ctrlNewsLetterNoRegistrados.prototype.Mostrar(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_divFormularioError');
    },

    EnviadoCallback: function (result, userContext, methodName) {
        if (result == '1') {
            GUI.ctrlNewsLetterNoRegistrados.prototype.Mostrar(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_divFormularioEnviado');
        }
        else if (result == '-41') {
            GUI.ctrlNewsLetterNoRegistrados.prototype.Mostrar(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_divFormularioYaSuscrito');
        }
        else {
            GUI.ctrlNewsLetterNoRegistrados.prototype.Mostrar(GUI.ctrlNewsLetterNoRegistrados.prototype._ROOT() + '_divFormularioError');
        }
    }

}

GUI.ctrlNewsLetterNoRegistrados.registerClass('GUI.ctrlNewsLetterNoRegistrados', Sys.Component);
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if (typeof (GUI) != 'undefined') { Sys.Application.add_init(function() { $create(GUI.ctrlNewsLetterNoRegistrados, { 'id': 'ctrlNewsLetterNoRegistrados' }); }); }
