// <![CDATA[
function Convert()
{
	var sCodeFrom, sCodeTo,
		nRateFrom, nRateTo,
		nUnitFrom, nUnitTo,
		nAmount;

	sCodeFrom = code[document.getElementById('select3').selectedIndex - 1];
	sCodeTo   = code[document.getElementById('select').selectedIndex - 1];

	if (!sCodeFrom || !sCodeTo) { return false; }

	nRateFrom = rate[document.getElementById('select3').selectedIndex - 1];
	nRateTo   = rate[document.getElementById('select').selectedIndex - 1];

	nUnitFrom = unit[document.getElementById('select3').selectedIndex - 1];
	nUnitTo   = unit[document.getElementById('select').selectedIndex - 1];

	nAmount = document.getElementById('amount').value;

	if (IsNumeric(nAmount) == false) {
		alert(text);
		return false;
	}

	resultV = roundTo(nAmount * nRateFrom / nUnitFrom / nRateTo * nUnitTo, precision_calc);
	resultO = roundTo(1 * nRateFrom / nUnitFrom / nRateTo * nUnitTo, precision_calc);

	resultV2 = roundTo(nAmount * nRateTo / nUnitTo / nRateFrom * nUnitFrom, precision_calc);
	resultO2 = roundTo(1 * nRateTo / nUnitTo / nRateFrom * nUnitFrom, precision_calc);

	nAmount = roundTo(nAmount, precision_base);

	document.getElementById('result').innerHTML =
		'<span class="body13-bold">' +
		addCommas(nAmount) + ' ' + sCodeFrom +
		' = ' + 
		addCommas(resultV) + ' ' + sCodeTo +
		'</span>' +
		' (1 ' + sCodeFrom + 
		' = ' + addCommas(resultO) + ' ' + sCodeTo + 
		')<br>' + 
		addCommas(nAmount) + ' ' + sCodeTo +
		' = ' + 
		addCommas(resultV2) + ' ' + sCodeFrom +
		' (1 ' + sCodeTo + 
		' = ' + addCommas(resultO2) + ' ' + sCodeFrom + 
		')';

	return false;
}

function roundTo(num, precision)
{
	var multi = Math.pow(10, precision);
	return (Math.round(num * multi) / multi);
}

function addCommas(num)
{
	var n = Math.floor(num);
	var myNum = num + "";
	var myDec = "";

	if (myNum.indexOf('.', 0) > -1) {
		myDec = myNum.substring(myNum.indexOf('.', 0), myNum.length);
	}
	var arr = new Array('0'), i = 0;

	while (n > 0) {
		arr[i] = '' + n % 1000;
		n = Math.floor(n / 1000);
		i++;
	}

	arr = arr.reverse();
	for (var i in arr) if (i > 0) //padding zeros
		while (arr[i].length < 3) arr[i] = '0' + arr[i];

	return arr.join() + myDec;
}

function IsNumeric(strString)
// check for valid numeric strings
// thanks for function to http://www.pbdr.com/vbtips/asp/JavaNumberValid.htm
{
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}
// ]]>