function RoundOff(value, precision)
{
		value = "" + value;
		precision = parseInt(precision);

		var whole = "" + Math.round(value * Math.pow(10, precision));
		var decPoint = whole.length - precision;

		if (value < 1) {
				result = whole / Math.pow(10, precision);
				return result;
		}

		if (decPoint != 0) {
				result = whole.substring(0, decPoint);
				if (precision != 0) {
						result += ".";
				}
				result += whole.substring(decPoint, whole.length);
		}
		else {
				result = "0." + whole;
		}

		return result;
}

function CalculateValues()
{
	sysStaticPressure = parseFloat(document.savingsForm.sysStaticPressure.value);
	totalGPM = parseFloat(document.savingsForm.totalGPM.value);
	avgWateringTime = parseFloat(document.savingsForm.avgWateringTime.value);
	avgWateringDays = parseFloat(document.savingsForm.avgWateringDays.value);
	avgWateringWeeks = parseFloat(document.savingsForm.avgWateringWeeks.value);

	if (isNaN(sysStaticPressure)) {
		window.alert("Please enter a value for the 'System's Static Pressure' field.");
		document.savingsForm.sysStaticPressure.focus();
		return;
	}

	if (sysStaticPressure < 30) {
		window.alert("Pressure at the nozzle must be greater than 30 psi.");
		document.savingsForm.sysStaticPressure.focus();
		return;
	}
	else if (sysStaticPressure > 90) {
		window.alert("Pressure at the nozzle must be less than 90 psi.");
		document.savingsForm.sysStaticPressure.focus();
		return;
	}

	if (isNaN(totalGPM)) {
		window.alert("Please enter a value for the 'Flow' field.");
		document.savingsForm.totalGPM.focus();
		return;
	}

	if (isNaN(avgWateringTime)) {
		window.alert("Please enter a value for the 'Average Watering Time per Day' field.");
		document.savingsForm.avgWateringTime.focus();
		return;
	}

	if (isNaN(avgWateringDays)) {
		window.alert("Please enter a value for the 'Average Number of Watering Days per Week' field.");
		document.savingsForm.avgWateringDays.focus();
		return;
	}

	if (isNaN(avgWateringWeeks)) {
		window.alert("Please enter a value for the 'Average Watering Weeks per Year' field.");
		document.savingsForm.avgWateringWeeks.focus();
		return;
	}

	tempValue = Math.sqrt(sysStaticPressure / 2.31 * 2 * 32.2 / 62.4)
							/ (Math.sqrt(30 / 2.31 * 2 * 32.2 / 62.4)) - 1;

	tempValue = RoundOff(tempValue, 2);

	initWaste = totalGPM * tempValue;
	yearlyWaste = initWaste * avgWateringTime * avgWateringDays * avgWateringWeeks;
	
	document.savingsForm.initWaste.value = RoundOff(initWaste, 2);
	document.savingsForm.yearlyWaste.value = RoundOff(yearlyWaste, 2);

}