var page_sicode = {
    loaded: false,
    on_load: function(){
        if(!page_sicode.loaded){
            var obj;
            obj = document.getElementById("form_btnctl").addEventListener("click", page_sicode.frmbtnclick);
            obj = document.getElementById("form_txtctl").addEventListener("click", page_sicode.frmreturnclick);
            obj = document.getElementById("sice_email").addEventListener("click", page_sicode.frmjumpclick);
            obj = document.getElementById("codigo").addEventListener("blur", page_sicode.frmcleanname);
            obj = document.getElementById("codigo").addEventListener("keyup", page_sicode.frmbtnenter);
            page_sicode.loaded = true;
            global.on_load();
        }
    },
    frmbtnenter: function(e){
        if(e.keyCode == 13){
            page_sicode.frmbtnclick();
        }
    },frmbtnclick: function(){
        var validado = true;
        var customer = document.getElementById("codigo").value;
        var obj      = document.getElementById("codigo_err");
        obj.style    = "opacity:0";

        if(customer.length===0){
            page_sicode.worng_rsp("Indique un c¨®digo");
            validado = false;
        }else{
            page_sicode.callerAjax();
        }
    },
    callerAjax: function(){
        var codigo   = document.getElementById("codigo").value;
        var validata = document.getElementById("validata").innerHTML;
        /* Parametros para el llamado AJAX */
        var parameterAjax = {   metodo      : "GET",
                                url         : global.baseUrl + "/login/sicode_check.php",  
                                parameters  : "?codigo="+codigo+"&validata="+validata,
                                onsucces    : function(json){   
                                                                json = JSON.parse(json);
                                                                if(json.rsp){
                                                                    var userid   = json.userid ;
                                                                    global.utils.goPage("?id=si_addpass&userid="+userid+"&validata="+validata);
                                                                }else{
                                                                    page_sicode.worng_rsp();
                                                                }
                                                            },
                                onerror     : function(json){   
                                                                json = JSON.parse(json);
                                                                var msg = "";
                                                                if(json.rsp){
                                                                    msg = json.msg;
                                                                    msg = "CÃ³digo incorrecto";
                                                                    page_sicode.worng_rsp( msg);
                                                                }else{    
                                                                    page_sicode.worng_rsp();
                                                                }
                                                            } 
                            };
        global.utils.callAjax( parameterAjax);
    },
    
    valid_rsp: function(){
        var obj;
        obj         = document.getElementById("codigo_err");
        obj.style   = "opacity:0";
        obj         = document.getElementById("codigo");
        obj.style   = "border-color: #979797; border-width: 2px;";
    },  
    worng_rsp: function( msg){
        var obj;
        if (typeof msg == 'undefined'){
            msg = "C¨®digo incorrecto";
        }
        
        obj = document.getElementById("codigo_err");
        obj.innerHTML   = msg;
        obj.style       = "opacity:1";
        obj             = document.getElementById("codigo");
        obj.style       = "border-color: #f04449; border-width: 2px;";
    },
    parameterhref: function( searchvar){
        var enviado, igualdades, valores, a, salida;
        
        salida = "";
        enviado=location.href.split("?"); 
        igualdades=enviado[1].split("&"); 
        
        for(a=0;a<igualdades.length;a++){ 
            igualdades[a]=igualdades[a].split("+").join(" "); 
            valores=igualdades[a].split("="); 
            /*eval("var "+valores[0]+"='"+valores[1]+"'"); */
            if( valores[0] === searchvar){
                salida =  valores[1];
            }
        } 
        return salida;
    },
    frmreturnclick: function(){
        global.utils.goPage("?id=singup");
    },
     frmjumpclick: function(){
         var rif = document.getElementById("valrif").innerHTML;
         global.utils.goPage("?id=si_mailerror&rif="+rif);
     }
};
page_sicode.on_load();