var page_forgot = {
    loaded: false,
    on_load: function(){
        if(!page_forgot.loaded){
            var obj;
            obj = document.getElementById("form_btnctl").addEventListener("click", page_forgot.frmbtnclick);
            obj = document.getElementById("form_txtctl").addEventListener("click", page_forgot.frmreturnclick);
            obj = document.getElementById("e-mail").addEventListener("blur", page_forgot.frmcleanemail);
            obj = document.getElementById("e-mail").addEventListener("keyup", page_forgot.frmbtnenter);
    
            page_forgot.loaded = true;
            global.on_load();
        }
    },
    frmbtnenter: function(e){
        if(e.keyCode == 13){
            page_forgot.frmbtnclick();
        }
    },
    frmbtnclick: function(){
        var valinput1 = document.getElementById("e-mail").value;
        var objname   = document.getElementById("inpt1_err");
        objname.style = "opacity:0";
        
        var validate = global.utils.validateEmail(valinput1);

        if(validate){ 
            page_forgot.callerAjax( valinput1)
        }else{
            page_forgot.worng_email();
        }
    },
    worng_email: function(){
        var obj;
        obj             = document.getElementById("inpt1_err");
        obj.innerHTML   = "E-mail inválido. Revisa y vuelve a intentarlo";
        obj.style       = "opacity:1";
        obj             = document.getElementById("e-mail");
        obj.style       = "border-color: #f04449; border-width: 2px;";
    },
    frmcleanemail: function(){
        var objname         = document.getElementById("e-mail");
        objname.style       = "opacity:0";
        var objiname        = document.getElementById("e-mail");
        objiname.style      = "border-color: #979797; border-width: 2px;";
    },
    frmreturnclick: function(){
        global.utils.goPage("?id=login");
    },
    callerAjax: function(email){
        /* Parametros para el llamado AJAX */
        var parameterAjax = {   metodo      : "GET",
                                url         : global.baseUrl + "/login/forgot.php",
                                parameters  : "?email="+email,
                                onsucces    : function(json){   
                                                                json = JSON.parse(json);
                                                                if(json.rsp){
                                                                    global.utils.goPage("?id=forgotpasswordsuccess");
                                                                }
                                                            },
                                onerror     : function(json){   
                                                                json = JSON.parse(json);
                                                                //Evitar dar indicios de existencia de error
                                                                global.utils.goPage("?id=forgotpasswordsuccess");
                                                                //page_forgot.worng_email();
                                                            } 
                            };
        global.utils.callAjax( parameterAjax);
    }
};

page_forgot.on_load();

