document.addEventListener('DOMContentLoaded', function() { const contactForm = document.getElementById('contactForm'); const formMessage = document.getElementById('form-message'); if (contactForm) { contactForm.addEventListener('submit', function(event) { event.preventDefault(); const name = document.getElementById('name').value.trim(); const email = document.getElementById('email').value.trim(); const message = document.getElementById('message').value.trim(); if (!name || !email || !message) { showMessage('Please fill out all fields.', 'error'); return; } if (!validateEmail(email)) { showMessage('Please enter a valid email address.', 'error'); return; } const formData = new FormData(contactForm); fetch('add_contact.php', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { if (data.success) { showMessage('Thank you for your message! We will get back to you soon.', 'success'); contactForm.reset(); } else { showMessage(data.message || 'An error occurred. Please try again.', 'error'); } }) .catch(error => { console.error('Error:', error); showMessage('An error occurred. Please try again.', 'error'); }); }); } function showMessage(message, type) { formMessage.textContent = message; formMessage.className = type; } function validateEmail(email) { const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(String(email).toLowerCase()); } }); // --- Shopping Cart --- const shoppingCart = { getCart: () => { const cart = localStorage.getItem('shoppingCart'); return cart ? JSON.parse(cart) : { items: [], restaurantId: null }; }, saveCart: (cart) => { localStorage.setItem('shoppingCart', JSON.stringify(cart)); }, addItem: (item, restaurantId) => { const cart = shoppingCart.getCart(); if (cart.restaurantId && cart.restaurantId !== restaurantId) { // If the item is from a different restaurant, ask for confirmation to clear the cart if (!confirm('You have items from another restaurant in your cart. Do you want to clear your cart and add this item?')) { return; } // Clear the cart cart.items = []; } cart.restaurantId = restaurantId; const existingItem = cart.items.find(i => i.id === item.id); if (existingItem) { existingItem.quantity++; } else { cart.items.push({ ...item, quantity: 1 }); } shoppingCart.saveCart(cart); shoppingCart.updateCartCount(); alert(`${item.name} has been added to your cart.`); }, updateCartCount: () => { const cart = shoppingCart.getCart(); const cartCount = cart.items.reduce((total, item) => total + item.quantity, 0); const cartCountElement = document.getElementById('cart-count'); if (cartCountElement) { cartCountElement.textContent = cartCount; } } }; document.addEventListener('DOMContentLoaded', () => { shoppingCart.updateCartCount(); });