window.onload = function(){
    
     //add event press enter focus input or submit
    var inputPass = document.getElementById('Password');
    var inputRepeatPass = document.getElementById('RepeatPass');
    
    if (window.event.keyCode === 13 && !window.event.shiftKey) {
        inputPass.focus();
    }
     
    inputPass.addEventListener("keypress", function(e){
        if(e.keyCode == 13){
              inputRepeatPass.focus();
        }
    });        
    
    inputRepeatPass.addEventListener("keypress", function(e){
        if(e.keyCode == 13){
            chagePass();       
        }
    });
    
    //add email user    
    var email = getParameterByName("usr");
    var userData = document.getElementById('userData');
    userData.innerHTML = email;
    userData.style.fontWeight = "bold";
    userData.style.marginTop = "0px";
   
    //add event click button submit
    var buttonChangePass = document.getElementById('changePass');
    buttonChangePass.addEventListener('click', function(){
       chagePass();
   });
   
    // Hide or show a Password
    var showIcon = document.getElementById('icon-eye')
    if (showIcon){
        showIcon.addEventListener("click", function(){
            show('icon-eye', 'Password')
        });     
    }
    
    var showIcon2 = document.getElementById('icon-eye2')
    if (showIcon2){
        showIcon2.addEventListener("click", function(){
            show('icon-eye2', 'RepeatPass')
        });     
    }
   
//Close onload   
};

// Show or hide password with click eye icon
function show(a, input) {
    var icon = document.getElementById(a);
    var passwordInput = document.getElementById(input);
    if (passwordInput.getAttribute('type') == "password") {
        icon.removeAttribute("class");
        icon.setAttribute("class","fas fa-eye");
        passwordInput.removeAttribute("type");
        passwordInput.setAttribute("type","text");
    } else {
        passwordInput.removeAttribute("type");
        passwordInput.setAttribute('type','password');
        icon.removeAttribute("class");
        icon.setAttribute("class","fas fa-eye-slash");
    }
}

// function send new password
function chagePass(){
    var hash = getParameterByName("hash");
    var email = getParameterByName("usr");
    var repeatPass = document.getElementById('RepeatPass');
    var newPassInp = document.getElementById('Password')
    var newPass = newPassInp.value;
    var repeatNewPass = repeatPass.value;
    var msg = document.getElementById('msg');
    var req = {};
    
    if (newPass != repeatNewPass){
        repeatPass.style.border = "1px solid red";
        msg.innerHTML = "Int\u00E9ntelo nuevamente. Contraseñas no coinciden";
        msg.style.display = "block";
        msg.style.marginTop = "0px";
        msg.style.color = "red";
        msg.style.fontSize = "80%";
        setTimeout(function() {
            repeatPass.style.border = "0.87px solid #D3D2D2";
            msg.style.display = "none";    
        }, 3000);
    } else if(newPass == "" || repeatNewPass == "" || newPass == null || repeatNewPass == null || newPass == undefined || repeatNewPass == undefined ) {
        repeatPass.style.border = "1px solid red";
        newPassInp.style.border = "1px solid red";
        msg.innerHTML = "Int\u00E9ntelo nuevamente. Contraseñas no pueden ser vac\u00EDas";
        msg.style.display = "block";
        msg.style.marginTop = "0px";
        msg.style.color = "red";
        msg.style.fontSize = "80%";
        setTimeout(function() {
            repeatPass.style.border = "0.87px solid #D3D2D2";
            newPassInp.style.border = "0.87px solid #D3D2D2";
            msg.style.display = "none";    
        }, 3000);
    } else if(! isOkPass(newPass).result) {
        repeatPass.style.border = "1px solid red";
        newPassInp.style.border = "1px solid red";
        msg.innerHTML = "Int\u00E9ntelo nuevamente. Las contraseñas deben tener una longitud m\u00EDnima de 8 caracteres con una letra may\u00FAscula una letra min\u00FAscula un n\u00FAmero un s\u00EDmbolo o car\u00E1cter especial";
        msg.style.display = "block";
        msg.style.margin = "0px 11% 0px 11%";
        msg.style.color = "red";
        msg.style.fontSize = "80%";
        setTimeout(function() {
            repeatPass.style.border = "0.87px solid #D3D2D2";
            newPassInp.style.border = "0.87px solid #D3D2D2";
            msg.style.display = "none";    
        }, 5000);
    } else {
        var cryptPass = MD5(newPass);
        req.hash = hash;
        req.pwd = cryptPass;
        
         callWS("GET", "login/recover", req, response);
    
        function response(status, rsp){
            //var res=JSON.parse(rsp);
            switch(status){
        
                case 200:
                    resp = JSON.parse(rsp);
                    msg.innerHTML = resp.msg;
                    msg.style.display = "block";
                    msg.style.marginTop = "0px";
                    msg.style.color = "green";
                    msg.style.fontSize = "80%";
                    setTimeout(function() {
                        msg.style.display = "none";    
                        gotoPage("login","main","");
                    }, 1000);
                    break;
                    
                    
                case 400:
                    resp = JSON.parse(rsp);
                    msg.innerHTML = resp.msg;
                    msg.style.color = "red";
                    msg.style.display = "block";
                    msg.style.marginTop = "0px";
                    msg.style.fontSize = "80%";
                    setTimeout(function() {
                        badCampos.style.display = 'none';
                     }, 3000);
                    break;
                case 401:
                    resp = JSON.parse(rsp);
                    msg.innerHTML = "Enlace inv\u00E1lido";
                    msg.style.color = "red";
                    msg.style.marginTop = "0px";
                    msg.style.fontSize = "80%";
                    msg.style.display = "block";
                    setTimeout(function() {
                        msg.style.display = 'none';
                     }, 3000);
                    break;
                case 500:
                    msg.innerHTML = "Error Interno";
                    msg.style.display = "block";
                    setTimeout(function() {
                        internoLogin.style.display = 'none';
                     }, 3000);
                    break;
                default:
            }
        }
        
    }
    
}