/* ==========================================================================
   WBMCZ — shared front-end behaviour
   ========================================================================== */

/* ---------- Smooth scroll for in-page anchors ---------------------------- */
document.querySelectorAll('a[href^="#"]').forEach(function (anchor) {
    anchor.addEventListener('click', function (e) {
        var href = this.getAttribute('href');
        if (!href || href === '#') return;
        var target = document.querySelector(href);
        if (!target) return;
        e.preventDefault();
        target.scrollIntoView({ behavior: 'smooth', block: 'start' });
    });
});

/* ---------- Validation helpers ------------------------------------------- */
function validatePhone(phone) { return /^[0-9]{10}$/.test(phone); }
function validateEmail(email) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); }

/* ---------- Phone input: digits only, max 10 ----------------------------- */
document.querySelectorAll('input[type="tel"]').forEach(function (input) {
    input.addEventListener('input', function () {
        this.value = this.value.replace(/[^0-9]/g, '').slice(0, 10);
    });
});

/* ---------- Lazy images --------------------------------------------------- */
document.addEventListener('DOMContentLoaded', function () {
    var images = document.querySelectorAll('img[data-src]');
    if (!images.length || !('IntersectionObserver' in window)) return;
    var io = new IntersectionObserver(function (entries, obs) {
        entries.forEach(function (entry) {
            if (!entry.isIntersecting) return;
            var img = entry.target;
            img.src = img.dataset.src;
            img.classList.add('loaded');
            obs.unobserve(img);
        });
    });
    images.forEach(function (img) { io.observe(img); });
});

/* ---------- Back to top (navy + gold) ------------------------------------ */
(function () {
    if (document.getElementById('backToTop')) return;
    var btn = document.createElement('button');
    btn.id = 'backToTop';
    btn.type = 'button';
    btn.setAttribute('aria-label', 'Back to top');
    btn.innerHTML = '<i class="fas fa-arrow-up"></i>';
    btn.className = 'fab fab-top';
    document.body.appendChild(btn);

    window.addEventListener('scroll', function () {
        btn.classList.toggle('show', (window.scrollY || document.documentElement.scrollTop) > 320);
    }, { passive: true });

    btn.addEventListener('click', function () {
        window.scrollTo({ top: 0, behavior: 'smooth' });
    });
})();

/* ---------- Prevent double submit ----------------------------------------
   Forms that manage their own submit state (AJAX forms) opt out with the
   data-no-lock attribute, otherwise their button would stay disabled after
   the request finished.
   ------------------------------------------------------------------------ */
document.querySelectorAll('form').forEach(function (form) {
    if (form.hasAttribute('data-no-lock')) return;
    form.addEventListener('submit', function () {
        var submitBtn = this.querySelector('button[type="submit"]');
        if (!submitBtn || submitBtn.disabled) return;
        submitBtn.disabled = true;
        submitBtn.dataset.originalHtml = submitBtn.innerHTML;
        submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i>' + (submitBtn.dataset.loadingText || 'Processing...');
        // Safety valve: if navigation does not happen, restore the button.
        setTimeout(function () {
            if (submitBtn.disabled && submitBtn.dataset.originalHtml) {
                submitBtn.disabled = false;
                submitBtn.innerHTML = submitBtn.dataset.originalHtml;
            }
        }, 12000);
    });
});
