/* global $  */

var page_login = {
    loaded: false,
    xbaseUrl: "../api/login",
    on_load: function(){
        if(!page_login.loaded){
            var obj;
            obj = document.getElementById("form_btnctl").addEventListener("click", this.frmbtnclick);
            obj = document.getElementById("form_forget").addEventListener("click", this.frmfgtclick);
            /*obj = document.getElementById("form_txtctl").addEventListener("click", this.frmcreateclick);*/
            obj = document.getElementById("loginname").addEventListener("blur", this.frmcleanname);
            obj = document.getElementById("loginpass").addEventListener("blur", this.frmcleanpass);
            obj = document.getElementById("loginname").addEventListener("keyup", this.frmbtnenter);
            obj = document.getElementById("loginpass").addEventListener("keyup", this.frmbtnenter);
            
            page_login.loaded = true;
            //global.on_load();
        }
    },
    frmbtnenter: function(e){
        if(e.keyCode == 13){
            page_login.frmbtnclick();
        }
    },
    frmbtnclick: function(){
        var valloginname = document.getElementById("loginname").value;
        var valloginpass = document.getElementById("loginpass").value;
        
        var objname       = document.getElementById("name_err");
        objname.style = "opacity:0";
        var objpass      = document.getElementById("pass_err");
        objpass.style = "opacity:0";

        if(valloginname.length===0 || valloginpass.length===0){ 
            if(valloginname.length===0){
                objname.innerHTML = "Revisa Usuario";
                objname.style     = "opacity:1";
                var objiname      = document.getElementById("loginname");
                objiname.style    = "border-color: #f04449; border-width: 2px;";
            }
            if(valloginpass.length===0){
                objpass.innerHTML = "Revisa Clave";
                objpass.style     = "opacity:1"; 
                var objipass      = document.getElementById("loginpass");
                objipass.style    = "border-color: #f04449; border-width: 2px;";
            }
        }else{
            /* Parametros para el llamado AJAX */
            var parameterAjax = {   metodo      : "POST",
                                    url         : global.baseUrl + "/login/login_check.php",
                                    parameters  : "?user="+valloginname+"&pwd="+global.utils.MD5(valloginpass),
                                    onsucces    : function(json){   //console.log(json);
                                                                    json = JSON.parse(json);
                                                                    if(json.userid > 0){
                                                                        //global.utils.goPage("?id=home&sessid="+json.sessid+"&userid="+json.userid+"&username="+json.username);
                                                                        global.utils.goPage("?id=cruddocs&sessid="+json.sessid+"&userid="+json.userid+"&username="+json.username);
                                                                        
                                                                    }
                                                  },
                                    onerror     : function(json){   //console.log(json);
                                                                    json = JSON.parse(json);
                                                                    if (json.rsp){
                                                                        var msg = json.msg;
                                                                        if (typeof msg == 'undefined'){
                                        								    msg = "Revisa Usuario";
                                        							    }else{
                                                                            msg = json.msg;
                            								            }
                                                                        objname.innerHTML = "Revisa Usuario";
                                                                        objname.style     = "opacity:1";
                                                                        var objiname      = document.getElementById("loginname");
                                                                        objiname.style    = "border-color: #f04449; border-width: 2px;";
                                                                        objpass.innerHTML = msg;
                                                                        objpass.style     = "opacity:1"; 
                                                                        var objipass      = document.getElementById("loginpass");
                                                                        objipass.style    = "border-color: #f04449; border-width: 2px;";
                                                                    }
                                                                } 
                                };
            global.utils.callAjax( parameterAjax);
        }
  
    },
    frmfocusname: function(){
        var objname         = document.getElementById("name_err");
        objname.style       = "opacity:0";
        var objiname        = document.getElementById("loginname");
        objiname.style      = "border-color: #3483fa; border-width: 2px;";
    },
    frmcleanname: function(){
        var objname         = document.getElementById("name_err");
        objname.style       = "opacity:0";
        var objiname        = document.getElementById("loginname");
        objiname.style      = "border-color: #979797; border-width: 2px;";
    },
    frmcleanpass: function(){
        var objpass         = document.getElementById("pass_err");
        objpass.style       = "opacity:0";
        var objipass        = document.getElementById("loginpass");
        objipass.style      = "border-color: #979797; border-width: 2px;";
    },
    frmfgtclick: function(){
        global.utils.goPage("?id=forgot");
    },
        frmcreateclick: function(){
        global.utils.goPage("?id=singup");
    },
    utils: {
    }
}

page_login.on_load();