-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
170 lines (149 loc) · 5.66 KB
/
script.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Smooth scrolling for navigation links
document.querySelectorAll('.nav-link').forEach(link => {
link.addEventListener('click', event => {
event.preventDefault();
const targetId = link.getAttribute('href').substring(1);
document.getElementById(targetId).scrollIntoView({
behavior: 'smooth'
});
});
});
// Form Validation and Submission
const form = document.querySelector('form');
const nameInput = document.getElementById('name');
const emailInput = document.getElementById('email');
const messageInput = document.getElementById('message');
function validateForm() {
const name = nameInput.value.trim();
const email = emailInput.value.trim();
const message = messageInput.value.trim();
let valid = true;
if (name === "") {
valid = false;
showError(nameInput, "Name cannot be empty");
} else {
clearError(nameInput);
}
if (email === "") {
valid = false;
showError(emailInput, "Email cannot be empty");
} else if (!validateEmail(email)) {
valid = false;
showError(emailInput, "Please enter a valid email address");
} else {
clearError(emailInput);
}
if (message === "") {
valid = false;
showError(messageInput, "Message cannot be empty");
} else {
clearError(messageInput);
}
return valid;
}
form.addEventListener('submit', async (event) => {
event.preventDefault(); // Prevent default form submission
if (validateForm()) {
const formData = {
name: nameInput.value.trim(),
email: emailInput.value.trim(),
message: messageInput.value.trim(),
};
try {
const response = await fetch('http://localhost:3000/submit-form', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData),
});
const data = await response.json();
if (data.success) {
alert(data.message);
form.reset(); // Reset the form
}
} catch (error) {
console.error('Error submitting form:', error);
alert("There was an error submitting the form.");
}
}
});
// Email validation function
function validateEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
// Testimonials carousel with animation
const testimonials = [
{ text: "Edu Consultancy helped me find the perfect university!", author: "Student A" },
{ text: "Thanks to their advice, I got a scholarship!", author: "Student B" },
{ text: "Very professional and supportive consultants.", author: "Parent C" }
];
let currentTestimonialIndex = 0;
function showTestimonial(index) {
const testimonial = testimonials[index];
const testimonialText = document.getElementById("testimonial-text");
const testimonialAuthor = document.getElementById("testimonial-author");
// Fade out animation
testimonialText.classList.add('fade-out');
testimonialAuthor.classList.add('fade-out');
setTimeout(() => {
testimonialText.innerText = `"${testimonial.text}"`;
testimonialAuthor.innerText = `- ${testimonial.author}`;
// Fade in animation
testimonialText.classList.remove('fade-out');
testimonialAuthor.classList.remove('fade-out');
}, 300); // Match duration with CSS transition duration
}
function nextTestimonial() {
currentTestimonialIndex = (currentTestimonialIndex + 1) % testimonials.length;
showTestimonial(currentTestimonialIndex);
}
function prevTestimonial() {
currentTestimonialIndex = (currentTestimonialIndex - 1 + testimonials.length) % testimonials.length;
showTestimonial(currentTestimonialIndex);
}
if (document.getElementById("testimonials")) {
showTestimonial(currentTestimonialIndex);
document.getElementById("next-testimonial").addEventListener("click", nextTestimonial);
document.getElementById("prev-testimonial").addEventListener("click", prevTestimonial);
}
// Appointment Booking Modal
const appointmentButton = document.createElement('button');
appointmentButton.innerText = 'Book Appointment';
appointmentButton.classList.add('appointment-button');
document.querySelector('#services').appendChild(appointmentButton);
appointmentButton.addEventListener('click', () => {
const modal = document.createElement('div');
modal.className = 'modal';
modal.innerHTML = `
<div class="modal-content">
<span class="close">×</span>
<h2>Book an Appointment</h2>
<label for="appointment-date">Preferred Date (DD/MM/YYYY):</label>
<input type="text" id="appointment-date" placeholder="Enter date">
<button id="confirm-appointment">Confirm</button>
</div>
`;
document.body.appendChild(modal);
modal.querySelector('.close').onclick = () => {
modal.remove();
};
document.getElementById("confirm-appointment").onclick = async () => {
const appointmentDate = document.getElementById('appointment-date').value;
if (appointmentDate) {
const response = await fetch('http://localhost:3000/book-appointment', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ date: appointmentDate }),
});
const data = await response.json();
alert(data.message);
modal.remove();
} else {
alert("Please enter a valid date.");
}
};
});