/* ========================================================================= Quit Plan Builder ========================================================================= */ // "div[data-name='x']" selector helper function dn(name) { return "div[data-name='" + name + "']"; } // Feedback submit button: enable only when textarea has text function checkEmptyTextArea() { var v = $("#qp-feedback-msg").val(); var ok = typeof v !== "undefined" && v.trim().length !== 0; $(".submit-button-4") .attr("disabled", !ok) .css("background-color", ok ? "#111111" : "#dddddd"); } // Calendar button: enable only when a date is set. Returns true if empty. function isDateEmpty() { var v = $("#Date").val(); var empty = typeof v === "undefined" || v.trim().length === 0; $("#add-to-calendar") .attr("disabled", empty) .css("background-color", empty ? "#dddddd" : "#e12c33"); return empty; } // Section + page break pairs (used for reset + empty-hide) // NOTE: triggers reuses #reasons-page-break in the original — likely a copy/paste // slip. Swap to #triggers-page-break if that element exists in your markup. var SECTIONS = [ [ ".quit-result-reasons", ".quit-planner-result-reasons", "#reasons-page-break", ], [ ".quit-result-triggers-group", ".quit-plan-result-triggers", "#reasons-page-break", ], [ ".quit-result-craving", ".quit-plan-result-cravings", "#cravings-page-break", ], [ ".quit-plan-result-help", ".quit-planner-result-extra-help", "#extra-help-page-break", ], ]; // Trigger-group container -> category to hide when empty var TRIGGER_GROUPS = [ [".result-emotional-triggers", "#result-emotional-triggers-category"], [".result-social-triggers", "#result-social-triggers-category"], [".result-habit-triggers", "#result-habit-triggers-category"], [".user-defined-triggers", "#result-user-defined-trigger-category"], ]; function resetResultSections() { $.each(TRIGGER_GROUPS, function (i, g) { $(g[1]).show(); }); $.each(SECTIONS, function (i, s) { $(s[1]).show(); $(s[2]).addClass("html2pdf__page-break"); }); } function scrollToBuild() { $("html, body").animate({ scrollTop: $("#step-6").offset().top }, "slow"); } // Warn before leaving the page (browsers show their own generic text) window.addEventListener("beforeunload", function (e) { e.preventDefault(); e.returnValue = ""; }); $(function () { // Initial state $( ".quit-result-reasons, .quit-result-trigger, .quit-result-craving, .quit-plan-result-help, .quit-plan-result-section, #cal-button-block" ).hide(); $(".quit-plan-builder").show(); checkEmptyTextArea(); isDateEmpty(); // Checkbox toggles: show/hide matching result, recolour label $("label.w-checkbox").on("click", function () { var $input = $(this).find("input"); var on = $input.prop("checked"); $(this).css("color", on ? "white" : "#333"); $input.attr("aria-checked", on); $(".quit-plan-result-section") .find(dn($input.attr("data-name"))) [on ? "show" : "hide"](); }); // User-defined reason $("#reason-user-defined").on("blur", function () { $("#user-defined-reason-output")[$(this).val() !== "" ? "show" : "hide"](); }); // User-defined triggers (FIX: selector was undefined in the original's else branch) $(".user-defined-trigger").on("blur", function () { var has = $(this).val() !== ""; $(".quit-plan-result-section") .find(dn($(this).attr("data-name"))) [has ? "show" : "hide"](); }); // Keep editing: back to builder $(".keep-editing").on("click", function () { $(".quit-plan-builder").show(); resetResultSections(); $(".quit-plan-result-section").hide(); document.getElementById("select-quit-date").scrollIntoView(true); }); // Show results $("#show-quit-plan-result").on("click", function () { var $btn = $(this).html("Please wait..").attr("disabled", true); setTimeout(function () { $btn.attr("disabled", false).html("View your quit planner"); $(".quit-plan-result-section").show(); $(".quit-plan-builder").hide(); // Populate user-defined trigger results (FIX: off-by-one loop; .text() not .html()) $(".user-defined-trigger").each(function (i) { var $r = $(".quit-plan-result-section").find( dn("user-defined-trigger-" + (i + 1)) ); this.value !== "" ? $r.show().text(this.value) : $r.hide(); }); // Hide trigger categories with nothing selected $.each(TRIGGER_GROUPS, function (i, g) { if ($(g[0] + " div:visible").length === 0) $(g[1]).hide(); }); // Hide whole sections (and their page breaks) when empty $.each(SECTIONS, function (i, s) { if ($(s[0] + ":visible").length === 0) { $(s[1]).hide(); $(s[2]).removeClass("html2pdf__page-break"); } }); document.getElementById("quit-plan-result-section").scrollIntoView(true); }, 2000); }); // Feedback + calendar $("#qp-feedback-msg").on("keyup", checkEmptyTextArea); $("#Date").on("change", isDateEmpty); $("#add-to-calendar").on("click", function () { if (!isDateEmpty()) $("#cal-button-block").show(); }); // Mobile: scroll the rewards tab dock if (window.matchMedia("(max-width: 768px)").matches) { var $day = $("#1-day-tab"), $week = $("#1-week-tab"); $day.on("click", function () { $("#rewards-tabs").animate({ scrollLeft: 0 }, "slow"); }); $week.on("click", function () { $("#rewards-tabs").animate( { scrollLeft: $day.outerWidth(true) - 50 }, "slow" ); }); $("#1-month-tab").on("click", function () { $("#rewards-tabs").animate( { scrollLeft: $day.outerWidth(true) + $week.outerWidth(true) - 50 }, "slow" ); }); } // Step scroll buttons (1–5 share a +40 offset; 6 goes to the builder) for (var s = 1; s <= 5; s++) (function (n) { $("#scroll-to-" + n).on("click", function () { $("html, body").animate( { scrollTop: $("#step-" + n).offset().top + 40 }, "slow" ); }); })(s); $("#scroll-to-6").on("click", scrollToBuild); });