/**
 * Formats the given number to the specified decimals.
 */
function fmt (val, numDecs) {
	if ((val == null) || isNaN(val)) { return val; }
	var isRat = false;
	var nCnt = 0, dCnt = 0, rmd = 0;
	var num = new Array();
	val += "";
	for (var i = 0, j = 0; (i < val.length) && (dCnt <= numDecs); ++i) {
		var c = val.charAt(i);
		if (c == '.') {
			num[j++] = c;
			isRat = true;
		} else if (!isNaN(c)) {
			if (dCnt == numDecs) {
				rmd = new Number(c);
			} else {
				num[j++] = new Number(c);
			}
			if (isRat) {
				++dCnt;
			} else {
				++nCnt;
			}
		}
	}
	if (isRat) {
		if (dCnt > numDecs) {
			num = rnd(num, (rmd>4?1:0), num.length-1);
		} else if (dCnt < numDecs) {
			var dif = numDecs - dCnt;
			for (var i = num.length, j = 0; j < dif; ++i, ++j) { num[i] = 0; }
		}
	}
	if (dCnt == 0) {
		num[num.length] = '.';
		for (var i = num.length, j = 0; j < numDecs; ++i, ++j) { num[i] = 0; }
	}
	var tmp = "";
	for (var i = 0; i < num.length; ++i) { tmp += num[i]; }
	return tmp;
}

/**
 * Rounds the given number contained in the nArray.
 */
function rnd(nArray, carry, index) {
	if ((index >= 0) && (index < nArray.length)) {
		if (nArray[index] == '.') { rnd(nArray, carry, --index); }
		nArray[index] += carry;
		if ((index > 0) && (nArray[index] > 9)) {
			nArray[index] = 0;
			rnd(nArray, 1, --index);
		}
	}
	return nArray;
}

/**
 * Function used to call appropriate conversion functions.
 */
function convert(num, from, to) {
	return eval(from+"_"+to)(num);
}

/**
 * Converts cubic centimeters to milliliters
 */
function cc_ml(n) {
	return fmt(n, 2);
}

/**
 * Converts cubic centimeters to ounces.
 */
function cc_oz(n) {
	return fmt((n * 0.0338140226), 2);
}

/**
 * Converts fluid drams (dr) to ounces.
 */
function dr_oz(n) {
	return fmt((n * 0.0625), 2);
}

/**
 * Converts gallons to milliliters.
 */
function gal_ml(n) {
	return fmt((n * 3785.4118), 2);
}

/**
 * Converts gallons to ounces.
 */
function gal_oz(n) {
	return Math.round(n * 128);
}

/**
 * Converts inches to millimeters.
 */
function in_mm(n) {
	return fmt((n * 25.4), 2);
}

/**
 * Converts milliliters to cubic centimeters.
 */
function ml_cc(n) {
	return fmt(n, 2);
}

/**
 * Converts milliliters to gallons.
 */
function ml_gal(n) {
	return fmt((n * 0.000264172051), 2);
}

/**
 * Converts milliliters to ounces.
 */
function ml_oz(n) {
	return Math.round(n * 0.0338140226);
}

/**
 * Converts millimeters to inches.
 */
function mm_in(n) {
	return fmt((n * 0.0393700787), 2);
}

/**
 * Converts ounces to cubic centimeters.
 */
function oz_cc(n) {
	return fmt((n * 29.5735297), 2);
}

/**
 * Converts ounces to fluid drams.
 */
function oz_dr(n) {
	return fmt((n * 16), 2);
}

/**
 * Converts ounces to gallons.
 */
function oz_gal(n) {
	return fmt((n*0.0078125), 2);
}

/**
 * Converts ounces to milliliters.
 */
function oz_ml(n) {
	return Math.round(n * 29.5735297);
}

