-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
executable file
·290 lines (216 loc) · 8.7 KB
/
scripts.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
// this file contains scripts for client
// =========== prices =================
var cheese_cost = 2.0;
var pizzas = {
'Small': {
'Pepperoni': 8.99,
'Vegetarian': 9.99,
'Combo': 10.99
},
'Medium': {
'Pepperoni': 9.99,
'Vegetarian': 10.99,
'Combo': 11.99
},
'Large': {
'Pepperoni': 10.99,
'Vegetarian': 11.99,
'Combo': 12.99
}
};
// =========== prices end =============
// this function validates a pizza order form
function btnAddToCartHandler() {
var size = document.getElementById('size');
var errors = false;
document.getElementById('size_error').innerHTML = '';
if (!size.value) {
document.getElementById('size_error').innerHTML = 'Please, select pizza size!';
errors = true;
}
document.getElementById('kind_error').innerHTML = '';
var kind = document.getElementById('kind');
if (!kind.value) {
document.getElementById('kind_error').innerHTML = 'Please, select pizza kind!';
errors = true;
}
var quantity = parseInt(document.getElementById('quantity').value);
document.getElementById('error_quantity').innerHTML = '';
if (!quantity) {
document.getElementById('error_quantity').innerHTML = 'Please, enter a valid quantity!';
errors = true;
}
return !errors;
}
// this function calculates the price of the pizza
function btnCalculateHandler() {
var size = document.getElementById('size');
document.getElementById('size_error').innerHTML = '';
if (!size.value) {
document.getElementById('size_error').innerHTML = 'Please, select pizza size!';
}
document.getElementById('kind_error').innerHTML = '';
var kind = document.getElementById('kind');
if (!kind.value) {
document.getElementById('kind_error').innerHTML = 'Please, select pizza kind!';
}
var quantity = parseInt(document.getElementById('quantity').value);
document.getElementById('error_quantity').innerHTML = '';
if (!quantity) {
document.getElementById('error_quantity').innerHTML = 'Please, enter a valid quantity!';
}
// get cost of selected pizza
var cost = pizzas[size.value][kind.value];
if (!cost)
return false;
// get extra cheese flag
var extra_cheese = document.getElementById('extra_cheese').checked;
// add extra cheese cost if needed
if (extra_cheese) {
cost += cheese_cost;
}
document.getElementById('price').innerHTML = cost * quantity;
return false;
}
// this function invokes when user clicks on the 'Go to cart' button
function btnGoToCart() {
// change the location
window.location.href = '/checkout';
return false;
}
// this function invokes when main page loaded
function indexReady() {
document.getElementById('btn_add_to_cart').onclick = btnAddToCartHandler;
document.getElementById('calculate').onclick = btnCalculateHandler;
document.getElementById('btn_go_to_cart').onclick = btnGoToCart;
if (window.location.href.indexOf("added") != -1) {
document.getElementById('success').innerHTML = 'Pizza successfully added to cart!'
}
}
// this function validates checkout form
function btnCheckoutHandler() {
var errors = false;
var first_name = document.getElementById('first_name').value;
document.getElementById('error_first_name').innerHTML = '';
if (first_name.length < 1 || first_name.length > 20) {
document.getElementById('error_first_name').innerHTML = 'First name must be between 1 and 20 characters';
errors = true;
}
var last_name = document.getElementById('last_name').value;
document.getElementById('error_last_name').innerHTML = '';
if (last_name.length < 1 || last_name.length > 25) {
document.getElementById('error_last_name').innerHTML = 'Last name must be between 1 and 25 characters';
errors = true;
}
var address = document.getElementById('address').value;
document.getElementById('error_address').innerHTML = '';
if (address.length == 0) {
document.getElementById('error_address').innerHTML = 'Address field should be filled';
errors = true;
}
// get phone number
var phone = document.getElementById('phone').value;
document.getElementById('error_phone').innerHTML = '';
if (phone.length == 0) {
document.getElementById('error_phone').innerHTML = 'Phone field should be filled';
errors = true;
} else {
// in other case create regular expr. to check the format (123-123-1233 for example)
var phoneValid = /(\d){3}-(\d){3}-(\d){4}/;
if (!phoneValid.test(phone)) {
document.getElementById('error_phone').innerHTML = 'Invalid phone format';
errors = true;
}
}
var stype = document.forms[0].stype.value;
document.getElementById('error_sales_type').innerHTML = '';
if (!stype) {
document.getElementById('error_sales_type').innerHTML = 'Please, select payment type';
errors = true;
}
if (stype == 'credit') {
var type = document.forms[0].crtype.value;
var regex = '';
document.getElementById('error_cr_type').innerHTML = '';
if (!type) {
document.getElementById('error_cr_type').innerHTML = 'Please, select credit card type';
errors = true;
} else {
if (type == 'ax') {
regex = /(\d){4}-(\d){6}-(\d){5}/;
} else if (type == 'v') {
regex = /(\d){4}-(\d){4}-(\d){4}-(\d){4}/;
}
var cardNumber = document.getElementById('txt_cr_number').value;
document.getElementById('error_cr_number').innerHTML = '';
if (!cardNumber) {
document.getElementById('error_cr_number').innerHTML = 'Credit Card number is Required';
errors = true;
} else {
if (!regex.test(cardNumber)) {
document.getElementById('error_cr_number').innerHTML = 'Invalid Credit Card Number Format';
errors = true;
}
}
var crCardExpDate = document.getElementById('datepicker').value;
document.getElementById('error_cr_ex_date').innerHTML = '';
if (!crCardExpDate) {
document.getElementById('error_cr_ex_date').innerHTML = 'Please, enter Credit Card expiration Date';
errors = true;
}
}
}
return !errors;
}
function crCardTypeClick(element) {
if (element.value == 'ax') {
document.getElementById('txt_cr_number').setAttribute('placeholder', 'XXXX-XXXXXX-XXXXX');
} else {
document.getElementById('txt_cr_number').setAttribute('placeholder', 'XXXX-XXXX-XXXX-XXXX');
}
}
function saleTypeClick(element) {
if (element.value == 'cash') {
document.getElementById('cr_t').style.display = 'none';
document.getElementById('cr_number').style.display = 'none';
document.getElementById('cr_ex_date').style.display = 'none';
} else {
document.getElementById('cr_t').style.display = 'table-row';
document.getElementById('cr_number').style.display = 'table-row';
document.getElementById('cr_ex_date').style.display = 'table-row';
}
}
// this function invokes when checkout page loaded
function checkoutReady() {
var min = 1000, max = 10000;
var totalCost = 0;
var div = document.getElementById('full_order');
var id = Math.floor(Math.random() * (max - min + 1)) + min;
div.innerHTML += '<p>Order #' + id + '</p>';
if (myCart.length) {
myCart.forEach(function (element) {
var cost = pizzas[element.size][element.kind];
if (element.extra_cheese) {
cost += cheese_cost;
}
var str = '<p>' + element.size + ' ' + element.kind;
if (element.extra_cheese) {
str += ' with extra cheese';
}
cost *= element.quantity;
str += ' x' + element.quantity + ' ($' + cost + ')';
var pizza_id = element.size + '_' + element.kind;
if (element.extra_cheese) {
pizza_id += '_extra_cheese'
}
div.innerHTML += str + '<a href="./remove?pizza=' + pizza_id + '" style="display: block; float: right">Remove</a>';
totalCost += cost;
});
div.innerHTML += '<hr/>' + '<p>Total: $' + totalCost + '</p>';
document.getElementById('total_amount').innerHTML = '$' + totalCost;
} else {
div.innerHTML = '<p>You have no pizza in your cart</p>';
document.getElementById('btn_checkout').disabled = true;
}
document.getElementById('btn_checkout').onclick = btnCheckoutHandler;
}