function lightbox_login() {
  modal_loading();
  $.ajax({
    type: "POST",
    url: "?f=100&p=100",
    data: "a=lightbox_login",
    dataType: "json",
    success: function(json) {
      if (json.error) {
        alert(json.error);
        hide_modal();
        return;
      }

      $("#lightbox").html(json.data);
      show_modal();
      $("#username").focus();
    },
    error: function(error) {
      alert(error.responseText);
      hide_modal();
    }
  });
}

function login() {
  var username = $("#username").val();
  var password = $("#password").val();

  if (!username || !password) {
    alert("Please enter your username and password.");
    return;
  }

  $.ajax({
    type: "POST",
    url: "?f=100&p=100",
    data: "a=login&login_username="+username+"&login_password="+password,
    dataType: "json",
    success: function(json) {
      if (json.error) {
        alert(json.error);
        return;
      }

      window.location.reload();
    },
    error: function(error) {
      alert(error.responseText);
      hide_modal();
    }
  });
}

function logout() {
  $.ajax({
    type: "POST",
    url: "?f=100&p=100",
    data: "a=logout",
    dataType: "json",
    success: function(json) {
      if (json.error) {
        alert(json.error);
        return;
      }

      window.location.reload();
    },
    error: function(error) {
      alert(error.responseText);
      hide_modal();
    }
  });
}

function show_pw_reset_request() {
  $("#lightbox .content #page1").slideUp(400, function() {
    $("#lightbox #footer1").hide();
    $("#lightbox #footer2").show();
    $("#lightbox .content #page2").slideDown(400, function() {
      $("#barcode").focus();
    });
  })
}

function hide_pw_reset_request() {
  $("#lightbox .content #page2").slideUp(400, function() {
    $("#lightbox #footer2").hide();
    $("#lightbox #footer1").show();
    $("#lightbox .content #page1").slideDown(400, function() {
      $("#username").focus();
    });
  })
}

function reset_pw_request() {
  var barcode = $("#barcode").val();
  var email   = $("#email").val();

  if (!email || !barcode) {
    alert("Please enter your barcode and email address.");
    return;
  }

  $.ajax({
    type: "POST",
    url: "?f=100&p=100",
    data: "a=reset_pw_request&barcode="+barcode+"&email="+email,
    dataType: "json",
    success: function(json) {
      if (json.error) {
        alert(json.error);
        return;
      }

      alert("An email has been sent to you with instructions on how to reset your password.");
      window.location.reload();
    },
    error: function(error) {
      alert(error.responseText);
      hide_modal();
    }
  });
}

function reset_pw() {
  var pid  = $("#pid").val();
  var code = $("#code").val();
  var pw1  = $("#password1").val();
  var pw2  = $("#password2").val();

  if (!pw1 || !pw2) {
    alert("Please enter and confirm your new password.");
    return;
  }

  if (pw1 != pw2) {
    alert("The passwords you provided do not match.");
    return;
  }

  if (pw1.search(/^.*(?=.{6,})(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z]).*$/) == -1) {
    alert("Password not strong enough.");
    return;
  }

  $.ajax({
    type: "POST",
    url: "?f=100&p=100",
    data: "a=reset_pw&pid="+pid+"&code="+code+"&password="+pw1,
    dataType: "json",
    success: function(json) {
      if (json.error) {
        alert(json.error);
        return;
      }

      alert("Your password has been reset.");
      window.location.href = '?';
    },
    error: function(error) {
      alert(error.responseText);
      hide_modal();
    }
  });
}
