(function () { /* ========================== COLLECT URL PARAMS ========================== */ var urlParams = new URLSearchParams(window.location.search); var trackingData = {}; urlParams.forEach(function (value, key) { trackingData[key] = value; }); var currentPage = window.location.pathname.split("/").pop() || "index.html"; /* ========================== BUILD URL WITH PARAMS (SUBFOLDER SAFE) ========================== */ function buildUrlWithParams(baseUrl) { var url; if (baseUrl.startsWith("http")) { url = new URL(baseUrl); } else { // ✅ IMPORTANT FIX: use current URL as base url = new URL(baseUrl, window.location.href); } for (var key in trackingData) { if (trackingData.hasOwnProperty(key)) { url.searchParams.set(key, trackingData[key]); } } return url.toString(); } /* ========================== JVZOO REDIRECT ========================== */ window.goToJVZoo = function () { try { var formData = JSON.parse( localStorage.getItem("permitFormData") || "{}" ); for (var key in formData) { if (formData.hasOwnProperty(key) && formData[key]) { trackingData[key] = formData[key]; } } } catch (e) {} window.location.href = buildUrlWithParams( "https://www.jvzoo.com/b/115197/430855/99" ); }; /* ========================== INDEX PAGE — ZIP CAPTURE ========================== */ if (currentPage === "index.html") { document.addEventListener("DOMContentLoaded", function () { window.validateAndProceed = function () { var zipInput = document.getElementById("zipInput"); var zipValue = zipInput ? zipInput.value.trim() : ""; if (zipValue.length > 5) { zipValue = zipValue.substring(0, 5); } trackingData["zipCode"] = zipValue || ""; // ✅ stays in same folder window.location.href = buildUrlWithParams("approve.html"); }; }); } /* ========================== APPROVE PAGE ========================== */ if (currentPage === "approve.html") { document.addEventListener("DOMContentLoaded", function () { var qualifyBtn = document.querySelector( ".qualify-btn, .start-btn, a[href='verify1.html']" ); if (qualifyBtn) { qualifyBtn.addEventListener("click", function (e) { e.preventDefault(); trackingData["qualifyNow"] = "clicked"; window.location.href = buildUrlWithParams("verify1.html"); }); } }); } /* ========================== VERIFY PAGES ========================== */ if (currentPage.startsWith("verify")) { document.addEventListener("DOMContentLoaded", function () { document.querySelectorAll("a").forEach(function (link) { var text = link.textContent.trim().toLowerCase(); var href = link.getAttribute("href"); if ( (text === "true" || text === "yes") && href && href.endsWith(".html") ) { link.addEventListener("click", function (e) { e.preventDefault(); trackingData[currentPage.replace(".html", "")] = link.textContent.trim(); window.location.href = buildUrlWithParams(href); }); } }); }); } /* ========================== QUESTION PAGES ========================== */ if (currentPage.startsWith("question")) { document.addEventListener("click", function (e) { var link = e.target.closest("a"); if (link) { var href = link.getAttribute("href"); if (href && href.endsWith(".html") && !href.startsWith("http")) { e.preventDefault(); window.location.href = buildUrlWithParams(href); } } }); } /* ========================== APPLY PAGE ========================== */ if (currentPage === "apply.html") { document.addEventListener("DOMContentLoaded", function () { var continueBtn = document.querySelector(".continue-btn"); var proceedBtn = document.querySelector(".proceed-btn"); function proceed(e) { e.preventDefault(); trackingData["proceed"] = "clicked"; window.goToJVZoo(); } if (continueBtn) continueBtn.addEventListener("click", proceed); if (proceedBtn) proceedBtn.addEventListener("click", proceed); }); } })();