
function BLTcalculate()
{
	var A = document.BLT.BLTmaxweight.value;
	var B = document.BLT.BLTincline.value;
	var C = document.BLT.BLTfriction.value;
	var Maxw = +A;
	var Incline = +B;
	var Friction = +C;

	if (isNaN(Maxw) || Maxw<0 || A=="" ||
		isNaN(Incline) || Incline<0 ||
		isNaN(Friction) || Friction<0 || C=="") {
		document.BLT.BLTanswer.value = "Invalid Input";
		return;
	}
	if (B=="")
		document.BLT.BLTincline.value = "0";

	Incline *= Math.PI / 180; // to radians
	var T = Maxw * (Math.sin(Incline) + Friction * Math.cos(Incline));

	if (metric == true) {
		T *= 9.80665002864; // kg to N
	}

	document.BLT.BLTanswer.value = roundtwo(T);
}

function MPDcalculate()
{
	var A = document.MPD.MPDsmallest.value; // Smallest diameter input
	var B = document.MPD.MPDlength.value; // Length input
	var C = document.MPD.MPDspeed.value;  // Speed input
	var D = document.MPD.MPDrpm.value;  // RPM input or output
	var Small = +A;
	var Length = +B;
	var Speed = +C;
	var RPM = +D;
	
	var	RPM_MULTIPLIER;

	if (isNaN(Small) || Small<=0 || A=="" ||
		isNaN(Length) || Length<=0 || B=="" ||
		isNaN(Speed) || Speed<0 || 
		isNaN(RPM) || RPM<0) {
		document.MPD.MPDm.value = "Invalid Input";
		return;
	}

	if (metric == true) {
		Small /= 1000;
		Length /= 1000;
		RPM_MULTIPLIER =  60; // secs per min
	}
	else {
		Small /= 12;
		Length /= 12;
		RPM_MULTIPLIER = 1;
	}

	if (RPM > 0) { // calculate Speed
		Speed = RPM * Small * Math.PI / RPM_MULTIPLIER;
		document.MPD.MPDspeed.value = (metric == true) ? roundthree(Speed) : roundtwo(Speed);
		document.MPD.MPDrpm.value = RPM;
	}
	else {
		RPM = Speed / (Small * Math.PI) * RPM_MULTIPLIER;  // calculates the RPM
		document.MPD.MPDrpm.value = roundtwo(RPM);
		document.MPD.MPDspeed.value = Speed;
	}

	var M = RPM / Length;  // Calculates M
	if (metric == true) M /= 3.28084; // feet per meter
	document.MPD.MPDm.value = roundtwo(M);
	columnclear();
	if (M < 250)
		columnchange(1);
	else if(M >= 250 && M <= 499)
		columnchange(2);
	else if(M >= 500 && M <= 1000)
		columnchange(3);
}



function columnclear()  //changes column colors
{
	var A=document.getElementById("mtable"); // gets table with id="mtable"
	var B=A.getElementsByTagName("tr"); //  gets all the rows in table id mtable
	var C;
	for (i=1; i < B.length; i++) {
		C=B[i].getElementsByTagName("td"); // gets cells in the table mtable
		C[5].setAttribute("bgcolor", "", 0);
		C[6].setAttribute("bgcolor", "", 0);
		C[7].setAttribute("bgcolor", "", 0);
	}
}

function columnchange(column)  //changes column colors
{
	var A=document.getElementById("mtable"); // gets table with id="mtable"
	var B=A.getElementsByTagName("tr"); //  gets all the rows in table id mtable
	var C;
	for (i=1; i < B.length; i++) {
		C = B[i].getElementsByTagName("td"); // gets cells in the table mtable
		C[column+4].setAttribute("bgcolor", "#FFFFA4", 0);
	}
}


/********************** ENGLISH - METRIC CONVERSION ***********************/

var metric;
var saved_values = new Array();
//var KG = 2.20462262;
//var MM = 25.4;
var MS2FTM = 196.850394; // m/s to ft/min
var LBS2N = 4.4482216;

function convertm() {
	var mtable = document.getElementById("mtable");
	var calc1 = document.getElementById("calc1");
	var calc2 = document.getElementById("calc2");
	var i;

	metric = !metric;

	if (metric) {
		if (!saved_values.length) {
			for (i=2; mtable.rows[i]; i++) {
				saved_values[i] = new Array();
				saved_values[i][0] = mtable.rows[i].cells[3].innerHTML;
				saved_values[i][1] = mtable.rows[i].cells[4].innerHTML;
				saved_values[i][2] = mtable.rows[i].cells[5].innerHTML;
				saved_values[i][3] = mtable.rows[i].cells[6].innerHTML;
				saved_values[i][4] = mtable.rows[i].cells[7].innerHTML;
			}
		}

		calc1.rows[1].cells[0].innerHTML = calc1.rows[1].cells[0].innerHTML.replace(/lbs/, "kg");	
		calc1.rows[5].cells[0].innerHTML = calc1.rows[5].cells[0].innerHTML.replace(/\(lbs\)/, "(N)");	

		calc2.rows[1].cells[0].innerHTML = calc2.rows[1].cells[0].innerHTML.replace(/inches/, "mm");	
		calc2.rows[2].cells[0].innerHTML = calc2.rows[2].cells[0].innerHTML.replace(/inches/, "mm");	
		calc2.rows[4].cells[0].innerHTML = calc2.rows[4].cells[0].innerHTML.replace(/feet per minute/, "meters per sec");	

		mtable.rows[0].cells[1].innerHTML = mtable.rows[0].cells[1].innerHTML.replace(/lbs/, "N");
		mtable.rows[0].cells[2].innerHTML = mtable.rows[0].cells[2].innerHTML.replace(/inches/, "mm");

		for (i=2; mtable.rows[i]; i++) {
			mtable.rows[i].cells[3].innerHTML = Math.round((+saved_values[i][0] * LBS2N) * 10) / 10;
			mtable.rows[i].cells[4].innerHTML = Math.round((+saved_values[i][1] * LBS2N) * 10) / 10;
			if (saved_values[i][2].indexOf("**") == -1)
				mtable.rows[i].cells[5].innerHTML = Math.round(parseFloat(saved_values[i][2]) * MM2IN * 10) / 10;
			else
				mtable.rows[i].cells[5].innerHTML = (Math.round(parseFloat(saved_values[i][2]) * MM2IN * 10) / 10) + " <font color='red'>**</font>";
			if (saved_values[i][3].indexOf("**") == -1)
				mtable.rows[i].cells[6].innerHTML = Math.round(parseFloat(saved_values[i][3]) * MM2IN * 10) / 10;
			else
				mtable.rows[i].cells[6].innerHTML = (Math.round(parseFloat(saved_values[i][3]) * MM2IN * 10) / 10) + " <font color='red'>**</font>";

			if (saved_values[i][4].indexOf("**") == -1)
				mtable.rows[i].cells[7].innerHTML = Math.round(parseFloat(saved_values[i][4]) * MM2IN * 10) / 10;
			else
				mtable.rows[i].cells[7].innerHTML = (Math.round(parseFloat(saved_values[i][4]) * MM2IN * 10) / 10) + " <font color='red'>**</font>";
		}
	}
	else {
		calc1.rows[1].cells[0].innerHTML = calc1.rows[1].cells[0].innerHTML.replace(/kg/, "lbs");	
		calc1.rows[5].cells[0].innerHTML = calc1.rows[5].cells[0].innerHTML.replace(/\(N\)/, "(lbs)");	

		calc2.rows[1].cells[0].innerHTML = calc2.rows[1].cells[0].innerHTML.replace(/mm/, "inches");	
		calc2.rows[2].cells[0].innerHTML = calc2.rows[2].cells[0].innerHTML.replace(/mm/, "inches");	
		calc2.rows[4].cells[0].innerHTML = calc2.rows[4].cells[0].innerHTML.replace(/meters per sec/, "feet per minute");
		
		mtable.rows[0].cells[1].innerHTML = mtable.rows[0].cells[1].innerHTML.replace(/N/, "lbs");
		mtable.rows[0].cells[2].innerHTML = mtable.rows[0].cells[2].innerHTML.replace(/mm/, "inches");

		if (saved_values.length) {
			for (i=2; mtable.rows[i]; i++) {
				mtable.rows[i].cells[3].innerHTML = saved_values[i][0];
				mtable.rows[i].cells[4].innerHTML = saved_values[i][1];
				mtable.rows[i].cells[5].innerHTML = saved_values[i][2];
				mtable.rows[i].cells[6].innerHTML = saved_values[i][3];
				mtable.rows[i].cells[7].innerHTML = saved_values[i][4];
			}
		}
	}

	document.cookie = "metric=" + metric;
}

function convert_data() {
	var A = +document.BLT.BLTmaxweight.value;
	var B = +document.BLT.BLTanswer.value;
	var C = +document.MPD.MPDsmallest.value;
	var D = +document.MPD.MPDlength.value;
	var E = +document.MPD.MPDspeed.value;

	if (metric) {
		if (A != 0 && !isNaN(A))
			document.BLT.BLTmaxweight.value = roundtwo(A / LBS2KG);
		if (B != 0 && !isNaN(B))
			document.BLT.BLTanswer.value = roundtwo(B / LBS2KG);

		if (C != 0 && !isNaN(C))
			document.MPD.MPDsmallest.value = roundtwo(C * MM2IN); // in to mm
		if (D != 0 && !isNaN(D))
			document.MPD.MPDlength.value = roundtwo(D * MM2IN); // in to mm
		if (E != 0 && !isNaN(E))
			document.MPD.MPDspeed.value = roundthree(E / MS2FTM); // ft/min to m/s
	}
	else {
		if (A != 0 && !isNaN(A))
			document.BLT.BLTmaxweight.value = roundtwo(A * LBS2KG);
		if (B != 0 && !isNaN(B))
			document.BLT.BLTanswer.value = roundtwo(B * LBS2KG);

		if (C != 0 && !isNaN(C))
			document.MPD.MPDsmallest.value = roundtwo(C / MM2IN);
		if (D != 0 && !isNaN(D))
			document.MPD.MPDlength.value = roundtwo(D / MM2IN);
		if (E != 0 && !isNaN(E))
			document.MPD.MPDspeed.value = roundtwo(E * MS2FTM);
	}
}

function convert_init() {
	if (get_cookie) { // THIS RELIES ON GET_COOKIE ALREADY DEFINED
		if (get_cookie("metric") == "true") {
			metric = false; // convertm flips this
			convertm();
			document.getElementsByName("convert")[1].checked = true; // in case another page set the metric cookie
		}
		else {
			metric = false;
			document.getElementsByName("convert")[0].checked = true;
		}
	}
}

if (window.addEventListener) {
	window.addEventListener("load", convert_init, false);
}
else if (window.attachEvent) {
	window.attachEvent("onload", convert_init);
}

