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

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

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

		$(".amount_feedback").hide();
		var amount = $("input#amount").val();

		if (amount == "") {
			$(".amount_feedback").html("Amount cannot be empty.");
			$(".amount_feedback").show();
			$("input#amount").focus();

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

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

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

		var leverage = $("#leverage option:selected").val();

		var form_data = "amount=" + amount + "&leverage=" + leverage;

		$.ajax({
			url: "../helpers/trade-demo",
			type: "POST",
			data: form_data,
		}).done(function (response) {
			$("#proceed_btn").removeAttr("disabled");
			$(".button_spinner").hide();
			$("input#amount").prop("disabled", false);

			if (response == "success") {
				$("#errorPrompt").hide();
				window.location.href = "demo-trading-history";
			} else {
				$("#errorPrompt").html(response);
				$("#errorPrompt").show();
			}
		});

		return false;
	});
});
