-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathourJS.js
335 lines (289 loc) · 9.25 KB
/
ourJS.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
// calculations.js
var p1 = document.getElementById("p1");
var p2 = document.getElementById("p2");
var p3 = document.getElementById("p3");
var p4 = document.getElementById("p4");
var products = [p1, p2, p3, p4];
function displayProducts() {
for(var i = 0; i < products.length; i++) {
var x = products[i].innerText;
}
document.getElementById("orderSummary").innerHTML = "hi" + x.toString();
}
function calcPrice() {
var priceOfProduct = 0;
var unitPrice = 0;
var totalPrice = 0;
var numUnits = document.getElementsByName("units").value;
var productsNotSelected = 0;
for(var i = 0; i < products.length; i++) {
if(products[i].selected === true) {
priceOfProduct = parseInt(products[i].value);
unitPrice += priceOfProduct;
totalPrice += (priceOfProduct * parseInt(numUnits));
}
else
productsNotSelected++;
}
if(productsNotSelected === 4) {
alert("Please select at least one product.");
return false;
}
if(numUnits === "") {
alert("Please enter a number of units.");
return false;
}
document.getElementById("unit_price").innerHTML =
"Unit Price: $" + unitPrice;
document.getElementById("total_price").innerHTML =
"Total Price: $" + totalPrice;
}
function checkoutPrice() {
const TAX_AMOUNT = .08;
const SHIPPING_FEE = .03;
var shoppingAmt = Math.floor((Math.random() * 50) + 5); // test for now
var fixedAmt = shoppingAmt.toFixed(2);
var tax = shoppingAmt * TAX_AMOUNT;
var fixedTax = tax.toFixed(2);
var shippingCharges = shoppingAmt * SHIPPING_FEE;
var fixedShipping = shippingCharges.toFixed(2);
var grandTotal = shoppingAmt + tax + shippingCharges;
var fixedGrandTotal = grandTotal.toFixed(2);
document.getElementById("shoppingAmt").innerHTML = "$" + fixedAmt;
document.getElementById("taxAmt").innerHTML = "$" + fixedTax;
document.getElementById("shippingCharge").innerHTML = "$" + fixedShipping;
document.getElementById("totalAmt").innerHTML = "$" + fixedGrandTotal;
}
function gotoShipping() {
var productsNotSelected = 0;
for(var i = 0; i < products.length; i++)
if(products[i].selected !== true)
productsNotSelected++;
if(productsNotSelected === 4) {
alert("Please select at least one product.");
return false;
}
var units = document.getElementById("units").value;
if(units === "") {
alert("Please enter a number of units.");
return false;
}
else
location.href = "shippingInfo.html";
}
function gotoCart() {
var fullname = document.getElementsByName("fname").value;
var address1 = document.getElementsByName("street").value;
var address2 = document.getElementsByName("street2").value;
var city = document.getElementsByName("city").value;
var stateList = document.getElementsByName("state");
var states = stateList.options[stateList.selectedIndex].value;
var zip = document.getElementsByName("zip").value;
var phone = document.getElementsByName("phone").value;
var email = document.getElementsByName("email").value;
if(fullname === "") {
alert("Please enter a name.");
return false;
}
else if(address1 === "") {
alert("Please enter an address.");
return false;
}
else if(city === "") {
alert("Please enter a city.");
return false;
}
else if(states === "") {
alert("Please enter a state.");
return false;
}
else if(zip === "") {
alert("Please enter a zip code.");
return false;
}
else if(phone === "") {
alert("Please enter a phone number.");
return false;
}
else if(email === "") {
alert("Please enter an email address.");
}
else
location.href = "shoppingCart.html";
}
function gotoCheckout() {
var address = document.getElementsByName("address1").value;
var city = document.getElementsByName("city").value;
var stateList = document.getElementsByName("state");
var states = stateList.options[stateList.selectedIndex].value;
var zip = document.getElementsByName("zipcode").value;
if(address === "") {
alert("Please enter an address.");
return false;
}
else if(city === "") {
alert("Please enter a city.");
return false;
}
else if(states === "") {
alert("Please enter a state.");
return false;
}
else if(zip === "") {
alert("Please enter a zip code.");
return false;
}
else
location.href = "checkout.html";
}
function gotoConfirmationPage() {
var typeList = document.getElementsByName("cardtype");
var type = typeList.options[typeList.selectedIndex].value;
var number = document.getElementsByName("cardnumber").value;
var expiration = document.getElementsByName("expdate").value;
if(type === "") {
alert("Please enter payment method.");
return false;
}
else if(number === "") {
alert("Please enter a card number.");
return false;
}
else if(expiration === "") {
alert("Please enter an expiration date.");
return false;
}
else
location.href = "confirmationpage.html";
}
// CartJava.js
function validatePersonalInfo()
{
var _first = document.getElementsByName("fname").value;
var _street = document.getElementsByName("street").value;
var _street2 = document.getElementsByName("street2").value;
var _city = document.getElementsByName("city").value;
var _zip = document.getElementsByName("zip").value;
var _phone = document.getElementsByName("phone").value;
var _email = document.getElementsByName("email").value;
stringlength(_first,0,100);
stringlength(_street,0,100);
stringlength(_zip,0,100);
stringlength(_phone,0,100);
stringlength(_email,0,100);
var checkZip = checkNum(5);
//var phoneInput = document.getElementById("phone").value;
var validPhone = false;
var validZip = false;
if(checkZip === true)
validZip = true;
else
alert("Invalid Zip Code. " + validZip);
if(!checkPhone(_phone))
alert("Phone number is invalid. " + validPhone);
else
validPhone = true;
if(validZip && validPhone)
alert("Your form has been verified");
if(_first === "") {
alert("Please enter a full name.");
return false;
}
else if(_street === "") {
alert("Please enter your street name.");
return false;
}
else if(_city === "") {
alert("Please enter your city.");
return false;
}
else if(_zip === "") {
alert("Please enter your zip.");
return false;
}
else if(_phone === "") {
alert("Please enter your phone number.");
return false;
}
else if(_email === "") {
alert("Please enter your email.");
return false;
}
else
location.href = "shoppingCart.html";
}
function checkPhone(str) {
var regexp = /^(\d{10}|\d{3}-\d{3}-\d{4}|\(\d{3}\)\d{3}-\d{4})$/;
return regexp.test(str);
}
// function to validate date
function checkDate(str) {
var re = /^(\d{1,2}\/\d{4})$/;
if (form.startdate.value !== '' && !form.expdate.value.match(re)) {
alert("Invalid date format: " + form.expdate.value);
form.expdate.focus();
return false;
}
}
function checkNum(length) {
var zipEntry = document.getElementsByName("zip").value;
var zipNum = parseInt(zipEntry, 12);
if (zipEntry.length === length) {
if(zipNum !== 0 && isNaN(zipNum) === false)
return true;
else
return false;
}
else
return false;
}
function checkMail(str) {
var regexp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return regexp.test(str);
}
function stringlength(inputTxt, minlength, maxlength) {
var field = inputTxt;
var mnlen = minlength;
var mxlen = maxlength;
if (field.length < mnlen || field.length > mxlen) {
alert("Please input between " + mnlen + " and " + mxlen + " characters");
return false;
} else {
alert('Form Saved');
return true;
}
}
/*
//function to submit form
function SubmitForm() {
document.form1.submit();
}
*/
//function to check form
function checkForm(form1) {
if(!checkDate(form.expdate))
return false;
}
// AJAX code
function showStates() {
if(window.XMLHttpRequest)
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
else
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function () {
if(this.readyState === 4 && this.status === 200) {
var obj = eval(this.responseText);
var x = document.getElementById("stateDD");
for(i = 0; i < obj.length; i++) {
var option = document.createElement("option");
option.value = obj[i].stateCode;
option.text = obj[i].stateName;
x.add(option, x[i]);
}
}
};
xmlhttp.open("GET", "getStates.php", true);
xmlhttp.send();
}