Rock Value Calculating


/ Published in: HTML
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <html><head><title>Test</title>
  2. <script type="text/javascript">
  3.  
  4. // Metal Market Price *config*
  5. var Gold = 657.00;
  6. var Silver = 11;
  7. var Platinum = 12;
  8. var Palladium = 13;
  9. var Rhodium = 14;
  10.  
  11. var Aluminium = 20;
  12. var Copper = 21;
  13. var Nickel = 22;
  14. var Lead = 23;
  15. var Zinc = 24;
  16.  
  17. //Currency prefix
  18. var currencyPrefix = '$';
  19. var cp = currencyPrefix;
  20.  
  21. //Metal Market Price with currency prefix
  22. Gold2 = Gold + cp;
  23. Silver2 = Silver + cp;
  24. Platinum2 = Platinum + cp;
  25. Palladium2 = Palladium + cp;
  26. Rhodium2 = Rhodium + cp;
  27.  
  28. Aluminium2 = Aluminium + cp;
  29. Copper2 = Copper + cp;
  30. Nickel2 = Nickel + cp;
  31. Lead2 = Lead + cp;
  32. Zinc2 = Zinc + cp;
  33.  
  34. // Convert Values *config*
  35. var gramsPerTonne = 1000000;
  36. var gramsPerOunces = 31.1034768;
  37. var lbPerTonne = 2204.6226218;
  38. var ozPerTonne = gramsPerTonne / gramsPerOunces;
  39.  
  40. // Prefix and String *config*
  41. var valuePrefix = 'value';
  42. var unitPrefix = '/tonne';
  43. var up = unitPrefix;
  44. var invalideValue = 'N/A';
  45.  
  46. // Setting - cookie seperator
  47. var PARA_SPRT = '_';
  48.  
  49. // Main variables
  50. var subTotalPrecious = 0;
  51. var subTotalBase = 0;
  52. var totalMetal = 0;
  53. var singleMetal = 0;
  54. var defaultValue = cp + '0,00 ' + up;
  55. var totalGold = 0;
  56. var totalSilver = 0;
  57. var totalPlatinum = 0;
  58. var totalPalladium = 0;
  59. var totalRhodium = 0;
  60. var totalAluminium = 0;
  61. var totalCopper = 0;
  62. var totalNickel = 0;
  63. var totalLead = 0;
  64. var totalZinc = 0;
  65.  
  66. function CurrencyFormatted(amount)
  67. {
  68. var i = parseFloat(amount);
  69. if(isNaN(i)) { i = 0.00; }
  70. var minus = '';
  71. if(i < 0) { minus = '-'; }
  72. i = Math.abs(i);
  73. i = parseInt((i + .005) * 100);
  74. i = i / 100;
  75. s = new String(i);
  76. if(s.indexOf('.') < 0) { s += '.00'; }
  77. if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
  78. s = minus + s;
  79. return s;
  80. }
  81.  
  82.  
  83. function CommaFormatted(amount)
  84. {
  85. var delimiter = ",";
  86. var a = amount.split('.',2)
  87. var d = a[1];
  88. var i = parseInt(a[0]);
  89. if(isNaN(i)) { return ''; }
  90. var minus = '';
  91. if(i < 0) { minus = '-'; }
  92. i = Math.abs(i);
  93. var n = new String(i);
  94. var a = [];
  95. while(n.length > 3)
  96. {
  97. var nn = n.substr(n.length-3);
  98. a.unshift(nn);
  99. n = n.substr(0,n.length-3);
  100. }
  101. if(n.length > 0) { a.unshift(n); }
  102. n = a.join(delimiter);
  103. if(d.length < 1) { amount = n; }
  104. else { amount = n + '.' + d; }
  105. amount = minus + amount;
  106. return amount;
  107. }
  108. // function CommaFormatted()
  109.  
  110. // end of function CurrencyFormatted()
  111.  
  112. function UpdateElement(getId, string)
  113. {
  114. document.getElementById(getId).innerHTML = string;
  115. }
  116. // end of function UpdateElement()
  117.  
  118. function ValueCalculating(getValue,getId,isPrec)
  119. {
  120. var getInsert;
  121. switch (getId)
  122. {
  123. //Precious Metals
  124. case "GoldTotal" : getInsert = totalGold; break;
  125. case "SilverTotal" : getInsert = totalSilver; break;
  126. case "PlatinumTotal" : getInsert = totalPlatinum; break;
  127. case "PalladiumTotal" : getInsert = totalPalladium; break;
  128. case "RhodiumTotal" : getInsert = totalRhodium; break;
  129.  
  130. //Base Metals
  131. case "AluminiumTotal" : getInsert = totalAluminium; break;
  132. case "CopperTotal" : getInsert = totalCopper; break;
  133. case "NickelTotal" : getInsert = totalNickel; break;
  134. case "LeadTotal" : getInsert = totalLead; break;
  135. case "ZincTotal" : getInsert = totalZinc; break;
  136. }
  137.  
  138. //document.getElementById(getId).innerHTML = cp + getValue + ' ' + up; //insert the new value
  139. // alert (document.getElementById(getId).innerHTML); //test
  140. if(isNaN(getValue) | getValue < 0)
  141. {
  142.  
  143. switch (getId)
  144. {
  145. //Precious Metals
  146. case "GoldTotal" : totalGold = 0; break;
  147. case "SilverTotal" : totalSilver = 0; break;
  148. case "PlatinumTotal" : totalPlatinum = 0; break;
  149. case "PalladiumTotal" : totalPalladium = 0; break;
  150. case "RhodiumTotal" : totalRhodium = 0; break;
  151.  
  152. //Base Metals
  153. case "AluminiumTotal" : totalAluminium = 0; break;
  154. case "CopperTotal" : totalCopper = 0; break;
  155. case "NickelTotal" : totalNickel = 0; break;
  156. case "LeadTotal" : totalLead = 0; break;
  157. case "ZincTotal" : totalZinc = 0; break;
  158. }
  159.  
  160. if(isNaN(getValue))
  161. {
  162. UpdateElement(getId, invalideValue);
  163. }
  164. else
  165. {
  166. UpdateElement(getId, cp + '0.00 ' + up);
  167. }
  168. subTotalPrecious = totalGold + totalSilver + totalPlatinum + totalPalladium + totalRhodium;
  169. subPreIn = CurrencyFormatted(subTotalPrecious);
  170. subPreIn = CommaFormatted(subPreIn);
  171. subTotalBase = totalAluminium + totalCopper + totalNickel + totalLead + totalZinc; //update the Base Metal Total
  172. subBaseIn = CurrencyFormatted(subTotalBase);
  173. subBaseIn = CommaFormatted(subBaseIn);
  174. totalMetal = totalGold + totalSilver + totalPlatinum + totalPalladium + totalRhodium + totalAluminium + totalCopper + totalNickel + totalLead + totalZinc; //update the Metal Total
  175. totalIn = CurrencyFormatted(totalMetal);
  176. totalIn = CommaFormatted(totalIn);
  177.  
  178. UpdateElement("subTotalPre", cp + subPreIn + up);
  179. UpdateElement("subTotalBase", cp + subBaseIn + up);
  180. UpdateElement("totalMetal", cp + totalIn + up);
  181. }
  182. else
  183. {
  184. getValue = parseFloat(getValue); //convert string to number
  185. switch (getId)
  186. {
  187. //Precious Metals
  188. case "GoldTotal" : totalGold = getValue; getInsert = totalGold; break;
  189. case "SilverTotal" : totalSilver = getValue; getInsert = totalSilver; break;
  190. case "PlatinumTotal" : totalPlatinum = getValue; getInsert = totalPlatinum; break;
  191. case "PalladiumTotal" : totalPalladium = getValue; getInsert = totalPalladium; break;
  192. case "RhodiumTotal" : totalRhodium = getValue; getInsert = totalRhodium; break;
  193.  
  194. //Base Metals
  195. case "AluminiumTotal" : totalAluminium = getValue; getInsert = totalAluminium; break;
  196. case "CopperTotal" : totalCopper = getValue; getInsert = totalCopper; break;
  197. case "NickelTotal" : totalNickel = getValue; getInsert = totalNickel; break;
  198. case "LeadTotal" : totalLead = getValue; getInsert = totalLead; break;
  199. case "ZincTotal" : totalZinc = getValue; getInsert = totalZinc; break;
  200. }
  201.  
  202. if (isPrec)
  203. {
  204. subTotalPrecious = totalGold + totalSilver + totalPlatinum + totalPalladium + totalRhodium; //update the Precious Metal Total
  205. //alert(subTotalPrecious); //test
  206. calcTotalPre = CurrencyFormatted(subTotalPrecious);
  207. calcTotalPre = CommaFormatted(calcTotalPre);
  208. calcTotalPre = cp + calcTotalPre + ' ' + up; //formate the string to update
  209. // alert(calcTotalPre); //test
  210. UpdateElement("subTotalPre", calcTotalPre); //update the output
  211. }
  212. else
  213. {
  214. subTotalBase = totalAluminium + totalCopper + totalNickel + totalLead + totalZinc; //update the Base Metal Total
  215. //alert(subTotalBase); //test
  216. calcTotalBase = CurrencyFormatted(subTotalBase);
  217. calcTotalBase = CommaFormatted(calcTotalBase);
  218. calcTotalBase = cp + calcTotalBase + ' ' + up; //formate the string to update
  219. //alert(calcTotalBase); //test
  220. UpdateElement("subTotalBase", calcTotalBase); //update the output
  221. }
  222. totalMetal = totalGold + totalSilver + totalPlatinum + totalPalladium + totalRhodium + totalAluminium + totalCopper + totalNickel + totalLead + totalZinc; //update the Metal Total
  223. //alert(totalMetal); //test
  224. calcTotalMetal = CurrencyFormatted(totalMetal);
  225. calcTotalMetal = CommaFormatted(calcTotalMetal);
  226. calcTotalMetal = cp + calcTotalMetal + ' ' + up; //formate the string to update
  227. // alert(calcTotalMetal); //test
  228. UpdateElement("totalMetal", calcTotalMetal); //update the otput
  229.  
  230. getInsert = CurrencyFormatted(getInsert);
  231. getInsert = CommaFormatted(getInsert);
  232. UpdateElement(getId, cp + getInsert + ' ' + up)
  233. }
  234. }
  235.  
  236. function GetRockValue(itemId, amount, price, isPrec)
  237. {
  238. var valuePerTonne;
  239. var percentage;
  240. if (isPrec)
  241. valuePerTonne = price * ozPerTonne;
  242. else
  243. valuePerTonne = price * lbPerTonne;
  244.  
  245. if (itemId == "percenatge" )
  246. {
  247. if (amount > 100) return -1;
  248. percentage = amount/100;
  249. }
  250. else if(itemId == "oz")
  251. {
  252. if (amount > ozPerTonne) return -1;
  253. percentage = amount / ozPerTonne;
  254. }
  255. else if(itemId == "lb")
  256. {
  257. if (amount > lbPerTonne) return -1;
  258. percentage = amount / lbPerTonne;
  259. }
  260. else if(itemId == "grams")
  261. {
  262. if (amount > gramsPerTonne) return -1;
  263. percentage = amount / gramsPerTonne;
  264. }
  265. else
  266. percentage = 0;
  267.  
  268. value = valuePerTonne * percentage;
  269. return value;
  270. }
  271. //end of function GetRockValue()
  272.  
  273. function GetAmountValue(itemId)
  274. {
  275. var rockValue = document.getElementById(itemId).getAttribute("value");
  276. var rockValue = document.getElementById(itemId).value;
  277. rockValue = parseFloat(rockValue);
  278. //alert("4_" + rockValue);
  279. return rockValue;
  280. }
  281. //end of function GetAmountValue()
  282.  
  283. function GetConvertValue(itemId)
  284. {
  285. var convertValue = document.getElementById(itemId).getAttribute("value");
  286. var convertValue = document.getElementById(itemId).value;
  287. //alert("5_" + convertValue);
  288. return convertValue;
  289. }
  290. //end of function GetConvertValue()
  291.  
  292. function Calculate(getId,isPrec)
  293. {
  294. var amountText = "Amount";
  295. //var amountId = getId.amountText;
  296. var amountId = getId + amountText;
  297. //alert("1_" + amountId);
  298. var totalMetalId = getId + "Total";
  299. //alert("2_" + totalMetalId);
  300. var convertId = getId + "Convert";
  301. //alert("3_" + convertId);
  302. var amount = GetAmountValue(amountId);
  303. //alert("6_" + amount);
  304. var convert = GetConvertValue(convertId);
  305. //alert("7_" + convert);
  306. var valueId;
  307. switch (getId)
  308. {
  309. //Precious Metals
  310. case "Gold" : valueId = Gold; break;
  311. case "Silver" : valueId = Silver; break;
  312. case "Platinum" : valueId = Platinum; break;
  313. case "Palladium" : valueId = Palladium; break;
  314. case "Rhodium" : valueId = Rhodium; break;
  315.  
  316. //Base Metals
  317. case "Aluminium" : valueId = Aluminium; break;
  318. case "Copper" : valueId = Copper; break;
  319. case "Nickel" : valueId = Nickel; break;
  320. case "Lead" : valueId = Lead; break;
  321. case "Zinc" : valueId = Zinc; break;
  322. }
  323.  
  324. if (isPrec)
  325. {
  326. var insert = GetRockValue(convert, amount, valueId, true);
  327. //alert("8_" + insert);
  328. if(isNaN(amount) || insert < 0)
  329. {
  330. ValueCalculating(invalideValue,totalMetalId,isPrec);
  331. }
  332. else
  333. {
  334. ValueCalculating(insert,totalMetalId,isPrec);
  335. }
  336. }
  337. else
  338. {
  339. var insert = GetRockValue(convert, amount, valueId, false);
  340. //alert("9_" + insert);
  341. if(isNaN(amount) || insert < 0)
  342. {
  343. ValueCalculating(invalideValue,totalMetalId,isPrec);
  344. }
  345. else
  346. {
  347. ValueCalculating(insert,totalMetalId,isPrec);
  348. }
  349. }
  350. }
  351.  
  352. function CalculateGold(){Calculate("Gold",true);}
  353. function CalculateSilver(){Calculate("Silver",true);}
  354. function CalculatePlatinum(){Calculate("Platinum",true);}
  355. function CalculatePalladium(){Calculate("Palladium",true);}
  356. function CalculateRhodium(){Calculate("Rhodium",true);}
  357.  
  358. function CalculateAluminium(){Calculate("Aluminium",false);}
  359. function CalculateCopper(){Calculate("Copper",false);}
  360. function CalculateNickel(){Calculate("Nickel",false);}
  361. function CalculateLead(){Calculate("Lead",false);}
  362. function CalculateZinc(){Calculate("Zinc",false);}
  363. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  364. </head>
  365.  
  366. <table width="600" border="1" cellspacing="0" cellpadding="0">
  367. <tr>
  368. <td width="71">Metal</td>
  369. <td width="163">Amount</td>
  370. <td width="88">Units/Weight</td>
  371. <td colspan="2">Metal Market Price</td>
  372. <td width="128">Value of the Rock</td>
  373. </tr>
  374. <tr>
  375. <td colspan="6">Precious Metals</td>
  376. </tr>
  377. <tr>
  378. <td>Gold</td>
  379. <td><input type="text" id="GoldAmount" size="8" maxlength="10" onKeyUp="javascript:CalculateGold()"></td>
  380. <td><select id="GoldConvert" onChange="javascript:CalculateGold()">
  381. <option value="percentage">%</option>
  382. <option value="oz" selected>ounces/tonne</option>
  383. <option value="grams">grams/tonne</option>
  384. <option value="lb">pounds/tonne</option>
  385. </select></td>
  386. <td width="45"><script type="text/javascript"> document.writeln(Gold + cp);</script></td>
  387. <td width="91">/oz</td>
  388. <td id="GoldTotal"><script type="text/javascript"> document.writeln(defaultValue);</script></td>
  389. </tr>
  390. <tr>
  391. <tr>
  392. <td>Silver</td>
  393. <td><input type="text" id="SilverAmount" size="8" maxlength="10" onKeyUp="javascript:CalculateSilver()"></td>
  394. <td><select id="SilverConvert" onChange="javascript:CalculateSilver()">
  395. <option value="percentage">%</option>
  396. <option value="oz" selected>ounces/tonne</option>
  397. <option value="grams">grams/tonne</option>
  398. <option value="lb">pounds/tonne</option>
  399. </select></td>
  400. <td width="45"><script type="text/javascript"> document.writeln(Silver + cp);</script></td>
  401. <td width="91">/oz</td>
  402. <td id="SilverTotal"><script type="text/javascript"> document.writeln(defaultValue);</script></td>
  403. </tr>
  404. <tr>
  405. <tr>
  406. <tr>
  407. <td>Platinum</td>
  408. <td><input type="text" id="PlatinumAmount" size="8" maxlength="10" onKeyUp="javascript:CalculatePlatinum()"></td>
  409. <td><select id="PlatinumConvert" onChange="javascript:CalculatePlatinum()">
  410. <option value="percentage">%</option>
  411. <option value="oz" selected>ounces/tonne</option>
  412. <option value="grams">grams/tonne</option>
  413. <option value="lb">pounds/tonne</option>
  414. </select></td>
  415. <td width="45"><script type="text/javascript"> document.writeln(Platinum + cp);</script></td>
  416. <td width="91">/oz</td>
  417. <td id="PlatinumTotal"><script type="text/javascript"> document.writeln(defaultValue);</script></td>
  418. </tr>
  419. <tr>
  420. <tr>
  421. <tr>
  422. <td>Palladium</td>
  423. <td><input type="text" id="PalladiumAmount" size="8" maxlength="10" onKeyUp="javascript:CalculatePalladium()"></td>
  424. <td><select id="PalladiumConvert" onChange="javascript:CalculatePalladium()">
  425. <option value="percentage">%</option>
  426. <option value="oz" selected>ounces/tonne</option>
  427. <option value="grams">grams/tonne</option>
  428. <option value="lb">pounds/tonne</option>
  429. </select></td>
  430. <td width="45"><script type="text/javascript"> document.writeln(Palladium + cp);</script></td>
  431. <td width="91">/oz</td>
  432. <td id="PalladiumTotal"><script type="text/javascript"> document.writeln(defaultValue);</script></td>
  433. </tr>
  434. <tr>
  435. <tr>
  436. <td>Rhodium</td>
  437. <td><input type="text" id="RhodiumAmount" size="8" maxlength="10" onKeyUp="javascript:CalculateRhodium()"></td>
  438. <td><select id="RhodiumConvert" onChange="javascript:CalculateRhodium()">
  439. <option value="percentage">%</option>
  440. <option value="oz" selected>ounces/tonne</option>
  441. <option value="grams">grams/tonne</option>
  442. <option value="lb">pounds/tonne</option>
  443. </select></td>
  444. <td width="45"><script type="text/javascript"> document.writeln(Rhodium + cp);</script></td>
  445. <td width="91">/oz</td>
  446. <td id="RhodiumTotal"><script type="text/javascript"> document.writeln(defaultValue);</script></td>
  447. </tr>
  448. <tr>
  449. <tr>
  450. <td colspan="5">Precious Metal Sub-total >></td>
  451. <td><div id="subTotalPre"><script type="text/javascript"> document.writeln(defaultValue);</script></div></td>
  452. </tr>
  453. <tr>
  454. <td colspan="6">Base Metals</td>
  455. </tr>
  456. <tr>
  457. <tr>
  458. <td>Aluminium</td>
  459. <td><input type="text" id="AluminiumAmount" size="8" maxlength="10" onKeyUp="javascript:CalculateAluminium()"></td>
  460. <td><select id="AluminiumConvert" onChange="javascript:CalculateAluminium()">
  461. <option value="percentage">%</option>
  462. <option value="oz" selected>ounces/tonne</option>
  463. <option value="grams">grams/tonne</option>
  464. <option value="lb">pounds/tonne</option>
  465. </select></td>
  466. <td width="45"><script type="text/javascript"> document.writeln(Aluminium + cp);</script></td>
  467. <td width="91">/lb</td>
  468. <td id="AluminiumTotal"><script type="text/javascript"> document.writeln(defaultValue);</script></td>
  469. </tr>
  470. <tr>
  471. <tr>
  472. <td>Copper</td>
  473. <td><input type="text" id="CopperAmount" size="8" maxlength="10" onKeyUp="javascript:CalculateCopper()"></td>
  474. <td><select id="CopperConvert" onChange="javascript:CalculateCopper()">
  475. <option value="percentage">%</option>
  476. <option value="oz" selected>ounces/tonne</option>
  477. <option value="grams">grams/tonne</option>
  478. <option value="lb">pounds/tonne</option>
  479. </select></td>
  480. <td width="45"><script type="text/javascript"> document.writeln(Copper + cp);</script></td>
  481. <td width="91">/lb</td>
  482. <td id="CopperTotal"><script type="text/javascript"> document.writeln(defaultValue);</script></td>
  483. </tr>
  484. <tr>
  485. <tr>
  486. <td>Nickel</td>
  487. <td><input type="text" id="NickelAmount" size="8" maxlength="10" onKeyUp="javascript:CalculateNickel()"></td>
  488. <td><select id="NickelConvert" onChange="javascript:CalculateNickel()">
  489. <option value="percentage">%</option>
  490. <option value="oz" selected>ounces/tonne</option>
  491. <option value="grams">grams/tonne</option>
  492. <option value="lb">pounds/tonne</option>
  493. </select></td>
  494. <td width="45"><script type="text/javascript"> document.writeln(Nickel + cp);</script></td>
  495. <td width="91">/lb</td>
  496. <td id="NickelTotal"><script type="text/javascript"> document.writeln(defaultValue);</script></td>
  497. </tr>
  498. <tr>
  499. <tr>
  500. <td>Lead</td>
  501. <td><input type="text" id="LeadAmount" size="8" maxlength="10" onKeyUp="javascript:CalculateLead()"></td>
  502. <td><select id="LeadConvert" onChange="javascript:CalculateLead()">
  503. <option value="percentage">%</option>
  504. <option value="oz" selected>ounces/tonne</option>
  505. <option value="grams">grams/tonne</option>
  506. <option value="lb">pounds/tonne</option>
  507. </select></td>
  508. <td width="45"><script type="text/javascript"> document.writeln(Lead + cp);</script></td>
  509. <td width="91">/lb</td>
  510. <td id="LeadTotal"><script type="text/javascript"> document.writeln(defaultValue);</script></td>
  511. </tr>
  512. <tr>
  513. <tr>
  514. <td>Zinc</td>
  515. <td><input type="text" id="ZincAmount" size="8" maxlength="10" onKeyUp="javascript:CalculateZinc()"></td>
  516. <td><select id="ZincConvert" onChange="javascript:CalculateZinc()">
  517. <option value="percentage">%</option>
  518. <option value="oz" selected>ounces/tonne</option>
  519. <option value="grams">grams/tonne</option>
  520. <option value="lb">pounds/tonne</option>
  521. </select></td>
  522. <td width="45"><script type="text/javascript"> document.writeln(Zinc + cp);</script></td>
  523. <td width="91">/lb</td>
  524. <td id="ZincTotal"><script type="text/javascript"> document.writeln(defaultValue);</script></td>
  525. </tr>
  526. <tr>
  527. <td colspan="5">Base Metal Sub-total >></td>
  528. <td><div id="subTotalBase"><script type="text/javascript"> document.writeln(defaultValue);</script></div></td>
  529. </tr>
  530. <tr>
  531. <td colspan="5">Total Gross Metal Value($USD)>></td>
  532. <td><div id="totalMetal"><script type="text/javascript"> document.writeln(defaultValue);</script></div></td>
  533. </tr>
  534. <p> </p>
  535. </body></html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.