forked from GarimaSingh0109/WasteManagment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
278 lines (230 loc) · 9.67 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Function For Smooth Scrolling
function smoothScrolling() {
const lenis = new Lenis();
lenis.on('scroll', (e) => {
console.log(e);
});
function raf(time) {
lenis.raf(time);
requestAnimationFrame(raf);
}
requestAnimationFrame(raf);
}
smoothScrolling();
function validateSignup() {
const fullName = document.querySelector('input[name="fullName"]').value;
const email = document.querySelector('input[name="email"]').value;
const password1 = document.querySelector('input[name="password1"]').value;
const password2 = document.querySelector('input[name="password2"]').value;
const errorMessage = document.getElementById('error-message');
// Clear previous error message
errorMessage.innerText = '';
// Check if fields are empty
if (!fullName || !email || !password1 || !password2) {
errorMessage.innerText = 'Please fill all the details.';
return false; // Prevent form submission
}
// Password validation: at least 8 characters, one uppercase, one lowercase, and one special character
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_]).{8,}$/;
if (!passwordRegex.test(password1)) {
errorMessage.innerText = 'Password must be at least 8 characters long, including uppercase, lowercase, and special characters.';
return false; // Prevent form submission
}
// Check if passwords match
if (password1 !== password2) {
errorMessage.innerText = 'Passwords do not match.';
return false; // Prevent form submission
}
return true; // Allow form submission
}
function validateLogin() {
const email = document.querySelector('input[name="email"]').value;
const password1 = document.querySelector('input[name="password1"]').value;
const errorMessage = document.getElementById('error-message');
// Clear previous error message
errorMessage.innerText = '';
// Check if fields are empty
if (!email || !password1) {
errorMessage.innerText = 'Please fill all the details.';
return false; // Prevent form submission
}
return true; // Allow form submission
}
document.addEventListener('DOMContentLoaded', (event) => {
const menuToggle = document.getElementById('menu-toggle');
const navMenu = document.getElementById('nav-menu');
const themeToggle = document.getElementById('theme-toggle');
const body = document.body;
// Menu toggle functionality
if (menuToggle && navMenu) {
menuToggle.addEventListener('click', () => {
navMenu.classList.toggle('show');
});
}
// Theme toggle functionality
if (themeToggle) {
themeToggle.addEventListener('click', () => {
body.classList.toggle('dark-mode');
themeToggle.textContent = body.classList.contains('dark-mode') ? '☀️' : '🌓';
});
}
// FAQ toggle functionality
const faqItems = document.querySelectorAll('.faq-item');
faqItems.forEach(item => {
const question = item.querySelector('h3');
const answer = item.querySelector('p');
if (question && answer) {
// Initially hide the answer
answer.style.display = 'none';
question.addEventListener('click', () => {
answer.style.display = answer.style.display === 'block' ? 'none' : 'block';
});
}
});
// Progress bar functionality
const progressBar = document.getElementById('progress-bar');
if (progressBar) {
window.addEventListener('scroll', () => {
const windowHeight = window.innerHeight;
const documentHeight = document.documentElement.scrollHeight;
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const scrollPercentage = (scrollTop / (documentHeight - windowHeight)) * 100;
progressBar.style.width = scrollPercentage + '%';
});
}
// Image upload functionality
const imageInput = document.getElementById('image-input');
const uploadButton = document.getElementById('upload-button');
const classificationResult = document.getElementById('classification-result');
const disposalInformation = document.getElementById('disposal-information');
if (uploadButton) {
uploadButton.addEventListener('click', () => {
const image = imageInput.files[0];
const formData = new FormData();
formData.append('image', image);
fetch('/classify', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
classificationResult.textContent = data.classification;
disposalInformation.textContent = data.disposalInformation;
})
.catch(error => console.error(error));
});
}
// Feedback form validation
const feedbackForm = document.getElementById('feedback-form');
if (feedbackForm) {
const feedbackName = feedbackForm.querySelector('input[type="text"]');
const feedbackEmail = feedbackForm.querySelector('input[type="email"]');
const feedbackMessage = feedbackForm.querySelector('textarea');
feedbackForm.addEventListener('submit', (event) => {
event.preventDefault(); // Prevent the form from submitting
const name = feedbackName.value.trim();
const email = feedbackEmail.value.trim();
const message = feedbackMessage.value.trim();
let valid = true;
// Clear previous error messages
feedbackName.setCustomValidity('');
feedbackEmail.setCustomValidity('');
// Name validation
if (name.length < 2) {
feedbackName.setCustomValidity('Name must be at least 2 characters long.');
valid = false;
}
// Email validation
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailPattern.test(email)) {
feedbackEmail.setCustomValidity('Please enter a valid email address.');
valid = false;
}
// Message validation
if (message.length < 10) {
alert('Message must be at least 10 characters long.');
valid = false;
}
if (valid) {
alert('Feedback submitted successfully!');
feedbackForm.reset(); // Reset the form after successful submission
} else {
feedbackName.reportValidity();
feedbackEmail.reportValidity();
}
});
}
// Newsletter form validation
const newsletterForm = document.getElementById('newsletter-form');
if (newsletterForm) {
const newsletterEmail = newsletterForm.querySelector('input[type="email"]');
const newsletterErrorMessage = document.getElementById('newsletter-error-message');
newsletterForm.addEventListener('submit', (event) => {
event.preventDefault(); // Prevent the form from submitting
const email = newsletterEmail.value.trim();
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// Clear previous error message
newsletterErrorMessage.textContent = '';
if (!emailPattern.test(email)) {
newsletterErrorMessage.textContent = 'Please enter a valid email address.';
} else {
alert('Subscribed successfully!');
newsletterForm.reset();
}
});
}
// Attach form validation to respective forms
const signupForm = document.getElementById('form1');
if (signupForm) {
signupForm.addEventListener('submit', (event) => {
if (!validateSignup()) {
event.preventDefault(); // Prevent form submission
}
});
}
const loginForm = document.getElementById('form2');
if (loginForm) {
loginForm.addEventListener('submit', (event) => {
if (!validateLogin()) {
event.preventDefault(); // Prevent form submission
}
});
}
});
//Ecotips.html
const tipsSlider = document.querySelector('.eco-tips-content');
const slides = document.querySelectorAll('.eco-tip');
const prevSlide = document.getElementById('prev-slide');
const nextSlide = document.getElementById('next-slide');
let currentIndex = 0;
// Function to update the slider's position
const updateSlider = () => {
const slideWidth = slides[0].clientWidth; // Calculate the width of a single slide
tipsSlider.style.transform = `translateX(-${currentIndex * slideWidth}px)`;
};
// Set the transition for smooth sliding
tipsSlider.style.transition = 'transform 0.3s ease-in-out';
// Handle next slide button click
nextSlide.addEventListener('click', () => {
if (currentIndex < slides.length - 1) {
currentIndex++;
} else {
currentIndex = 0; // Loop back to the first slide
}
updateSlider();
});
// Handle previous slide button click
prevSlide.addEventListener('click', () => {
if (currentIndex > 0) {
currentIndex--;
} else {
currentIndex = slides.length - 1; // Loop back to the last slide
}
updateSlider();
});
// Update slider position on window resize to ensure it adapts to the new slide width
window.addEventListener('resize', () => {
updateSlider();
});
// Make sure the slider position is correct when the page loads
window.addEventListener('load', updateSlider);