-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddToCart.js
346 lines (306 loc) · 9.35 KB
/
addToCart.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
// Global arrays
var quantities = [];
var prices = [];
var names = [];
var sums = [];
function addToCart1(qty1)
{
// Adds item quantity to an array
quantities.push(qty1);
// Gets item price and name from web page
var itemPrice = document.getElementById("itemPrice1").innerHTML;
var itemName = document.getElementById("itemName1").innerHTML;
var itemTotal = itemPrice * qty1;
// Checks if there is something added to quantity array and if yes - adds item price and name to price and name arrays
if (quantities.length != 0 && quantities[0] != 0)
{
prices.push(itemPrice);
names.push(itemName);
sums.push(itemTotal);
}
// If quantities array is empty or has zero on index 0 - remove index 0 and alert the customer
else
{
quantities.shift();
alert("Please choose desired quantity!");
}
}
function addToCart2(qty2)
{
// Adds item quantity to an array
quantities.push(qty2);
// Gets item price and name from web page
var itemPrice = document.getElementById("itemPrice2").innerHTML;
var itemName = document.getElementById("itemName2").innerHTML;
var itemTotal = itemPrice * qty2;
// Checks if there is something added to quantity array and if yes - adds item price and name to price and name arrays
if (quantities.length != 0 && quantities[0] != 0)
{
prices.push(itemPrice);
names.push(itemName);
sums.push(itemTotal);
}
// If quantities array is empty or has zero on index 0 - remove index 0 and alert the customer
else
{
quantities.shift();
alert("Please choose desired quantity!");
}
}
function addToCart3(qty3)
{
// Adds item quantity to an array
quantities.push(qty3);
// Gets item price and name from web page
var itemPrice = document.getElementById("itemPrice3").innerHTML;
var itemName = document.getElementById("itemName3").innerHTML;
var itemTotal = itemPrice * qty3;
// Checks if there is something added to quantity array and if yes - adds item price and name to price and name arrays
if (quantities.length != 0 && quantities[0] != 0)
{
prices.push(itemPrice);
names.push(itemName);
sums.push(itemTotal);
}
// If quantities array is empty or has zero on index 0 - remove index 0 and alert the customer
else
{
quantities.shift();
alert("Please choose desired quantity!");
}
}
function addToCart4(qty4)
{
// Adds item quantity to an array
quantities.push(qty4);
// Gets item price and name from web page
var itemPrice = document.getElementById("itemPrice4").innerHTML;
var itemName = document.getElementById("itemName4").innerHTML;
var itemTotal = itemPrice * qty4;
// Checks if there is something added to quantity array and if yes - adds item price and name to price and name arrays
if (quantities.length != 0 && quantities[0] != 0)
{
prices.push(itemPrice);
names.push(itemName);
sums.push(itemTotal);
}
// If quantities array is empty or has zero on index 0 - remove index 0 and alert the customer
else
{
quantities.shift();
alert("Please choose desired quantity!");
}
}
function addToCart5(qty5)
{
// Adds item quantity to an array
quantities.push(qty5);
// Gets item price and name from web page
var itemPrice = document.getElementById("itemPrice5").innerHTML;
var itemName = document.getElementById("itemName5").innerHTML;
var itemTotal = itemPrice * qty5;
// Checks if there is something added to quantity array and if yes - adds item price and name to price and name arrays
if (quantities.length != 0 && quantities[0] != 0)
{
prices.push(itemPrice);
names.push(itemName);
sums.push(itemTotal);
}
// If quantities array is empty or has zero on index 0 - remove index 0 and alert the customer
else
{
quantities.shift();
alert("Please choose desired quantity!");
}
}
function addToCart6(qty6)
{
// Adds item quantity to an array
quantities.push(qty6);
// Gets item price and name from web page
var itemPrice = document.getElementById("itemPrice6").innerHTML;
var itemName = document.getElementById("itemName6").innerHTML;
var itemTotal = itemPrice * qty6;
// Checks if there is something added to quantity array and if yes - adds item price and name to price and name arrays
if (quantities.length != 0 && quantities[0] != 0)
{
prices.push(itemPrice);
names.push(itemName);
sums.push(itemTotal);
}
// If quantities array is empty or has zero on index 0 - remove index 0 and alert the customer
else
{
quantities.shift();
alert("Please choose desired quantity!");
}
}
function showCart() {
// Declare variables and create the header, footer, and caption.
var eTable = document.createElement("table");
var eThead = document.createElement("thead");
var eTbody = document.createElement("tbody");
var eTfoot = document.createElement("tfoot");
var eRow, eCell;
var i, j;
var sumTotal = 0.0;
var sumTemp = 0.0;
for(i in sums)
{
sumTemp += parseFloat(sums[i]);
sumTotal = sumTemp.toFixed(2);
}
// Declare stock data that would normally be read in from a stock Web site.
var heading = ["Name","Price","Qty","Total"];
// Insert the created elements into oTable.
eTable.appendChild(eThead);
eTable.appendChild(eTbody);
eTable.appendChild(eTfoot);
// Insert a row into the header and set its background color.
eRow = document.createElement("tr");
eThead.appendChild(eRow);
// Create and insert cells into the header row.
for (i=0; i<heading.length; i++)
{
eCell = document.createElement("th");
eCell.innerHTML = heading[i];
eRow.appendChild(eCell);
}
// Insert rows and cells into bodies.
for (i=0; i<quantities.length; i++)
{
eRow = document.createElement("tr");
eTbody.appendChild(eRow);
for (j=0; j<1; j++)
{
eCell = document.createElement("td");
eCell.innerHTML = names[i];
eRow.appendChild(eCell);
}
for (j=0; j<1; j++)
{
eCell = document.createElement("td");
eCell.innerHTML = "\u00A3" + prices[i];
eRow.appendChild(eCell);
}
for (j=0; j<1; j++)
{
eCell = document.createElement("td");
eCell.innerHTML = quantities[i];
eRow.appendChild(eCell);
}
for (j=0; j<1; j++)
{
eCell = document.createElement("td");
eCell.innerHTML = "\u00A3" + (sums[i]).toFixed(2);
eRow.appendChild(eCell);
}
}
// Create and insert rows and cells into the footer row.
eRow = document.createElement("tr");
eTfoot.appendChild(eRow);
eCell = document.createElement("td");
eRow.appendChild(eCell);
eCell.colSpan = "4";
eCell.innerHTML = "Total: \u00A3" + sumTotal;
// Insert the table into the document tree.
document.getElementById("itemsInShoppingCart").appendChild(eTable);
// Add optional table attributes
eTable.setAttribute("id", "shoppingCartTable");
eThead.setAttribute("id", "shoppingCartTableHeading");
eTfoot.setAttribute("id", "shoppingCartTableFooter");
// Calls the functions to style up the table
stripedTable();
highlightRows();
}
// Function to stripe the table
function stripedTable(){
if (!document.getElementsByTagName)
{
return false;
}
var tables = document.getElementsByTagName("tbody");
for (var i=0; i<tables.length; i++)
{
var odd=false;
var rows=tables[i].getElementsByTagName("tr");
for (var j=0; j<rows.length; j++)
{
if (odd==true)
{
rows[j].setAttribute("class", "shoppingCartTableRowOdd");
odd=false;
}
else
{
odd=true;
}
}
}
}
// Function to highlight rows
function highlightRows(){
if (!document.getElementsByTagName)
{
return false;
}
var rows=document.getElementsByTagName("tr");
for ( var i=0; i<rows.length; i++)
{
rows[i].onmouseover=function()
{
this.style.fontWeight="bold";
this.style.fontSize="large";
}
rows[i].onmouseout=function()
{
this.style.fontWeight="normal";
this.style.fontSize="medium";
}
}
}
// Validation funtions
function validateTheFormKeyUp(){ // validates each character on input
// Get fomr inputs
var phone = document.validationForm.userPhone.value;
var dot = phone.lastIndexOf(".");
// Loop through each character in input field
for (var i=0; i<phone.length; i++)
{
// Phone validation - If Not a Number - show warning
if (isNaN(phone) || phone < 0 || phone.length > 14 || phone != parseInt(phone,10) || dot >= 0)
{
document.getElementById("invalid-data").innerHTML = "<small>Please enter a valid phone number</small>";
}
// Else - accept the input
else
{
document.getElementById("invalid-data").innerHTML = "<small></small>";
}
}
}
function validateTheForm(){ // validates whole form when sent
// Get form inputs
var phone = document.validationForm.userPhone.value;
var email = document.validationForm.userEmail.value;
var firstName = document.validationForm.userName.value;
var surname = document.validationForm.userSurname.value;
var dotPhone = phone.lastIndexOf(".");
var dotEmail = email.lastIndexOf(".");
var eta = email.indexOf("@");
// Phone validation - If Not a Number / if there is nothing entered / if there is more than 14 characters / if there is decimal delimiter between numbers / if there is a dot among the numbers - show warning
if (isNaN(phone) || phone < 0 || phone.length > 14 || phone != parseInt(phone,10) || dotPhone >= 0)
{
alert("Please enter a valid phone number!");
}
// Email validation - if @ is first character / if dot goes before @ / if there is less than 2 characters after dot - show alert
else if (eta < 1 || dotEmail < eta || dotEmail+2 >= email.length)
{
alert("Please enter a valid email address!");
}
// Else - accept the input
else
{
alert("Thank you, "+firstName+"! Your form has been submitted!");
}
}