var convValEditValue = "";

function convEval(declaration, value)
{
	return eval(declaration);
}

function convToBaseUnit(unitInfo, value)
{
	return eval(unitInfo.from);
}

function convFromBaseUnit(unitInfo, value)
{
	return eval(unitInfo.to);
}

function log10(value)
{
	return Math.log(value)/Math.LN10;
}

function pow10(value)
{
	return Math.pow(10, value);
}

function keyPressNS(e)
{
	if (e.which == 13) convert();
}

function keyPressIE() 
{
	if (window.event.keyCode == 13)
	{
		window.event.returnValue = false;
		convert();
	}
}

if (navigator.appName.indexOf('Microsoft') < 0) 
{
	window.captureEvents(Event.KEYPRESS);
	window.onKeyPress = keyPressNS;
}

function validateInput(inp)
{
	return (inp.search(convValRegExp) == 0);
}

function roundResult(res)
{
	var dispExp = Math.abs(res) < 1e-6;
	if (dispExp)
	{
		res = res / 1e10;
	}

	var parts = res.toString().split("e");
	var n, exp;
	
	if (parts.length > 1)
	{	
		res = parseFloat(parts[0]);
		exp = parseInt(parts[1]);
		n = Math.floor(res);
		res = n + Math.round((res - n)*1000000)/1000000;
		var adjust = Math.abs(Math.abs(res) - 10) < 1e-6;
		
		if (dispExp || adjust)
		{
			exp += 10;
			if (adjust)
			{
				res /= 10;
				exp += 1;
			}
		}
			
		return res + "e" + exp;
	}
	else if (dispExp)
	{
		return 0;
	}
	else 
	{ 
		n = Math.floor(res);
		return n + Math.round((res - n)*1000000)/1000000;
	}
}

function resetResult()
{
	var theForm = document.forms["ConvForm"];
	theForm["convResEdit"].value = "";
	theForm["convResEdit"].title = "";

	if (theForm["convValEdit"].style.color != "")
	try
	{
		theForm["convValEdit"].style.color = "";
		theForm["convValEdit"].value = convValEditValue;
	}
	catch (e)
	{
	}
	theForm["convValEdit"].select();
}
