WS_waitscreen = true;
var lang;

function start(){
    
    // Window Load
    document.getElementsByClassName('pageCnt')[0].style.visibility="visible";
    /*
    var waitScreen = document.getElementById("waitScreen");
    document.body.removeChild(waitScreen);
    */
    WS_waitscreen = false;
    // Go to Login Page
    document.getElementById('send').addEventListener("click", function(){
        noError('password','epassword')
        document.getElementById('epass').style.bottom = '10px';
        noError('confirmPassword','econfirm')
        document.getElementById('econf').style.bottom = '10px';
        recover()
    }); 
    
    // Show or hide icon eye password
    document.getElementById('iconEye').addEventListener("click", function(){
        show('iconEye','password')
    }); 
    
    // Show or hide icon eye password confirm
    document.getElementById('iconEyeConfirm').addEventListener("click", function(){
        show('iconEyeConfirm','confirmPassword')
    }); 
    
    // Remove red errors 
    document.getElementsByTagName('body')[0].addEventListener("click", function(){
        if (document.getElementById('password').value !== '' && isOkPass(document.getElementById('password').value).result){
            noError('password','epassword')
            document.getElementById('epass').style.bottom = '10px';
        }
        if (document.getElementById('confirmPassword').value !== '' 
            && document.getElementById('econfirm').innerHTML !== getLangById('loginrecoverInvalid2')[lang] && isOkPass(document.getElementById('confirmPassword').value).result){
            noError('confirmPassword','econfirm')
            document.getElementById('econf').style.bottom = '10px';
        }
    }); 
    
    // Focus
    document.getElementById('password').addEventListener('keydown', (event) => focusE(event, 'confirmPassword'));
    document.getElementById('confirmPassword').addEventListener('keydown', (event) => focusE(event, 'send'));
    
    setLang()
}

// Show or Hide Password
function show(iconId,passwordId) {
    var icon = document.getElementById(iconId);
    var passwordInput = document.getElementById(passwordId);
    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 recover(){
    var pwd = document.getElementById('password');
    var cnf = document.getElementById('confirmPassword');
    if ( pwd.value === "" ){
        error('password','epassword',getLangById('loginrecover1')[lang]);
            document.getElementById('epass').style.bottom = '34px';   
    }
    if ( cnf.value === "" ){
        error('confirmPassword','econfirm',getLangById('loginrecover1')[lang]);
        document.getElementById('econf').style.bottom = '34px';   
    }
    if ( pwd.value == cnf.value && pwd.value !== ''){
        
        if(isOkPass(pwd.value).result){
         
             var req = {
                "hash": getParameterByName('hash'),
                "pwd": MD5(pwd.value)
            }
            //document.getElementById('waitScreen').style.display ="table";
            callWS("GET", "login/recover", req, response);
            function response(status, rsp){
                switch(status){
                    case 200:
                        document.getElementById('econfirm').innerHTML = getLangById('loginrecover200')[lang];
                        document.getElementById('econf').style.bottom = '34px';   
                        setTimeout(function(){ 
                            gotoPage('login','main',''); 
                        }, 5000);
                        break;
                    case 401:
                        error('confirmPassword','econfirm',getLangById('loginrecover401')[lang]);
                        document.getElementById('econf').style.bottom = '34px';   
                        break;
                    case 409:
                        break;                    
                    case 500:
                        error('confirmPassword','econfirm',getLangById('loginrecover500')[lang]);
                        document.getElementById('econf').style.bottom = '34px';   
                    break;
                    default:
                        document.getElementById('econfirm').innerHTML = getLangById('loginforgotDefault')[lang];
                    break;
                }
                //document.getElementById('waitScreen').style.display ="none";
            }    
            
        }else{
            error('password','epassword','');
            error('confirmPassword','econfirm',getLangById('loginrecoverInvalid1')[lang]);
            document.getElementById('econf').style.bottom = '65px';
            
            if (screen.width<= 388){
                document.getElementById('econf').style.bottom = '82px'
            }
        }
        
        
    }else if (cnf.value !== '' && pwd.value !== cnf.value) {
        error('confirmPassword','econfirm',getLangById('loginrecoverInvalid2')[lang]);
            document.getElementById('econf').style.bottom = '34px';   
    }
}

function isOkPass(p){
    var anUpperCase = /[A-Z]/;
    var aLowerCase = /[a-z]/; 
    var aNumber = /[0-9]/;
    var aSpecial = /[!|@|#|$|%|^|&|*|(|)|-|_|.|=|+|\/]/;
    var obj = {};
    obj.result = true;

    if(p.length < 8){
        obj.result=false;
        obj.error="Not long enough!"
        return obj;
    }

    var numUpper = 0;
    var numLower = 0;
    var numNums = 0;
    var numSpecials = 0;
    for(var i=0; i<p.length; i++){
        if(anUpperCase.test(p[i]))
            numUpper++;
        else if(aLowerCase.test(p[i]))
            numLower++;
        else if(aNumber.test(p[i]))
            numNums++;
        else if(aSpecial.test(p[i]))
            numSpecials++;
    }

    if(numUpper < 1 || numLower < 1 || numNums < 1 || numSpecials <1){
        obj.result=false;
        obj.error="Wrong Format!";
        return obj;
    }
    return obj;
}

function setLang(){
    
    addFooter()
    
    lang = getParameterByName('lang') ? getParameterByName('lang') : 'esp';
    localStorage.setItem('lang', lang );
    
    /* inner HTML */
    setWordLanginnerHTML('changepassword','changepassword');
    setWordLanginnerHTML('initialtext','initialtext');
    setWordLanginnerHTML('newpassword','newpassword');
    setWordLanginnerHTML('newpasswordconfirm','newpasswordconfirm');
    setWordLanginnerHTML('send','change');
    setWordLanginnerHTML('ts','ts');
    
    /* placeholder */
    setWordLangPlaceHolder('password','password');
    setWordLangPlaceHolder('confirmPassword','confirmpassword');
    
    //Mostrar HTML cargado
    esconderHTML(false);   
}
