Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2. function subCalc(the_form) {
  3.   var subtotal = 0;
  4.   var subtemp = 0;
  5.   var totalHrs = 0;
  6.  
  7.   // put the calories and form fields into parallel arrays.
  8.   var calorie_array = new Array()
  9.   calorie_array[0] = 72;
  10.   calorie_array[1] = 86;
  11.   calorie_array[2] = 100;
  12.   calorie_array[3] = 126;
  13.   calorie_array[4] = 130;
  14.   calorie_array[5] = 180;
  15.   calorie_array[6] = 156;
  16.   calorie_array[7] = 182;
  17.   calorie_array[8] = 210;
  18.   calorie_array[9] = 250;
  19.   calorie_array[10] = 270;
  20.   calorie_array[11] = 390;
  21.   calorie_array[12] = 390;
  22.   calorie_array[13] = 546;
  23.  
  24.   var time_array = new Array()
  25.   time_array[0] = the_form.sleeping_time;
  26.   time_array[1] = the_form.TV_time;
  27.   time_array[2] = the_form.sit_time;
  28.   time_array[3] = the_form.cook_time;
  29.   time_array[4] = the_form.stand_time;
  30.   time_array[5] = the_form.wash_time;
  31.   time_array[6] = the_form.walk_S_time;
  32.   time_array[7] = the_form.house_time;
  33.   time_array[8] = the_form.walk_M_time;
  34.   time_array[9] = the_form.gard_time;
  35.   time_array[10] = the_form.danc_time;
  36.   time_array[11] = the_form.stairs_time;
  37.   time_array[12] = the_form.jog_time;
  38.   time_array[13] = the_form.sqsh_time;
  39.  
  40.   var sub_array = new Array()
  41.   sub_array[0] = the_form.cal0;
  42.   sub_array[1] = the_form.cal1;
  43.   sub_array[2] = the_form.cal2;
  44.   sub_array[3] = the_form.cal3;
  45.   sub_array[4] = the_form.cal4;
  46.   sub_array[5] = the_form.cal5;
  47.   sub_array[6] = the_form.cal6;
  48.   sub_array[7] = the_form.cal7;
  49.   sub_array[8] = the_form.cal8;
  50.   sub_array[9] = the_form.cal9;
  51.   sub_array[10] = the_form.cal10;
  52.   sub_array[11] = the_form.cal11;
  53.   sub_array[12] = the_form.cal12;
  54.   sub_array[13] = the_form.cal13;
  55.  
  56.  
  57.   for(i = 0; i < calorie_array.length; i++) {
  58.     // Give subtemp the value or the calorie times the time.
  59.     subtemp = (calorie_array[i] * time_array[i].value);
  60.    
  61.     // Put the converted string into the form field.
  62.     sub_array[i].value = checkAmount(subtemp);
  63.    
  64.     // Add the converted number value to subtotal.
  65.     subtotal += roundFloat(subtemp);
  66.   }
  67.   for(i = 0; i < time_array.length; i++) {
  68.     hours = +(time_array[i].value);
  69.     totalHrs += roundFloat(hours);
  70.   }
  71.  
  72.   return [subtemp,subtotal,totalHrs];
  73. }
  74.  
  75. function totalCalc() {
  76.   var form;
  77.   var subtotal;
  78.   var total;
  79.   var totalHrs;
  80.   var hours;
  81.   var totals = [];  //Declare new array
  82.  
  83.   form = document.calc_form;
  84.  
  85.   //Get the totals from subCalc (returns an array)
  86.   totals = subCalc(form);
  87.  
  88.   // get the value of subtotal from totalCalc.
  89.   subtotal = totals[1];
  90.   totalHrs = totals[2];
  91.  
  92.   // Add the NUMBER values and subtotal.
  93.   total = subtotal;
  94.  
  95.   // Convert this number into a string and display.
  96.   form.total.value = checkAmount(total);
  97.  
  98.   // Adds total hours.
  99.   hours = totalHrs
  100.   form.total_hrs.value = checkAmount(hours);
  101. }
  102.  
  103. function roundFloat(num) {
  104.   num = parseFloat(num);
  105.   num = Math.round(100*num)/100
  106.  
  107.   return num
  108. }
  109.  
  110. function checkAmount(num) {
  111.   // Convert into a floating point number.
  112.   num = parseFloat(num)
  113.   // Round the number off.
  114.   num = Math.round(100*num)/100
  115.   // Turn into a string.
  116.   num = String(num)
  117.   // Return the converted string.
  118.   return num
  119. }
  120. </script>
  121. <form name="calc_form" id="form" method="post">
  122. <table width="310" border="0" bgcolor="#EAEAEA">
  123. <tr>
  124. <th width="95"><h2>Activity</h2></th><th width="70"><h2>Time <br>(in hours)</h2></th>
  125. <th width="101"><h2>Calories used per hour</h2></th>
  126. </tr>
  127. <tr class="table-text">
  128. <td class="right"><p class="table-text">Sleeping</p></td>
  129. <td width="70">
  130. <p class="table-text">
  131. <input name="sleeping_time" type="text" id="sleeping_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  132. hrs</p></td>
  133. <td width="101">
  134. <p class="table-text">
  135. <input name="cal0" type="text" id="cal0" size="6" maxlength="6" />
  136. kcals</p></td>
  137. </tr>
  138. <tr>
  139. <td class="right"><p class="table-text">Eating/Reading/<br>Watching TV</p></td>
  140. <td width="70"><p class="table-text">
  141. <input name="TV_time" type="text" id="TV_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  142. hrs</p></td>
  143. <td width="101"><p class="table-text">
  144. <input name="cal1" type="text" id="cal1" size="6" maxlength="6" />
  145. </span>kcals</td>
  146. </tr>
  147. <tr>
  148. <td class="right"><p class="table-text">Sitting</p></td>
  149. <td width="70"><p class="table-text">
  150. <input name="sit_time" type="text" id="sit_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  151. hrs</p></td>
  152. <td width="101"><p class="table-text">
  153. <input name="cal2" type="text" id="cal2" size="6" maxlength="6" />
  154. kcals</p></td>
  155. </tr>
  156. <tr>
  157. <td class="right"><p class="table-text">Cooking</p></td>
  158. <td width="70"><p class="table-text">
  159. <input name="cook_time" type="text" id="cook_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  160. hrs</p></td>
  161. <td width="101"><p class="table-text">
  162. <input name="cal3" type="text" id="cal3" size="6" maxlength="6" />
  163. kcals</p></td>
  164. </tr>
  165. <tr>
  166. <td class="right"><p class="table-text">Standing</p></td>
  167. <td width="70"><p class="table-text">
  168. <input name="stand_time" type="text" id="stand_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  169. hrs</p></td>
  170. <td width="101"><p class="table-text">
  171. <input name="cal4" type="text" id="cal4" size="6" maxlength="6" />
  172. kcals</p></td>
  173. </tr>
  174. <tr>
  175. <td class="right"><p class="table-text">Washing &amp; Dressing</p></td>
  176. <td width="70"><p class="table-text">
  177. <input name="wash_time" type="text" id="wash_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  178. hrs</p></td>
  179. <td width="101"><p class="table-text">
  180. <input name="cal5" type="text" id="cal5" size="6" maxlength="6" />
  181. kcals</p></td>
  182. </tr>
  183. <tr>
  184. <td class="right"><p class="table-text">Walking Slowly</p></td>
  185. <td width="70"><p class="table-text">
  186. <input name="walk_S_time" type="text" id="walk_S_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  187. hrs</p></td>
  188. <td width="101"><p class="table-text">
  189. <input name="cal6" type="text" id="cal6" size="6" maxlength="6" />
  190. kcals</p></td>
  191. </tr>
  192. <tr>
  193. <td class="right"><p class="table-text">Light housework</p></td>
  194. <td width="70"><p class="table-text">
  195. <input name="house_time" type="text" id="house_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  196. hrs</p></td>
  197. <td width="101"><p class="table-text">
  198. <input name="cal7" type="text" id="cal7" size="6" maxlength="6" />
  199. kcals</p></td>
  200. </tr>
  201. <tr>
  202. <td class="right"><p class="table-text">Walking moderately</p></td>
  203. <td width="70"><p class="table-text">
  204. <input name="walk_M_time" type="text" id="walk_M_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  205. hrs</p></td>
  206. <td width="101"><p class="table-text">
  207. <input name="cal8" type="text" id="cal8" size="6" maxlength="6" />
  208. kcals</p></td>
  209. </tr>
  210. <tr>
  211. <td class="right"><p class="table-text">Light gardening</p></td>
  212. <td width="70"><p class="table-text">
  213. <input name="gard_time" type="text" id="gard_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  214. hrs</p></td>
  215. <td width="101"><p class="table-text">
  216. <input name="cal9" type="text" id="cal9" size="6" maxlength="6" />
  217. kcals</p></td>
  218. </tr>
  219. <tr>
  220. <td class="right"><p class="table-text">Dancing</p></td>
  221. <td width="70"><p class="table-text">
  222. <input name="danc_time" type="text" id="danc_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  223. hrs</p></td>
  224. <td width="101"><p class="table-text">
  225. <input name="cal10" type="text" id="cal10" size="6" maxlength="6" />
  226. kcals</p></td>
  227. </tr>
  228. <tr>
  229. <td class="right"><p class="table-text">Walking up stairs</p></td>
  230. <td width="70"><p class="table-text">
  231. <input name="stairs_time" type="text" id="stairs_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  232. hrs</p></td>
  233. <td width="101"><p class="table-text">
  234. <input name="cal11" type="text" id="cal11" size="6" maxlength="6" />
  235. kcals</p></td>
  236. </tr>
  237. <tr>
  238. <td class="right"><p class="table-text">Jogging</p></td>
  239. <td width="70"><p class="table-text">
  240. <input name="jog_time" type="text" id="jog_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  241. hrs</p></td>
  242. <td width="101"><p class="table-text">
  243. <input name="cal12" type="text" id="cal12" size="6" maxlength="6" />
  244. kcals</p></td>
  245. </tr>
  246. <tr>
  247. <td class="right"><p class="table-text">Squash</p></td>
  248. <td width="70"><p class="table-text">
  249. <input name="sqsh_time" type="text" id="sqsh_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" />
  250. hrs</p></td>
  251. <td width="101"><p class="table-text">
  252. <input name="cal13" type="text" id="cal13" size="6" maxlength="6" />
  253. kcals</p></td>
  254. </tr>
  255.  
  256. <tr>
  257. <td><p class="table-text">&nbsp;</p></td>
  258. <td width="70"><p class="table-text"></p></td>
  259. <td width="101"><p class="table-text"></p></td>
  260. </tr>
  261.  
  262. <tr>
  263. <td><p class="table-text"><strong>Totals = </strong></p></td>
  264. <td width="70"><p class="table-text">
  265. <input name="total_hrs" type="text" id="total_hrs" size="3" />
  266. hrs</p></td>
  267. <td width="101"><p class="table-text">
  268. <input name="total" type="text" id="total" size="6" />
  269. kcals</p></td>
  270. </tr>
  271.  
  272. <tr>
  273. <td colspan="2"><INPUT name="reset" value="Reset" TYPE="reset"> </td>
  274. <td colspan="2"><input name="Calculate Total" type="button" id="Calculate Total" value="Calculate Total" onclick="totalCalc();" /></td>
  275. </tr>
  276. </table>
  277. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement