/* Scoped to: history-template-contact-form */
(function() {
  'use strict';
  var componentRoots = document.querySelectorAll('[data-component-slug="history-template-contact-form"]');
  componentRoots.forEach(function(componentRoot) {
    // Helper to query within component, with fallback to document for sibling elements
    var $ = function(sel) { 
      return componentRoot.querySelector(sel) || document.querySelector('[data-component-slug="history-template-contact-form"] ~ ' + sel) || document.querySelector(sel);
    };
    var $$ = function(sel) { 
      var results = componentRoot.querySelectorAll(sel);
      return results.length ? results : document.querySelectorAll(sel);
    };
    // Original component JS runs in this scope
    (function () { setTimeout(function () { var rawSuccess = "{{success_message}}"; var rawError = "{{error_message}}"; rawSuccess = rawSuccess && rawSuccess.indexOf("{{") === -1 ? rawSuccess.trim() : ""; rawError = rawError && rawError.indexOf("{{") === -1 ? rawError.trim() : ""; var CONFIG = { apiEndpoint: "{{form_api_endpoint}}", successMessage: rawSuccess || "Thank you! Your enquiry has been submitted.", errorMessage: rawError || "Something went wrong. Please try again.", redirectUrl: "{{redirect_url}}", redirectDelay: parseInt("{{redirect_delay}}") || 2000, }; var forms = document.querySelectorAll("form[data-contact-form]:not([data-initialized])"); forms.forEach(function (form) { form.setAttribute("data-initialized", "true"); var statusEl = form.querySelector(".form-status"); form.addEventListener("submit", function (e) { e.preventDefault(); var isValid = true; var groups = form.querySelectorAll(".form-group"); groups.forEach(function (group) { var input = group.querySelector("input, textarea"); var errorEl = group.querySelector(".form-error"); group.classList.remove("error"); errorEl.textContent = ""; if (!input || !input.value.trim()) { group.classList.add("error"); errorEl.textContent = "This field is required"; isValid = false; return; } if (input.type === "email") { var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(input.value.trim())) { group.classList.add("error"); errorEl.textContent = "Enter a valid email address"; isValid = false; } } }); if (!isValid) return; var formData = new FormData(form); var data = {}; formData.forEach(function (value, key) { data[key] = value; }); if (!CONFIG.apiEndpoint || CONFIG.apiEndpoint.indexOf("http") !== 0) { showStatus("success"); form.reset(); return; } fetch(CONFIG.apiEndpoint, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }) .then(function (res) { if (!res.ok) throw new Error("Network error"); return res.json(); }) .then(function () { showStatus("success"); form.reset(); if ( CONFIG.redirectUrl && (CONFIG.redirectUrl.indexOf("/") === 0 || CONFIG.redirectUrl.indexOf("http") === 0) ) { setTimeout(function () { window.location.href = CONFIG.redirectUrl; }, CONFIG.redirectDelay); } }) .catch(function () { showStatus("error"); }); }); function showStatus(type) { if (!statusEl) return; statusEl.style.display = "block"; statusEl.textContent = type === "success" ? CONFIG.successMessage : CONFIG.errorMessage; statusEl.className = "form-status " + type; } }); }, 0); })();
  });
})();