$(function () {
	$("#reset-password-btn").click(function (event) {
		event.preventDefault();

		$("#feedbackPrompt").hide();
		$("#feedbackPrompt").html("");

		$("#reset-password-btn").attr("disabled", "");

		$(".email_feedback").hide();
		var email_pattern =
			/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		var email = $("input#email").val();
		if (email == "" || !email.match(email_pattern)) {
			$(".email_feedback").html("Enter a valid email address.");
			$(".email_feedback").show();
			$("input#email").focus();

			$("#reset-password-btn").removeAttr("disabled");
			return false;
		}

		$(".button_spinner").show();

		$("input#password").prop("disabled", true);

		var form_data = "email=" + email;

		$.ajax({
			url: "helpers/forgot-password",
			type: "POST",
			data: form_data,
		}).done(function (response) {
			if (response == "success") {
				$("#feedbackPrompt").html(
					'<div class="alert alert-success" role="alert"> A password reset link has been sent to ' +
						email +
						". Please check to reset your password.</div>"
				);
			} else {
				$("#feedbackPrompt").html(
					'<div class="alert alert-danger" role="alert">' +
						response +
						"</div>"
				);
			}

			$("#feedbackPrompt").show();
			$("#reset-password-btn").removeAttr("disabled");
			$(".button_spinner").hide();

			$("input#password").prop("disabled", false);
		});
		return false;
	});
});
