jhylands

Bike clculator

May 9th, 2013
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********************
  2.  
  3. The below JavaScript bike calcs are the intellectual propery and
  4. copyright of Roy Walter (walt_at_brookhouse_dot_co_dot_uk).
  5. Nothing below is rocket science but if you rip it off without
  6. acknowledgement your are either lazy, stupid or both.
  7. If you like something enough to use it in your own work
  8. you can always show your appreciation
  9. by making a PayPal donation to the email above.
  10.  
  11. �Copyright 2008 Roy Walter. All rights reserved.
  12.  
  13. ********************/
  14.  
  15. function calcGearSize(mode) {
  16.  
  17.     var w = document.gearing.wheelsize.value;
  18.     var f=document.gearing.front.value;
  19.     var r=document.gearing.rear.value;
  20.    
  21.     if (!showError(0))
  22.         return;
  23.    
  24.     rarr = r.split(',');
  25.    
  26.     if (rarr.length > 1) {
  27.         calcGearSizeMulti(f,r,mode);
  28.         return;
  29.     }
  30.    
  31.     if (w==0)
  32.         w = document.gearing.wheelother.value;
  33.    
  34.     var sz=(w*f)/r;
  35.     sz = Math.round(sz*Math.pow(10,2))/Math.pow(10,2); 
  36.     document.gearing.out1.value=sz;
  37. }
  38. function calcGearSizeMulti(f,r,mode) {
  39.  
  40.     /* mode 0 means just show gear ratios
  41.        mode 1 means show speed or cadence */
  42.  
  43.     var counter=0;
  44.     var gears = new Array();
  45.    
  46.     if (document.gearing.wheelsize.value==0)
  47.         var w = document.gearing.wheelother.value;
  48.     else
  49.         var w = document.gearing.wheelsize.value;
  50.    
  51.     farr = f.split(',');
  52.     rarr = r.split(',');
  53.    
  54.     farr = farr.sort(sortNumbers);
  55.     rarr = rarr.sort(sortNumbers);
  56.    
  57.     var t = "";
  58.     t = "<html><head>";
  59.     t = t+"<meta http-equiv=\"Content-Type\" content=\"application/xls\">";
  60.     t = t+"<title>My Gear Table</title></head><style>td {font-family:Verdana;font-size:11px;/*width:50px;*/text-align:center;}a:hover{text-decoration:none;}</style><body>";
  61.     t = t + "<table border=1 cellpadding=5 cellspacing=1 align=center style=\"margin-top:50\">";
  62.     t = t + "<tr><td align=center colspan="+(rarr.length+1)+"><strong>Gear Table</strong><br>";
  63.    
  64.     if (mode==1) {
  65.         if (document.gearing.mode.value=='rpm')
  66.             t = t + "gear size in inches plus <font color=red>speeds</font> attained at "+document.gearing.param.value+" rpm";
  67.         else
  68.             t = t + "gear size in inches plus <font color=red>cadence</font> required to achieve "+document.gearing.param.value+" "+document.gearing.mode.value+")";
  69.     } else {
  70.         t = t + "gear size in inches";
  71.     }
  72.        
  73.     t = t + "</td></tr>";
  74.    
  75.     t = t + "<tr><td>&nbsp;</td>";
  76.    
  77.     for (var n=0;n<rarr.length;n++)
  78.         t = t+"<td  bgcolor=\"CCFF66\" align=center>"+rarr[n]+"</td>";
  79.    
  80.     t = t+"</tr>";
  81.    
  82.     for (var i=0;i<farr.length;i++) {
  83.         t = t + "<tr><td bgcolor=\"CCFF66\" width=30>"+farr[i]+"</td>";
  84.         for (var n=0;n<rarr.length;n++) {  
  85.             counter++;
  86.             g = (w*farr[i])/rarr[n];
  87.             g = Math.round(g*Math.pow(10,1))/Math.pow(10,1);
  88.            
  89.             if (mode==0) {
  90.                 t = t+"<td>"+g+"</td>";
  91.             } else {
  92.                 if (document.gearing.mode.value=='rpm')
  93.                     t = t+"<td>"+g+"<br>"+calcSpeed(g)+"</td>";
  94.                 else
  95.                     t = t+"<td>"+g+"<br><font color=red>"+calcCadence(g)+"</font></td>";
  96.             }
  97.         }
  98.         t = t + "</tr>";
  99.     }
  100.    
  101.     /*
  102.     t = t + "<tr><td style=\"text-align:left; font-size:9px\" colspan="+(rarr.length+1)+">To download in Excel format choose File - Save As... from the menu and replace the suggested ";
  103.     t = t + ".htm file extension with .xls, i.e. bikecalc.htm becomes bikecalc.xls.</td></tr>";
  104.     */
  105.     t = t + "</table>";
  106.     // t = t + "<p align=center style=\"font-family: Verdana;font-size:10px;\"><a href=# onclick=\"window.open('aboutxls.htm','','height=200,width=200');\">about saving in Excel format</a></p>";
  107.     t = t + "</body></html>";
  108.    
  109.     document.gearing.out1.value = "";
  110.     document.gearing.out2.value = "";
  111.  
  112.     hwnd = window.open("geartable.xls","geartable");
  113.     hwnd.document.write(t);
  114.     hwnd.document.close();
  115. }
  116. function calcSpeed(g) {
  117.  
  118.     g = g*3.141;
  119.     var vm = ((g*document.gearing.param.value)*60)/63360;
  120.     vm = Math.round(vm*Math.pow(10,1))/Math.pow(10,1); 
  121.     vk = vm*1.6;
  122.     vk = Math.round(vk*Math.pow(10,1))/Math.pow(10,1); 
  123.     document.gearing.out2.value = vm+" mph/"+vk+" kph";
  124.     return "<font color=red><strong>"+vm+" mph</strong><br>"+vk+" kph</font>";
  125. }
  126. function calcCadence(g) {
  127.  
  128.     /*
  129.     var g = document.gearing.out1.value;
  130.     */
  131.     var v = document.gearing.param.value;
  132.    
  133.     mode = document.gearing.mode.value;
  134.    
  135.     if (mode=='mph')
  136.         v = 63360*v; // inches per hour
  137.     else
  138.         v = 39600*v;
  139.     v = v/60; // inches per minute
  140.     c = (v/g)/3.141;
  141.     c = Math.round(c*Math.pow(10,1))/Math.pow(10,1);   
  142.     document.gearing.out2.value = c+" rpm";
  143.     return c;
  144. }  
  145. function calcWhat(mode) {
  146.  
  147.     if (!showError(1))
  148.         return;
  149.  
  150.     calcGearSize(mode);
  151.    
  152.     if (document.gearing.mode.value=='rpm')
  153.         calcSpeed(document.gearing.out1.value);
  154.     else
  155.         calcCadence(document.gearing.out1.value);
  156. }
  157. function sortNumbers(a,b) {
  158.    
  159.     return a-b;
  160. }
  161. function mailme() {
  162.  
  163.     var arr = new Array();
  164.    
  165.     arr[0] = "mailto:";
  166.     arr[1] = '@';
  167.     var addr = arr[0]+"garliestonhouse"+arr[1]+"yahoo.co.uk";
  168.     window.location = addr;
  169. }
  170. function switchLabel(mode) {
  171.  
  172.     el = document.getElementById('label1');
  173.  
  174.     switch (mode) {
  175.         case 'rpm':
  176.             el.innerHTML = "calculate speed";
  177.             break;
  178.         default:
  179.             el.innerHTML = "calculate cadence";
  180.     }
  181.    
  182.     document.gearing.param.value='';
  183.     document.gearing.out2.value='';
  184. }
  185. function makeWhite(f) {
  186.  
  187.     document.gearing[f.name].style.backgroundColor='white';
  188. }
  189. function showError(n) {
  190.  
  191.     bOK = true;
  192.  
  193.     if (!document.gearing.front.value) {
  194.         bOK = false;
  195.         document.gearing.front.style.backgroundColor = 'pink';
  196.     }
  197.     if (!document.gearing.rear.value) {
  198.         bOK = false;
  199.         document.gearing.rear.style.backgroundColor = 'pink';
  200.     }
  201.     if (n==1) {
  202.         if (!document.gearing.param.value) {
  203.             bOK = false;
  204.             document.gearing.param.style.backgroundColor = 'pink';
  205.         }
  206.     } else {
  207.         document.gearing.param.style.backgroundColor = 'white';
  208.     }
  209.    
  210.     if (!bOK) {
  211.         alert('You must enter a value in the red box(es).');
  212.     }
  213.  
  214.     return bOK;
  215. }
  216. function resetForm() {
  217.    
  218.     el = document.getElementById('label1');
  219.     el.innerHTML = 'calculate speed';
  220.     document.gearing.front.style.backgroundColor='white';
  221.     document.gearing.rear.style.backgroundColor='white';
  222.     document.gearing.param.style.backgroundColor='white';
  223.     document.gearing.reset();
  224. }
  225. function showMe(f,v) {
  226.  
  227.     document.gearing[f].value = v;
  228. }
  229. function sendMail() {
  230.  
  231.     var at = "@";
  232.  
  233.     window.location = "mailto:garliestonhouse"+at+"yahoo.co.uk?subject=Performance calculators";
  234. }
Advertisement
Add Comment
Please, Sign In to add comment