WS_waitscreen = true;

window.onload = function(){
    
    // Window Load
    document.getElementsByClassName('pageCnt')[0].style.visibility="visible";
    var waitScreen = document.getElementById("waitScreen");
    document.body.removeChild(waitScreen);
    WS_waitscreen = false;
    
    // Go to Register Page
    document.getElementById('register').addEventListener("click", function(){
        gotoPage('register','register-term','');
    }); 
    
    // Hide or show a Password
    document.getElementById('icon-eye').addEventListener("click", function(){
        show('icon-eye');
    }); 
    
    // Login
    document.getElementById('login').addEventListener("click", function(){
        login();
    }); 
    
    // Recover
    document.getElementById('forgot').addEventListener("click", function(){
        gotoPage('recover','forgot','');
    });
    
    
    
    document.getElementById('lan').addEventListener("change", function(){
        localStorage.setItem('lang', document.getElementById('lan').value);
        changeLang()
    })
    
    // Remove red errors 
    document.getElementsByTagName('body')[0].addEventListener("click", function(){
        if (document.getElementById('user').value !== ''){
            noError('user','euser');
        }
        if (document.getElementById('password').value !== ''){
            noError('password','epassword');
        }
        if (document.getElementById('lan').value !== ''){
            noError('lan','elan');
        }
    }); 
    
    document.getElementById('user').addEventListener('keydown', (event) => focusE(event, 'password'));
    
    document.getElementById('password').addEventListener('keydown', (event) => focusE(event, 'login'));
    
    document.getElementById('lan').addEventListener("click", function(){
         if(document.getElementById('lan').value!=''){
             document.getElementById('lan').style.color="#262624 !important;"
         }
    }); 
    
    if(localStorage.getItem('lang')){
        document.getElementById('lan').value = localStorage.getItem('lang')
    }else{
        localStorage.setItem('lang', document.getElementById('lan').value);
    }
    
    setLang();
}

// Show or hide password with eye icon
function show(a) {
    var icon = document.getElementById(a);
    var passwordInput = document.getElementById('password');
    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");
    }
}

// Service login/login
function login(){
    var user = document.getElementById('user');
    var pwd = document.getElementById('password');
    var lan = document.getElementById('lan');
    if ( user.value === "" || pwd.value === "" || lan.value==="" ){
        if ( user.value === "" ) {
            error('user','euser',getLangById('loginlogin0')[lang]);
        }
        if ( pwd.value === "" ){
            error('password','epassword',getLangById('loginlogin1')[lang]);
        }   
        if ( lan.value === "" ){
            error('lan','elan',getLangById('loginlogin2')[lang]);
        } 
        return;
    } else {
        var req = {
            "usr": user.value,
            "pwd": MD5(pwd.value),
            "lang" : lan
        }
        callWS("GET", "login/login", req, response);
        
        function response(status, rsp){
            switch(status){
                case 200:
                    gotoPage('register','regC',{ 'sessionid': JSON.parse(rsp).sessionid })
                    break;
                case 400:
                    break;
                case 401:
                    error('password','epassword',getLangById('loginlogin401')[lang]);
                    break;
                case 402:
                    document.getElementById('epassword').innerHTML = getLangById('loginlogin402')[lang]
                    break;
                case 403:
                    document.getElementById('epassword').innerHTML = getLangById('loginlogin403')[lang]
                    break;
                case 404:
                    document.getElementById('epassword').innerHTML = getLangById('loginlogin404')[lang]
                    break;
                case 405:
                    document.getElementById('epassword').innerHTML = getLangById('loginlogin405')[lang]
                    break;
                case 500:
                    document.getElementById('epassword').innerHTML = getLangById('loginlogin500')[lang]
                    break;
                default:
                    document.getElementById('epassword').innerHTML = getLangById('loginloginDefault')[lang]
                    break;
            }
        }   
    }
}

function setLang(){
    
    lang = localStorage.getItem('lang');
    
    /* inner HTML */
    setWordLanginnerHTML('access','access');
    setWordLanginnerHTML('lang','lang');
    setWordLanginnerHTML('esp','spanish');
    setWordLanginnerHTML('eng','english');
    setWordLanginnerHTML('userLabel','user');
    setWordLanginnerHTML('passLabel','password');
    setWordLanginnerHTML('forgot','forgot');
    setWordLanginnerHTML('login','login');
    setWordLanginnerHTML('register','sigin');
    setWordLanginnerHTML('ts','ts');
    
    /* placeholder */
    setWordLangPlaceHolder('user','userph');
    setWordLangPlaceHolder('password','passwordph');
    
    //Mostrar HTML cargado
    esconderHTML(false);
}

function changeLang(){
    
    lang = localStorage.getItem('lang');
    
    /* inner HTML */
    setWordLanginnerHTML('access','access');
    setWordLanginnerHTML('lang','lang');
    setWordLanginnerHTML('esp','spanish');
    setWordLanginnerHTML('eng','english');
    setWordLanginnerHTML('userLabel','user');
    setWordLanginnerHTML('passLabel','password');
    setWordLanginnerHTML('forgot','forgot');
    setWordLanginnerHTML('login','login');
    setWordLanginnerHTML('register','sigin');
    setWordLanginnerHTML('ts','ts');
    
    /* placeholder */
    setWordLangPlaceHolder('user','userph');
    setWordLangPlaceHolder('password','passwordph');
}