var LBS2N = 4.4482216;

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;
}

function ORINGcalculate()
{
	var A = document.ORING.ORINGstretch.value; // Stretch % input
	var B = document.ORING.ORINGdia.value; // Cord dia input
	var Stretch = +A;
	var Dia = +B;

	if (isNaN(Stretch) || Stretch<0 || A=="" ||
		isNaN(Dia) || Dia<0 || B=="") {
		document.ORING.ORINGforce.value = "Invalid Input";
		document.ORING.ORINGtdrop.value = "";
		return;
	}

	// values for first radio button, 85A
	var minval=5;  // <<--- WHAT IS THIS? not used
	var mult=0.0123;

	if (document.ORING.ORINGradio[1].checked) { // The 1 is the second button, first button is 0
		minval=3;
		mult=0.0065;
	}

	if (metric == true)
		Dia /= MM2IN;

	Stretch /= 100;
	var Area = Math.pow(Dia,2) / 4 * Math.PI;  //This is the Area
	var F = (50 * Stretch + ((Stretch<0.04)?0:1)) * Area / mult;   // Calculates needed force
	if (metric == true) {
		F *= LBS2N; // / LBS2KG;
		document.ORING.ORINGforce.value = roundone(F) + " N";  //prints the force
		document.ORING.ORINGtdrop.value = roundone(F*0.6) + " N"; //prints the tension drop after 1 week
	}
	else {
		document.ORING.ORINGforce.value = roundone(F) + " lbs";  //prints the force
		document.ORING.ORINGtdrop.value = roundone(F*0.6) + " lbs"; //prints the tension drop after 1 week
	}
}

function FLATcalculate()
{
	var A = document.FLAT.FLATstretch.value; // Stretch % input
	var B = document.FLAT.FLATthick.value; // Thickness input
	var C = document.FLAT.FLATwidth.value; //width input
	var Stretch = +A;
	var Thick = +B;
	var Width = +C;

	if (isNaN(Stretch) || Stretch<0 || A=="" ||
		isNaN(Thick) || Thick<0 || B=="" ||
		isNaN(Width) || Width<0 || C=="") {
		document.FLAT.FLATforce.value = "Invalid Input";
		document.FLAT.FLATtdrop.value = "";
		return;
	}

	var minval = 5; // <<-- THIS IS NOT USED, WHAT IS IT?
	var mult = 0.0123;

	if (document.FLAT.FLATradio[1].checked) { // The 1 is the second button, first button is 0
		minval=3;
		mult=0.0065;
	}

	if (metric == true) {
		Thick /= MM2IN;
		Width /= MM2IN;
	}

	Stretch /= 100;
	var Area = Width * Thick;  // area
	var F = (50 * Stretch + ((Stretch<0.04)?0:1)) * Area / mult; //Force
	if (metric == true) {
		F /= LBS2N; //LBS2KG;
		document.FLAT.FLATforce.value = roundone(F) + " N";  //displays force
		document.FLAT.FLATtdrop.value = roundone(F*0.6) + " N";// displays tension drop
	}
	else {
		document.FLAT.FLATforce.value = roundone(F) + " lbs";  //displays force
		document.FLAT.FLATtdrop.value = roundone(F*0.6) + " lbs";// displays tension drop
	}
}

var metric;

function convertm() {
	var Otable = document.ORING.getElementsByTagName("table")[0];
	var Ftable = document.FLAT.getElementsByTagName("table")[0];

	metric = !metric;

	if (metric) {
		Otable.rows[2].cells[0].innerHTML = Otable.rows[2].cells[0].innerHTML.replace(/inches/, "mm");
		Otable.rows[4].cells[0].innerHTML = Otable.rows[4].cells[0].innerHTML.replace(/lbs/, "N");
		Otable.rows[5].cells[0].innerHTML = Otable.rows[5].cells[0].innerHTML.replace(/lbs/, "N");

		Ftable.rows[2].cells[0].innerHTML = Ftable.rows[2].cells[0].innerHTML.replace(/inches/, "mm");
		Ftable.rows[3].cells[0].innerHTML = Ftable.rows[3].cells[0].innerHTML.replace(/inches/, "mm");
		Ftable.rows[5].cells[0].innerHTML = Ftable.rows[5].cells[0].innerHTML.replace(/lbs/, "N");
		Ftable.rows[6].cells[0].innerHTML = Ftable.rows[6].cells[0].innerHTML.replace(/lbs/, "N");
	}
	else {
		Otable.rows[2].cells[0].innerHTML = Otable.rows[2].cells[0].innerHTML.replace(/mm/, "inches");
		Otable.rows[4].cells[0].innerHTML = Otable.rows[4].cells[0].innerHTML.replace(/N/, "lbs");
		Otable.rows[5].cells[0].innerHTML = Otable.rows[5].cells[0].innerHTML.replace(/N/, "lbs");

		Ftable.rows[2].cells[0].innerHTML = Ftable.rows[2].cells[0].innerHTML.replace(/mm/, "inches");
		Ftable.rows[3].cells[0].innerHTML = Ftable.rows[3].cells[0].innerHTML.replace(/mm/, "inches");
		Ftable.rows[5].cells[0].innerHTML = Ftable.rows[5].cells[0].innerHTML.replace(/N/, "lbs");
		Ftable.rows[6].cells[0].innerHTML = Ftable.rows[6].cells[0].innerHTML.replace(/N/, "lbs");
	}

	document.cookie = "metric=" + metric;
}

function convert_data() {
	var A = +document.ORING.ORINGdia.value;
	var B = parseFloat(document.ORING.ORINGforce.value);
	var C = parseFloat(document.ORING.ORINGtdrop.value);
	var D = +document.FLAT.FLATthick.value;
	var E = +document.FLAT.FLATwidth.value;
	var F = parseFloat(document.FLAT.FLATforce.value);
	var G = parseFloat(document.FLAT.FLATtdrop.value);

	if (metric) {
		if (A != 0 && !isNaN(A))
			document.ORING.ORINGdia.value = roundtwo(A * MM2IN);
		if (B != 0 && !isNaN(B))
			document.ORING.ORINGforce.value = roundone(B * LBS2N) + " N";
		if (C != 0 && !isNaN(C))
			document.ORING.ORINGtdrop.value = roundone(C * LBS2N) + " N";
		if (D != 0 && !isNaN(D))
			document.FLAT.FLATthick.value = roundtwo(D * MM2IN);
		if (E != 0 && !isNaN(E))
			document.FLAT.FLATwidth.value = roundtwo(E * MM2IN);
		if (F != 0 && !isNaN(F))
			document.FLAT.FLATforce.value = roundone(F * LBS2N) + " N";
		if (G != 0 && !isNaN(G))
			document.FLAT.FLATtdrop.value = roundone(G * LBS2N) + " N";
	}
	else {
		if (A != 0 && !isNaN(A))
			document.ORING.ORINGdia.value = roundtwo(A / MM2IN);
		if (parseFloat(B) != 0 && !isNaN(B))
			document.ORING.ORINGforce.value = roundone(B / LBS2N) + " lbs";
		if (parseFloat(C) != 0 && !isNaN(C))
			document.ORING.ORINGtdrop.value = roundone(C / LBS2N) + " lbs";
		if (D != 0 && !isNaN(D))
			document.FLAT.FLATthick.value = roundtwo(D / MM2IN);
		if (E != 0 && !isNaN(E))
			document.FLAT.FLATwidth.value = roundtwo(E / MM2IN);
		if (parseFloat(F) != 0 && !isNaN(F))
			document.FLAT.FLATforce.value = roundone(F / LBS2N) + " lbs";
		if (parseFloat(G) != 0 && !isNaN(G))
			document.FLAT.FLATtdrop.value = roundone(G / LBS2N) + " lbs";
	}
}

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);
}

