-
Notifications
You must be signed in to change notification settings - Fork 0
/
script2.js
558 lines (436 loc) · 21.3 KB
/
script2.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
$(document).ready(function(){
// input group
$('.form-otp').find('input').each(function() {
$(this).on('keyup', function(e) {
var parent = $($(this).parent());
if (e.keyCode === 8 || e.keyCode === 37) {
var prev = parent.find('input#' + $(this).data('previous'));
if (prev.length) {
$(prev).select();
}
} else if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 96 && e.keyCode <= 105) || e.keyCode === 39) {
var next = parent.find('input#' + $(this).data('next'));
if (next.length) {
$(next).select();
}
}
var counter = 0;
$('.form-otp').find('input').each(function() {
if ($(this).val() == '') {
counter++;
} else {
}
});
if (counter > 0) {
$('.js-otp-confirm').addClass('disabled');
} else {
$('.js-otp-confirm').removeClass('disabled');
}
});
});
// fin input group
$("#show-order").click(function(){
var div_order = document.getElementById("div-order");
var styles = window.getComputedStyle(div_order);
var display = styles.getPropertyValue('display');
console.log(display);
if(display=='none'){
$("#div-order").css("display", "block");
// $("#div-order").addClass('d-block');
}
if(display=='block'){
$("#div-order").css("display", "none");
// $("#div-order").addClass('d-block');
}
let title_show_order=$("#title-show-order");
if(title_show_order.html()==='Hide order summary'){
title_show_order.html('');
title_show_order.html('Show order summary');
$("#div-order").removeClass('d-block');
$("#div-order").addClass('d-none');
}
else {
$("#div-order").removeClass('d-none');
$("#div-order").addClass('d-block');
title_show_order.html('');
title_show_order.html('Hide order summary');
}
let icon_up_down = $("#down-path").attr('d');
if(icon_up_down=='M832 352H192l320 320 320-320z'){
$("#down-path").attr("d","M192 672h640L512 352 192 672z");
}
if(icon_up_down=='M192 672h640L512 352 192 672z'){
$("#down-path").attr("d","M832 352H192l320 320 320-320z");
}
// $("#div-order").toggle(50,"swing");
});
// billing-select
let billing_select=$("#billing-select");
billing_select.change(function(){
// alert($(this).val())
if($(this).val()==0){
$("#billing-adress-infos").removeClass("d-none");
//first-name
let errorContainerFname = document.getElementById('error-container-fname')
let errorContainerLname = document.getElementById('error-container-lname')
let errorContainerApartement = document.getElementById('error-container-apartement')
let errorContainerAdress = document.getElementById('error-container-adress')
let errorContainerPostal = document.getElementById('error-container-postal')
let errorContainerCity = document.getElementById('error-container-city')
fname.addEventListener('focus', function(){
document.getElementById('fname-div').style.border = '2px solid rgb(17,17,17)';
});
fname.addEventListener('blur', function(e){
if(e.target.value!="") {
e.target.nextElementSibling.classList.remove('danger-color');
document.getElementById('fname-div').classList.remove('empty-field')
document.querySelector('#email-error-fname').remove();
}
else {
let errorMsg = "<span class='error-text danger-color' id='email-error-fname'>First name required</span>"
e.target.nextElementSibling.classList.add('danger-color');
document.getElementById('fname-div').classList.add('empty-field','mb-4')
// document.getElementById('names-container').classList.add('mb-4')
if(!document.getElementById('email-error-fname')) errorContainerFname.insertAdjacentHTML("beforeend",errorMsg)
}
});
//last-name
lname.addEventListener('focus', function(){
document.getElementById('lname-div').style.border = '2px solid rgb(17,17,17)';
});
lname.addEventListener('blur', function(e){
if(e.target.value!="") {
e.target.nextElementSibling.classList.remove('danger-color');
document.getElementById('lname-div').classList.remove('empty-field')
if(document.querySelector('#email-error-lname'))document.querySelector('.error-text').remove();
}
else {
let errorMsg = "<p class='error-text danger-color' id='email-error-lname'>First name required</p>"
e.target.nextElementSibling.classList.add('danger-color');
document.getElementById('lname-div').classList.add('empty-field','mb-4')
// document.getElementById('names-container').classList.add('mb-4')
if(!document.getElementById('email-error-lname')) errorContainerLname.insertAdjacentHTML("beforeend",errorMsg)
}
});
//adress
adress.addEventListener('focus', function(){
document.getElementById('adress-div').style.border = '2px solid rgb(17,17,17)';
});
adress.addEventListener('blur', function(e){
if(e.target.value!="") {
e.target.nextElementSibling.classList.remove('danger-color');
document.getElementById('adress-div').classList.remove('empty-field')
document.querySelector('#email-error-adress').remove();
}
else {
let errorMsg = "<p class='error-text danger-color' id='email-error-adress'>Address required</p>"
e.target.nextElementSibling.classList.add('danger-color');
document.getElementById('adress-div').classList.add('empty-field','mb-4')
// document.getElementById('names-container').classList.add('mb-4')
if(!document.getElementById('email-error-adress')) errorContainerAdress.insertAdjacentHTML("beforeend",errorMsg)
}
});
//postal
var zipRegex = /^\d{5}(-\d{4})?$/;
postal.addEventListener('focus', function(){
document.getElementById('postal-div').style.border = '2px solid rgb(17,17,17)';
});
postal.addEventListener('blur', function(e){
if(e.target.value!="") {
e.target.nextElementSibling.classList.remove('danger-color');
document.getElementById('postal-div').classList.remove('empty-field','mb-4')
if(document.querySelector('#email-error-postal')) document.querySelector('#email-error-postal').remove();
if(!zipRegex.test(e.target.value)) {
let errorMsg = "<span class='error-text danger-color' id='email-error-postal'>Invalid Zip code</span>"
e.target.nextElementSibling.classList.add('danger-color');
document.getElementById('postal-div').classList.add('empty-field','mb-4','fs-5')
if(!document.getElementById('email-error-postal')) errorContainerPostal.insertAdjacentHTML("beforeend",errorMsg)
}
}
else {
let errorMsg = "<span class='error-text danger-color' id='email-error-postal'>Zip code required</span>"
document.getElementById('postal-div').classList.remove('empty-field','mb-4')
e.target.nextElementSibling.classList.add('danger-color');
document.getElementById('postal-div').classList.add('empty-field','mb-4')
if(!document.getElementById('email-error-postal')) errorContainerPostal.insertAdjacentHTML("beforeend",errorMsg)
else{
errorContainerPostal.innerHTML=''
errorContainerPostal.insertAdjacentHTML("beforeend",errorMsg)
}
}
});
//city
city.addEventListener('focus', function(){
document.getElementById('city-div').style.border = '2px solid rgb(17,17,17)';
});
city.addEventListener('blur', function(e){
if(e.target.value!="") {
e.target.nextElementSibling.classList.remove('danger-color');
document.getElementById('city-div').classList.remove('empty-field')
document.querySelector('#email-error-city').remove();
}
else {
let errorMsg = "<p class='error-text danger-color' id='email-error-city'>City / town required</p>"
e.target.nextElementSibling.classList.add('danger-color');
document.getElementById('city-div').classList.add('empty-field','mb-4')
if(!document.getElementById('email-error-city')) errorContainerCity.insertAdjacentHTML("beforeend",errorMsg)
}
});
}else{
$("#billing-adress-infos").addClass("d-none");
}
});
// verification input code receive
$('.input-group-lg').keyup(function(){
if(
($('#digit-1').val()!='')
&& ($('#digit-2').val()!='')
&&($('#digit-3').val()!='')
&&($('#digit-4').val()!='')
&&($('#digit-5').val()!='')
&&($('#digit-6').val()!='')
){
$('#done').prop('disabled',false);
$('#paiment-2').prop('disabled',false);
}else{
$('#done').prop('disabled',true);
$('paiment-2').prop('disabled',true);
}
})
// verification input CARD paiment
$('.input-group-card').keyup(function(){
if(
($('#credit-card').val()!='')
&& ($('#expiration').val()!='')
&&($('#security').val()!='')
&&($('#card-holder-fname').val()!='')
&&($('#card-holder-lname').val()!='')
){
$('#paiment-1').prop('disabled',false);
}else{
$('paiment-1').prop('disabled',true);
}
})
// btn continue
let btnContinueToshipping=$(".btn-to-shipping");
// let btnToReturn=$(".btn-to-return");
// var btnContinue=0;
btnContinueToshipping.click(function(){
});
});
// localstorage
let titleDom= document.querySelector("#title-product");
let imgDom= document.querySelector("#img-product");
let priceDom= document.getElementsByClassName("price-product");
titleDom.innerHTML =localStorage.getItem("title");
for(let i=0;i<priceDom.length;i++){
priceDom[i].innerHTML ="<span>$</span>"+localStorage.getItem("price");
}
imgDom.setAttribute("src",localStorage.getItem("imgelink"));
var current_media_query = "";
getCurrentMediaQuery();
function getCurrentMediaQuery() {
if (window.matchMedia("(min-width: 992px)").matches) {
current_media_query = "desktop";
console.log(current_media_query);
} else {
current_media_query = "mobile";
$("#div-order").addClass('d-none');
$("#title-show-order").html('Show order summary');
console.log(current_media_query);
}
return current_media_query;
}
$(window).on("resize", function() {
getCurrentMediaQuery();
});
// const mediaQuery = window.matchMedia("(max-width: 991px)");
// mediaQuery.addEventListener("change", (e) => {
// var div_order = document.getElementById("div-order");
// var styles = window.getComputedStyle(div_order);
// var display = styles.getPropertyValue('display');
// console.log(display);
// if(display=='block'){
// document.getElementById("div-order").style.display='none';
// }
// });
//get inputs in localstorage
let PaymentAddressContact =document.getElementById('PaymentAddressContact');
PaymentAddressContact.innerHTML=localStorage.getItem("email");
let PaymentAddressShipToContent =document.getElementById('PaymentAddressShipToContent');
PaymentAddressShipToContent.innerHTML=localStorage.getItem("adress")+','+localStorage.getItem("city")+','+localStorage.getItem("postal")+','+localStorage.getItem("region")+','+'United States';
//credit card number
let creditCardContainer = document.getElementById('credit-card-div')
let creditCard = document.getElementById('credit-card')
document.querySelector("#credit-card").addEventListener("keypress", function (evt) {
const input = evt.target.value.split(' ').join('')
if (evt.which < 48 || evt.which > 57)
{
evt.preventDefault();
}
if(input.length>=19) evt.preventDefault();
});
document.getElementById('credit-card').addEventListener('input',function(e){
e.target.value = e.target.value.replace(/[^\d]/g, "");
e.target.value = e.target.value.replace(/(\d{4})/g, "$1 ").trim();
})
//expiration date
document.querySelector("#expiration").addEventListener("keypress", function (evt) {
let input = evt.target.value
if (evt.which < 48 || evt.which > 57)
{
evt.preventDefault();
}
if(input.length>=7) evt.preventDefault();
});
document.getElementById('expiration').addEventListener("keydown", function(event) {
setTimeout(()=>{
const value = event.target.value;
if (value.length === 2 ) {
event.target.value = `${value}/`;
}
},500)
});
document.getElementById('expiration').addEventListener("keyup", function(event) {
const value = event.target.value;
if (value.length === 2 ) {
event.target.value = `${value}/`;
}
});
var cardRegex = /^(?:(4[0-9]{12}(?:[0-9]{3})?)|(5[1-5][0-9]{14})|(6(?:011|5[0-9][0-9])[0-9]{12})|(3[47][0-9]{13})|(3(?:0[0-5]|[68][0-9])[0-9]{11})|((?:2131|1800|35\d{3})\d{11}))$/;
creditCard.addEventListener('focus', function(e){
// alert('focused')
});
creditCard.addEventListener('blur', function(e){
// alert('worked')
const input = e.target.value.split(' ').join('')
if(input!="") {
console.log(e.target.value.split(' ').join(''))
e.target.nextElementSibling.classList.remove('danger-color');
document.getElementById('credit-card-div').classList.remove('empty-field')
if(document.getElementById('email-error-credit-card'))document.getElementById('email-error-credit-card').remove();
if(!cardRegex.test(input)) {
// alert('incorrect card regex')
let errorMsg = "<p class='error-text danger-color' id='email-error-credit-card'>Invalid credit card number. </p>"
e.target.nextElementSibling.classList.add('danger-color');
document.getElementById('credit-card-div').classList.add('empty-field', 'mb-0')
if(!document.getElementById('email-error-credit-card')) document.querySelector('#credit-card-error').insertAdjacentHTML("beforeend",errorMsg)
}
// else alert('correct card regex')
}
else {
let errorMsg = "<p class='error-text danger-color' id='email-error-credit-card'>Card number required</p>"
e.target.nextElementSibling.classList.add('danger-color');
document.getElementById('credit-card-div').classList.add('empty-field')
// document.getElementById('names-container').classList.add('mb-4')
if(!document.getElementById('email-error-credit-card')) document.querySelector('#credit-card-error').insertAdjacentHTML("beforeend",errorMsg)
else{
document.querySelector('#credit-card-error').innerHTML=''
document.querySelector('#credit-card-error').insertAdjacentHTML("beforeend",errorMsg)
}
}
});
//coupon
coupon.addEventListener('focus', function(e){
document.getElementById('coupon-div').style.border = '2px solid rgb(129, 104, 66)';
});
coupon.addEventListener('blur', function(e){
if(e.target.value!="") {
e.target.nextElementSibling.classList.remove('danger-color');
document.getElementById('coupon-div').classList.remove('empty-field')
document.querySelector('#error-coupon').remove();
}
else {
let errorMsg = "<p class='error-text danger-color' id='error-coupon'>empty field</p>"
e.target.nextElementSibling.classList.add('danger-color');
document.getElementById('coupon-div').classList.add('empty-field')
if(!e.target.nextElementSibling.nextElementSibling) e.target.parentNode.insertAdjacentHTML("beforeend",errorMsg)
}
});
//expiration date
document.getElementById('expiration').addEventListener('blur', function(e){
if(e.target.value!="") {
e.target.nextElementSibling.classList.remove('danger-color');
document.getElementById('expiration-div').classList.remove('empty-field')
if(document.querySelector('#email-error-expiration')) document.querySelector('#email-error-expiration').remove();
}
else {
let errorMsg = "<p class='error-text danger-color' id='email-error-expiration'>Expiration date required</p>"
e.target.nextElementSibling.classList.add('danger-color');
document.getElementById('expiration-div').classList.add('empty-field')
document.getElementById('expiration-div').classList.add('mb-3')
if(!document.getElementById('email-error-expiration')) document.getElementById('error-container-expiration').insertAdjacentHTML("beforeend",errorMsg)
}
});
//security
document.querySelector("#security").addEventListener("keypress", function (evt) {
const input = evt.target.value.split(' ').join('')
if (evt.which < 48 || evt.which > 57)
{
evt.preventDefault();
}
if(input.length>=4) evt.preventDefault();
});
document.getElementById('security').addEventListener('blur', function(e){
if(e.target.value!="") {
e.target.nextElementSibling.classList.remove('danger-color');
document.getElementById('security-div').classList.remove('empty-field')
if( document.querySelector('#email-error-security')) document.querySelector('#email-error-security').remove();
}
else {
let errorMsg = "<p class='error-text danger-color' id='email-error-security'>security date required</p>"
e.target.nextElementSibling.classList.add('danger-color');
document.getElementById('security-div').classList.add('empty-field')
document.getElementById('security-div').classList.add('mb-3')
if(!document.getElementById('email-error-security')) document.getElementById('error-container-security').insertAdjacentHTML("beforeend",errorMsg)
}
});
//card-holder fname
document.getElementById('card-holder-fname').addEventListener('blur', function(e){
if(e.target.value!="") {
e.target.nextElementSibling.classList.remove('danger-color');
document.getElementById('card_holder_fname-div').classList.remove('empty-field')
document.querySelector('#email-error-card-holder-fname').remove();
}
else {
let errorMsg = "<p class='error-text danger-color' id='email-error-card-holder-fname'>card holder name required</p>"
e.target.nextElementSibling.classList.add('danger-color');
document.getElementById('card_holder_fname-div').classList.add('empty-field')
document.getElementById('card_holder_fname-div').classList.add('mb-3')
if(!document.getElementById('email-error-card-holder-fname')) document.getElementById('error-container-card-holder-fname').insertAdjacentHTML("beforeend",errorMsg)
}
});
// ----------------------------------------------------------
let errorContainerEmail = document.getElementById('error-container-email')
let errorContainerFname = document.getElementById('error-container-fname')
let errorContainerLname = document.getElementById('error-container-lname')
let errorContainerApartement = document.getElementById('error-container-apartement')
let errorContainerAdress = document.getElementById('error-container-adress')
let errorContainerPostal = document.getElementById('error-container-postal')
let errorContainerCity = document.getElementById('error-container-city')
let errorContainerPhone = document.getElementById('error-container-phone')
//responsive credit card
function myFunction(x) {
if (x.matches) {
document.getElementById('card-holder-container').classList.add('flex-wrap')
document.getElementById('expiration-security-container').classList.add('flex-wrap')
document.getElementById('expiration-div').classList.add('w-100')
document.getElementById('expiration-div').classList.remove('w-50')
document.getElementById('security-div').classList.add('w-100')
document.getElementById('security-div').classList.remove('w-50')
// document.getElementById('card_holder_fname-div').classList.add('w-100')
// document.getElementById('card_holder_fname-div').classList.remove('w-50')
// document.getElementById('card_holder_Last_Name-div').classList.add('w-100')
// document.getElementById('card_holder_Last_Name-div').classList.remove('w-50')
} else {
document.getElementById('expiration-div').classList.remove('w-100')
document.getElementById('expiration-div').classList.add('w-50')
document.getElementById('security-div').classList.remove('w-100')
document.getElementById('security-div').classList.add('w-50')
document.getElementById('card-holder-container').classList.remove('flex-wrap')
document.getElementById('expiration-security-container').classList.remove('flex-wrap')
}
}
var x = window.matchMedia("(max-width: 700px)")
myFunction(x)
x.addListener(myFunction)