$(document).ready(function () {
	$("#delete_account_btn").click(function (event) {
		event.preventDefault();

		$("#delete_account_btn").attr("disabled", "");

		$(".password_feedback").hide();
		var password = encodeURIComponent($("input#password").val());

		$(".password_feedback").hide();

		if (password == "") {
			$(".password_feedback").html("Password cannot be empty.");
			$(".password_feedback").show();
			$("input#password").focus();

			$("#delete_account_btn").removeAttr("disabled");
			return false;
		}

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

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

		var form_data = "password=" + password;

		$.ajax({
			url: "../helpers/delete-account",
			type: "POST",
			data: form_data,
		}).done(function (response) {
			console.log(response);

			if (response == "success") {
				window.location.href = "../login";
			} else {
				$(".password_feedback").html(response);
				$(".password_feedback").show();
				$("#delete_account_btn").removeAttr("disabled");
				$(".button_spinner").hide();

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

		return false;
	});
});
