/**** beltlengthcalculator ****/

function CheckStretch(A) {
	var input = A.value.trim();
	var value = +input.replace(/%$/,"");
	if (value > 0 && value < 1) {
		alert("Possible entry error!\nYou have entered "+value+" as the Stretch Factor %.\nIf you meant to calculate for "+(value*100)+"% enter "+(value*100)+"");
		}
	A.value = value;
}

/**** method one ****/

function methodoneClearOthers(me)
{
	if (me.value == '') return;
	for (var i=1; i < me.form.elements.length; i++) { //i=1 skip the first one
		if (me != me.form.elements[i] && me.form.elements[i].type == "text")
			me.form.elements[i].value = "";
	}
}

function methodoneCalculate() {
	var A = document.methodone.oneCrossSec.value;
	var CS = +A;
	var ID = +document.methodone.InnerDiam.value;
	var OD = +document.methodone.OuterDiam.value;
	var IC = +document.methodone.InnerCircum.value;
	var OC = +document.methodone.OuterCircum.value;
	var CL = +document.methodone.CutLength.value;

	if (isNaN(CS) || CS < 0 || A=="" ||
		isNaN(ID) || ID < 0 ||
		isNaN(OD) || OD < 0 ||
		isNaN(IC) || IC < 0 ||
		isNaN(OC) || OC < 0 ||
		isNaN(CL) || CL < 0) {
		document.methodone.CutLength.value = "Invalid Input";
		return;
	}
	
	if (ID > 0) {
		OD = ID + 2 * CS;
		IC = ID * Math.PI;
		OC = OD * Math.PI;
		CL = (ID+CS) * Math.PI;
	 }
	 else if (OD > 0) {
		ID = OD - 2 * CS;
		IC = ID * Math.PI;
		OC = OD * Math.PI;
		CL = (OD-CS) * Math.PI;
	 }
	 else if (IC > 0) {
		ID = IC/Math.PI;
		OD = ID + 2 * CS;
		OC = OD * Math.PI;
		CL = IC + Math.PI * CS;
	 }
	 else if (OC > 0) {
		OD = OC/Math.PI;
		ID = OD - 2 * CS;
		IC = ID * Math.PI;
		CL = OC - CS * Math.PI;
	 } 
	 else if (CL > 0) {
		IC = CL - CS * Math.PI;
		OC = CL + CS * Math.PI;
	 	OD = OC / Math.PI;
		ID = IC / Math.PI;
	 }
	 else {
		ID = 0;
		OD = 0;
		IC = 0;
		OC = 0;
		CL = 0;
	}

    // Check that the result is a finite number. If so, display the results

	if (!isNaN(CL) && CL > 0 &&
		CL != Number.POSITIVE_INFINITY &&
		CL != Number.NEGATIVE_INFINITY)  {
		document.methodone.InnerDiam.value = roundthree(ID);
		document.methodone.OuterDiam.value = roundthree(OD);
		document.methodone.InnerCircum.value = roundthree(IC);
		document.methodone.OuterCircum.value = roundthree(OC);
		document.methodone.CutLength.value = roundthree(CL);
	}
	else {
		document.methodone.CutLength.value = "Invalid Input";
	}
}


/**** method two ****/

function mtwo() 
{
	var A = document.methodtwo.slength.value; // This is String length
	var B = document.methodtwo.sdiameter.value; // This is String cross section diameter
	var C = document.methodtwo.bdiameter.value; // This is the Belt Diameter
	var D = document.methodtwo.stretch.value; //This is the stretch factor
	var Slen = +A;
	var Scross = +B;
	var Bdia = +C;
	var Stretch = +D;

	if (isNaN(Slen) || Slen<0 || A=="" ||
		isNaN(Scross) || Scross<0 ||
		isNaN(Bdia) || Bdia<0 || C=="" ||
		isNaN(Stretch) || Stretch<0) {
		document.methodtwo.answer.value = "Invalid Input";
		return;
	}
	if (B=="")
		document.methodtwo.sdiameter.value = "0";
	if (D=="")
		document.methodtwo.stretch.value = "0";

	Stretch /= 100;
	Stretch += 1.0;

	var PitchDiaString = Slen/Math.PI;
	var PitchDiaBelt = PitchDiaString - Scross + Bdia;
	var PC = Math.PI * PitchDiaBelt; // Pitch Circumference
	var answer = PC/Stretch; // with stretch
	document.methodtwo.answer.value = roundthree(answer);
}


/**** method three ****/

function TRCalculate() {
	var A = document.methodthree.CenterDistance.value;
	var B = document.methodthree.Pulley1.value;
	var C = document.methodthree.Groove1.value;
	var D = document.methodthree.Pulley2.value;
	var E = document.methodthree.Groove2.value;
	var F = document.methodthree.threeCrossSec.value;
	var G = document.methodthree.threeStretch.value;
	var H = document.methodthree.Angle.value;
	var TRCenterDistance = +A;
	var TRPulley1Diam = +B;
	var TRGroove1Depth = +C;
	var TRPulley2Diam = +D;
	var TRGroove2Depth = +E;
	var TRCrossSec = +F;
	var TRStretch = +G;
	var TRAngle = +H;

	
	if (isNaN(TRCenterDistance) || TRCenterDistance < 0 || A=="" ||
		isNaN(TRPulley1Diam) || TRPulley1Diam < 0 || B=="" ||
		isNaN(TRGroove1Depth) || TRGroove1Depth < 0 ||
		isNaN(TRPulley2Diam) || TRPulley2Diam < 0 || D=="" ||
		isNaN(TRGroove2Depth) || TRGroove2Depth < 0 ||
		isNaN(TRCrossSec) || TRCrossSec < 0 || F=="" ||
		isNaN(TRStretch) || TRStretch < 0 ||
		isNaN(TRAngle) || TRAngle < 0) {
		document.methodthree.StretchCutLength.value = "Invalid Input";
		return;
	}

	if (C=="")
		document.methodthree.Groove1.value = "0";
	if (E=="")
		document.methodthree.Groove2.value = "0";
	if (G=="")
		document.methodthree.threeStretch.value = "0";
	if (H=="")
		document.methodthree.Angle.value = "0";

	if (TRAngle > 180) {
		document.methodthree.Angle.value = "180";
		TRAngle = 180;
	}

	TRStretch /= 100;
	TRStretch += 1.0;

	// account for stretching the cord and thinning
//	TRCrossSec = TRCrossSec / Math.sqrt(TRStretch);
	 

/*
	var InsideDiam1 = ((TRPulley1Diam * 1.0)-(TRGroove1Depth * 2.0));
	var PitchDiam1 = InsideDiam1 + (TRCrossSec * 1.0);

	var InsideDiam2 = ((TRPulley2Diam * 1.0)-(TRGroove2Depth * 2.0));
	var PitchDiam2 = InsideDiam2 + (TRCrossSec * 1.0);
	
	var Angle = Math.asin((PitchDiam1-PitchDiam2)/(2.0*TRCenterDistance));
	var Term1 = (2.0*TRCenterDistance*Math.cos(Angle));
	var Term2 = ((PI / 2.0)*(PitchDiam1+PitchDiam2));
	var Term3 = (Angle*(PitchDiam1-PitchDiam2));
	var Divisor = (TRStretch);
	
	var TRCutLength = ((Term1+Term2+Term3)/Divisor);

      if ((!(isNaN(TRCutLength)) && 
        (TRCutLength != Number.POSITIVE_INFINITY) &&
        (TRCutLength != Number.NEGATIVE_INFINITY)) &&
	  (TRCutLength > 0)) {
	var answer =TRCutLength;
	//document.methodthree.StretchCutLength.value=Math.round((answer*1)*100)/100;
*/

	var P1radius = (TRPulley1Diam + TRCrossSec)/2 - TRGroove1Depth;
	var P2radius = (TRPulley2Diam + TRCrossSec)/2 - TRGroove2Depth;

	if (P1radius < 0 || P2radius < 0) {
		alert("Pulley dimensions are invalid.");
		return;
	}

	if (TRPulley1Diam+TRPulley2Diam > 2*TRCenterDistance) {
		alert("Center distance is too short for pulleys.");
		return;
	}

	var answer = Math.pow(P1radius, 2) + Math.pow(P2radius, 2) +
		Math.pow(TRCenterDistance, 2) - 2 * P1radius * P2radius * Math.cos(TRAngle * Math.PI/180);
	answer = Math.sqrt(answer) * 2;
	answer += Math.PI * (P1radius + P2radius);
	answer /= TRStretch;

	if (!isNaN(answer) && answer > 0 &&
		answer != Number.POSITIVE_INFINITY &&
		answer != Number.NEGATIVE_INFINITY) {
		//document.getElementById("BeltCutLength").innerHTML = Math.round(answer*100)/100;
		document.methodthree.StretchCutLength.value = roundthree(answer);
	}
	else
		document.methodthree.StretchCutLength.value = "Invalid Input"; 
}


/**** method four ****/

function SPCalculate() {
	var A = document.methodfour.Roller.value;
	var B = document.methodfour.Groove.value;
	var C = document.methodfour.fourCrossSec.value;
	var D = document.methodfour.fourStretch.value;
	var SPRoller = +A;
	var SPGroove = +B;
	var SPCrossSec = +C;
	var SPStretch = +D;

	if (isNaN(SPRoller) || SPRoller < 0 || A=="" ||
		isNaN(SPGroove) || SPGroove < 0 ||
		isNaN(SPCrossSec) || SPCrossSec < 0 || C=="" ||
		isNaN(SPStretch) || SPStretch < 0) {
		document.methodfour.StretchCutLength.value = "Invalid Input";
		return;
	}

	if (B=="")
		document.methodfour.Groove.value = 0;
	if (D=="")
		document.methodfour.fourStretch.value = 0;

	SPStretch /= 100;
	SPStretch += 1.0;
	
	var InsideDiameter = SPRoller - 2 * SPGroove;
	var SPCutLength = Math.PI * (InsideDiameter + SPCrossSec)/SPStretch;
	
	if (!isNaN(SPCutLength) && SPCutLength > 0 &&
		SPCutLength != Number.POSITIVE_INFINITY &&
		SPCutLength != Number.NEGATIVE_INFINITY) {
		document.methodfour.StretchCutLength.value = roundthree(SPCutLength);
	}
	else
		document.methodfour.StretchCutLength.value = "Invalid Input"; 
}
