-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
484 lines (386 loc) · 16.6 KB
/
scripts.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
let currentUser = null;
let storedPassword = "password"; // For demo purposes, in real applications use secure methods for storing passwords
document.addEventListener('DOMContentLoaded', () => {
showPage('home');
});
function showPage(pageId) {
const pages = document.querySelectorAll('.page');
pages.forEach(page => {
page.style.display = 'none';
});
document.getElementById(pageId).style.display = 'block';
}
function login() {
const user = document.getElementById('username').value;
const password = document.getElementById('password').value;
if (user && password === storedPassword) {
currentUser = user;
document.getElementById('user').innerText = user;
showPage('welcome');
document.getElementById('menu-login').style.display = 'none';
document.getElementById('menu-register').style.display = 'none';
document.getElementById('menu-logout').style.display = 'block';
document.getElementById('menu-rentalform').style.display = 'block';
} else {
alert('Invalid username or password');
showPage('login-form');
}
}
document.getElementById('register_form').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent the form from submitting
// Fetch input values
var password_register = document.getElementById('password-register').value;
var repeatPassword = document.getElementById('repeatPassword').value;
const password_registered = document.getElementById('password-register').value;
// Check if passwords match
if (password_register !== repeatPassword)
{
alert('Password do not match');
showPage('register-form');
}
else
{
storedPassword = password_registered;
alert('Successfully register');
showPage('login-form');
}
});
function register()
{
var password_register = document.getElementById('password-register').value;
var repeatPassword = document.getElementById('repeatPassword').value;
const password_registered = document.getElementById('password-register').value;
// Check if passwords match
if (password_register !== repeatPassword)
{
alert('Password do not match');
showPage('register-form');
}
else {
storedPassword = password_registered;
alert('Successfully register');
showPage('login-form');
}
}
function logout() {
currentUser = null;
showPage('home');
document.getElementById('menu-login').style.display = 'block';
document.getElementById('menu-register').style.display = 'block';
document.getElementById('menu-logout').style.display = 'none';
document.getElementById('menu-rentalform').style.display = 'none';
}
function changePassword() {
const newPassword = document.getElementById('new-password').value;
if (newPassword) {
storedPassword = newPassword;
alert('Password changed successfully');
showPage('home');
} else {
alert('Please enter a new password');
}
}
/* Home Slideshow */
let slideIndex = 0;
function showSlides() {
let slides = document.getElementsByClassName("slide");
// Hide all slides
for (let i = 0; i < slides.length; i++) {
slides[i].classList.remove("active");
}
// Increment the slide index
slideIndex++;
// If the slide index exceeds the number of slides, reset it to 1
if (slideIndex > slides.length) {
slideIndex = 1;
}
// Display the current slide
slides[slideIndex - 1].classList.add("active");
// Change slide every 2 seconds
setTimeout(showSlides, 2000);
}
// Initialize the slideshow
showSlides();
/* Ours Cars Java */
const Standardimg = [
{ src: 'images/BYD E6 Electric.png', text: 'BYD E6 Electric'},
{ src: 'images/Hyundai Ioniq 5 Electric.png', text: 'Hyundai Ioniq 5 Electric' },
{ src: 'images/Kia EV6 Electric.png', text: 'Kia EV6 Electric' }
];
const Premiumimg = [
{ src: 'images/MG 4 EV.png', text: 'MG 4 EV'},
{ src: 'images/Audi Q4 e-tron Electric.png', text: 'Audi Q4 e-tron Electric' },
{ src: 'images/Mazda MX-30 Electric.png', text: 'Mazda MX-30 Electric' }
];
const Luxuryimg = [
{ src: 'images/Tesla Model 3.png', text: 'Tesla Model 3' },
{ src: 'images/Mercedes-Benz EQS Electric.png', text: 'Mercedes-Benz EQS Electric' },
{ src: 'images/BMW iX3 Electric.png', text: 'BMW iX3 Electric' }
];
let currentIndex = 0;
const StandardImage = document.getElementById('StandardImage');
const Standard_Text = document.getElementById('Standard_Text');
const PremiumImage = document.getElementById('PremiumImage');
const Premium_Text = document.getElementById('Premium_Text');
const LuxuryImage = document.getElementById('LuxuryImage');
const Luxury_Text = document.getElementById('Luxury_Text');
document.getElementById('Standardnext').addEventListener('click', () => {
currentIndex = (currentIndex + 1) % Standardimg.length;
StandardupdateImageText();
});
document.getElementById('Standardprev').addEventListener('click', () => {
currentIndex = (currentIndex - 1 + Standardimg.length) % Standardimg.length;
StandardupdateImageText();
});
document.getElementById('Premiumnext').addEventListener('click', () => {
currentIndex = (currentIndex + 1) % Premiumimg.length;
PremiumupdateImageText();
});
document.getElementById('Premiumprev').addEventListener('click', () => {
currentIndex = (currentIndex - 1 + Premiumimg.length) % Premiumimg.length;
PremiumupdateImageText();
});
document.getElementById('Luxurynext').addEventListener('click', () => {
currentIndex = (currentIndex + 1) % Luxuryimg.length;
LuxuryupdateImageText();
});
document.getElementById('Luxuryprev').addEventListener('click', () => {
currentIndex = (currentIndex - 1 + Luxuryimg.length) % Luxuryimg.length;
LuxuryupdateImageText();
});
function StandardupdateImageText() {
StandardImage.src = Standardimg[currentIndex].src;
Standard_Text.textContent = Standardimg[currentIndex].text;
}
function PremiumupdateImageText() {
PremiumImage.src = Premiumimg[currentIndex].src;
Premium_Text.textContent = Premiumimg[currentIndex].text;
}
function LuxuryupdateImageText() {
LuxuryImage.src = Luxuryimg[currentIndex].src;
Luxury_Text.textContent = Luxuryimg[currentIndex].text;
}
/* Rental Form Java */
document.getElementById('rental_form').addEventListener('submit', function(event) {
event.preventDefault();
var Card_NumberInput = document.getElementById('cardNumber');
var Card_Number_requiredLength = 16;
var CVVInput = document.getElementById('cvv');
var CVV_requiredLength = 3;
var postalcodeInput = document.getElementById('postalcode');
var postalcode_requiredLength = 6;
var phoneInput = document.getElementById('phone');
var phone_requiredLength = 8;
// Validate form
let valid = true;
if (Card_NumberInput.value.length !== Card_Number_requiredLength) {
valid = false;
alert('The Card Number must be exactly ' + Card_Number_requiredLength + ' digits long.');
event.preventDefault(); // Prevent form submission
}
else if (CVVInput.value.length !== CVV_requiredLength) {
valid = false;
alert('The CVV must be exactly ' + CVV_requiredLength + ' digits long.');
event.preventDefault(); // Prevent form submission
}
else if (postalcodeInput.value.length !== postalcode_requiredLength) {
valid = false;
alert('The Postal Code must be exactly ' + postalcode_requiredLength + ' digits long.');
event.preventDefault(); // Prevent form submission
}
else if (phoneInput.value.length !== phone_requiredLength) {
valid = false;
alert('The Phone Number must be exactly ' + phone_requiredLength + ' digits long.');
event.preventDefault(); // Prevent form submission
}
// Show message
if (valid)
{
const RentalNumber = 'REF' + Math.floor(Math.random() * 1000000);
alert (`Rental successful! Your Rental number is ${RentalNumber}.`);
document.getElementById('RentalNumber').innerText = RentalNumber;
showPage('rented')
}
});
/* cardNumber */
document.getElementById('cardNumber').addEventListener('input', function(event) {
var Card_NumberInput = event.target;
var Card_Number = 16;
// Remove non-digit characters
Card_NumberInput.value = Card_NumberInput.value.replace(/\D/g, '');
// Ensure input value does not exceed max length
if (Card_NumberInput.value.length > Card_Number) {
Card_NumberInput.value = Card_NumberInput.value.slice(0, Card_Number);
}
});
/* cvv */
document.getElementById('cvv').addEventListener('input', function(event) {
var CVVInput = event.target;
var CVV = 3;
// Remove non-digit characters
CVVInput.value = CVVInput.value.replace(/\D/g, '');
// Ensure input value does not exceed max length
if (CVVInput.value.length > CVV) {
CVVInput.value = CVVInput.value.slice(0, CVV);
}
});
/* postalcode */
document.getElementById('postalcode').addEventListener('input', function(event) {
var postalcodeInput = event.target;
var postalcode = 6;
// Remove non-digit characters
postalcodeInput.value = postalcodeInput.value.replace(/\D/g, '');
// Ensure input value does not exceed max length
if (postalcodeInput.value.length > postalcode) {
postalcodeInput.value = postalcodeInput.value.slice(0, postalcode);
}
});
/* phone */
document.getElementById('phone').addEventListener('input', function(event) {
var phoneInput = event.target;
var phone = 8;
// Remove non-digit characters
phoneInput.value = phoneInput.value.replace(/\D/g, '');
// Ensure input value does not exceed max length
if (phoneInput.value.length > phone) {
phoneInput.value = phoneInput.value.slice(0, phone);
}
});
/* level */
document.getElementById('level').addEventListener('input', function(event) {
var levelInput = event.target;
var level = 2;
// Remove non-digit characters
levelInput.value = levelInput.value.replace(/\D/g, '');
// Ensure input value does not exceed max length
if (levelInput.value.length > level) {
levelInput.value = levelInput.value.slice(0, level);
}
});
/* unit number */
document.getElementById('unit-number').addEventListener('input', function(event) {
var unitInput = event.target;
var unit = 3;
// Remove non-digit characters
unitInput.value = unitInput.value.replace(/\D/g, '');
// Ensure input value does not exceed max length
if (unitInput.value.length > unit) {
unitInput.value = unitInput.value.slice(0, unit);
}
});
/* Start and End Date */
document.addEventListener('DOMContentLoaded', () => {
const today = new Date();
const startDateInput = document.getElementById('pickup-date');
const endDateInput = document.getElementById('return-date');
// Function to format date to YYYY-MM-DD
function formatDate(date) {
return date.toISOString().split('T')[0];
}
// Set minimum date for start date to today
startDateInput.setAttribute('min', formatDate(today));
endDateInput.setAttribute('min', formatDate(today));
});
const cars = [
{ brand: "BYD E6 Electric", price: 100, inStock: true },
{ brand: "Hyundai Ioniq 5 Electric", price: 100, inStock: false },
{ brand: "Kia EV6 Electric", price: 100, inStock: true },
{ brand: "MG 4 EV", price: 200, inStock: true },
{ brand: "Audi Q4 e-tron Electric", price: 200, inStock: false },
{ brand: "Mazda MX-30 Electric", price: 200, inStock: true },
{ brand: "Tesla Model 3", price: 300, inStock: false },
{ brand: "Mercedes-Benz EQS Electric", price: 300, inStock: true },
{ brand: "BMW iX3 Electric", price: 300, inStock: true }
];
const carSelect = document.getElementById('carSelect');
const priceInput = document.getElementById('price');
const daysInput = document.getElementById('day');
const totalPriceInput = document.getElementById('total');
// Populate dropdown with car brands
cars.forEach((car, index) => {
const option = document.createElement('option');
option.value = index;
option.text = car.brand;
if (!car.inStock) {
option.disabled = true;
option.text += " (Out of Stock)";
}
carSelect.appendChild(option);
});
function updateCarDetails() {
const selectedCar = cars[carSelect.value];
priceInput.value = `$${selectedCar.price}`;
stockInput.value = selectedCar.inStock ? "In Stock" : "Out of Stock";
calculateTotal();
}
function calculateDays() {
const startDate = new Date(document.getElementById('pickup-date').value);
const endDate = new Date(document.getElementById('return-date').value);
if (startDate && endDate && endDate >= startDate) {
const timeDiff = endDate - startDate;
const days = timeDiff / (1000 * 60 * 60 * 24) + 1;
daysInput.value = days;
} else {
daysInput.value = 0;
}
calculateTotal();
}
function calculateTotal(event) {
if (event) event.preventDefault(); // Prevent form submission if called from submit event
const selectedCar = cars[carSelect.value];
const days = parseInt(daysInput.value) || 0;
const total = selectedCar.price * days;
totalPriceInput.value = `$${total}`;
}
function togglePasswordRegister() {
const passwordInput = document.getElementById('password-register');
const toggleIcon = document.querySelector('.toggle-password');
if (passwordInput.type === 'password') {
passwordInput.type = 'text';
toggleIcon.classList.remove('fa-eye');
toggleIcon.classList.add('fa-eye-slash');
} else {
passwordInput.type = 'password';
toggleIcon.classList.remove('fa-eye-slash');
toggleIcon.classList.add('fa-eye');
}
}
function togglePasswordRepeat() {
const passwordInput = document.getElementById('repeatPassword');
const toggleIcon = document.querySelector('.toggle-password');
if (passwordInput.type === 'password') {
passwordInput.type = 'text';
toggleIcon.classList.remove('fa-eye');
toggleIcon.classList.add('fa-eye-slash');
} else {
passwordInput.type = 'password';
toggleIcon.classList.remove('fa-eye-slash');
toggleIcon.classList.add('fa-eye');
}
}
function togglePasswordLogin() {
const passwordInput = document.getElementById('password');
const toggleIcon = document.querySelector('.toggle-password');
if (passwordInput.type === 'password') {
passwordInput.type = 'text';
toggleIcon.classList.remove('fa-eye');
toggleIcon.classList.add('fa-eye-slash');
} else {
passwordInput.type = 'password';
toggleIcon.classList.remove('fa-eye-slash');
toggleIcon.classList.add('fa-eye');
}
}
function togglePasswordReset() {
const passwordInput = document.getElementById('new-password');
const toggleIcon = document.querySelector('.toggle-password');
if (passwordInput.type === 'password') {
passwordInput.type = 'text';
toggleIcon.classList.remove('fa-eye');
toggleIcon.classList.add('fa-eye-slash');
} else {
passwordInput.type = 'password';
toggleIcon.classList.remove('fa-eye-slash');
toggleIcon.classList.add('fa-eye');
}
}