WS_waitscreen = true;
var lang;

window.onload = function(){
    
    // Window Load
    document.getElementsByClassName('pageCnt')[0].style.visibility="visible";
    var waitScreen = document.getElementById("waitScreen");
    document.body.removeChild(waitScreen);
    WS_waitscreen = false;
    document.getElementsByTagName('body')[0].style.opacity="1";
    
    // Go to Login Page
    document.getElementById('cancel').addEventListener("click", function(){
        gotoPage('login','main','');
    }); 
    
    // Recover
    document.getElementById('send').addEventListener("click", function(){
        noError('email','euser');
        recover();
    });
    
    // Remove red errors 
    document.getElementsByTagName('body')[0].addEventListener("click", function(){
        if (document.getElementById('email').value !== '' && document.getElementById('email').validity.valid ){
            noError('email','euser');
        }
        if(!document.getElementById('email').validity.valid){
            error('email','euser',getLangById('loginforgot1')[lang]);
        }
    }); 
    
    // Focus 
    document.getElementById('email').addEventListener('keydown', (event) => emailE(event));
    
    setLang();
}

function recover(){
    var email = document.getElementById('email');
    if ( email.value === "" ){
        error('email','euser',getLangById('loginforgot0')[lang]);
        return;
    } 
    else if (!email.validity.valid){
        error('email','euser',getLangById('loginforgot1')[lang]);
    }else {
        var req = {
            "email": email.value,
            "lang" : lang
        }
        document.getElementById('waitScreen').style.display ="table";
        callWS("GET", "login/forgot", req, response);
        
        function response(status, rsp){
            switch(status){
                case 200:
                    gotoPage('recover','alert','');
                    break;
                case 401:
                    document.getElementById('euser').innerHTML = getLangById('loginforgot401')[lang];
                    break;
                case 500:
                    document.getElementById('epassword').innerHTML = getLangById('loginforgot500')[lang];
                    break;
                default:
                    document.getElementById('epassword').innerHTML = getLangById('loginforgotDefault')[lang];
                    break;
            }
            document.getElementById('waitScreen').style.display ="none";
        }   
    }
}

// Error email
function emailE(event) {
    if (document.getElementById('email').validity.valid) {
        noError('email','euser')
    } else{
        error('email','euser',getLangById('loginforgot1')[lang]);
    }
}

function setLang(){
    
    lang = localStorage.getItem('lang') ? localStorage.getItem('lang') : 'esp';
    
    /* inner HTML */
    setWordLanginnerHTML('changepassword','changepassword');
    setWordLanginnerHTML('initialtext','initialtext');
    setWordLanginnerHTML('emailLabel','email');
    setWordLanginnerHTML('send','send');
    setWordLanginnerHTML('cancel','cancel');
    setWordLanginnerHTML('ts','ts');
    
    /* placeholder */
    setWordLangPlaceHolder('email','email');
    
    //Mostrar HTML cargado
    esconderHTML(false);   
}