/**** beltlengthcalculator ****/

/**** 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() {
	commapoint=0;
	var A = document.methodone.oneCrossSec.value;
	var CS = todouble(A);
	var ID = todouble(document.methodone.InnerDiam.value);
	var OD = todouble(document.methodone.OuterDiam.value);
	var IC = todouble(document.methodone.InnerCircum.value);
	var OC = todouble(document.methodone.OuterCircum.value);

	var CL;
	if (document.methodone.CutLength.value == "Invalid Input") {
		document.methodone.CutLength.value = "";
		CL = 0;
	}
	else
		CL = todouble(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 = commacheck(roundthree(ID));
		document.methodone.OuterDiam.value = commacheck(roundthree(OD));
		document.methodone.InnerCircum.value = commacheck(roundthree(IC));
		document.methodone.OuterCircum.value = commacheck(roundthree(OC));
		document.methodone.CutLength.value = commacheck(roundthree(CL));
	}
	else {
		document.methodone.CutLength.value = "Invalid Input";
	}
}


/**** method two ****/

function mtwo() 
{
	commapoint=0;
	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 = todouble(A);
	var Scross = todouble(B);
	var Bdia = todouble(C);
	var Stretch = todouble(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 = commacheck(roundthree(answer));
}


/**** method three ****/

function TRCalculate() {
	commapoint=0;
	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 = todouble(A);
	var TRPulley1Diam = todouble(B);
	var TRGroove1Depth = todouble(C);
	var TRPulley2Diam = todouble(D);
	var TRGroove2Depth = todouble(E);
	var TRCrossSec = todouble(F);
	var TRStretch = todouble(G);
	var TRAngle = todouble(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 = commacheck(roundthree(answer));

		// report results
		var params =
			"?CS=" + encodeURIComponent(TRCrossSec) +
			"&P1=" + encodeURIComponent(TRPulley1Diam) +
			"&G1=" + encodeURIComponent(TRGroove1Depth) +
			"&P2=" + encodeURIComponent(TRPulley2Diam) +
			"&G2=" + encodeURIComponent(TRGroove2Depth) +
			"&CD=" + encodeURIComponent(TRCenterDistance) +
			"&ST=" + encodeURIComponent(TRStretch) +
			"&AG=" + encodeURIComponent(TRAngle) +
			"&CL=" + encodeURIComponent(roundthree(answer));
		ajax_do_get("calc_results.php" + params);
	}
	else
		document.methodthree.StretchCutLength.value = "Invalid Input"; 
}


/**** method four ****/

function SPCalculate() {
	commapoint=0;
	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 = todouble(A);
	var SPGroove = todouble(B);
	var SPCrossSec = todouble(C);
	var SPStretch = todouble(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 = commacheck(roundthree(SPCutLength));
	}
	else
		document.methodfour.StretchCutLength.value = "Invalid Input"; 
}

