Ext.ns('Ext.ux.clv2')

/**
 * This Function is used when the user press the enter key in the Login Panel(index.jspx)
 * Due to the interaction required by the UxD Design handle this file carefully.
 **/

function handleKeyPressLogin(evnt, appContext, username, password, rememberMe){
    var key=evnt.keyCode || evnt.which;
    //Char code of enter key
    if (key==13){
        //Call to the login method
        loginAttempt(appContext, appContext+'/j_spring_security_check', username, password, rememberMe);
    }
}

function setItemFocus(evnt,id){
     var key=evnt.keyCode || evnt.which;
    //Char code of enter key
    if (key==13){
        var item = document.getElementById(id);
        item.focus();
    }
}
function loginAttempt(appContext, path, username, password, rememberMe){
    var conn = new Ext.data.Connection();
     conn.request({
     url: path,
     params: {"j_username": username, "j_password": password, "_spring_security_remember_me": rememberMe},
     method: 'POST',
     success: function(response, options) {
        switch(response.responseText){
            case "false":
                toggleDisplay('loginErrorHeader', true);
                toggleDisplay('loginHeader', false);
                document.loginForm.j_username.value="";
                document.loginForm.j_password.value="";
            break;
            case "enforce":
                showForgotLogin(username);
            break;
            case "activation":
                alert("The user should activate your account");
            break;
            case "true":
                if (document.loginForm._spring_security_remember_me.checked) {
                    toMem();
                }
                document.loginForm.submit();         
            break;
        }        
    },
    failure: function(response, options) {
        showErrorMessage('system');
    }
});
}

function forgotLogin(show){
   toggleDisplay('forgotLoginPanel', show);
   toggleDisplay('loginPanel', !show);
}

function resetLoginPanel(){
   toggleDisplay('forgotLoginPanel', false);
   toggleDisplay('successfulReset', false);
   toggleDisplay('loginPanel', true);
}

function validateEmailAddress(username, path){
    var conn = new Ext.data.Connection();
     conn.request({
     url: path,
     params: {"username": username},
     method: 'POST',
     success: function(response, options) {
        if(response.responseText == "false"){
            toggleDisplay('forgotLoginError', true);
            toggleDisplay('resetParagraphs', false);
        }else{
           toggleDisplay('forgotLoginPanel', false);
           toggleDisplay('forgotLoginError', false);
           toggleDisplay('resetParagraphs', false);
           toggleDisplay('successfulReset', true);
           document.getElementById('resetUserPassword').innerHTML = username;
        }
    },
    failure: function(response, options) {
        showErrorMessage('connection');
    }
});
}

/******************************** RememberMe Funtionality **************************************/
function newCookie(name,value,days) {
 var days = "365";  // Number of days for the cookie to last
                 // can be modified it according to your needs
 var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();
    }
   document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameSG = name + "=";
  var nuller = '';
  if (document.cookie.indexOf(nameSG) == -1)
    return nuller;

  var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameSG) == 0) return c.substring(nameSG.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
    newCookie(name,"",1);
}

function toMem() {
    // add a new cookie
    newCookie('clUser', document.loginForm.j_username.value);
}

function delMem() {
    eraseCookie('clUser');   // make sure to add the eraseCookie function for every field
    document.loginForm.j_username.value = '';   // add a line for every field
}

function remCookie() {
    document.loginForm.j_username.value = readCookie("clUser");
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  remCookie();
});

/******************************** End RememberMe Funtionality **************************************/