

function MaillistSubscribeInitSelectList(wizard, properties)
{
  db.SearchNodes({ "type" : "maillist" }, function(transaction_id, result_code, node_list)
  {
    var container = $("#template-maillist-lists").tmpl(node_list); 
    
    container.find(".popup-list-item-container").bind("click", function(event)
    {
      var element = $(event.currentTarget);
      
      properties["node_maillist"] = node_list[element.attr("data")]; 
      
      wizard.Next();
    });
    
    wizard.Show(container);
  });
}

function MaillistSubscribeInitEnterInformation(wizard, properties)
{
  var container = $("#template-maillist-subscribe-form").tmpl();
  
  container.find("form").bind("submit", function(event)
  {
    event.preventDefault();
    wizard.Next();
  });
  
  wizard.Show(container);
}

function MaillistSubscribeValidateEnterInformation(wizard, properties)
{
  var name        = "";
  var email       = "";
  var profession  = "";
  
  jQuery.each($(".popup-form-input"), function(n, element)
  {
    element = $(element);

    if (element.attr("name") == "name")
    {
      name = element.val();
    }
    else if (element.attr("name") == "email")
    {
      email = element.val();
    }
    else if (element.attr("name") == "profession")
    {
      profession = element.val();
    }
  });

  if ("" == name || "" == email)
  {
    return false;
  }

  properties["name"] = name;
  properties["email"] = email;
  properties["profession"] = profession;

  return true;
}

function MaillistSubscribeInitSubscribe(wizard, properties)
{
  var in_node = { 
    "type" : "maillist_subscriber", 
    "name" : properties["name"],
    "attributes" : { 
      "Email" : properties["email"],
      "Profession" : properties["profession"]
    }
  };
 
  db.CreateNode(in_node, function(transaction_id, result_code, out_node)
  {
    if (MURRIX_RESULT_CODE_OK == result_code)
    {
      db.LinkNodes(properties["node_maillist"]["id"], out_node["id"], "subscriber", function(transaction_id, result_code, node_up, node_down, role)
      {
        if (MURRIX_RESULT_CODE_OK == result_code)
        {
          wizard.Show($("#template-maillist-subscribe-done").tmpl({ "maillist" : node_up, "subscriber" : node_down }));
        }
        else
        {
          wizard.Show($("#template-maillist-subscribe-error").tmpl({ "maillist" : properties["node_maillist"], "result_code" : result_code }));
        }
      });
    }
    else
    {
      wizard.Show($("#template-maillist-subscribe-error").tmpl({ "maillist" : properties["node_maillist"], "result_code" : result_code }));
    }
  });  
}

var maillist_subscribe_wizard = [
  { "init" : MaillistSubscribeInitSelectList,       "validate" : null },
  { "init" : MaillistSubscribeInitEnterInformation, "validate" : MaillistSubscribeValidateEnterInformation },
  { "init" : MaillistSubscribeInitSubscribe,        "validate" : null }
];


