<script>
document.addEventListener("DOMContentLoaded", function() {
    const serviceSelect = document.getElementById("service");
    const customAmountInput = document.getElementById("custom_amount");
    const totalAmountSpan = document.getElementById("total-amount");
    const recurringCheckbox = document.getElementById("recurring");
    const frequencySelect = document.getElementById("frequency");
    const recurringText = document.getElementById("recurring-text");
    const recurringFrequency = document.getElementById("recurring-frequency");

    const donationForm = document.getElementById("donation-form");
    const cardNumberInput = document.getElementById("cardNumber");

    // 🔹 Luhn algorithm for credit card validation
    function validCreditCard(number) {
        number = number.replace(/\D/g, ""); // remove non-digits
        let sum = 0, shouldDouble = false;
        for (let i = number.length - 1; i >= 0; i--) {
            let digit = parseInt(number.charAt(i));
            if (shouldDouble) {
                digit *= 2;
                if (digit > 9) digit -= 9;
            }
            sum += digit;
            shouldDouble = !shouldDouble;
        }
        return (sum % 10) === 0;
    }

    // Donation summary update
    function updateSummary() {
        let amount = 0;
        const selectedOption = serviceSelect.options[serviceSelect.selectedIndex];

        if (selectedOption && selectedOption.dataset.amount) {
            amount = parseFloat(selectedOption.dataset.amount);
        }
        if (customAmountInput.value) {
            amount = parseFloat(customAmountInput.value);
        }

        totalAmountSpan.textContent = "$" + amount.toFixed(2);

        if (recurringCheckbox.checked) {
            frequencySelect.disabled = false;
            recurringText.style.display = "block";
            recurringFrequency.textContent = frequencySelect.options[frequencySelect.selectedIndex].text;
        } else {
            frequencySelect.disabled = true;
            recurringText.style.display = "none";
        }
    }

    serviceSelect.addEventListener("change", updateSummary);
    customAmountInput.addEventListener("input", updateSummary);
    recurringCheckbox.addEventListener("change", updateSummary);
    frequencySelect.addEventListener("change", updateSummary);
    updateSummary(); // Initial load

    // Validate card number before submission
    donationForm.addEventListener("submit", function(e) {
        const cardNum = cardNumberInput.value.trim();
        if (!validCreditCard(cardNum)) {
            e.preventDefault();
            alert("Invalid credit card number. Please check and try again.");
            cardNumberInput.focus();
            return false;
        }
    });
});
</script>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://iyacakiadventures.com/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://iyacakiadventures.com/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://iyacakiadventures.com/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://iyacakiadventures.com/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://iyacakiadventures.com/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
