-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
111 lines (70 loc) · 2.45 KB
/
script.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
// JavaScript Document
(function() {
"use strict";
var state = document.getElementById("id_state");
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('cart_custom').addEventListener('submit', estimateTotal);
var v_btn_estimate = document.getElementById('btn_estimate');
v_btn_estimate.disabled = true;
state.addEventListener('change', function(){
if(state.value === ''){
v_btn_estimate.disabled = true;
console.log("True");
}
else{
v_btn_estimate.disabled = false;
console.log("False");
}
});
});
function estimateTotal(event) {
event.preventDefault();
if( state.value === ''){
alert('Please select the state you want to send your shipment');
state.focus();
}
var sneakers = parseInt(document.getElementById("bb_sneakers").value, 10 )|| 0;
var jersey = parseInt(document.getElementById("a_jersey").value, 10 )|| 0;
var supple = parseInt(document.getElementById("p_supple").value, 10 )|| 0;
var water = parseInt(document.getElementById("m_water").value, 10 )|| 0;
console.log(water);
var state_index = state.value;
var shipping = document.querySelector('input[name=ship]:checked').value || '';
var special_gift = document.getElementById("gift").checked;
var special_mailing = document.getElementById("mailing").checked;
var special_recipes = document.getElementById("recipes").checked;
var t_message = document.getElementById("message").value;
var shippingCostPer,
totalShippingCost,
taxFactor = 1,
totalItemPrice,
estimate;
var t_quantity = sneakers + jersey + supple + water;
totalItemPrice = (sneakers * 90 ) + (jersey * 25) + (supple * 30) + (water * 4);
if (state.value === 'CA'){
taxFactor = 1.075;
}
else if(state.value ==='WA'){
taxFactor = 1.065;
}
switch(shipping){
case 'us':
shippingCostPer = 2;
break;
case 'ups':
shippingCostPer = 3;
break;
default :
shippingCostPer = 0;
break;
}
totalShippingCost = shippingCostPer * t_quantity;
estimate = '$' +((totalItemPrice * taxFactor ) + totalShippingCost).toFixed(2);
document.getElementById('total_estimate').value=estimate;
var result_html = document.getElementById('results');
result_html.innerHTML = 'Total Item: ' + t_quantity + '<br>';
result_html.innerHTML +='Total Shipping: $' + totalShippingCost.toFixed(2) + '<br>';
result_html.innerHTML +='Tax:' + (( taxFactor - 1 )*100).toFixed(2) + '%';
result_html.innerHTML +='(State:' + state_index + ')';
}
})();