/*
 * ONE CMS: jslib/one_user_verification.js
 * Ajax Library to handle logins
 *
 * Joris Dormans (2006)
 *
 * requires:
 * 3dparty/md5.js,
 * jslib/xmlhttprequest.js
 *
 */


function User(objectName, useCookie) {
  this.objectName = objectName;
  this.xhReqLogin = new XHReqCaller(objectName+".xhReqLogin");
  this.xhReqPopup = new XHReqCaller(objectName+".xhReqPopup");
  this.username = "";
  this.idUser = 0;
  this.permissions = 0;
  this.isAdmin = false;
  this.loggedIn = false;
  this.HTMLPatent = "";
  this.data;
  this.onLogin = "";
  this.onLogout = "";
  this.userCookie = new Cookie(document, "bbauserkey", 24*7*6);
  this.getSeed();
  if (!this.verifyUser()) {
    if (useCookie) this.verifyUserCookie();
  }
}

User.prototype.getSeed = function () {
  var xml = new JKL.ParseXML("services/one_getseed.php");
  var seed = xml.parse();
  this.seed = seed.seed;
}


User.prototype.showLogin = function(HTMLParent, onLogin, onLogout, rememberMe) {
  this.HTMLParent = HTMLParent;
  this.onLogin = onLogin;
  this.onLogout = onLogout;

  var res="";
  if (!this.loggedIn) {
    res += "<div class='one_cms_login'><form>";
    res += "  <ul>";
    res += "    <li>"+gLanguage.loginUsername;
    res += "      <input size='10' id='"+this.objectName+"username' /></li>";
    res += "    <li>"+gLanguage.loginPassword;
    res += "      <input type='password' size='10' id='"+this.objectName+"password' /></li>";
    res += "    <li><input type='submit' value='"+gLanguage.loginLogin+"' onclick="+this.objectName+".login() /></li>";
    res += "  </ul>";
    if (rememberMe) res += "    <p><input type='checkbox' id='autoLogin' /> "+gLanguage.loginAuto+"</p>";
    res += "</form></div>";
  } else {
    res += "<div class='one_cms_login'>";
    res += "  <ul>";
    res += "    <li>"+gLanguage.loginLoggedInAs+" <em class='one_cms_login_username'>"+this.username+"</em></li> ";
    res += "    <li><input type='button' value='"+gLanguage.loginLogout+"' onclick="+this.objectName+".logout() /></li>";
    res += "  </ul>";
    res += "</div>";

  }
  if (HTMLParent) {
    $(HTMLParent).innerHTML=res;
    if ($(this.objectName+"username")) $(this.objectName+"username").focus();
  }
}

User.prototype.verifyUserCookie = function() {
  if (this.userCookie.load()) {
    this.response=this.xhReqLogin.sGet("services/one_login.php?c="+hex_md5(this.userCookie.key+this.seed));
    return  (this.verifyUser());
  } else return (false);
}


User.prototype.verifyUser = function() {
  var xml = new JKL.ParseXML("services/one_user_info.php");
  this.data = xml.parse();
  if(this.data.user.username!=undefined) {
    this.username=this.data.user.username;
    this.idUser=this.data.user.id_user;
    this.loggedIn = true;
    this.permissions=this.data.user.permissions;
    this.isAdmin = (this.permissions == -1) ? true : false;
    if ($('autoLogin')) {
      if ($('autoLogin').checked) {
        var key = this.xhReqPopup.sGet("services/one_user_cookie.php");
        this.userCookie.key=key;
        this.userCookie.store();
      }
    }
    return true;
  } else {
    this.username="";
    this.idUser=0;
    this.loggedIn = false;
    this.permissions=this.data.user.permissions; //default permissions are returned
    this.isAdmin = false;
    return false;
  }
}

User.prototype.logoutResponse = function() {
  this.verifyUser();
  if (this.response.substr(1,4)=="goto") document.location=this.response.substr(5);
  else if (this.onLogout!="") eval(this.onLogout);
  else if (this.HTMLParent!="") this.showLogin(this.HTMLParent, this.onLogin, this.onLogout);
}

User.prototype.logout = function() {
  this.userCookie.remove();
  this.response=this.xhReqLogin.sGet("services/one_logout.php");
  this.logoutResponse();
}

User.prototype.loginResponse = function() {
  if (this.verifyUser()) {
    if (this.response.substr(1,4)=="goto") document.location=this.response.substr(5);
    else if (this.onLogin!="") eval(this.onLogin);
  } else {
    alert(gLanguage.loginFailed);
    this.passname="";
    $(this.objectName+"password").value="";
    $(this.objectName+"password").focus();
    return;
  }
  //refresh the loginform;
  if (this.HTMLParent!="") this.showLogin(this.HTMLParent, this.onLogin, this.onLogout);
}

User.prototype.login = function() {
  this.username=$(this.objectName+"username").value;
  this.password=$(this.objectName+"password").value;

  this.response=this.xhReqLogin.sGet("services/one_login.php?u="+escape(this.username)+"&p="+hex_md5(hex_md5(this.password)+this.seed));
  this.loginResponse();
}

