Skip to content

Instantly share code, notes, and snippets.

@qoelet
Created April 25, 2012 02:46
Show Gist options
  • Save qoelet/2485773 to your computer and use it in GitHub Desktop.
Save qoelet/2485773 to your computer and use it in GitHub Desktop.
var nWheel=1
// was 0
var modeval=1
//modeval is 1 for gear inches, 12.532 for meters, crank _diam._ for gain ratio
//this currently is not working
var cassette=1
// tjl
// Extra multiplier for MPH data - based on GearInches
var mphmult=1
var modename
//var hubMult
var numHubs
var hubname
var nHub
//----------------------------------------------------------
function get_rings()
{
rings = new Array(parseFloat(document.ringform.ring1.value),
parseFloat(document.ringform.ring2.value),
parseFloat(document.ringform.ring3.value),
0);
return rings;
}
function get_rears()
{
rears = new Array(parseFloat(document.ringform.rear1.value),
parseFloat(document.ringform.rear2.value),
parseFloat(document.ringform.rear3.value),
parseFloat(document.ringform.rear4.value),
parseFloat(document.ringform.rear5.value),
parseFloat(document.ringform.rear6.value),
parseFloat(document.ringform.rear7.value),
parseFloat(document.ringform.rear8.value),
parseFloat(document.ringform.rear9.value),
parseFloat(document.ringform.rear10.value),
0);
return rears;
}
//----------------------------------------------------------
function calc_rings(rings, rears)
{
wheelval=parseFloat(nWheel);
ringData = new Array(5);
for (n=0; rings[n]>0; n++) {
ringData[n] = new Array(15);
for (c=0; rears[c]>0; c++) {
ringData[n][c]=mphmult*(rings[n]/rears[c])*wheelval/nMode;
// tjl - Do rounding at printout instead - ringData[n][c]=rounder(mphmult*(rings[n]/rears[c])*wheelval/nMode);
}
}
return ringData;
}
//----------------------------------------------------------
function converter()
{
var viewInteger, viewString
viewInteger=document.ringform.diameter.selectedIndex
viewString=document.ringform.diameter.options[viewInteger].value
tiresize=document.ringform.diameter.options[viewInteger].text
nWheel=parseFloat(viewString)
viewInteger=document.ringform.crankdiam.selectedIndex
viewString=document.ringform.crankdiam.options[viewInteger].value
cranklength=document.ringform.crankdiam.options[viewInteger].text
nCrank=parseFloat(viewString)
viewInteger=document.ringform.modeval.selectedIndex
viewString=document.ringform.modeval.options[viewInteger].value
modename=document.ringform.modeval.options[viewInteger].text
nMode=parseFloat(viewString)
//tjl
if(nMode >= 30){
mphmult = nMode / 336.135
nMode = 1
}else{
mphmult = 1.0
}
viewInteger=document.ringform.cassette.selectedIndex
viewString=document.ringform.cassette.options[viewInteger].value
cassettename=document.ringform.cassette.options[viewInteger].text
nCassette=viewString
viewInteger=document.ringform.hubmodel.selectedIndex
viewString=document.ringform.hubmodel.options[viewInteger].value
hubname=document.ringform.hubmodel.options[viewInteger].text
nHub = viewString
if (nMode==0)
{
nMode=nCrank
}
main()
}
//----------------------------------------------------------
//
// This function rounds the passed number to one decimal place
// and returns it as a string.
//
// Future Enhancement: It should probably check to make sure that
// the variable passed in *is* a number.
//
function rounder(n)
{
strGearTemp = String(Math.round(n*10) / 10.0);
if (strGearTemp.indexOf(".") < 0) {
strGearTemp += ".0";
}
return strGearTemp;
}
//----------------------------------------------------------
// tjl
function rounder2(n)
{
strGearTemp = String(Math.round(n*100) / 100.0);
if (strGearTemp.indexOf(".") < 0) {
strGearTemp += ".0";
}
return strGearTemp;
}
//----------------------------------------------------------
function main()
{
rings = get_rings();
if((rings[0] == 0) || isNaN(rings[0])) {
alert("Enter a chainring value!");
} else {
if (nCassette == "Custom") {
rears = get_rears();
} else {
rears = parse_cassette();
}
ringData = calc_rings(rings, rears);
hubMult = parse_hubmodel(nHub)
numHubs = hubMult.length - 1
showit(ringData);
}
}
//----------------------------------------------------------
function parse_hubmodel(){
temp_hubs = nHub.split("-",14);
hubs = new Array(temp_hubs.length+1);
hubs = temp_hubs;
hubs[temp_hubs.length] = 0;
return hubs;
}
//----------------------------------------------------------
function parse_cassette(){
temp_rears = nCassette.split("-",14);
rears = new Array(temp_rears.length+1);
rears = temp_rears;
rears[temp_rears.length] = 0;
return rears;
}
//----------------------------------------------------------
// **********percentage-rear
function percentrear(){
var percentage=1
percentage=rounder(rears[c+1]/rears[c]*100-100)
if (rears[c]>rears[c+1])
{
percentage=rounder(rears[c]/rears[c+1]*100-100)
}
if (rears[c+1]>0)
{
gearwin.document.write("<TR><TD align=center ><FONT SIZE=-1>"+percentage+" %<\/FONT>");
}
}
//----------------------------------------------------------
// **********percentage-front
function percentfront1()
{
var percentage=1
percentage=rounder(rings[0]/rings[1]*100-100)
if (rings[0]<rings[1])
{
percentage=rounder(rings[1]/rings[0]*100-100)
}
if (rings[1]>0)
{
gearwin.document.write("<TD align=center width=12%><FONT SIZE=-1>"+percentage+" %<\/FONT><\/TD>");
}
}
//----------------------------------------------------------
function percentfront2()
{
var percentage=1
percentage=rounder(rings[1]/rings[2]*100-100)
if (rings[1]<rings[2])
{
percentage=rounder(rings[2]/rings[1]*100-100)
}
if (rings[2]>0)
{
gearwin.document.write("<TD align=center width=12%><FONT SIZE=-1>"+percentage+" %<\/FONT><\/TD>");
}
}
//----------------------------------------------------------
// **********output-big
function showit(ringData)
{
// gearwin=open("format.html","secwin","width=400,height=450,toolbar=no,scrollbars=yes,menubar");
gearwin=open("","secwin");
gearwin.document.write("<BODY background=../images/beige.gif>")
gearwin.document.write("<FORM><H3> <\/H3><br><br>")
//tjl
if (mphmult != 1)
gearwin.document.write("<CENTER><H2>Gear chart using "+modename+"<\/H2><\/CENTER>")
else if (nMode==1)
gearwin.document.write("<CENTER><H2>Gear chart using <A HREF=../gloss_g.html#gearinch>Gear Inches<\/A><\/H2><\/CENTER>")
else if (nMode==12.532)
gearwin.document.write("<CENTER><H2>Gear chart using <A HREF=../gloss_da-o.html#development>Meters Development<\/A><\/H2><\/CENTER>")
else
gearwin.document.write("<CENTER><H2>Gear chart using <A HREF=../gain.html>Gain Ratios<\/A><\/H2><\/CENTER>")
gearwin.document.write("<CENTER><i><h3>For "+tiresize+" tire with "+cranklength+" cranks<\/h3><\/i><\/CENTER>")
gearwin.document.write("<CENTER><i><H3>With " +cassettename+ " Cassette<\/H3><\/i>")
if(numHubs > 1){
gearwin.document.write("<CENTER><i><H3>With " +hubname+ " Rear Hub<\/H3><\/i>")
}
// big table to hold three columns of tables
gearwin.document.write("<CENTER><TABLE border=1 cellpadding=2><TR>")
if(numHubs > 1){
for(hub=numHubs-1; hub>=0; hub--) {
// tjl - first label tables (if more than one)
if(hub == 0){
gearwin.document.write("<TH bgcolor=yellow><font color=red>"+rounder2(hubMult[hub])+" (High)<\/font><\/TH>");
}else if(hub == (numHubs-1)){
gearwin.document.write("<TH bgcolor=yellow><font color=red>"+rounder2(hubMult[hub])+" (Low)<\/font><\/TH>");
}else{
gearwin.document.write("<TH bgcolor=yellow><font color=red>"+rounder2(hubMult[hub])+"<\/TH>");
}
}
gearwin.document.write("<\/TR><TR>");
}
for(hub=numHubs-1; hub>=0; hub--) {
gearwin.document.write("<TD><CENTER><TABLE border=1 cellpadding=2><TR align=center ><TD><\/TD> <TH bgcolor=yellow ><font color=red>"+rings[0]+"<\/font><\/TH>");
// ********** header of output
if(rings[1]>0)
{
percentfront1()
gearwin.document.write("<TH bgcolor=yellow><font color=red>"+rings[1]+"<\/font><\/TH>");
if(rings[2]>0)
{
percentfront2()
gearwin.document.write("<TH bgcolor=yellow ><font color=red>"+rings[2]+"<\/font><\/TH>");
}
}
gearwin.document.write("<\/TR>")
// ******** loop to calculate gears for each sprocket
for (c=0; rears[c]>0; c++)
{
gearwin.document.write("<TR><TH bgcolor=yellow><FONT COLOR=RED>"+rears[c]+"<\/FONT><\/TH><TH bgcolor=white>"+rounder(hubMult[hub]*ringData[0][c])+"<\/TH>");
if (rings[1]>0)
{
gearwin.document.write("<TD><\/TD><TH bgcolor=white>"+rounder(hubMult[hub]*ringData[1][c])+"<\/TH>");
if (rings[2]>0)
{
gearwin.document.write("<TD><\/TD><TH bgcolor=white>"+rounder(hubMult[hub]*ringData[2][c])+"<\/TH>")
}
}
gearwin.document.write("<\/TR>")
percentrear()
}
gearwin.document.write("<\/TABLE>")
//tjl
// end first big table
gearwin.document.write("<\/TD>")
}
gearwin.document.write("<\/TR><\/TABLE>")
gearwin.document.write("<P>This small version of the chart can be printed, cut out, and taped to your bicycle for reference.<P>");
// tjl - another big loop for a row of tables for the stickers
gearwin.document.write("<CENTER><TABLE border=1 cellpadding=0><TR>")
if(numHubs > 1){
for(hub=numHubs-1; hub>=0; hub--) {
// tjl - first label tables (if more than one)
if(hub == 0){
gearwin.document.write("<TH><font size=-1>High<\/font><\/TH>");
}else if(hub == (numHubs-1)){
gearwin.document.write("<TH><font size=-1>Low<\/font><\/TH>");
}else{
gearwin.document.write("<TH>-<\/TH>");
}
}
gearwin.document.write("<\/TR><TR>");
}
for(hub=numHubs-1; hub>=0; hub--) {
// ******** loop to calculate sticker
gearwin.document.write("<TD><TABLE border cellpadding=0 bgcolor=white>");
for (c=0; rears[c]>0; c++)
{
gearwin.document.write("<TR><TH><font size=-1>"+rounder(hubMult[hub]*ringData[0][c])+"<\/font><\/TH>");
if(rings[1]>0)
{
gearwin.document.write("<TH><font size=-1>"+rounder(hubMult[hub]*ringData[1][c])+"<\/font><\/TH>");
if(rings[2]>0)
gearwin.document.write("<TH><font size=-1>"+rounder(hubMult[hub]*ringData[2][c])+"<\/font><\/TH>")
}
gearwin.document.write("<\/TR>")
}
gearwin.document.write("<\/TABLE>")
//tjl
// end big table
gearwin.document.write("<\/TD>")
}
gearwin.document.write("<\/TR><\/TABLE>")
//tjl
gearwin.document.write("<P><INPUT TYPE=button VALUE=PRINT onClick=print(this.form)>")
gearwin.document.write("<INPUT TYPE=button VALUE=PRINT onClick=window.print(this)>")
gearwin.document.write("<INPUT TYPE=button VALUE=CLOSE onClick=window.close(this)>")
gearwin.document.write("<H3>Note: This is under construction, and the print function does not yet work.<\/H3>")
gearwin.document.write("<\/FORM>")
gearwin.document.write("<H4>Gear Chart by <A HREF=../home.html>Sheldon Brown<\/A><P><A HREF=index.html>http://sheldonbrown.com/gears<\/A><\/H4>")
gearwin.document.write("<\/BODY>")
gearwin.document.write("")
gearwin.document.close ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment