/* AGGEGATE CALCULATOR
-------------------------------------------------*/

//initialize
var idepth
var ithickness
var iwidth
var ilength
var icubicyards
var icubictons

function calculateConcrete()
{
	//starting values
	ithickness = document.Concrete.Thickness.value
	iwidth = document.Concrete.Width.value;
	ilength = document.Concrete.Length.value;
	imeasure = document.Concrete.Measure.value;
	icubicyards = "";

	
	//assume the worst
	var isGood = false;
	
	//check correct user entry
	isGood = isNumber('concrete');
	
	if (isGood)
	{
		//massage floating point values 
		
		if (imeasure == "inches")
		{
			 ithickness = ithickness / 12;
			 icubicyards = ithickness * iwidth * ilength / 27;
		} else if (imeasure == "feet") {
			icubicyards = ithickness * iwidth * ilength / 27;	
		}
		//assign
		document.Concrete.CubicYards.value = round(icubicyards);
 }
}
 
function calculateAggregate()
{
	//starting values
	ithickness = document.Aggregate.Thickness.value
	iwidth = document.Aggregate.Width.value;
	ilength = document.Aggregate.Length.value;
	istone = document.Aggregate.Stone.value;
	imeasure = document.Aggregate.aMeasure.value;
	icubicyards = "";
	icubictons = "";
	
	var isGood = false;
	
	//validate
	isGood = isNumber('concrete');
	
	if (isGood)
	{
		if (imeasure == "inches")
		{
			ithickness = ithickness / 12;
			icubictons =(ithickness * iwidth * ilength / 27) * istone / 2000;
		} else if (imeasure == "feet") {
			icubictons =(ithickness * iwidth * ilength / 27) * istone / 2000;
		}
		
		//output
		var answer = document.getElementById("calculatorAnswer");
		answer.innerHTML = "<b>= "+round(icubictons)+"</b> tons";
	}
}
 
 
function isNumber(theForm)
{
	if (theForm == 'concrete')
	{
		if ((isNaN(ithickness) == true) || (ithickness == ''))
		{
			alert ("Please enter a number value in inches for the depth.");
			document.Concrete.Thickness.value = '';
			document.Concrete.Thickness.focus();
			return false;
		}
	} else {
		if ((isNaN(idepth) == true) || (idepth == ''))
		{
			alert ("Please enter a number value in inches for the depth.");
			document.Concrete.Depth.value = '';
			document.Concrete.Depth.focus();
			return false;
		}
	}
	
	if ((isNaN(iwidth) == true) || (iwidth == ''))
	{
		if (theForm == 'concrete')
		{
			alert ("Please enter a number value in feet for the width.");
			document.Concrete.Width.value = '';
			document.Concrete.Width.focus();
		} else {
			alert ("Please enter a number value in inches for the width.");
			document.Concrete.Width.value = '';
			document.Concrete.Width.focus();
		}
		return false;
	}
	
	if ((isNaN(ilength) == true) || (ilength == ''))
	{
		if (theForm == 'concrete')
		{
			alert ("Please enter a number value in feet for the length.");
			document.Concrete.Length.value = '';
			document.Concrete.Length.focus();
		} else {
			alert ("Please enter a number value in feet for the length.");
			document.FootingCalculator.Length.value = '';
			document.FootingCalculator.Length.focus();
		}
		return false;
	}
return true;
}


function round(number,X) {
	// rounds number to X decimal places, defaults to 2
	X = (!X ? 1 : X);
	return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

