-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.js
497 lines (442 loc) · 19 KB
/
order.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
$(document).ready( function() {
var cart = new shopping_cart("jadrn028");
$('#count').text(cart.size());
updateDisplay();
isCartEmpty();
//Deleting Item from Cart
$('.deleteButton').on('click', function() {
var cart = new shopping_cart("jadrn028");
var cartArray = cart.getCartArray();
var index = $(this).closest('td').parent()[0].sectionRowIndex-1;
var sku = cartArray[index][0];
cart.delete(sku);
updateDisplay();
location.reload();
});
//Updating Cart Quantity - first checking whether quantity is legal or not !
$('.updateButton').on('click', function() {
var newQuantity = document.getElementsByName("quantity_field")[$(this).closest('td').parent()[0].sectionRowIndex-1].value.trim();
if (newQuantity == 0 || newQuantity > 999)
{
document.getElementsByName("quantity_field")[0].focus();
document.getElementById("emptyCardBox").style.display = "block";
document.getElementById("emptyCardBox").innerHTML = "Please Enter the Valid Quantity!! Range is 1-999!";
}
else
{
document.getElementById("emptyCardBox").style.display = "none";
var cart = new shopping_cart("jadrn028");
var cartArray = cart.getCartArray();
var index = $(this).closest('td').parent()[0].sectionRowIndex-1;
var sku = cartArray[index][0];
cart.setQuantity(sku, newQuantity);
updateDisplay();
location.reload();
}
});
function isCartEmpty() {
if (cart.size() == 0){
$('.order_button_class').hide();
$('#cart').html("");
$('#count').text(cart.size());
document.getElementById("emptyCardBox").style.display = "block";
document.getElementById("emptyCardBox").innerHTML = "Empty Cart!";
}
else{
$('.order_button_class').show();
// console.log($('#cart').html());
}
}
//Update display of the order screen
function updateDisplay() {
var cartArray = cart.getCartArray();
// console.log(cartArray);
var toWrite = "<table>";
toWrite += "<tr><th>Product</th><th>Quantity</th><th>Action</th><th>Price per qty</th><th>Total Price</th></tr>";
var totalAmount = 0;
for(i=0; i < cartArray.length; i++)
{
var total_price = 0;
toWrite += "<tr>";
toWrite += "<td>"+"<img src=\"/~jadrn000/PROJ4_IMAGES/"+cartArray[i][0]+".jpg\" alt=\""+cartArray[i][0]+"\""+"width=\"120px\" />"+"</td>";
toWrite += "<td class='quantity_td'>"+cartArray[i][1]+"</td>";
toWrite += "<td>"+ "<input type = 'number' min='1' step='1' name='quantity_field' />"+"<input type='button' value='Update quantity' class='updateButton' />"+"<br />"+"<input type='button' value='Remove from Cart' class='deleteButton' />"+"</td>";
toWrite += "<td class='price_id'>"+ "$" +cartArray[i][2]+"</td>";
//calculate price for total quantity and show it in the table
total_price += cartArray[i][2] * cartArray[i][1];
var roundedPrice = (total_price).toFixed(2);
totalAmount = parseFloat(totalAmount) + parseFloat(roundedPrice);
toWrite += "<td class='price_id'>"+ "$" +roundedPrice+"</td>";
toWrite += "</tr>";
}
toWrite += "</table>";
toWrite += "<br /><br />";
toWrite += "<table class='price_table'>";
toWrite += "<tr>";
toWrite +="<td>"+"<h5>"+'Total Order'+"</h5>" +"</td>";
var totalOrderAmount = (totalAmount).toFixed(2);
toWrite += "<td>"+"<h6>"+"$"+totalOrderAmount+"</h6>" +"</td>";
toWrite += "</tr>";
toWrite += "<tr>";
toWrite +="<td>"+"<h5>"+'Total Tax (8%)'+"</h5>" +"</td>";
var totalTax = (totalOrderAmount * 0.08).toFixed(2);
toWrite += "<td>"+"<h6>"+"$"+totalTax+"</h6>" +"</td>";
toWrite += "</tr>";
toWrite += "<tr>";
toWrite +="<td>"+"<h5>"+'Total Shipping Charges ($2 per item)'+"</h5>" +"</td>";
var roundedShipping = (cartArray.length * 2).toFixed(2);
toWrite += "<td>"+"<h6>"+"$"+roundedShipping+"</h6>" +"</td>";
toWrite += "</tr>";
toWrite += "<tr>";
toWrite +="<td>"+"<h5>"+'Final Order Amount'+"</h5>" +"</td>";
var finalOrder = parseFloat(totalOrderAmount) + parseFloat(totalTax) + parseFloat(roundedShipping);
var totalTax = (finalOrder).toFixed(2);
toWrite += "<td>"+"<h6>"+"$"+totalTax+"</h6>" +"</td>";
toWrite += "</tr>";
toWrite += "</table>";
$('#cart').html(toWrite);
$('#count').text(cart.size());
}
//Submit Button of dialog modal
$(':submit').on('click', function(e) {
e.preventDefault();
if (validateForm()){
// alert("validation done");
$('form').serialize();
$('form').submit();
// confirmationLayout();
}
else{
alert("Something went wrong, please try again later");
}
});
//Shipping Address to be same as billing address checkmark
$('#checkbox_address').click(function() {
if($("#checkbox_address").is(':checked')){
addressCheckbox(true);
}
else{
// unchecked
addressCheckbox(false);
}
});
//reset button of dialog modal
$(':reset').on('click', function(e) {
document.getElementById("errorBox").style.display = "none";
});
//adding dashes to phone shipping
$('#phone_s_id').on('blur', function() {
var $this = $(this);
if ($this.val().length != 0) {
$('[name="phone_s"]').val($this.val().substring(0,3) + "-" + $this.val().substring(3,6) + "-" + $this.val().substring(6,10));
}
});
//adding dashes to phone billing
$('#phone_b_id').on('blur', function() {
var $this = $(this);
if ($this.val().length != 0) {
$('[name="phone_b"]').val($this.val().substring(0,3) + "-" + $this.val().substring(3,6) + "-" + $this.val().substring(6,10));
}
});
//Setting up dialog modal
$( "#dialog-modal" ).dialog({
height: 600,
width: 800,
modal: true,
autoOpen: false
});
//order button click action - for opening of dialog modal
$('#order_button').on('click', function($e) {
$("#dialog-modal").dialog('open');
});
});
//Submit button clicked action
//Validating form
function validateForm() {
/* ---------- VALIDATING BILLING ADDRESS ------------- */
//Validating First Name
if (document.getElementsByName("fname_b")[0].value.trim() == "" || document.getElementsByName("fname_b")[0].value.trim().length == 0)
{
document.getElementsByName("fname_b")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the First Name! Minimum 1 letter!";
return false;
}
else
{
document.getElementById("errorBox").style.display = "none";
}
//Validating Last Name
if (document.getElementsByName("lname_b")[0].value.trim() == "" || document.getElementsByName("lname_b")[0].value.trim().length == 0)
{
document.getElementsByName("lname_b")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the Last Name! Minimum 1 letter!";
return false;
}
else
{
document.getElementById("errorBox").style.display = "none";
}
//Validating Address 1
if (document.getElementsByName("address1_b")[0].value.trim() == "" || document.getElementsByName("address1_b")[0].value.trim().length < 2)
{
document.getElementsByName("address1_b")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the Address! Minimum 2 letter!";
return false;
}
else
{
document.getElementById("errorBox").style.display = "none";
}
//Validate State
if (document.getElementsByName("state_b")[0].value == "none") {
document.getElementsByName("state_b")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please select a state!";
return false;
}
//Validating City
if (document.getElementsByName("city_b")[0].value.trim().length < 2)
{
document.getElementsByName("city_b")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please enter a city! Minimum 2 letters!";
return false;
}
//Validate Zip
if (document.getElementsByName("zip_b")[0].value.trim() == "" || document.getElementsByName("zip_b")[0].value.trim().length != 5)
{
document.getElementsByName("zip_b")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the Valid Zip! Exactly 5 digits!";
return false;
}
else
{
var regexName=/^\d{5}$|^\d{5}-\d{4}$/;
if(regexName.test(document.getElementsByName("zip_b")[0].value.trim()))
{
document.getElementById("errorBox").style.display = "none";
}
else
{
document.getElementsByName("zip_b")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the Valid Zip! Only numbers allowed!";
return false;
}
}
//Validate Phone Number
if (document.getElementsByName("phone_b")[0].value.trim() == "" || document.getElementsByName("phone_b")[0].value.trim().length != 12)
{
document.getElementsByName("phone_b")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the valid Phone!";
return false;
}
else
{
var regexName=/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if(regexName.test(document.getElementsByName("phone_b")[0].value.trim()))
{
document.getElementById("errorBox").style.display = "none";
}
else
{
document.getElementsByName("phone_b")[0].value.focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the Valid Phone! Only numbers allowed!";
return false;
}
}
/* ---------- VALIDATING SHIPPING ADDRESS ------------- */
//Validating First Name
if (document.getElementsByName("fname_s")[0].value.trim() == "" || document.getElementsByName("fname_s")[0].value.trim().length == 0)
{
document.getElementsByName("fname_s")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the First Name! Minimum 1 letter!";
return false;
}
else
{
document.getElementById("errorBox").style.display = "none";
}
//Validating Last Name
if (document.getElementsByName("lname_s")[0].value.trim() == "" || document.getElementsByName("lname_s")[0].value.trim().length == 0)
{
document.getElementsByName("lname_s")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the Last Name! Minimum 1 letter!";
return false;
}
else
{
document.getElementById("errorBox").style.display = "none";
}
//Validating Address 1
if (document.getElementsByName("address1_s")[0].value.trim() == "" || document.getElementsByName("address1_s")[0].value.trim().length < 2)
{
document.getElementsByName("address1_s")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the Address! Minimum 2 letter!";
return false;
}
else
{
document.getElementById("errorBox").style.display = "none";
}
//Validate State
if (document.getElementsByName("state_s")[0].value == "none") {
document.getElementsByName("state_s")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please select a state!";
return false;
}
//Validating City
if (document.getElementsByName("city_s")[0].value.trim().length < 2)
{
document.getElementsByName("city_s")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please enter a city! Minimum 2 letters!";
return false;
}
//Validate Zip
if (document.getElementsByName("zip_s")[0].value.trim() == "" || document.getElementsByName("zip_s")[0].value.trim().length != 5)
{
document.getElementsByName("zip_s")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the Valid Zip! Exactly 5 digits!";
return false;
}
else
{
var regexName=/^\d{5}$|^\d{5}-\d{4}$/;
if(regexName.test(document.getElementsByName("zip_s")[0].value.trim()))
{
document.getElementById("errorBox").style.display = "none";
}
else
{
document.getElementsByName("zip_s")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the Valid Zip! Only numbers allowed!";
return false;
}
}
// //Validate Phone Number
if (document.getElementsByName("phone_s")[0].value.trim() == "" || document.getElementsByName("phone_s")[0].value.trim().length != 12)
{
document.getElementsByName("phone_s")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the valid Phone!";
return false;
}
else
{
var regexName=/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if(regexName.test(document.getElementsByName("phone_s")[0].value.trim()))
{
document.getElementById("errorBox").style.display = "none";
}
else
{
document.getElementsByName("phone_s")[0].value.focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the Valid Phone! Only numbers allowed!";
return false;
}
}
/* ---------- VALIDATING PAYMENT DETAILS ------------- */
//Validating Payment Type Radio Button
//Getting value of Payment type Radio Button
var radioPayment = document.getElementsByName('payment_type');
var isPaymentTypeSelected = false;
for (var i = 0, length = radioPayment.length; i < length; i++) {
if (radioPayment[i].checked) {
isPaymentTypeSelected = true;
}
}
if (isPaymentTypeSelected == true) {
document.getElementById("errorBox").style.display = "none";
}
else {
scrollto();
document.getElementsByName('payment_type')[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please select the payment type!";
return false;
}
//Validate Card Number
if (document.getElementsByName("card_number")[0].value.trim() == "" || document.getElementsByName("card_number")[0].value.trim().length < 12)
{
document.getElementsByName("card_number")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the Valid Card Number! It should be more than 12 digits!";
return false;
}
else
{
var regexName = /^[0-9]/;
var resultRegex = regexName.test(document.getElementsByName("card_number")[0].value.trim());
//console.log(resultRegex);
if (resultRegex == true) {
document.getElementById("errorBox").style.display = "none";
}
else{
document.getElementsByName("card_number")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please Enter the Valid Card Number! It should be more than 12 digits!";
return false;
}
}
//Validate Expiration Month
if (document.getElementsByName("month_card")[0].value == "none") {
document.getElementsByName("month_card")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please select a month!";
return false;
}
//Validate Expiration Year
if (document.getElementsByName("year_card")[0].value == "none") {
document.getElementsByName("year_card")[0].focus();
document.getElementById("errorBox").style.display = "block";
document.getElementById("errorBox").innerHTML = "Please select an year!";
return false;
}
return true;
}
//for scrolling to top - div
function scrollto() {
var etop = $('#errorBox').offset().top;
$("#dialog-modal").scrollTop(etop);
}
//For address checkbox - shipping / billing
function addressCheckbox(isChecked){
//Checked
if (isChecked == true){
//Putting all the values of billing to shipping address
$('[name="fname_s"]').val($('[name="fname_b"]').val());
$('[name="lname_s"]').val($('[name="lname_b"]').val());
$('[name="address1_s"]').val($('[name="address1_b"]').val());
$('[name="address2_s"]').val($('[name="address2_b"]').val());
$('[name="state_s"]').val($('[name="state_b"]').val());
$('[name="city_s"]').val($('[name="city_b"]').val());
$('[name="zip_s"]').val($('[name="zip_b"]').val());
$('[name="phone_s"]').val($('[name="phone_b"]').val());
}
//Unchecked
else{
//Clearing all the shipping values
$('[name="fname_s"]').val("");
$('[name="lname_s"]').val("");
$('[name="address1_s"]').val("");
$('[name="address2_s"]').val("");
$('[name="state_s"]').val("CA");
$('[name="city_s"]').val("");
$('[name="zip_s"]').val("");
$('[name="phone_s"]').val("");
}
}