function getTotalEstimate() {
	var currentCostsTotal = new Number();
	
	jQuery("table#cost-estimator input.unitstext").each(function(){
		
		// get number of units * unit cost
		$this = jQuery(this);
		$parent = $this.parent("td").parent("tr") ;
		var numUnits  = $this.val();
		var unitCost  = $parent.children("td.cost-td").children("input.cost").val();		
		var thetotal  = unitCost * numUnits ;
		// fill out the input and text with new total for item
		$parent.children("td.total-td").children("input.total").val(thetotal.toFixed(2));
		$parent.children("td.total-td").children("span.totalbox").html('$' + thetotal.toFixed(2));
		// add to current total
		currentCostsTotal = Number(currentCostsTotal + thetotal);
	});     
	  

	
	var currentSuppliesCostsTotal = new Number();
	
	jQuery("table#supplies-estimator input.unitstext").each(function(){
		
		// get number of units * unit cost
		$this = jQuery(this);
		$parent = $this.parent("td").parent("tr") ;
		var numUnits  = $this.val();
		var unitCost  = $parent.children("td.cost-td").children("input.cost").val();		
		var thetotal  = unitCost * numUnits ;
		// fill out the input and text with new total for item
		$parent.children("td.total-td").children("input.total").val(thetotal.toFixed(2));
		$parent.children("td.total-td").children("span.totalbox").html('$' + thetotal.toFixed(2));
		// add to current total
		currentSuppliesCostsTotal = Number(currentSuppliesCostsTotal + thetotal);
	});	 
	
  thePstRecovery  = currentSuppliesCostsTotal * .08 ;
	jQuery("input[name$='pstRecovery']").val(thePstRecovery.toFixed(2));
	jQuery("#pstRecovery").html("$" + thePstRecovery.toFixed(2));	

	currentSuppliesCostsTotal = currentSuppliesCostsTotal + thePstRecovery ;
	
	jQuery("input[name$='suppliestotal']").val(currentSuppliesCostsTotal.toFixed(2));
	jQuery("#suppliestotal").html("$" + currentSuppliesCostsTotal.toFixed(2));	
	
	jQuery("input[name$='suppliestotalB']").val(currentSuppliesCostsTotal.toFixed(2));
	jQuery("#suppliestotalB").html("$" + currentSuppliesCostsTotal.toFixed(2));	
	                                                                                   
	                 
 
	// add up supplies total and costs total:
	finalTotal = currentSuppliesCostsTotal + currentCostsTotal ;
 
	// set the total estimate:
	
	jQuery("#totalestimate").html("$" + finalTotal.toFixed(2))
	jQuery("input[name$='totalestimate']").val(finalTotal.toFixed(2));    
	jQuery("#totalestimateB").html("$" + finalTotal.toFixed(2));
	jQuery("#totalEstimateInput").val(finalTotal.toFixed(2));    	
	
	// determine early payment savings:
	var mini_mole_price = Number(jQuery("table#cost-estimator input:text").eq(0).val()) ;
	var beaver_price = Number(jQuery("table#cost-estimator input:text").eq(3).val()) ;
	var pionjar_price = Number(jQuery("table#cost-estimator input:text").eq(4).val()) ;
	
	var totalsavings = finalTotal - ((mini_mole_price + beaver_price + pionjar_price)*10);
	jQuery("#savingsestimate").html("$" + totalsavings.toFixed(2));
	jQuery("input[name$='savingsestimate']").val(totalsavings.toFixed(2)) ;               

	
}
                

function calculateBoreholes() {
totalBoreholeSize =  3.14*(jQuery("#boreholeSize").val()/2*2.5)*(jQuery("#boreholeSize").val()/2*2.5)*(jQuery("#boreholeLinearFeet").val()*30)/1000*1.1/22.7;

jQuery("#bagsOfHoleplug").val(totalBoreholeSize.toFixed(2));
} // end calculateBoreholes

function calculateSand() {
totalSand=((3.14*(jQuery("#boreholeSizeB").val()/2*2.5)*(jQuery("#boreholeSizeB").val()/2*2.5)*(jQuery("#linearFeetB").val()*30))-(3.14*(jQuery("#wellpipeSizeB").val()/2*2.5)*(jQuery("#wellpipeSizeB").val()/2*2.5)*(jQuery("#linearFeetB").val()*30)))/1000*1.5/22.7;

jQuery("#bagsOfSandB").val(totalSand.toFixed(2))

}   // end calculateSand


function calculateHoleplug() {
totalSandC = ((3.14*(jQuery("#boreholeSizeC").val()/2*2.5)*(jQuery("#boreholeSizeC").val()/2*2.5)*(jQuery("#linearFeetC").val()*30))-(3.14*(jQuery("#wellpipeSizeC").val()/2*2.5)*(jQuery("#wellpipeSizeC").val()/2*2.5)*(jQuery("#linearFeetC").val()*30)))/1000*1.1/22.7

jQuery("#bagsOfSandC").val(totalSandC.toFixed(2))

}   // end calculateHoleplug

function calculateMetresToFeet() {
  jQuery("#measurementFeet").val(jQuery("#measurementMetres").val()* 3.28); 

}   // end calculateMetresToFeet

jQuery(document).ready(function() {

		jQuery("table#conversion-table input:text").change(function() {
		 	calculateMetresToFeet();   
		});
		      
		jQuery("table#boreholes-table input:text").change(function() {
		 	calculateBoreholes();   
		});
		
		jQuery("table#sand-table input:text").change(function() {
		 	calculateSand();   
		});
		
		jQuery("table#holeplug-table input:text").change(function() {
		 	calculateHoleplug();   
		});		



		
		
		// Watch for changes to the forms
		jQuery("input.unitstext").change(function() {
			  getTotalEstimate();
			
		})  ;


		                     
		
});                           

