$(function() { initialiseBeforeTestForm(); initialiseTestForm(); }); var durationInterval = false; var testStartTime = null; function initialiseBeforeTestForm() { $("#before-test-form").on("submit", function() { $(this).find("button").prepend(" ").attr("disabled", ""); $(this).find("div[data-submitting-div]").removeClass("d-none"); }); } function initialiseTestForm() { const $form = $("#test-form"); const $submitButton = $("#test-submit-button"); const $alert = $("#test-submit-alert"); const $alertIcon = $alert.find(".fa"); const $alertHeading = $alert.find("strong"); const $alertText = $("#test-submit-status"); const showAlert = (alertClass, icon, heading, text) => { $alert.prop("hidden", false); $alert.attr("class", `alert ${alertClass}`); $alertIcon.attr("class", `fa fa-fw ${icon}`); $alertHeading.text(heading); $alertText.text(text); }; const hideAlert = () => { $alert.prop("hidden", true); }; $form.on("submit", function(event) { event.preventDefault(); showAlert("alert-info", "fa-sync", "Submitting...", "Please wait..."); $submitButton.prop("disabled", true); const formData = new FormData($form.get(0)); $.ajax({ type: "post", url: window.location, data: formData, processData: false, contentType: false, async: false, cache: false, success: (response) => { try { if(!response.success && response.message) { throw response.message; } else if(!response.success) { throw "Please try again later."; } showAlert("alert-success", "fa-check", "Submit Successful", "Thank you for participating in this test. You will be redirected to the Tests page in three seconds."); setTimeout(function() { window.location = "/account/tests"; }, 3000); } catch(err) { showAlert("alert-danger", "fa-exclamation-circle", "Error", err); $submitButton.prop("disabled", false); } }, error: function(request, status, error) { console.log(request, status, error); showAlert("alert-danger", "fa-exclamation-circle", "No Internet Connectivity", "Please check your Internet connectivity and try again."); $submitButton.prop("disabled", false); } }); }); }