//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
//Validate fields, return error alerts
function validateForm() {
	var form = document.ppiForm;

	if (document.ppiForm.pCaliper.value == "")
		{
		alert("Enter Caliper of Paper.");
		document.ppiForm.pCaliper.focus();
		return (false);
		}

	if (isNaN(document.ppiForm.pCaliper.value))
		{
		alert("Caliper must be a valid number.");
		document.ppiForm.pCaliper.focus();
		return (false);
		}

	CalculatePPI(form.pCaliper.value);

	return true;
}

//Do calculations
function CalculatePPI(pCaliper) {
	var ppi = 2 / pCaliper;
	document.ppiForm.Answer.value = formatNumber(ppi,0);
}

//Format the answer
function formatNumber(val,places) {
	var power = 1;
	if (places> 0) {
		power = Math.pow(10,places);
	}
	// Round the val to X decimal places.
	var roundedNum = (Math.round(val * power)/power) + "";

	// check the result for decimals
	var decpos = roundedNum.indexOf(".");

	if (decpos>= 0) {
		var decDiff = places-(roundedNum.length-(decpos+1));
	} else {
		var decDiff = places;
		if (places> 0) {
			roundedNum += ".";
		}
	}

	// add missing zeros to satisify the decimal request.
	for (var x = 0; x < decDiff; x++) {
		roundedNum += "0";
	}
    return roundedNum;
}
