document.addEventListener('DOMContentLoaded', function () { // Smooth scroll for navigation links document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function (e) { e.preventDefault(); document.querySelector(this.getAttribute('href')).scrollIntoView({ behavior: 'smooth' }); }); }); const contactForm = document.getElementById('contactForm'); const formAlert = document.getElementById('form-alert'); if (contactForm) { contactForm.addEventListener('submit', function (e) { e.preventDefault(); e.stopPropagation(); if (!contactForm.checkValidity()) { contactForm.classList.add('was-validated'); return; } const formData = new FormData(contactForm); const submitButton = contactForm.querySelector('button[type="submit"]'); const originalButtonText = submitButton.innerHTML; submitButton.disabled = true; submitButton.innerHTML = ' Sending...'; fetch('contact_handler.php', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { formAlert.classList.remove('d-none', 'alert-danger', 'alert-success'); if (data.success) { formAlert.classList.add('alert-success'); formAlert.textContent = data.message; contactForm.reset(); contactForm.classList.remove('was-validated'); } else { formAlert.classList.add('alert-danger'); formAlert.textContent = data.error || 'An unknown error occurred.'; } }) .catch(error => { formAlert.classList.remove('d-none', 'alert-success'); formAlert.classList.add('alert-danger'); formAlert.textContent = 'A network error occurred. Please try again.'; console.error('Fetch Error:', error); }) .finally(() => { submitButton.disabled = false; submitButton.innerHTML = originalButtonText; }); }); } });