`; return template; } async function sendEmail(formData) { try { const emailTemplate = generateEmailTemplate(formData); const response = await fetch('https://bestarion.com/wp-json/custom/v1/send-email/', { method: 'POST', headers: { 'Content-Type': 'application/json', },redirect: "follow", body: JSON.stringify({ subject: `Price Lock Request from ${formData.companyName}`, message: emailTemplate, to: formData.email, // Send to customer name: formData.fullName, email: formData.email }) }); console.log(response); if (!response.ok) { throw new Error('Failed to send email'); } return await response.json(); } catch (error) { console.error('Error sending email:', error); throw error; } } // Update the form submission handler document.querySelector('.submit-button').addEventListener('click', async function(e) { e.preventDefault(); const companyName = document.getElementById('companyName').value; const fullName = document.getElementById('fullName').value; const email = document.getElementById('email').value; const phone = document.getElementById('phone').value; const comments = document.getElementById('comments').value; const consent = document.getElementById('consent').checked; if (!companyName || !fullName || !email || !consent) { alert('Please fill in all required fields'); return; } if (!isValidEmail(email)) { alert('Please enter a valid email address'); return; } // Collect form data const formData = { companyName, fullName, email, phone, comments, consent, pricing: { model: models, serviceBook, servicePayroll, bookkeeping, payroll, hourlyRate, grandTotal: document.getElementById('grand-total').textContent } }; try { // Show loading state const submitButton = document.querySelector('.submit-button'); submitButton.textContent = 'Sending...'; submitButton.disabled = true; // Send email await sendEmail(formData); // Show notification message document.querySelector('.notification-message').style.display = 'block'; // Close modal and show success message setTimeout(() => { document.getElementById("contactModal").style.display = "none"; window.location.reload() }, 5000); } catch (error) { alert('Failed to send your request. Please try again later.'); } finally { // Reset button state submitButton.textContent = 'Lock in my price'; submitButton.disabled = false; } }); function isValidEmail(email) { return /^[^s@]+@[^s@]+.[^s@]+$/.test(email); }