/* Sprocket distance calculator
Copyright 2006 by Dale Heatherington (coded June 13, 2006) */

function center_distance(dataform,pitch,N1,N2,nlinks)
{
	
      /*
          Formula found on internet in a spreadsheet by Matthew J. Adams
         (B1/8) * (2*B4-B2-B3 + SQRT((2*B4-B2-B3)^2-(8/3.14159^2)*((B2-B3)^2)) )
         B1 = chain pitch
	 B2 = Sprocket 1 teeth
	 B3 = Sprocket 2 teeth
	 B4 = Links 
	 
	 Rewritten below in Javascript
	 */
      
	 var a,b,c,d,e;
	 
	 a = pitch / 8;	 
	 b = 2 * nlinks - N1 - N2;	 
	 c = b*b;	 
	 d = 8/(Math.PI * Math.PI);	 
	 e =  N1 - N2 ;
	 e = e*e;
	
     dataform.distance.value = a * (b + Math.sqrt(c - d * e ));
     dataform.ratio.value = N2/N1;
     
 }

function chain_length(dataform,pitch,N1,N2,distance)
{

 /*  
    Formula found on internet in a spreadsheet by Matthew J. Adams
    (2*E4/E1+E2/2+E3/2+((E2-E3)/(2*3.14159))^2/(E4/E1))  
    
 	E1 = chain pitch
	E2 = sprocket 1 teeth
	E3 = sprocket 2 teeth
	E4 = Center distance
     
	Rewritten below in Javascript
  */
  
    var a,b,c;
    
    a = (N1-N2)/(2 * Math.PI);
    a = a * a;
    
    dataform.links.value = (2*distance/pitch+N1/2+N2/2+ a /(distance/pitch));
    dataform.length.value = pitch * dataform.links.value;
    dataform.ratio.value = N2/N1;

}


