var dpth
var wdthdm
var lngth
var cyds
      
function FlatworkCalc() {
	dpth = document.FlatCalc.Thickness.value;
	wdthdm = document.FlatCalc.Width.value;
	lngth = document.FlatCalc.Length.value; 
	cyds = "";
	var check = false;
	check = nmbr('flat');
        if (check) {
		dpth = floatFix(dpth / 12, 4);
		cyds = floatFix(dpth * wdthdm * lngth / 27, 2);
		if (cyds < .001) {
			alert("The result is too small.\nPlease enter new values.");
			cyds = 0;
		}
		if (cyds >= 100) {
			cyds = Math.round(cyds);
		}
		document.FlatCalc.CubicYards.value = cyds;
	}
}
       
function PierCalculator() {
	wdthdm = document.PierCalc.Width.value;
	lngth = document.PierCalc.Length.value; 
	cyds = "";
	var check = false;
	check = nmbr('footing');
	if (check) {
		wdthdm = floatFix(wdthdm / 12, 2);
		cyds = floatFix(((((wdthdm / 2) * (wdthdm / 2)) * 3.14) * lngth) / 27, 2);
		document.PierCalc.CubicYards.value = cyds;
	}
	
}
       
function nmbr(Frm) {
	if (Frm == 'flat') {
		if ((isNaN(dpth) == true) || (dpth == '')) {
			alert ("Enter a measurement in inches for the thickness of your concrete.");
			document.FlatCalc.Thickness.value = '';
			document.FlatCalc.Thickness.focus();
			return false;
		}
	} 
	if ((isNaN(wdthdm) == true) || (wdthdm == '')) {
		if (Frm == 'flat') {
			alert ("Enter a measurement in feet for the width of your flatwork.");
			document.FlatCalc.Width.value = '';
			document.FlatCalc.Width.focus();
			}
		else {
		alert ("Enter a measurement in inches for the diameter of your pier.");
		document.PierCalc.Width.value = '';
		document.PierCalc.Width.focus();
		}
	return false;
	}
	if ((isNaN(lngth) == true) || (lngth == '')) {
		if (Frm == 'flat') {
			alert ("Enter a measurement in feet for the length of your flatwork.");
			document.FlatCalc.Length.value = '';
			document.FlatCalc.Length.focus();
			}
		else {
			alert ("Enter a measurement in feet for the depth of your pier.");
			document.PierCalc.Length.value = '';
			document.PierCalc.Length.focus();
			}
		return false;
		}
	return true;
}
   
function floatFix(value, places) {
	var res = "" + Math.round(value * Math.pow(10, places));
	var dec = res.length - places;
	if (places != 0) {
		outstring = res.substring(0, dec) + "." + res.substring(dec, res.length);
		if (dec < 0) {
			temp = Math.abs(dec);
			outstring = ".";
			for (i=1;i<=temp;i++) {
				outstring = outstring + "0";
			}
			outstring = outstring + res;
		}
	} else {
		outstring = res;
	}

	return (outstring);
}
