var page_singup = {
    loaded: false,
    on_load: function(){
        if(!page_singup.loaded){
            var obj;
            obj = document.getElementById("form_btnctl").addEventListener("click", page_singup.frmbtnclick);
            obj = document.getElementById("form_txtctl").addEventListener("click", page_singup.frmreturnclick);
            obj = document.getElementById("customer").addEventListener("blur", page_singup.frmcleanname);
            obj = document.getElementById("customer").addEventListener("keyup", page_singup.frmbtnenter);
            obj = document.getElementById("idfiscal").addEventListener("blur", page_singup.frmcleanpass);
            document.getElementById("idfiscal").addEventListener("keyup", function(){
                document.getElementById("idfiscal").value = document.getElementById("idfiscal").value.toUpperCase();
            });
            obj = document.getElementById("idfiscal").addEventListener("keyup", page_singup.frmbtnenter);
            page_singup.loaded = true;
            global.on_load();
        }
    },
    frmbtnenter: function(e){
        if(e.keyCode == 13){
            page_singup.frmbtnclick();
        }
    },frmbtnclick: function(){
        var validado = true;
        var customer = document.getElementById("customer").value;
        var rif = document.getElementById("idfiscal").value;
        
        var objname     = document.getElementById("customer_err");
        objname.style   = "opacity:0";
        var objpass     = document.getElementById("idfiscal_err");
        objpass.style   = "opacity:0";


        if(customer.length===0){
            page_singup.worng_usr();
            validado = false;
        }

        /* Reviosa que el primer caracter corresponsa a RIF  */
        if(validado){
            validado = global.utils.validateRif(rif);
            if(!validado){
                page_singup.worng_rif();
            }
        }
        if (validado){
            page_singup.valid_usr();
            page_singup.valid_rif();
            page_singup.callerAjax();
        }
      },
    callerAjax: function(){
        var customer = document.getElementById("customer").value;
        var rif = document.getElementById("idfiscal").value;
        /* Parametros para el llamado AJAX */
        var parameterAjax = {   metodo      : "GET",
                                url         : global.baseUrl + "/login/singup_check.php",
                                parameters  : "?customer="+customer+"&rif="+rif,
                                onsucces    : function(json){   //console.log(json);
                                                                json = JSON.parse(json);
                                                                if(json.rsp){
                                                                    var email       = json.email;
                                                                    var validata    = json.validata;
                                                                    global.utils.goPage("?id=si_code&email="+email+"&validata="+validata+"&rif="+rif);
                                                                }else{
                                                                    page_singup.worng_usr();
                                                                    page_singup.worng_rif();
                                                                }
                                                            },
                                onerror     : function(json){   //console.log(json);
                                                                json = JSON.parse(json);
                                                                var msg = "";
                                                                if(json.rsp){
                                                                    msg = json.msg;
                                                                    page_singup.worng_usr( msg);
                                                                }else{    
                                                                    page_singup.worng_usr();
                                                                    page_singup.worng_rif();
                                                                }
                                                            } 
                            };
        global.utils.callAjax( parameterAjax);
    },
    
    valid_usr: function(){
        var obj;
        obj         = document.getElementById("customer_err");
        obj.style   = "opacity:0";
        obj         = document.getElementById("customer");
        obj.style   = "border-color: #979797; border-width: 2px;";
    },  
    worng_usr: function( msg){
        var obj;
        if (typeof msg == 'undefined'){
            msg = "Revisa Usuario";
        }
        obj = document.getElementById("customer_err");
        obj.innerHTML   = msg;
        obj.style       = "opacity:1";
        obj             = document.getElementById("customer");
        obj.style       = "border-color: #f04449; border-width: 2px;";
    },
    valid_rif: function(){
        var obj;
        obj             = document.getElementById("idfiscal_err");
        obj.style       = "opacity:0";
        obj             = document.getElementById("idfiscal");
        obj.style       = "border-color: #979797; border-width: 2px;";        
    },
    worng_rif: function(){
        var obj;
        obj = document.getElementById("idfiscal_err");
        obj.innerHTML   = "Revisa RIF (Ej. J123456789)";
        obj.style       = "opacity:1"; 
        obj             = document.getElementById("idfiscal");
        obj.style       = "border-color: #f04449; border-width: 2px;"; 
    },
    frmcleanname: function(){
        var objname         = document.getElementById("customer_err");
        objname.style       = "opacity:0";
        var objiname        = document.getElementById("customer");
        objiname.style      = "border-color: #979797; border-width: 2px;";
    },
    frmcleanpass: function(){
        var objpass         = document.getElementById("idfiscal_err");
        objpass.style       = "opacity:0";
        var objipass        = document.getElementById("idfiscal");
        objipass.style      = "border-color: #979797; border-width: 2px;";
    },
    frmfgtclick: function(){
     /*   global.utils.goPage("?id=forgot");*/
    },
        frmreturnclick: function(){
        global.utils.goPage("?id=login");
    }
};

page_singup.on_load();