-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.js
More file actions
148 lines (125 loc) · 5.46 KB
/
script2.js
File metadata and controls
148 lines (125 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
document.addEventListener('DOMContentLoaded', function() {
// Form elements
const modal = document.getElementById('privacyModal');
const privacyLink = document.getElementById('privacyLink');
const closeBtn = document.querySelector('.close');
const privacyCheckbox = document.getElementById('privacyCheck');
const submitButton = document.querySelector('.btn-submit-enhanced');
const form = document.querySelector('.form-enhanced');
// Required fields
const nameInput = form?.querySelector('input[name="name"]');
const emailInput = form?.querySelector('input[type="email"]');
const phoneInput = form?.querySelector('input[type="tel"]');
// Disable submit button by default
if (submitButton) {
submitButton.disabled = true;
}
// Function to check if all required fields are filled
function validateForm() {
if (!nameInput || !emailInput || !phoneInput || !privacyCheckbox) return false;
const isNameValid = nameInput.value.trim() !== '';
const isEmailValid = emailInput.value.trim() !== '' && emailInput.validity.valid;
const isPhoneValid = phoneInput.value.trim() !== '';
const isPrivacyChecked = privacyCheckbox.checked;
return isNameValid && isEmailValid && isPhoneValid && isPrivacyChecked;
}
// Update button state based on form validity
function updateButtonState() {
const isValid = validateForm();
if (submitButton) {
submitButton.disabled = !isValid;
submitButton.style.opacity = isValid ? '1' : '0.6';
submitButton.style.cursor = isValid ? 'pointer' : 'not-allowed';
}
}
// Add event listeners to all form fields
if (form) {
// Check on input changes
[nameInput, emailInput, phoneInput].forEach(input => {
if (input) {
input.addEventListener('input', updateButtonState);
}
});
// Check on checkbox change
if (privacyCheckbox) {
privacyCheckbox.addEventListener('change', updateButtonState);
}
// Initial check
updateButtonState();
}
// Open modal when privacy link is clicked
if (privacyLink) {
privacyLink.addEventListener('click', function(e) {
e.preventDefault();
modal.style.display = 'block';
});
}
// Close modal when X is clicked
if (closeBtn) {
closeBtn.addEventListener('click', function() {
modal.style.display = 'none';
});
}
// Close modal when clicking outside the modal content
window.addEventListener('click', function(e) {
if (e.target === modal) {
modal.style.display = 'none';
}
});
if (!form) return;
form.addEventListener('submit', async function(e) {
e.preventDefault();
const submitButton = form.querySelector('button[type="submit"]');
const originalText = submitButton.textContent;
const statusDiv = document.getElementById('form-status');
try {
// Show loading state
const statusDiv = document.getElementById('form-status');
statusDiv.textContent = 'Sending your information...';
statusDiv.style.color = '#3B82F6'; // Blue color for info
submitButton.disabled = true;
submitButton.textContent = 'Sending...';
// Get form data safely
const nameInput = form.querySelector('[name="name"]');
const emailInput = form.querySelector('[name="email"]');
const phoneInput = form.querySelector('[name="phone"]');
const countryCodeInput = form.querySelector('[name="country_code"]');
if (!nameInput || !emailInput || !phoneInput) {
throw new Error('Required form fields are missing');
}
const formData = {
name: nameInput.value,
email: emailInput.value,
phone: countryCodeInput.value + phoneInput.value,
};
// Send data
const response = await fetch('https://am.devnagri.com/webhook/e3e13025-5eb5-4402-9ade-4c4cbd41333d', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData)
});
console.log('Response status:', response.status);
// Handle success response
if (response.ok) {
statusDiv.textContent = 'Thank you! Your information has been submitted successfully.';
statusDiv.style.color = '#10B981'; // Green color for success
form.reset();
// Optionally redirect after a short delay
// setTimeout(() => {
// window.location.href = 'contact-us/index.html';
// }, 1500);
} else {
statusDiv.textContent = 'There was an error submitting the form. Please try again.';
statusDiv.style.color = '#EF4444'; // Red color for error
}
} catch (error) {
console.error('Error:', error);
} finally {
// Reset button state
submitButton.disabled = false;
submitButton.textContent = originalText;
}
});
});