SecurityModule = function()
{
  var STATE_DEFAULT = 0, STATE_FORGOT_PW = 1;
  this.state = STATE_DEFAULT;
  
  this.login = function()
  {
    switch(this.state)
    {
      case STATE_DEFAULT:
        var u = document.forms["login_form"].elements["email"].value;
        var p = document.forms["login_form"].elements["password"].value;
        var url = "./include/login.php?email=" + u + "&password=" + p;
        break;
      case STATE_FORGOT_PW:
        var e = document.forms["login_form"].elements["email"].value;
        var url = "./include/login.php?action=get_new_pw&email=" + e;
        break;
    }
    new ajax_json(url, this._login);
  }
  this._login = function(data)
  {
    if (data.json.msg && data.json.msg != "") { $("table.logon_table td#error_msg div").html(data.json.msg).fadeIn(300); return false; }
    document.forms["login_form"].action = _doc_root + "/";
    document.forms["login_form"].onsubmit = null;
    document.forms["login_form"].submit();
    //document.location.href = _doc_root;
  }
  this.log_out = function()
  {
    var url = "./include/login.php?action=logout";
    new ajax_json(url, this._log_out);
  }
  this._log_out = function()
  {
    document.location.href = _doc_root;
  }
  this.send_activate_mail = function()
  {
    var url = "./modules/ajax/activate_account.php";
    new ajax_json(url, this._login);
  }
  this.registreer = function()
  {
    document.location.href = _doc_root + "/register_account/";
  }
  this.save_profile = function(json)
  {
    var o;
    if (json.pasfoto)
    {
      for(n in json.pasfoto)
      {
        $("#img_" + json.pasfoto[n]).attr("src", _doc_root + "/_data/pasfotos/150/" + json.ID + ".png");
        $("#div_" + json.pasfoto[n]).fadeIn(300);
        o = $("input:file[name=" + json.pasfoto[n] + "]");
        o.replaceWith(o.parent().html());
      }
    }
    alert("Jouw gegevens zijn opgeslagen.");
    document.location.href = _doc_root;
  }
  this._register = function()
  {
    alert("Jouw registratie is gelukt. Gebruik de link in je mailbox om jouw account te activeren. Daarna is je account klaar voor gebruik.");
    document.location.href = _doc_root + "/";
  }
  this.forgot_pw = function()
  {
    var url = "./include/login.php?action=forgot";
    new ajax_json(url, function(xml)
    {
      $("#login_panel div.tile").html(xml.json.html);
    });
  }
  this.reset_password_submit = function()
  {
    var url = _doc_root + "/modules/account/recover_password_send.php?email=" + $("input[name=email]").val();
    new ajax_json(url, this._reset_password_submit);
  }
  this._reset_password_submit = function(xml)
  {
    $("#login_panel div.tile").html(xml.json.html);
    o_security.clear_msg();
  }
  this.reset = function()
  {
    this.state = STATE_DEFAULT;
    var url = "./include/login.php?action=reset";
    new ajax_json(url, this._login);
  }
  this.clear = function()
  {
    var url = "./include/login.php?action=clear";
    new ajax_json(url, function()
    {
      $("form[name=login_form] input").val("");
      $("form[name=login_form] img#img_clear").fadeOut(200);
    });
  }
  this.clear_msg = function()
  {
    $.timeout(function(){
      if ($("td#login_msg div").is(":visible")) 
        $.timeout(function() { $("td#login_msg div").hide(500); }, 5500);
    }, 10);
  }
  this.clear_msg();
}
var o_security = new SecurityModule();

