-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckout.js
274 lines (245 loc) · 8.24 KB
/
checkout.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
const priceFormat = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', minimumFractionDigits: 2});
var product;
var method;
var address;
var tax = 0;
/*const getProduct = ()=>
{
let query = window.location.search.substring(1);
let query_list = query.split("&");
var dict = new Object();
for (var i = 0; i < query_list.length; ++i)
{
let kv = query_list[i].split("=");
dict[kv[0]] = decodeURIComponent(kv[1]);
}
return dict;
}
function productToString() {
var str = product["make"] + " ";
str += product["model"] + " ";
str += product["trim"];
return str;
}
function setupCheckout() {
product = getProduct();
document.getElementById("product").innerText = "Order for " + productToString();
}*/
function setupCheckout() {
getFinalPrice();
document.getElementById("zip").addEventListener("blur", getFinalPrice);
getFinalPlace()
document.getElementById("zip").addEventListener("blur", getFinalPlace);
}
function productToString() {
var str = make + " ";
str += model + " ";
str += trim;
return str;
}
function isDigit(event, fieldName) {
var key = event.keyCode;
if(key > 47 && key < 58) {
return true;
}
//alert("Only digits are allowed in the " + fieldName + " field.");
return false;
}
function eraseNonDigits(value) {
var str = "";
for(var i = 0; i < value.length; ++i) {
if(value.charCodeAt(i) > 47 && value.charCodeAt(i) < 58) {
str += value.charAt(i);
}
}
return str;
}
function conformIntlCode(value) {
document.getElementById("code").value = "+" + eraseNonDigits(value);
conformPhoneNumber("");
}
function setCursorPosition(field, position) {
if(field.setSelectionRange) {
//field.focus();
field.setSelectionRange(position, position);
}
}
function conformPhoneNumber(value) {
var num = eraseNonDigits(value);
if(document.getElementById("code").value === "+1") {
var str = "( ) - ";
for(var i = 0; i < num.length; ++i) {
str = str.replace(" ", num.charAt(i));
}
var phone = document.getElementById("phone");
document.getElementById("phone").value = str;
var cursor = 1 + num.length;
if(cursor >= 5) cursor += 1;
if(cursor >= 9) cursor += 1;
setCursorPosition(document.getElementById("phone"), cursor);
} else
document.getElementById("phone").value = num;
}
function fillAddress() {
address = new Array(5);
address[0] = document.getElementById("country");
address[1] = document.getElementById("address");
address[2] = document.getElementById("city");
address[3] = document.getElementById("state");
address[4] = document.getElementById("zip");
}
function enableAddress() {
method = document.getElementById("method").value;
fillAddress();
if(method === "pickup") {
address.forEach(field => field.disabled = true);
} else {
address.forEach(field => field.disabled = false);
}
}
function addressToString() {
var str = address[1].value + ", ";
str += address[2].value + ", ";
str += address[3].value + " " + address[4].value;
return str;
}
function fieldsEmpty(listOfFields, listOfFieldsNames){
for (i = 0; i<listOfFields.length; ++i)
{
if(method === "pickup" && i === 5) i = 10;
let clearedElement = listOfFields[i].trim();
if(clearedElement === "")
{
alert("Your " + listOfFieldsNames[i] + " cannot be blank. Please enter a " + listOfFieldsNames[i] + ".");
return true;
}
}
return false;
}
function creditCheck(card,cvv){
if(card.length <= 19 && cvv.length == 3){
return true;
}
else{
alert("Your Credit card info is invalid. Please check CVV or Card");
return false;
}
}
function emailCheck(email) {
var emailRegExp = /^\w+[\w-\.]*\@\w+((-\w+)|\w*)\.[a-z]{2,3}$/
if(emailRegExp.test(email)) {
return true;
}
alert("Your E-mail is invalid. Please use a valid address.");
return false;
}
function getFinalPrice() {
fillAddress();
if(address[4].value === "") {
document.getElementById("totalCost").innerText = priceFormat.format(price);
} else {
getTax(address[4].value);
}
}
function submitCheckout() {
//product = document.getElementById("product").innerText;
var checkoutForm = document.getElementById("checkoutForm");
var firstname = checkoutForm.firstname.value;
var lastname = checkoutForm.lastname.value;
var code = checkoutForm.code.value;
var phone = checkoutForm.phone.value;
var email = checkoutForm.email.value;
fillAddress();
method = checkoutForm.method.value;
var card = checkoutForm.card.value;
var cvv = checkoutForm.cvv.value;
let allFields = [firstname, lastname, code, phone, email, address[0].value, address[1].value, address[2].value, address[3].value, address[4].value, card, cvv];
let allFieldsName = ["First Name", "Last Name", "International Code", "Phone Number", "Email-Address", "Country", "Street Address", "City", "State/Provence", "Postal Code", "Card Number", "CVV"];
if (!fieldsEmpty(allFields,allFieldsName) && creditCheck(card,cvv) && emailCheck(email)){
console.log("Hi, " + firstname + " " + lastname + "!");
var composeEmail = "mailto:" + email
+ "?subject=Vending Cars Order Confirmation"
+ "&body=Hi, " + firstname + " " + lastname + "! Your order for " + productToString() + " has been placed ";
if(method === "pickup") {
composeEmail += "and will be ready for pickup. ";
} else if(method === "standard") {
composeEmail += "and will be delivered to " + addressToString() + " in 7 days. ";
} else {
composeEmail += "and will be delivered to " + addressToString() + " in 4 days. ";
}
composeEmail += "%0A%0AIf there is any delay, we will call your number at " + phone + ". "
+ "%0A%0AThank you for shopping from Vending Cars!%0A%0A";
window.location.href = composeEmail;
}
}
function getTax(zip)
{
if (window.XMLHttpRequest)
{ // IE7+, Firefox, Chrome, Opera, Safari
var xhr = new XMLHttpRequest();
}
else
{ // IE5, IE6
var xhr = new ActiveXObject ("Microsoft.XMLHTTP");
}
// Register the embedded handler function
// This function will be called when the server returns
// (the "callback" function)
xhr.onreadystatechange = function ()
{ // 4 means finished, and 200 means okay.
if (xhr.readyState == 4 && xhr.status == 200)
{
var result = xhr.responseText;
tax = parseFloat(result);
document.getElementById("totalCost").innerText = priceFormat.format(price + price * tax);
}
}
// Call the response software component
xhr.open ("GET", "getTaxRate.php?zip=" + zip);
xhr.send (null);
}
function clearAddressFields(){
document.getElementById("city").innerText = "";
document.getElementById("state").innerText = "";
document.getElementById("country").innerText = "";
}
function getFinalPlace() {
fillAddress();
if(address[4].value != ""){
getPlace(address[4].value);
}
}
function getPlace (zip)
{
if (window.XMLHttpRequest)
{ // IE7+, Firefox, Chrome, Opera, Safari
var xhr = new XMLHttpRequest();
}
else
{ // IE5, IE6
var xhr = new ActiveXObject ("Microsoft.XMLHTTP");
}
// Register the embedded handler function
// This function will be called when the server returns
// (the "callback" function)
xhr.onreadystatechange = function ()
{ // 4 means finished, and 200 means okay.
if (xhr.readyState == 4 && xhr.status == 200)
{ // Data should look like "Irvine, California"
var result = xhr.responseText;
var place = result.split (', ');
document.getElementById ("city").value = place[0].trim();
document.getElementById ("state").value = place[1].trim();
console.log(place[0].trim())
if ((place[0].trim() != "")){
document.getElementById ("country").value = "United States";
}
else{
document.getElementById ("country").value = "";
}
}
}
// Call the response software component
xhr.open ("GET", "getCityState.php?zip=" + zip);
xhr.send (null);
}