var page_si_addpass = {
    loaded: false,
    baseUrl: "https://retenciones.mercadolibre.com.ve/mlretenciones/customer/api",
    on_load: function(){
        if(!page_si_addpass.loaded){
            page_si_addpass.frmshow_clave1();
            page_si_addpass.frmshow_clave2();
            var obj;
            obj = document.getElementById("clave1").addEventListener("click", page_si_addpass.frmshow_clave1);
            obj = document.getElementById("clave1").addEventListener("keyup", page_si_addpass.frmbtnenter);
            obj = document.getElementById("form_show1").addEventListener("click", page_si_addpass.frmshow_input1);
            obj = document.getElementById("clave2").addEventListener("click", page_si_addpass.frmshow_clave2);
            obj = document.getElementById("clave2").addEventListener("keyup", page_si_addpass.frmbtnenter);
            obj = document.getElementById("form_show2").addEventListener("click", page_si_addpass.frmshow_input2);
            
            obj = document.getElementById("form_btnctl").addEventListener("click", page_si_addpass.frmbtnclick);
            page_si_addpass.loaded = true;
            global.on_load();
        }
    },
    frmbtnenter: function(e){
        if(e.keyCode == 13){
            page_si_addpass.frmbtnclick();
        }
    },
    frmbtnclick: function(){
        var vpass1, vpass2, obj, validado;
        vpass1 = document.getElementById("clave1");
        vpass2 = document.getElementById("clave2");
        vhash  = document.getElementById("hash");
        validado = true;
        
        obj         = document.getElementById("clave1_err");
        obj.style   = "opacity:0";
        obj         = document.getElementById("clave2_err");
        obj.style = "opacity:0";
        obj         = document.getElementById("hash_err");
        obj.style = "opacity:0";


        if(vpass1.value.length===0 || vpass2.value.length===0 || vhash.value.length===0){ 
            if(vpass1.value.length===0){
                page_si_addpass.worng_rsp1();
                validado = false;        
            }
            if(vpass2.value.length===0){
                page_si_addpass.worng_rsp2();
                validado = false;    
            }
            if(vhash.value.length===0){
                page_si_addpass.worng_rsp3();
                validado = false;    
            }
        }


        if( validado ){
            if(vpass1.value != vpass2.value){

                obj                 = document.getElementById("clave1_err");
                obj.innerHTML       = "Clave no coincide";
                obj.style           = "opacity:1";
                obj                 = document.getElementById("clave1");
                obj.style           = "border-color: #f04449; border-width: 2px;";
                
                obj                 = document.getElementById("clave2_err");
                obj.innerHTML       = "Clave no coincide";
                obj.style           = "opacity:1";
                obj                 = document.getElementById("clave2");
                obj.style           = "border-color: #f04449; border-width: 2px;";
                validado            = false;
            }
        }
        
        if( validado ){
            page_si_addpass.callerAjax();
        }
  
    },
    callerAjax: function(){
        var userid  = document.getElementById("validata").getAttribute('value');
        var sessid  = document.getElementById("validata").innerHTML;
            sessid  = sessid.trim(); // Se eliminan los espacios en blancos
        var clave   = document.getElementById("clave1").value;
            clave   = global.utils.MD5( clave);
        var hash    = document.getElementById("hash").value;

//console.log(userid);
//console.log(sessid);
//console.log(clave);
//console.log(hash);

     
        /* Parametros para el llamado AJAX */
        var parameterAjax = {   metodo      : "GET",
                                url         : global.baseUrl + "/login/si_addpass.php",  
                                parameters  : "?sessid="+sessid+"&userid="+userid+"&clave="+clave+"&hash="+hash,
                                onsucces    : function(json){   //console.log(json);
                                                                json = JSON.parse(json);
                                                                if(json.rsp){
                                                                    global.utils.goPage("?id=login");
                                                                }else{
                                                                    page_si_addpass.worng_rsp();
                                                                }
                                                            },
                                onerror     : function(json){   //console.log(json);
                                                                json = JSON.parse(json);
                                                                var msg = json.msg;
                                                                if(json.rsp){
                                                                    msg = json.msg;
                                                                    page_si_addpass.worng_rsp1();
                                                                    page_si_addpass.worng_rsp2();
                                                                }else{    
                                                                    page_si_addpass.worng_rsp1(msg);
                                                                    page_si_addpass.worng_rsp2();
                                                                }
                                                            } 
                            };
        global.utils.callAjax( parameterAjax);
    },
    worng_rsp1: function(){
        var obj;
        obj                 = document.getElementById("clave1_err");
        obj.innerHTML       = "Clave no coincide";
        obj.style           = "opacity:1";
        obj                 = document.getElementById("clave1");
        obj.style           = "border-color: #f04449; border-width: 2px;";
    },
    worng_rsp2: function(){
        var obj;
        obj                 = document.getElementById("clave2_err");
        obj.innerHTML       = "Clave no coincide";
        obj.style           = "opacity:1";
        obj                 = document.getElementById("clave2");
        obj.style           = "border-color: #f04449; border-width: 2px;";
    },
    worng_rsp3: function(){
        var obj;
        obj                 = document.getElementById("hash_err");
        obj.innerHTML       = "Dato requerido";
        obj.style           = "opacity:1";
        obj                 = document.getElementById("hash");
        obj.style           = "border-color: #f04449; border-width: 2px;";
    },
    frmshow_clave1: function(){
        var obj;
        obj       = document.getElementById("form_show1");
        obj.style = "opacity:1";
    },
    frmshow_input1: function(){
        var oShw = document.getElementById("form_show1");
        var obj   = document.getElementById("clave1");
        if(obj.type === "password"){
            obj.type = "text";
            oShw.innerHTML = "Ocultar";  
        }else{
            obj.type = "password";
            oShw.innerHTML = "Mostrar";  
        }
    },
    frmshow_clave2: function(){
        var obj;
        obj       = document.getElementById("form_show2");
        obj.style = "opacity:1";
    },
    frmshow_input2: function(){
        var oShw = document.getElementById("form_show2");
        var obj   = document.getElementById("clave2");
        if(obj.type === "password"){
            obj.type = "text";
            oShw.innerHTML = "Ocultar";  
        }else{
            obj.type = "password";
            oShw.innerHTML = "Mostrar";  
        }
    },
    utils: {
    }
}

page_si_addpass.on_load();