-
Notifications
You must be signed in to change notification settings - Fork 0
/
BarbellCalculator.js
195 lines (164 loc) · 4.86 KB
/
BarbellCalculator.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
var bag15_used = 0;
var bag10_used = 0;
var bag5_used = 0;
var ExternalBags = 4;
var ExternalBagstag = 2;
var Bag5 = 4;
var Bag10 = 4;
var Bag15 = 12;
var Bags = 12;
var BarWeight = 45;
function SizeCalc() {
reset();
var weight = 0;
weight = document.getElementById("Weight").value;
weight = weight - BarWeight;
if (weight > (15 * Bag15)) {
alert("Oh, Let's get more bags!");
return false;
} else if (weight < 0) {
alert("Hey man! Don't kidding me!");
return false;
}
while (weight >= 10) {
if (weight >= 30 && bag15_used < Bag15) {
weight = weight - 30;
bag15_used++;
continue;
} else if (weight >= 20 && bag10_used < Bag10) {
weight = weight - 20;
bag10_used++;
continue;
} else if (weight >= 10 && bag5_used < Bag5) {
weight = weight - 10;
bag5_used++;
continue;
}
else
{
alert("Unexpected error happens!");
return false;
}
}
var svgid = '';
for (i = 1; i <= Bags/2; i++) {
svgid_1 = 'svg' + i;
svgid_2 = 'svg' + (i+9);
if (bag15_used >= i){
changetext('15 LB', svgid_1);
changetext('15 LB', svgid_2);
} else if ((bag15_used + bag10_used) >= i && (bag15_used < i)) {
changetext('10 LB', svgid_1);
changetext('10 LB', svgid_2);
} else if ((bag15_used + bag10_used + bag5_used) >= i && (bag15_used + bag10_used) < i) {
changetext('5 LB', svgid_1);
changetext('5 LB', svgid_2);
}
else
{
blurRect(svgid_1);
blurRect(svgid_2);
}
}
}
function text_Init(){
for (i = 1; i <= 18; i++) {
svgid = 'svg' + i;
createtext(svgid);
}
}
function createtext(svgid){
var textid = 'text' + svgid;
var text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
text.setAttribute('x', '35');
text.setAttribute('id', textid);
text.setAttribute('y', '35');
text.setAttribute('fill', 'black');
text.setAttribute('font-size', '20');
text.textContent = '';
var svg = document.getElementById(svgid);
svg.appendChild(text);
}
function changetext(value, svgid){
var textid = 'text' + svgid;
var text = document.getElementById(textid);
text.textContent = value;
var svg = document.getElementById(svgid);
svg.appendChild(text);
}
function blurRect(svgid){
document.getElementById(svgid).style.opacity= 0.2;
}
function clearRect(svgid){
document.getElementById(svgid).style.opacity= 1;
}
function reset(){
for (i = 1; i <= 18; i++) {
svgid = 'svg' + i;
changetext('', svgid);
clearRect(svgid);
}
bag15_used = 0;
bag10_used = 0;
bag5_used = 0;
}
$(document).ready (function () {
text_Init();
hide_calculator();
// show_calculator();
setting();
$("#Reset").click(reset);
$("#Calculate").click(SizeCalc);
});
// function show_calculator() {
// var id='';
// for (var i = 1; i <= ExternalBagstag; i++) {
// id = ".external" + i;
// // $(id).removeClass("rect_none");
// $(id).attr("class", "rect_obvious rect_none");
// }
// }
// function hide_calculator(){
// var id='';
// for (var i = 1; i < 9; i++) {
// id = ".external" + i;
// $(id).hide();
// }
// }
function setting(){
// ExternalBags = document.getElementById("ExternalBags").value;
var selIndex = document.getElementById("ExternalBags").selectedIndex;
ExternalBags = 1*document.getElementById("ExternalBags").options[selIndex].innerHTML;
ExternalBagstag = ExternalBags / 2;
Bag5 = 1*ExternalBags;
Bag10 = 1*ExternalBags;
Bag15 = 3*ExternalBags;
Bags = 3*ExternalBags;
BarWeight = document.getElementById("BarWeight").value;
hide_calculator();
}
function hide_calculator() {
var id='';
var style="rect_style"
var color="rect_color_"
if (ExternalBags == 2) {
for (var i = 1; i < 4; i++) {
id = ".external" + i+" rect";
$(id).attr("class","rect_obvious rect_none");
};
$(".external1 rect").attr("class","rect_obvious rect_color_1 rect_style");
} else if (ExternalBags == 4) {
for (var i = 1; i < 4; i++) {
id = ".external" + i+" rect";
$(id).attr("class","rect_obvious rect_none");
};
$(".external1 rect").attr("class","rect_obvious rect_color_1 rect_style");
$(".external2 rect").attr("class","rect_obvious rect_color_2 rect_style");
} else if (ExternalBags == 6){
$(".external1 rect").attr("class","rect_obvious rect_color_1 rect_style");
$(".external2 rect").attr("class","rect_obvious rect_color_2 rect_style");
$(".external3 rect").attr("class","rect_obvious rect_color_3 rect_style");
} else{
alert("Unexpected error!");
};
}