/**************************************************************************************

*  Library:	geargrndr.js

*

*  Description:	This library contains functions specific to calculating 

*               gear ratios and all related interesting numbers related

*               to Jr Dragster drive trains.

* Author:  Chris Schnick  December 7, 1999

* Special thanks to Chris Clark, Ron Pope and Jamie Vernon

*

***************************************************************************************/



function isEmpty(s)

{

   return ((s == null) || (s.length == 0))

}



function isDigit (c)

{   

	return ((c >= "0") && (c <= "9"))

}



function isInteger (s)

{   var i;



    if (isEmpty(s)) 

       if (isInteger.arguments.length == 1) return false;

       else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++)

    {   

        var c = s.charAt(i);

        if (!isDigit(c)) return false;

    }

    return true;

}



function isSignedInteger (s)

{   

	if (isEmpty(s)) 

       if (isSignedInteger.arguments.length == 1) return false;

       else return (isSignedInteger.arguments[1] == true);



    else {

        var startPos = 0;

        var secondArg = false;



        if (isSignedInteger.arguments.length > 1)

            secondArg = isSignedInteger.arguments[1];



        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )

           startPos = 1;    

        return (isInteger(s.substring(startPos, s.length), secondArg))

    }

}



function isPositiveInteger (s) 

{

	var secondArg = false;

	

    if (isPositiveInteger.arguments.length > 1)

        secondArg = isPositiveInteger.arguments[1];



    return (isSignedInteger(s, secondArg)

         && ( (isEmpty(s) && secondArg)  || (parseInt(s) > 0) ) );

}



function isFloat (s)

{   var i;

    var seenDecimalPoint = false;



    if (isEmpty(s)) 

       if (isFloat.arguments.length == 1) return false;

       else return (isFloat.arguments[1] == true);



    if (s == ".") return false;



    for (i = 0; i < s.length; i++)

    {   

        var c = s.charAt(i);



        if ((c == ".") && !seenDecimalPoint) seenDecimalPoint = true;

        else if (!isDigit(c)) return false;

    }



    return true;

}



function validateFields(form) 

{

	var editOkay = true;

	var alertMsg = "";

	var errMsgPrefix = "The field, ";

	var errIntMsgSuffix = " must be a positive interger value.";

	var errNumMsgSuffix = " must be an numeric value (may include decimals).";

	

	var EngineRPM = form.rpm.value;

	if (isPositiveInteger(EngineRPM)){

		}

	else {

		editOkay = false;

		alertMsg = alertMsg + errMsgPrefix + " 'Engine RPM' " + errIntMsgSuffix + "\n\n"; 

		}



	var ClutchRatio = form.fcr.value;

	if (isFloat(ClutchRatio)){}

	else {

		editOkay = false;

		alertMsg = alertMsg + errMsgPrefix + " 'Final Clutch Ratio' " + errNumMsgSuffix + "\n\n"; 

		}



	var FrontSprocket = form.fsp.value;

	if (isPositiveInteger(FrontSprocket)){}

	else {

		editOkay = false;

		alertMsg = alertMsg + errMsgPrefix + " 'Front Sprocket' " + errIntMsgSuffix + "\n\n"; 

		}



	var RearSprocket = form.rsp.value;

	if (isPositiveInteger(RearSprocket)){}

	else {

		editOkay = false;

		alertMsg = alertMsg + errMsgPrefix + " 'Rear Sprocket' " + errIntMsgSuffix + "\n\n"; 

		}



	var TireRollout = form.tro.value;

	if (isFloat(TireRollout)){}

	else {

		editOkay = false;

		alertMsg = alertMsg + errMsgPrefix + " 'Tire Rollout' " + errNumMsgSuffix + "\n\n";

		}


	var NewTireRollout = form.ntro.value;

	if (isFloat(NewTireRollout)){}

	else {

		editOkay = false;

		alertMsg = alertMsg + errMsgPrefix + " 'New Tire Rollout' " + errNumMsgSuffix + "\n\n";

		}



	if (editOkay)

		{

		return true

		}

	else

		{

		alert(alertMsg);

		return false

		}	

}


function validateRollout(form) 
{
	var editOkay = true;
	var alertMsg = "";
	var errMsgPrefix = "The field, ";
	var errIntMsgSuffix = " must be a positive interger value.";
	var errNumMsgSuffix = " must be an numeric value (may include decimals).";

	var TireDiameter = form.tds.value;
	if (isFloat(TireDiameter)){}
	else {
		editOkay = false;
		alertMsg = alertMsg + errMsgPrefix + " 'Tire Diameter' " + errNumMsgSuffix + "\n\n"; 
		}

	if (editOkay)
		{
		return true
		}
	else
		{
		alert(alertMsg);
		return false
		}	
}


function swapImage(imgName, imgSrc)
{
window.document[imgName].src=imgSrc;
}


function computeAnalysis(form) 

{

	if (validateFields(form)) {

		form.fcr.value = parseFloat(parseInt(parseFloat(form.fcr.value)*100)/100);

		form.jar.value = (1 / parseFloat(form.fcr.value)) * parseFloat(form.rpm.value)
		form.jar.value = parseFloat(parseInt(parseFloat(form.jar.value)*1)/1);

		form.ratio.value = parseInt(form.rsp.value) / parseInt(form.fsp.value);
		form.ratio.value = parseFloat(parseInt(parseFloat(form.ratio.value)*1000)/1000);
		
		form.mph.value = (parseFloat(form.jar.value) / parseFloat(form.ratio.value) * parseFloat(form.tro.value) / 1056);
		form.mph.value = parseFloat(parseInt(parseFloat(form.mph.value)*1000)/1000);

		form.nratio.value = parseFloat(parseInt((1/ ((parseFloat(form.mph.value) * 1056) / (parseFloat(form.jar.value) * parseFloat(form.ntro.value))))*1000)/1000);

		if (parseInt(form.rpm.value) <= 3600) {
			swapImage("tach", "tachlow.gif");
		}
		else { if (parseInt(form.rpm.value) < 6000) {
			swapImage("tach", "tachmed.gif");
			}
		     else
			{
			swapImage("tach", "tachhigh.gif");
			}
		}

		if (parseInt(form.fcr.value) <  1) {
			swapImage("clutch", "cl_ovrdrv.gif");
		}
		else { if (parseFloat(form.fcr.value) == 1) {
			swapImage("clutch", "cl_one.gif");
			}
		     else
			{
			swapImage("clutch", "cl_low.gif");
			}
		}

		if (parseInt(form.mph.value) <  4) {
			swapImage("mphpic", "mphTrike.gif");
		}
		else { 	if (parseFloat(form.mph.value) <= 9)
			{
				swapImage("mphpic", "mphbike.gif");
			}
			else { 	if (parseFloat(form.mph.value) <= 100) 
				{
					swapImage("mphpic", "mphjrdrag.gif");
				}
				else {	if (parseFloat(form.mph.value) <= 180) 
					{
						swapImage("mphpic", "mphcar.gif");
					}
					else
					{
						swapImage("mphpic", "mphTF.gif");
					}
				}
			}
		}





		return true

		}

	else {

		form.jar.value = "";
		form.ratio.value = "";
		form.mph.value = "";
		form.nratio.value = "";

		return false

	}

}

function computeRollout(form) 

{
	if (validateRollout(form)) {
		form.estrollout.value = parseFloat(parseInt(parseFloat(form.tds.value)*314.15962)/100);
	}
}


function initAnalysis() 

{
	computeAnalysis(window.document.theForm);
	computeRollout(window.document.theForm);
}



function plus(current, amount, form) 

{
	current.value = (parseFloat(current.value)*1000 + (amount*1000)) / 1000;
	computeAnalysis(form);
	computeRollout(form);
}



function minus(current, amount, form) 

{
	current.value = (parseFloat(current.value)*1000 - (amount*1000)) / 1000;
	computeAnalysis(form);
	computeRollout(form);
}

