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);
            obj = document.getElementById("form_txtctl").addEventListener("click", page_si_addpass.frmcreateclick);
            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");
        validado = true;
        
        obj         = document.getElementById("clave1_err");
        obj.style   = "opacity:0";
        obj         = document.getElementById("clave2_err");
        obj.style = "opacity:0";


        if(vpass1.value.length===0 || vpass2.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( 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);
       
        /* Parametros para el llamado AJAX */
        var parameterAjax = {   metodo      : "GET",
                                url         : global.baseUrl + "/login/si_addpass.php",  
                                parameters  : "?sessid="+sessid+"&userid="+userid+"&clave="+clave,
                                onsucces    : function(json){   
                                                                json = JSON.parse(json);
                                                                if(json.rsp){
                                                                    global.utils.goPage("?id=login");
                                                                }else{
                                                                    page_si_addpass.worng_rsp();
                                                                }
                                                            },
                                onerror     : function(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;";
    },
    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";  
        }
    },
    frmcreateclick: function(){
        global.utils.goPage("?id=login");
    },
    utils: {
    }
}

page_si_addpass.on_load();