
function DbLoginInitEnterInformation(wizard, properties)
{
  var container = $("#template-db-login-form").tmpl(); 
    
  container.find("form").bind("submit", function(event)
  {
    event.preventDefault();
    wizard.Next();
  });

  wizard.Show(container);
}

function DbLoginValidateEnterInformation(wizard, properties)
{
  var username = "";
  var password = "";
  
  jQuery.each($(".popup-form-input"), function(n, element)
  {
    element = $(element);

    if (element.attr("name") == "username")
    {
      username = element.val();
    }
    else if (element.attr("name") == "password")
    {
      password = element.val();
    }
  });

  if ("" == username || "" == password)
  {
    return false;
  }


  properties["username"] = username;
  properties["password"] = password;

  return true;
}

function DbLoginInitLogin(wizard, properties)
{
  var in_node = { 
    "type" : "maillist_subscriber", 
    "name" : properties["name"],
    "attributes" : { 
      "Email" : properties["email"]
    }
  };
 
  db.Login(properties["username"], properties["password"], function(transaction_id, result_code, user_node_data)
  {
    if (MURRIX_RESULT_CODE_OK == result_code)
    {
      wizard.Show($("#template-db-login-success").tmpl(user_node_data));
    }
    else
    {
      wizard.Show($("#template-db-login-failure").tmpl({ "result_code" : result_code }));
    }
  });  
}

var db_login_wizard = [
  { "init" : DbLoginInitEnterInformation, "validate" : DbLoginValidateEnterInformation },
  { "init" : DbLoginInitLogin,            "validate" : null }
];


