|
| 1 | +/** |
| 2 | + * contact-form.js - Main contact page form handler |
| 3 | + */ |
| 4 | + |
| 5 | +document.getElementById('mainContactForm').addEventListener('submit', async function(e) { |
| 6 | + e.preventDefault(); |
| 7 | + |
| 8 | + var submitBtn = document.querySelector('#mainContactForm button[type="submit"]'); |
| 9 | + var originalBtnText = submitBtn.textContent; |
| 10 | + |
| 11 | + // Disable button and show loading state |
| 12 | + submitBtn.disabled = true; |
| 13 | + submitBtn.textContent = 'Sending...'; |
| 14 | + |
| 15 | + var formData = { |
| 16 | + name: document.getElementById('contactName').value, |
| 17 | + email: document.getElementById('contactEmail').value, |
| 18 | + type: document.getElementById('contactType').value, |
| 19 | + service: document.getElementById('contactService').value, |
| 20 | + source: document.getElementById('contactSource').value, |
| 21 | + urgency: document.getElementById('contactUrgency').value, |
| 22 | + subject: document.getElementById('contactSubject').value, |
| 23 | + message: document.getElementById('contactMessage').value |
| 24 | + }; |
| 25 | + |
| 26 | + try { |
| 27 | + var response = await fetch('https://contact.unityailab.com/api/contact', { |
| 28 | + method: 'POST', |
| 29 | + headers: { |
| 30 | + 'Content-Type': 'application/json' |
| 31 | + }, |
| 32 | + body: JSON.stringify(formData) |
| 33 | + }); |
| 34 | + |
| 35 | + var result = await response.json(); |
| 36 | + |
| 37 | + if (response.ok && result.success) { |
| 38 | + // Success - show message and reset form |
| 39 | + alert('Thank you! Your message has been sent successfully. We\'ll get back to you soon.'); |
| 40 | + document.getElementById('mainContactForm').reset(); |
| 41 | + } else { |
| 42 | + // API error |
| 43 | + alert(result.error || 'Failed to send message. Please try again.'); |
| 44 | + } |
| 45 | + } catch (error) { |
| 46 | + console.error('Contact form error:', error); |
| 47 | + // Network error - fall back to mailto |
| 48 | + var recipient = 'unityailabcontact@gmail.com'; |
| 49 | + var mailtoSubject = '[' + formData.type + '] ' + formData.subject; |
| 50 | + var mailtoBody = 'Name: ' + formData.name + '\nEmail: ' + formData.email + '\nType of Inquiry: ' + formData.type + '\n'; |
| 51 | + if (formData.service && formData.service !== 'Not Applicable') { |
| 52 | + mailtoBody += 'Service of Interest: ' + formData.service + '\n'; |
| 53 | + } |
| 54 | + mailtoBody += 'How they heard about us: ' + formData.source + '\nPriority Level: ' + formData.urgency + '\n\nMessage:\n' + formData.message; |
| 55 | + var mailtoURL = 'mailto:' + recipient + '?subject=' + encodeURIComponent(mailtoSubject) + '&body=' + encodeURIComponent(mailtoBody); |
| 56 | + |
| 57 | + if (confirm('Could not connect to server. Would you like to send via email instead?')) { |
| 58 | + window.location.href = mailtoURL; |
| 59 | + } |
| 60 | + } finally { |
| 61 | + // Re-enable button |
| 62 | + submitBtn.disabled = false; |
| 63 | + submitBtn.textContent = originalBtnText; |
| 64 | + } |
| 65 | +}); |
0 commit comments