//Dropdown menu to navigate to other calculators
function calcNav(navForm) {
	var URL = document.navForm.calcPage.options[document.navForm.calcPage.selectedIndex].value;
	window.location.href = URL;
}


//CALCULATOR SCRIPT
function perRound(num, precision) {
	var precision = 5; //default value if not passed from caller, change if desired
	// remark if passed from caller
	precision = parseInt(precision); // make certain the decimal precision is an integer
	var result1 = num * Math.pow(10, precision);
	var result2 = Math.round(result1);
	var result3 = result2 / Math.pow(10, precision);
	return result3;
}

function netscapeKeyPress(e) {
	if (e.which == 13)
		valConv();
}

function microsoftKeyPress() {
	if (window.event.keyCode == 13)
		valConv();
}

if (navigator.appName == 'Netscape') {
	window.captureEvents(Event.KEYPRESS);
	window.onKeyPress = netscapeKeyPress;
}

function valConv() {
	var FromVal, ToVal, FromName, ToName, v1, Factor;

	v1 = document.convForm.what.value;
	v1 = stripBad(v1);
	eval('v1 = parseFloat(' + v1 + ');');
	if (isNaN(v1)) v1 = 1;
	v1 = Math.abs(v1);
	document.convForm.what.value = v1;

	FromVal = document.convForm.from[document.convForm.from.selectedIndex].value;
	ToVal = document.convForm.to[document.convForm.to.selectedIndex].value;
	FromName = document.convForm.from.options[document.convForm.from.selectedIndex].text;
	ToName = document.convForm.to.options[document.convForm.to.selectedIndex].text;

	Factor = eval("(" + FromVal + ")/(" + ToVal + ")");

	document.convForm.answer.value = space(v1) + " " + FromName + " = " + space(get_result(v1, Factor)) + " " + ToName;
}

function resetanswer() {
	document.convForm.answer.value = "";
}

function get_result(ff,factor) {
	ff *= factor;

	var s = ff.toString(), first, last = '';
	var i = s.indexOf('e');

	if (i != -1) {
		first = s.substring(0, i);
		last = s.substring(i);
	}
	else
	first = s;

	return perRound(first) + last;
}

function stripBad(string) {
	for (var i=0, output='', valid="eE+/*-0123456789.()"; i<string.length; i++)
		if (valid.indexOf(string.charAt(i)) != -1)
		output += string.charAt(i)
	return output;
}

function space (num) {
	num = num + '';
	// exit if scientific notation
	if (num.indexOf('e') > -1){ return num; }

	var dec = num.indexOf('.');

	var left, right = '';
	if (dec >= 0)
	{
		left = num.substring(0, dec);
		right = num.substring(dec + 1);
	}
	else
		left = num;

	var new_left = '', new_right = '';
	for (var i = 0; i < right.length; i++)
	{
		new_right += right.charAt(i);
		if (i % 3 == 2 && i != right.length - 1)
			new_right += '';
	}
	for (var i = left.length - 1; i >= 0; i--)
	{
		new_left = left.charAt(i) + new_left;
		if ((left.length - 1 - i) % 3 == 2 && i != 0)
			new_left = ',' + new_left;
	}

	return (dec >= 0) ? new_left + '.' + new_right : new_left;
}

function helpQty(){
	var myMessage = '';
	myMessage = ":: Conversion Help ::\n\n";
	myMessage += "This is the amount of the 'From' unit you want to convert to the 'To' unit.\n\n";
	myMessage += "You can enter a basic quantity value, such as\n10\n100\n1267.9874\n\n";
	myMessage += "Or you may enter formulas, such as\n100+37+99\n33 * 56 + (100-23) * 17\n93*56-13*4.";

	alert(myMessage);
	return;
}

function helpResult() {
	var myMessage = '';
	myMessage = ":: Result Help ::\n\n";
	myMessage += "This is the result of your conversion, suitable for copy and paste as needed.\n\n";
	myMessage += "The format follows the U.S. convention of using a comma separator between\n";
	myMessage += "groups of three digits and a period in the decimal place.";

	alert(myMessage);
	return;
}
