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);
    document.getElementsByTagName('body')[0].style.opacity="1";*/
    
    WS_waitscreen = false;
    // 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]);
    }
    if(event.keyCode==13 && document.getElementById('email').validity.valid){
        noError('email','euser');
        recover();
    }
}

function setLang(){
    
    addFooter()
    
    lang = sessionStorage.getItem('lang') ? sessionStorage.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);   
}