window.onload = function(){
    
    //add event press enter focus input or submit
    var inputEmail = document.getElementById('email');
    if (window.event.keyCode === 13 && !window.event.shiftKey) {
         inputEmail.focus();
    }
     
    inputEmail.addEventListener("keypress", function(e){
        if(e.keyCode == 13){
            forgotSendEmail();    
        }
    });        
    
    //add event click button cancel
    var main = document.getElementById('goToMain');
    if(main){
        main.addEventListener("click", function(){
            gotoPage("login","main",""); 
        });        
    }
    
    //add event click button submit 
    var sendEmail = document.getElementById('goToForgotmsg');
    sendEmail.addEventListener("click", function(){
        forgotSendEmail();
    });
    
//Close onLoad
};

function forgotSendEmail(){
    var emailItem = document.getElementById("email");
    var ErrorMsg = document.getElementById("ErrorMsg");
    var email = emailItem.value;
    var req = {};
    if(email === '' || email === null || email === undefined || email === " " || email === ' ' || email === ""){
        ErrorMsg.innerHTML = 'Introduzca un email';
        ErrorMsg.style.color= 'red';
        ErrorMsg.style.fontSize= "80%";
        ErrorMsg.style.display = "block";
        emailItem.style.border = "1px solid #FF0000";
        setTimeout(function() {
            ErrorMsg.style.display = 'none';
            emailItem.style.border = "0.87px solid #D3D2D2";
         }, 3000);
         return 0;
    }
    req.email = email;
    
    
        callWS("GET", "login/forgot", req, response);
        console.log("llamando funcion", req);
    function response(status, rsp){
        var resp = JSON.parse(rsp);
        switch(status){
            case 200:
                // send Email localStorage.setItem("email", email);
                 gotoPage("login","forgotmsg","");
                break;
           
            case 401:
                ErrorMsg.innerHTML = resp.msg;
                ErrorMsg.style.color= 'red';
                ErrorMsg.style.fontSize= "80%";
                ErrorMsg.style.display = "block";
                emailItem.style.border = "1px solid #FF0000";
                setTimeout(function() {
                    ErrorMsg.style.display = 'none';
                    emailItem.style.border = "0.87px solid #D3D2D2";
                 }, 3000);
                break;
                
             case 500:
                ErrorMsg.innerHTML = "Error de conexión";
                ErrorMsg.style.display = "block";
                setTimeout(function() {
                    ErrorMsg.style.display = 'none';
                 }, 3000);
                break;
                
            default:
            }
        }
}