$(function() {
  $("#loginButton").click(function() {
    if (!hmac_sha1_vm_test()) {
      var insecure = confirm("Secure login is unavailable, attempt normal login?\n(password will not be encrypted before sending)");
      if (insecure == true) {
        $("#login").submit();
      }
    } else {
      $(this).val("Processing...");
      $.ajax({
        url: "/login/"+$("#username").val(),
        complete: function(request) {
          if (request.status != 200) {
            var insecure = confirm("Secure login failed, attempt normal login?\n(password will not be encrypted before sending)");
            if (insecure == true) {
              $("#login").submit();
            } else {
              $("#loginButton").val("Login");
            }
          } else {
            var salt = request.responseText;
            var a1 = str_hmac_sha1(b642str(salt), $("#password").val());
            var a2 = b64_hmac_sha1(b642str($("#nonce").val()), a1);
            $("#password").val("");
            $("#a2").val(a2);
            $("#login").submit();
          }
        }
      });
    }
    return false;
  });
});