// universal conversions used by other js files
var LBS2KG = 2.20462262;
var MM2IN = 25.4;
var LBS2N = 4.4482216;
var MS2FTM = 196.850394; // m/s to ft/min

var metric; // metric or english?

function roundone(x) { return Math.round(x*10)/10; }
function roundtwo(x) { return Math.round(x*100)/100; }
function roundthree(x) { return Math.round(x*1000)/1000; }
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); }


function CheckStretch(A) {
	var value = todouble(A.value.replace(/%$/,""));
	if (value > 0 && value < 1) {
		alert("Possible entry error!\nYou have entered "+value+" as the Stretch Factor %.\nIf you wanted "+(value*100)+"% enter "+(value*100)+".");
	}
	A.value = value;
}


/* not necessary now
// ex: onkeypress="enter_submits_form(event);"
function enter_submits_form(e)
{
	var key;
	if (window.event) // IE
		key = e.keyCode;
	else
		key = e.which;
	if (key == '\r') {
		if (e.srcElement) { // IE
			e.srcElement.onchange();
			e.srcElement.form.onsubmit();
		}
		else {
			e.target.onchange();
			e.target.form.onsubmit();
		}
	}
}
*/

var commapoint=0; // 1=comma, 0=dont know, assume decimal, -1=decimal

function fraction(me) {
	var input = me.value;
	var slash = input.indexOf("/");
	if (slash == -1 || input.indexOf(".") != -1) {
		me.value = commacheck(todouble(me.value));
		return;
	}
	var space = input.substring(0, slash).lastIndexOf(" ");
	var x;
	if (space != -1) { // space found, eg "1 1/4"
		if (space < slash) { // fraction must come after integer
			x = +(input.substring(0, space)) + +(input.substring(space + 1, slash)) / +(input.substring(slash + 1));
			me.value = (isNaN(x)) ? 0 : x;
		}
	}
	else {
		x = +(input.substring(0, slash)) / +(input.substring(slash + 1));
		me.value = (isNaN(x)) ? 0 : x;
	}
}

function todouble(X) {
	X = X.trim(X);
	if (X == "") return 0;
	var comma = X.indexOf(",");
	if (comma > -1) {
		if (commapoint == 0)
			commapoint = 1;
		X = X.substring(0, comma) + '.' + X.substring(comma+1);
		return parseFloat(X);
	}
	if (X.indexOf(".") > -1) // decimal point found
		commapoint = -1;
	return parseFloat(X);
}

function commacheck(X) {
	X = X.toString();
	if (commapoint > 0) {
		// replace . with ,
		var decimal = X.indexOf(".");
		if (decimal > -1) {
			return X.substring(0, decimal) + ',' + X.substring(decimal+1);
		}
	}
	return X;
}

function convert_mminch(frm)
{
	commapoint=0;
	var A=todouble(frm.inch.value); //parseFloat(frm.inch.value);
	var B=todouble(frm.mm.value); //parseFloat(frm.mm.value);

	if (isNaN(A)) A=0;
	if (isNaN(B)) B=0;
	if (A==0) {
		frm.inch.value = commacheck(roundthree(B/MM2IN));
		frm.mm.value = commacheck(B);
	}
	else {
		frm.inch.value = commacheck(A);
		frm.mm.value = commacheck(roundthree(A*MM2IN));
	}
}

function convert_kglbs(frm)
{
	commapoint=0;
	var A=todouble(frm.kg.value); //parseFloat(frm.kg.value);
	var B=todouble(frm.lbs.value); //parseFloat(frm.lbs.value);
	if (isNaN(A)) A=0;
	if (isNaN(B)) B=0;
	if (A==0) {
		frm.kg.value = commacheck(roundthree(B/LBS2KG));
		frm.lbs.value = commacheck(B);
	}
	else {
		frm.kg.value = commacheck(A);
		frm.lbs.value = commacheck(roundthree(A*LBS2KG));
	}
}


