// universal conversions used by other js files
var LBS2KG = 2.20462262;
var MM2IN = 25.4;

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 fraction(me) {
	var input = me.value.trim();
	var slash = input.indexOf("/");
	var space = input.indexOf(" ");
	if (slash != -1 && input.indexOf(".") == -1) {
		if (space != -1) { // space found, eg "1 1/4"
			if (space < slash) { // fraction must come after integer
				me.value = parseInt(input) + parseInt(input.substring(space + 1, slash)) / parseInt(input.substring(slash + 1))
			}
		}
		else {
			me.value = parseInt(input.substring(0, slash)) / parseInt(input.substring(slash + 1))
		}
	}
}

/* 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();
		}
	}
}
*/

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

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

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

