	<!-- Aggregate Tonnage Calculator -->
	function fnCalcAgg() {
		var aggAmt = 0;
		var volAmt = 0;
		var vol = (document.calculator.txtWidth.value * document.calculator.txtLength.value * document.calculator.txtDepth.value);
		var varWidth = document.calculator.txtWidth.value;
		var varLength = document.calculator.txtLength.value;
		var varDepth = document.calculator.txtDepth.value;
		var varDensity = document.calculator.txtDensity.value;
		
		if (document.calculator.Aggregate.value == "N/A") {
			if (fnValidity(varWidth) == false ||
				fnValidity(varLength) == false ||
				fnValidity(varDepth) == false) {
				window.alert("Please enter a positive number in each of the Step 3 fields.");
				return false;
			}
			else{
			volAmt = fnSetVol(vol);
			document.getElementById("totalvolume").innerHTML = document.calculator.volumetext.value + " " + volAmt ;
			document.getElementById("totaltons").innerHTML = "&nbsp;";
			document.calculator.calcrun.value = "ran";
			return true;
			}	
		}
		else {
			if (fnValidity(varWidth) == false ||
				fnValidity(varLength) == false ||
				fnValidity(varDensity) == false ||
				fnValidity(varDepth) == false ) {
				window.alert("Please enter a positive number in each of the four input fields.");
				return false;
			}
			else{
			volAmt = fnSetVol(vol);
			aggAmt = volAmt * varDensity;
			document.getElementById("totalvolume").innerHTML = document.calculator.volumetext.value + " " + volAmt ;
			document.getElementById("totaltons").innerHTML = document.calculator.weighttext.value + " " + formatvalue(aggAmt,5);
			document.calculator.calcrun.value = "ran";
			return true;
			}
		}
	}
	
	function fnresetcalc() {
		if (document.calculator.calcrun.value == "ran") {
			document.calculator.calcrun.value = "no";
			document.getElementById("totalvolume").innerHTML = "&nbsp;";
			document.getElementById("totaltons").innerHTML = "&nbsp;";
		}
	}
	
	function fnSetVol(vol2) {
		var setvol = 0;
		if (document.calculator.Measurement[0].checked) {
			if (document.calculator.FeetInches[0].checked) {
				setvol = (vol2 / 12.0) / 27;
			}
			else {
				setvol = vol2 / 27;
			}
		}
		else {
			if (document.calculator.FeetInches[0].checked) {
				setvol = vol2 / 100;
			}
			else {
				setvol = vol2;
			}
		}
		return formatvalue(setvol,5);
	}
	
	
	function fnValidity(valfield) {
		
		var parsecheck = "";
		parsecheck = "" + parseFloat(valfield);
	   	if (parseFloat(valfield) < 0 ||
		   parseFloat(valfield) == 0 ||
		   parsecheck == "NaN") {
		   return false;
	   	}
	}

	
	function formatvalue(input, rsize) {
	   var invalid = "**************************";
	   var nines = "999999999999999999999999";
	   var strin = "" + input;
	   var fltin = parseFloat(strin);
	   if (strin.indexOf("e") != -1 ||
		   fltin > parseFloat(nines.substring(0,rsize)+".4"))
		  return invalid.substring(0, rsize);
	   if (strin.length <= rsize) return strin;
	   var rounded = "" + (fltin + 
		  (fltin - parseFloat(strin.substring(0, rsize))));
	   return rounded.substring(0, rsize);
	}
	
		
	function fnSetDensity() {
		var denamt = 0;
		if (document.calculator.Measurement[0].checked) {
			document.getElementById("divdensity").innerHTML = document.calculator.Aggregate.value;
			document.calculator.txtDensity.value = document.calculator.Aggregate.value;
		}
		else if (document.calculator.Aggregate.value == "N/A") {
			document.calculator.txtDensity.value = document.calculator.Aggregate.value;
			document.getElementById("divdensity").innerHTML = document.calculator.Aggregate.value;
		}
		else {
			denamt = document.calculator.Aggregate.value * 1.308;
			document.calculator.txtDensity.value = formatvalue(denamt,4);
			document.getElementById("divdensity").innerHTML = formatvalue(denamt,4);
		}
		fnresetcalc();
	}
	
	function fnSetMeasureType(MI) {
		if (MI == "Metric"){
			document.getElementById("densitytext").innerHTML = "Density (tonnes/cubic metre):";
			document.getElementById("lengthtext").innerHTML = "Length (Metres):";
			document.getElementById("inchestext").innerHTML = "Centimetres";
			document.getElementById("feettext").innerHTML = "Metres";
			document.calculator.volumetext.value = "Total Cubic Metres:";
			document.calculator.weighttext.value = "Total Tonnes:";
			document.getElementById("widthtext").innerHTML = "Width (Metres):";
			fnSetDensity();
			fnresetcalc();
		}
		else {
			document.getElementById("densitytext").innerHTML = "Density (tonnes/cubic yard):";
			document.getElementById("lengthtext").innerHTML = "Length (feet):";
			document.getElementById("inchestext").innerHTML = "Inches";
			document.getElementById("feettext").innerHTML = "Feet";
			document.calculator.volumetext.value = "Total Cubic Yards:";
			document.calculator.weighttext.value = "Total Tonnes:";
			document.getElementById("widthtext").innerHTML = "Width (Feet):";
			fnSetDensity();
			fnresetcalc();
			}
	}
