-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-after.js
58 lines (44 loc) · 2.24 KB
/
script-after.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
(function () {
"use strict";
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('cart-hplus').addEventListener('submit', estimateTotal);
function estimateTotal(event) {
event.preventDefault();
if (document.getElementById('s-state').value === '') {
alert('Please choose your shipping state.');
document.getElementById('s-state').focus();
}
var itemBball = parseInt(document.getElementById('txt-q-bball').value, 10) || 0,
itemJersey = parseInt(document.getElementById('txt-q-jersey').value, 10) || 0,
itemPower = parseInt(document.getElementById('txt-q-power').value, 10) || 0,
// don't have to use selectedIndex in modern browsers - just value
state = document.getElementById('s-state').value,
shippingMethod = document.querySelector('[name=r_method]:checked').value || '';
var taxFactor = 1;
if (state === 'CA') {
taxFactor = 1.075; // tax is 7.5% in California
}
// pickup, usps, ups
var shippingCostPer = 0;
switch (shippingMethod) {
case 'pickup':
shippingCostPer = 0;
break;
case 'usps':
shippingCostPer = 2;
break;
case 'ups':
shippingCostPer = 3;
break;
}
var totalItems = itemBball + itemJersey + itemPower,
shippingCost = totalItems * shippingCostPer;
var subtotal = ((itemBball * 90) + (itemJersey * 25) + (itemPower * 30)) * taxFactor;
var estimate = "$" + (subtotal + shippingCost).toFixed(2);
document.getElementById('txt-estimate').value = estimate;
document.getElementById('results').innerHTML = 'Total items: ' + totalItems + '<br>';
document.getElementById('results').innerHTML += 'Total shipping: $' + shippingCost.toFixed(2) + '<br>';
document.getElementById('results').innerHTML += 'Tax: ' + ((taxFactor - 1) * 100).toFixed(2) + '% (' + state + ')';
}
});
})();