-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
442 lines (362 loc) · 13.1 KB
/
Copy pathscript.js
File metadata and controls
442 lines (362 loc) · 13.1 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
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
// Emperors Multi Step Form JavaScript
// State Management & Form Handling
// Global State
const formState = {
currentStep: 1,
billing: 'monthly',
selectedPlan: null,
addOns: [],
personalInfo: {
fullName: '',
email: '',
phone: ''
}
};
// Pricing Data
const pricing = {
plans: {
basic: { monthly: 9, yearly: 72 },
pro: { monthly: 19, yearly: 152 },
enterprise: { monthly: 39, yearly: 312 }
},
addons: {
'online-service': { monthly: 1, yearly: 10 },
'larger-storage': { monthly: 2, yearly: 20 },
'customizable-profile': { monthly: 2, yearly: 20 }
}
};
// Initialization
document.addEventListener('DOMContentLoaded', () => {
console.log('Emperors Multi Step Form Initialized');
// Set up billing toggle
setupBillingToggle();
// Set up plan selection
setupPlanSelection();
// Set up add-on checkboxes
setupAddonCheckboxes();
// Set up form inputs
setupFormInputs();
// Update progress
updateProgress();
});
// Billing Toggle
function setupBillingToggle() {
const billingInputs = document.querySelectorAll('input[name="billing"]');
billingInputs.forEach(input => {
input.addEventListener('change', (e) => {
formState.billing = e.target.value;
updatePricing();
console.log('Billing updated to:', formState.billing);
});
});
}
function updatePricing() {
const isYearly = formState.billing === 'yearly';
// Update plan prices
document.querySelectorAll('.plan-card').forEach(card => {
const planType = card.dataset.plan;
const priceElement = card.querySelector('.price-amount');
const price = isYearly
? pricing.plans[planType].yearly
: pricing.plans[planType].monthly;
priceElement.textContent = `$${price}`;
// Update period text
const periodElement = card.querySelector('.price-period');
periodElement.textContent = isYearly ? '/yr' : '/mo';
});
// Update add-on prices
document.querySelectorAll('.addon-card').forEach(addon => {
const checkbox = addon.querySelector('input[type="checkbox"]');
const addonValue = checkbox.value;
const priceElement = addon.querySelector('.addon-price');
const price = isYearly
? pricing.addons[addonValue].yearly
: pricing.addons[addonValue].monthly;
priceElement.textContent = isYearly ? `+$${price}/yr` : `+$${price}/mo`;
});
}
// Plan Selection
function setupPlanSelection() {
const planCards = document.querySelectorAll('.plan-card');
planCards.forEach(card => {
const selectBtn = card.querySelector('.plan-select-btn');
selectBtn.addEventListener('click', (e) => {
e.stopPropagation();
// Remove previous selection
planCards.forEach(c => c.classList.remove('selected'));
// Add new selection
card.classList.add('selected');
formState.selectedPlan = card.dataset.plan;
console.log('Plan selected:', formState.selectedPlan);
// Clear error if exists
document.getElementById('step1Error').classList.remove('show');
});
});
}
// Addon Selection
function setupAddonCheckboxes() {
const addonCheckboxes = document.querySelectorAll('.addon-card input[type="checkbox"]');
addonCheckboxes.forEach(checkbox => {
checkbox.addEventListener('change', () => {
updateAddons();
});
});
}
function updateAddons() {
const checkedAddons = document.querySelectorAll('.addon-card input[type="checkbox"]:checked');
formState.addons = Array.from(checkedAddons).map(checkbox => ({
name: checkbox.value,
priceMonthly: parseFloat(checkbox.dataset.priceMonthly),
priceYearly: parseFloat(checkbox.dataset.priceYearly)
}));
console.log('Add-ons updated:', formState.addons);
}
// Form Inputs
function setupFormInputs() {
const nameInput = document.getElementById('fullName');
const emailInput = document.getElementById('email');
const phoneInput = document.getElementById('phone');
nameInput.addEventListener('input', (e) => {
formState.personalInfo.fullName = e.target.value;
clearFieldError('nameError');
e.target.classList.remove('error');
});
emailInput.addEventListener('input', (e) => {
formState.personalInfo.email = e.target.value;
clearFieldError('emailError');
e.target.classList.remove('error');
});
phoneInput.addEventListener('input', (e) => {
formState.personalInfo.phone = e.target.value;
clearFieldError('phoneError');
e.target.classList.remove('error');
});
}
// Validation
function validateStep(step) {
switch(step) {
case 1:
return validateStep1();
case 2:
return true; // No required fields in step 2
case 3:
return validateStep3();
default:
return true;
}
}
function validateStep1() {
if (!formState.selectedPlan) {
showError('step1Error', 'Please select a plan to continue...');
return false;
}
return true;
}
function validateStep3() {
let isValid = true;
// Validate name
if (!formState.personalInfo.fullName.trim()) {
showFieldError('nameError', 'Full Name is required');
document.getElementById('fullName').classList.add('error');
isValid = false;
}
// Validate email
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!formState.personalInfo.email.trim()) {
showFieldError('emailError', 'Email is required');
document.getElementById('email').classList.add('error');
isValid = false;
} else if (!emailPattern.test(formState.personalInfo.email)) {
showFieldError('emailError', 'Please enter a valid email address');
document.getElementById('email').classList.add('error');
isValid = false;
}
// Validate phone
const phonePattern = /^[\+]?[(]?[0-9]{1,4}[)]?[-\s\.]?[(]?[0-9]{1,4}[)]?[-\s\.]?[0-9]{1,9}$/;
if (!formState.personalInfo.phone.trim()) {
showFieldError('phoneError', 'Phone number is required');
document.getElementById('phone').classList.add('error');
isValid = false;
} else if (!phonePattern.test(formState.personalInfo.phone.replace(/\s/g, ''))) {
showFieldError('phoneError', 'Please enter a valid phone number');
document.getElementById('phone').classList.add('error');
isValid = false;
}
return isValid;
}
function showError(elementId, message) {
const errorElement = document.getElementById(elementId);
errorElement.textContent = message;
errorElement.classList.add('show');
}
function showFieldError(elementId, message) {
const errorElement = document.getElementById(elementId);
errorElement.textContent = message;
errorElement.classList.add('show');
}
function clearFieldError(elementId) {
const errorElement = document.getElementById(elementId);
errorElement.classList.remove('show');
}
// Navigation
function nextStep() {
// Validate current step before moving to next
if (!validateStep(formState.currentStep)) {
console.log('Validation failed for step', formState.currentStep);
return;
}
// Move to next step
if (formState.currentStep < 4) {
formState.currentStep++;
showStep(formState.currentStep);
// Update summary if moving to step 4
if (formState.currentStep === 4) {
updateSummary();
}
console.log('Moved to step', formState.currentStep);
}
}
function previousStep() {
if (formState.currentStep > 1) {
formState.currentStep--;
showStep(formState.currentStep);
console.log('Moved back to step', formState.currentStep);
}
}
function goToStep(step) {
formState.currentStep = step;
showStep(step);
console.log('Jumped to step', step);
}
function showStep(step) {
// Hide all steps
document.querySelectorAll('.form-step').forEach(s => {
s.classList.remove('active');
});
// Show current step
const currentStep = document.querySelector('.form-step[data-step="${step}"]');
if (currentStep) {
currentStep.classList.add('active');
}
// Update progress
updateProgress();
// Update step indicators
updateStepIndicators();
// Scroll to top
window.scrollTo({ top: 0, behavior: 'smooth' });
}
function updateProgress() {
const progressFill = document.getElementById('progressFill');
const percentage = (formState.currentStep / 4) * 100;
progressFill.style.width = `${percentage}%`;
}
function updateStepIndicators() {
const indicators = document.querySelectorAll('.step-indicator');
indicators.forEach((indicator, index) => {
const stepNumber = index + 1;
indicator.classList.remove('active', 'completed');
if (stepNumber === formState.currentStep) {
indicator.classList.add('active');
} else if (stepNumber < formState.currentStep) {
indicator.classList.add('completed');
}
});
}
// Summary
function updateSummary() {
const isYearly = formState.billing === 'yearly';
const planName = formState.selectedPlan.charAt(0).toUpperCase() + formState.selectedPlan.slice(1);
const billingText = isYearly ? 'Yearly' : 'Monthly';
// Update plan name
document.getElementById('summaryPlanName').textContent = `${planName} (${billingText})`;
// Update plan price
const planPrice = isYearly
? pricing.plans[formState.selectedPlan].yearly
: pricing.plans[formState.selectedPlan].monthly;
document.getElementById('summaryPlanPrice').textContent = isYearly
? `$${planPrice}/yr`
: `$${planPrice}/mo`;
// Update add-ons
const addonsContainer = document.getElementById('summaryAddons');
addonsContainer.innerHTML = '';
if (formState.addons.length > 0) {
formState.addons.forEach(addon => {
const addonPrice = isYearly ? addon.priceYearly : addon.priceMonthly;
const addonName = addon.name.split('-').map(word =>
word.charAt(0).toUpperCase() + word.slice(1)
).join(' ');
const addonElement = document.createElement('div');
addonElement.className = 'summary-addon';
addonElement.innerHTML = `
<span class="summary-addon-name">${addonName}</span>
<span class="summary-addon-price">+$${addonPrice}/${isYearly ? 'yr' : 'mo'}</span>
`;
addonsContainer.appendChild(addonElement);
});
}
// Calculate total
let total = planPrice;
formState.addons.forEach(addon => {
total += isYearly ? addon.priceYearly : addon.priceMonthly;
});
// Update total
document.getElementById('totalLabel').textContent = isYearly
? 'Total (per year)'
: 'Total (per month)';
document.getElementById('totalAmount').textContent = isYearly
? `$${total}/yr`
: `$${total}/mo`;
}
// Confirmation
function confirmOrder() {
console.log('Order confirmed!');
console.log('Final Order:', formState);
// Show success screen
formState.currentStep = 5;
showStep(5);
}
function resetForm() {
// Reset state
formState.currentStep = 1;
formState.billing = 'monthly';
formState.selectedPlan = null;
formState.addons = [];
formState.personalInfo = {
fullName: '',
email: '',
phone: ''
};
// Reset UI
document.querySelectorAll('.plan-card').forEach(card => {
card.classList.remove('selected');
});
document.querySelectorAll('.addon-card input[type="checkbox"]').forEach(checkbox => {
checkbox.checked = false;
});
document.getElementById('fullName').value = '';
document.getElementById('email').value = '';
document.getElementById('phone').value = '';
document.querySelector('input[name="billing"][value="monthly"]').checked = true;
// Reset pricing
updatePricing();
// Go to step 1
showStep(1);
console.log('Form reset');
}
// Utility Functions
function formatPhoneNumber(value) {
// Auto-format phone number as user types (optional enhancement)
const cleaned = value.replace(/\D/g, '');
const match = cleaned.match(/^(\d{1,3})(\d{0,3})(\d{0,4})$/);
if (match) {
return [match[1], match[2], match[3]].filter(Boolean).join(' ');
}
return value;
}
// Expose functions to global scope for onclick handlers
window.nextStep = nextStep;
window.previousStep = previousStep;
window.goToStep = goToStep;
window.confirmOrder = confirmOrder;
window.resetForm = resetForm;
console.log('Emperor Multi-step form is ready!');