-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
788 lines (589 loc) · 20.1 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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
function ChangeView() {
var SignUpBox = document.getElementById("SignUpBox");
var SignInBox = document.getElementById("SignInBox");
SignUpBox.classList.toggle("d-none");
SignInBox.classList.toggle("d-none");
}
function signUp() {
var fn = document.getElementById("fname");
var ln = document.getElementById("lname");
var e = document.getElementById("email");
var pw = document.getElementById("password");
var m = document.getElementById("mobile");
var g = document.getElementById("gender");
var f = new FormData();
f.append("fname", fn.value);
f.append("lname", ln.value);
f.append("email", e.value);
f.append("password", pw.value);
f.append("mobile", m.value);
f.append("gender", g.value);
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.readyState == 4 && r.status == 200) {
var t = r.responseText;
if (t == "success") {
document.getElementById("msg").innerHTML = t;
document.getElementById("msg").className = "alert alert-success";
document.getElementById("msgdiv").className = "d-block";
} else {
document.getElementById("msg").innerHTML = t;
document.getElementById("msgdiv").className = "d-block";
}
}
}
r.open("POST", "signUpProcess.php", true);
r.send(f);
}
function signin() {
var email = document.getElementById("email2");
var password = document.getElementById("password2");
var rememberme = document.getElementById("rememberme");
var f = new FormData();
f.append("e", email.value);
f.append("p", password.value);
f.append("r", rememberme.checked);
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.readyState == 4 && r.status == 200) {
var t = r.responseText;
if (t == "success") {
window.location = "home.php";
} else {
alert(t);
}
}
}
r.open("POST", "signinProcess.php", true);
r.send(f);
}
var bm;
function forgotPassword() {
var email = document.getElementById("email2");
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.readyState == 4 && r.status == 200) {
var t = r.responseText;
if (t == "success") {
var m = document.getElementById("forgotPasswordModal");
bm = new bootstrap.Modal(m);
bm.show();
} else {
alert(t);
}
}
}
r.open("GET", "forgotPasswordProcess.php?e=" + email.value, true);
r.send();
}
function resetPassword() {
var email = document.getElementById("email2");
var np = document.getElementById("np");
var rnp = document.getElementById("rnp");
var vc = document.getElementById("vc");
var f = new FormData();
f.append("e", email.value);
f.append("np", np.value);
f.append("rnp", rnp.value);
f.append("vc", vc.value);
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.readyState == 4 && r.status == 200) {
var t = r.responseText;
if (t == "success") {
bm.hide();
alert("Your password has been updated.");
window.location.reload();
} else {
alert(t);
}
}
}
r.open("POST", "resetPasswordProcess.php", true);
r.send(f);
}
function showPassword() {
var np = document.getElementById("np");
var npb = document.getElementById("npb");
if (np.type == "password") {
np.type = "text";
npb.innerHTML = '<i class="bi bi-eye-slash-fill"></i>';
} else {
np.type = "password";
npb.innerHTML = '<i class="bi bi-eye"></i>';
}
}
function showPassword2() {
var rnp = document.getElementById("rnp");
var rnpb = document.getElementById("rnpb");
if (rnp.type == "password") {
rnp.type = "text";
rnpb.innerHTML = '<i class="bi bi-eye-slash-fill"></i>';
} else {
rnp.type = "password";
rnpb.innerHTML = '<i class="bi bi-eye"></i>';
}
}
function signout() {
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.readyState == 4 && r.status == 200) {
var t = r.responseText;
if (t == "success") {
// window.location.reload();
window.location = "index.php";
} else {
alert(t);
}
}
}
r.open("GET", "signoutProcess.php", true);
r.send();
}
function showPassword3() {
var pw = document.getElementById("pw");
var pwb = document.getElementById("pwb");
if (pw.type == "password") {
pw.type = "text";
pwb.innerHTML = '<i class="bi bi-eye-slash-fill"></i>';
} else {
pw.type = "password";
pwb.innerHTML = '<i class="bi bi-eye-fill"></i>';
}
}
function updateProfile() {
var profile_img = document.getElementById("profileImage");
var first_name = document.getElementById("fname");
var last_name = document.getElementById("lname");
var mobile_no = document.getElementById("mobile");
var password = document.getElementById("pw");
var email_address = document.getElementById("email");
var address_line_1 = document.getElementById("line1");
var address_line_2 = document.getElementById("line2");
var province = document.getElementById("province");
var district = document.getElementById("district");
var city = document.getElementById("city");
var postal_code = document.getElementById("pc");
var f = new FormData();
f.append("img", profile_img.files[0]);
f.append("fn", first_name.value);
f.append("ln", last_name.value);
f.append("mn", mobile_no.value);
f.append("pw", password.value);
f.append("ea", email_address.value);
f.append("al1", address_line_1.value);
f.append("al2", address_line_2.value);
f.append("p", province.value);
f.append("d", district.value);
f.append("c", city.value);
f.append("pc", postal_code.value);
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
if (t == "success") {
signout();
// window.location = "home.php";
} else {
alert(t);
}
}
}
r.open("POST", "userProfileUpdateProcess.php", true);
r.send(f);
}
function loadBrands() {
var category = document.getElementById("category").value;
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
document.getElementById("brand").innerHTML = t;
}
}
r.open("GET", "loadBrandProcess.php?c=" + category, true);
r.send();
}
function changeProductImage() {
var images = document.getElementById("imageuploader");
images.onchange = function () {
var file_count = images.files.length;
if (file_count <= 3) {
for (var x = 0; x < file_count; x++) {
var file = this.files[x];
var url = window.URL.createObjectURL(file);
document.getElementById("i" + x).src = url;
}
} else {
alert(file_count + " files uploaded. You are proceed to upload 03 or less than 03 files.");
}
}
}
function addProduct() {
var category = document.getElementById("category");
var brand = document.getElementById("brand");
var model = document.getElementById("model");
var title = document.getElementById("title");
var condition = 0;
if (document.getElementById("b").checked) {
condition = 1;
} else if (document.getElementById("u").checked) {
condition = 2;
}
var clr = document.getElementById("clr");
var qty = document.getElementById("qty");
var cost = document.getElementById("cost");
var dwc = document.getElementById("dwc");
var doc = document.getElementById("doc");
var desc = document.getElementById("desc");
var image = document.getElementById("imageuploader");
var f = new FormData();
f.append("ca", category.value);
f.append("b", brand.value);
f.append("m", model.value);
f.append("t", title.value);
f.append("con", condition);
f.append("col", clr.value);
f.append("qty", qty.value);
f.append("cost", cost.value);
f.append("dwc", dwc.value);
f.append("doc", doc.value);
f.append("desc", desc.value);
var file_count = image.files.length;
for (var x = 0; x < file_count; x++) {
f.append("img" + x, image.files[x]);
}
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
if (t == "success") {
window.location.reload();
} else {
alert(t);
}
}
}
r.open("POST", "addProductProcess.php", true);
r.send(f);
}
function changeStatus(id) {
var product_id = id;
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
if (t == "activated" || t == "deactivated") {
window.location.reload();
} else {
alert(t);
}
}
}
r.open("GET", "changeStatusProcess.php?p=" + product_id, true);
r.send();
}
function sort(x) {
var search = document.getElementById("s");
var time = "0";
if (document.getElementById("n").checked) {
time = "1";
} else if (document.getElementById("o").checked) {
time = "2";
}
var qty = "0";
if (document.getElementById("h").checked) {
qty = "1";
} else if (document.getElementById("l").checked) {
qty = "2";
}
var condition = "0";
if (document.getElementById("b").checked) {
condition = "1";
} else if (document.getElementById("u").checked) {
condition = "2";
}
var f = new FormData();
f.append("s", search.value);
f.append("t", time);
f.append("q", qty);
f.append("c", condition);
f.append("page", x);
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
document.getElementById("sort").innerHTML = t;
}
}
r.open("POST", "sortProcess.php", true);
r.send(f);
}
function clearSort() {
window.location.reload();
}
function sendId(id) {
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
if (t == "success") {
window.location = "updateProduct.php";
} else {
alert(t);
}
}
}
r.open("GET", "sendIdProcess.php?id=" + id, true);
r.send();
}
function updateProduct() {
var title = document.getElementById("t");
var qty = document.getElementById("q");
var dwc = document.getElementById("dwc");
var doc = document.getElementById("doc");
var description = document.getElementById("d");
var image = document.getElementById("imageuploader");
var f = new FormData();
f.append("t", title.value);
f.append("q", qty.value);
f.append("dwc", dwc.value);
f.append("doc", doc.value);
f.append("d", description.value);
var file_count = image.files.length;
for (var x = 0; x < file_count; x++) {
f.append("i" + x, image.files[x]);
}
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
if (t == "success") {
window.location = "myProducts.php";
} else if (t == "Invalid Image Count") {
if (confirm("Don't you want to update Product Images?") == true) {
window.location = "myProducts.php";
} else {
alert("Select images.");
}
} else {
alert(t);
}
}
}
r.open("POST", "updateProductProcess.php", true);
r.send(f);
}
function basicSearch(x) {
var text = document.getElementById("kw").value;
var select = document.getElementById("c").value;
var f = new FormData();
f.append("t", text);
f.append("s", select);
f.append("page", x);
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
document.getElementById("basicSearchResult").innerHTML = t;
}
}
r.open("POST", "basicSearchProcess.php", true);
r.send(f);
}
function advancedSearch(x) {
var txt = document.getElementById("t");
var category = document.getElementById("c1");
var brand = document.getElementById("b1");
var model = document.getElementById("m");
var condition = document.getElementById("c2");
var color = document.getElementById("c3");
var from = document.getElementById("pf");
var to = document.getElementById("pt");
var sort = document.getElementById("s");
var f = new FormData();
f.append("t", txt.value);
f.append("cat", category.value);
f.append("b", brand.value);
f.append("mo", model.value);
f.append("con", condition.value);
f.append("col", color.value);
f.append("pf", from.value);
f.append("pt", to.value);
f.append("s", sort.value);
f.append("page", x);
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
document.getElementById("view_area").innerHTML = t;
}
}
r.open("POST", "advancedSearchProcess.php", true);
r.send(f);
}
function changeMainImg(id) {
var new_img = document.getElementById("product_img" + id).src;
var main_img = document.getElementById("mainImg");
main_img.style.backgroundImage = "url(" + new_img + ")";
}
function qty_inc(qty) {
var input = document.getElementById("qty_input");
if (input.value < qty) {
var new_value = parseInt(input.value) + 1;
input.value = new_value;
} else {
alert("You have reched to the Maximum");
input.value = qty;
}
}
function qty_dec() {
var input = document.getElementById("qty_input");
if (input.value > 1) {
var new_value = parseInt(input.value) - 1;
input.value = new_value;
} else {
alert("You have reched to the Minimum");
input.value = 1;
}
}
function check_value(qty) {
var input = document.getElementById("qty_input");
if (input.value < 1) {
alert("You must add 1 or more");
input.value = 1;
} else if (input.value > qty) {
alert("Insufficieant quantity");
input.value = qty;
}
}
function addToWatchlist(id) {
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
if (t == "Added") {
alert("Product added to the watchlist successfully.");
window.location.reload();
} else if (t == "Removed") {
alert("Product removed from watchlist successfully.");
window.location.reload();
} else {
alert(t);
}
}
}
r.open("GET", "addWatchListProcess.php?id=" + id, true);
r.send();
}
function removeFromWatchlist(id) {
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
if (t == "Deleted") {
window.location.reload();
} else {
alert(t);
}
}
}
r.open("GET", "removeFromWatchListProcess.php?id=" + id, true);
r.send();
}
function addToCart(id) {
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
if (t == "This Product Already Exists In The Cart") {
alert("This Product Already Exists In The Cart");
} else if (t == "Product Added") {
alert("Product Added");
} else {
alert(t);
}
}
}
r.open("GET", "addToCartProcess.php?id=" + id, true);
r.send();
}
function removeFromCart(id) {
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
if (t == "deleted") {
window.location.reload();
} else {
alert(t);
}
}
}
r.open("GET", "removeFromCartProcess.php?id=" + id, true);
r.send();
}
function paynow(pid) {
var qty = document.getElementById("qty_input").value;
var r = new XMLHttpRequest();
r.onreadystatechange = function () {
if (r.status == 200 && r.readyState == 4) {
var t = r.responseText;
var obj = JSON.parse(t);
var umail = obj["umail"];
var amount = obj["amount"];
if (t == "address error") {
alert("Please Update Your Profile.");
window.location = "userProfile.php";
} else {
// Payment completed. It can be a successful failure.
payhere.onCompleted = function onCompleted(orderId) {
console.log("Payment completed. OrderID:" + orderId);
window.location = "invoice.php";
// Note: validate the payment and show success or failure page to the customer
};
// Payment window closed
payhere.onDismissed = function onDismissed() {
// Note: Prompt user to pay again or show an error page
console.log("Payment dismissed");
};
// Error occurred
payhere.onError = function onError(error) {
// Note: show an error page
console.log("Error:" + error);
};
// Put the payment variables here
var payment = {
"sandbox": true,
"merchant_id": "1224207", // Replace your Merchant ID
"return_url": "http://localhost/mkshop/singleProductView.php?id=" + pid, // Important
"cancel_url": "http://localhost/mkshop/singleProductView.php?id=" + pid, // Important
"notify_url": "http://sample.com/notify",
"order_id": obj["id"],
"items": obj["item"],
"amount": amount,
"currency": "LKR",
"hash": obj["hash"], // *Replace with generated hash retrieved from backend
"first_name": obj["fname"],
"last_name": obj["lname"],
"email": umail,
"phone": obj["mobile"],
"address": obj["address"],
"city": obj["city"],
"country": "Sri Lanka",
"delivery_address": obj["address"],
"delivery_city": obj["city"],
"delivery_country": "Sri Lanka",
"custom_1": "",
"custom_2": ""
};
// Show the payhere.js popup, when "PayHere Pay" is clicked
// document.getElementById('payhere-payment').onclick = function (e) {
payhere.startPayment(payment);
// };
}
}
}
r.open("GET", "payNowProcess.php?id=" + pid + "&qty=" + qty, true);
r.send();
}