document.addEventListener('DOMContentLoaded', function() {
const contactForm = document.getElementById('contactForm');
const toastContainer = document.querySelector('.toast-container');
function showToast(message, isSuccess = true) {
const toastId = 'toast-' + Date.now();
const toastHtml = `
`;
toastContainer.insertAdjacentHTML('beforeend', toastHtml);
const toastElement = document.getElementById(toastId);
const toast = new bootstrap.Toast(toastElement);
toast.show();
toastElement.addEventListener('hidden.bs.toast', () => {
toastElement.remove();
});
}
if (contactForm) {
contactForm.addEventListener('submit', function(e) {
e.preventDefault();
const submitBtn = contactForm.querySelector('button[type="submit"]');
const originalBtnText = submitBtn.innerHTML;
submitBtn.disabled = true;
submitBtn.innerHTML = ' Sending...';
const formData = new FormData(contactForm);
fetch('process_contact.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
showToast(data.message || 'Message sent successfully!');
contactForm.reset();
} else {
showToast(data.error || 'Something went wrong.', false);
}
})
.catch(error => {
console.error('Error:', error);
showToast('Failed to connect to the server.', false);
})
.finally(() => {
submitBtn.disabled = false;
submitBtn.innerHTML = originalBtnText;
});
});
}
// Smooth scroll for nav links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
window.scrollTo({
top: target.offsetTop - 70,
behavior: 'smooth'
});
}
});
});
});