﻿
(function ($)
{
  $.fn.modalForm = function (opts)
  {
    opts.autoOpen = false;
    opts.height = 330;
    opts.width = 350;
    opts.modal = true;
    opts.buttons = {};
    var id = this.attr("id") + "";
    var formQ = "#" + id + " form";
    $(formQ).submit(function ()
    {
      $("#" + id + " input").attr('disabled', 'disabled');
      $("#" + id).children().fadeTo(100, 0.3);
      if (event && event.stopPropagation) event.stopPropagation();
      if (event && event.preventDefault) event.preventDefault();
      return false;
    });
    opts.close = function ()
    {
      $("#" + id).clearDisabled();
    };
    opts.buttons[opts.okBtn] = function ()
    {
      $(formQ).submitValidate();
    };
    opts.buttons.Cancel = function ()
    {
      $(this).dialog("close");
    };
    this.dialog(opts);

    this.find("input").keydown(function ()
    {
      if (event.keyCode == "13")
      {
        $(formQ).submitValidate();
        return false;
      }
    });

    var shortId = id.substr(0, id.indexOf("-"));
    $("#" + shortId + "-link").click(function ()
    {
      $("#" + id).dialog("open");
      return false;
    });

  };
  $.fn.validate = function (validateFunc)
  {
    if (validateFunc === undefined)
    {
      var validateFunc = this.data("onvalidate");
      if (validateFunc)
        return validateFunc();
      else
        return true;
    }
    else
      this.data("onvalidate", validateFunc);
  };
  $.fn.submitValidate = function ()
  {
    if (!this.validate())
      return false;
    return this.submit();
  };
  $.fn.clearDisabled = function ()
  {
    this.find("input").removeAttr('disabled');
    this.children().fadeTo(0, 1);
  };
})(jQuery);


$(function ()
{

  $("#login-popup").modalForm({ okBtn: "Login" });
  $("#activate-popup").modalForm({ okBtn: "Activate" });
  $("#changepassword-popup").modalForm({ okBtn: "Change Password" });
  $("#forgotpassword-popup").modalForm({ okBtn: "Reset Password" });
  $("#changepassword-popup form").validate(function()
  {
    var popup = $("#changepassword-popup");
    var oldpass = popup.find("#oldpassword"),
        newpass1 = popup.find("#newpassword1"),
        newpass2 = popup.find("#newpassword2");
    var allFields = $([]).add(oldpass).add(newpass1).add(newpass2);
    var tips = popup.find(".validatetips");

    var updateTips = function (t)
    {
      tips
				.text(t)
				.addClass("ui-state-highlight");
      setTimeout(function ()
      {
        tips.removeClass("ui-state-highlight", 1500);
      }, 500);
    };

    var checkLength = function (o, n, min, max)
    {
      var valid = true;
      if (max)
        valid = o.val().length <= max && o.val().length >= min;
      else
        valid = o.val().length >= min;
      if (!valid)
      {
        o.addClass("ui-state-error");
        if (max)
          updateTips("Length of '" + n + "' must be between " + min + " and " + max + ".");
        else if (min == 1)
          updateTips("The '" + n + "' field is obligatory.");
        else
          updateTips("'" + n + "' must be at least " + min + " chars.");
        return false;
      } else
      {
        return true;
      }
    };

    var checkEqual = function (o1, o2, msg)
    {
      if ((o1.val() + "") != (o2.val() + ""))
      {
        o2.addClass("ui-state-error");
        updateTips(msg);
        return false;
      }
      else
        return true;
    };

    var bValid = true;
    allFields.removeClass("ui-state-error");
    tips.text(" ");
    bValid = bValid && checkLength(oldpass, "Old password", 1);
    bValid = bValid && checkLength(newpass1, "New password", 8);
    bValid = bValid && checkEqual(newpass1, newpass2, "The two new passwords must be identical");

    popup.clearDisabled();
    if (bValid)
    {
      return true;
    }
    else
    {
      popup.find("input").filter(".ui-state-error").first().focus().select();
      return false;
    }
  });

});

$login = {};
(function ($login)
{
  
  $login.Success = function()
  {
    window.location.href = "/my";
  };

  $login.Failed = function()
  {
    alert("Login failed");
    $("#login-popup").clearDisabled();
    $("#login-popup input").first().focus().select();
  };

})($login);

$activate = {};
(function ($activate)
{
  
  $activate.Success = function()
  {
    $("#activate-popup").clearDisabled();
    alert("Your license was succesfully activated.\nSee your activation code next to the license.");
    window.location.href = "/my";
  };

  $activate.Failed = function(message)
  {
    alert("Activation failed: " + message);
    $("#activate-popup").clearDisabled();
    $("#activate-popup input").first().focus();
  };

})($activate);

$changePassword = {};
(function ($changePassword)
{

  $changePassword.Success = function ()
  {
    $("#changepassword-popup").clearDisabled();
    alert("Your password was succesfully changed.");
    window.location.reload();
  };

  $changePassword.Failed = function (message)
  {
    alert("Change password failed: " + message);
    $("#changepassword-popup").clearDisabled();
    $("#changepassword-popup input").first().focus();
  };

})($changePassword);

$resetPassword = {};
(function ($resetPassword)
{

  $resetPassword.Success = function ()
  {
    $("#forgotpassword-popup").clearDisabled();
    alert("Your password was succesfully reset. We have sent you an email with your new password to the provided address.");
    window.location.reload();
  };

  $resetPassword.Failed = function (message)
  {
    alert("Password reset failed: " + message);
    $("#forgotpassword-popup").clearDisabled();
    $("#forgotpassword-popup input").first().focus();
  };

})($resetPassword);

