// Floating Sub-Floor Material Cost Estimate

function m2f(size) {
	return (size * 3.28)
}

function calculate(floor_length, floor_width)
	{
	var measurements = "feet";

	var mOption = -1;
    for (i = document.calculator.system.length - 1; i > -1; i--) {
        if (document.calculator.system[i].checked) {
            mOption = i; 
			i = -1;
        }
    }
    if (mOption == -1) {
        alert("You must select a system of measurement");
        return false;
    }
   /*
    alert("You selected button number " + mOption
    + " which has a value of "
    + document.calculator.system[mOption].value);
   */
   
	var area_m = floor_length * floor_width;
	var perimeter_m = (floor_length * 2) + (floor_width * 2);
	
	if (mOption == 1) {
		var area = area_m;
		var perimeter = perimeter_m;
	} else {
		var area = m2f(floor_length) * m2f(floor_width);
		var perimeter = (m2f(floor_length) * 2) + (m2f(floor_width) * 2);
	}
	
	// alert("area: "+area+" Perimeter: "+perimeter)
	
	var sheets = Math.ceil((area * 1.25)/25);
	var sheets_cost =  sheets * 20;
	
	var gluegal = Math.ceil(sheets/40)
    var gluegal_cost = gluegal * 20;
	
    /*
	var tubes = Math.ceil(sheets/2);
	var tubes_cost = tubes * 2;

	var lots = sheets * 40
	var lots_boxes = Math.ceil((sheets * 40) / 1000);
	var lots_cost = lots_boxes * 7;
	*/

	// Perimeter strips were replaced by Foam blocks.
	
	var boxes_perim = Math.ceil(perimeter / 450);
	var boxes_sqrft = Math.ceil(area / 450);
	var boxes_leftover = (boxes_sqrft * 450) - area;	
	if (perimeter <= boxes_leftover){
		boxes_sqrft = boxes_sqrft - 1;	
	}
	
	var boxes = boxes_perim + boxes_sqrft;
	// var boxes_cost = 450 + (boxes - 1) * 350;
	var boxes_cost = 450 + (boxes - 1) * 300;
	
	var strips = Math.ceil(perimeter/5);
	var strips_cost = strips * 5;
	
	var glue = Math.ceil(sheets / 4);
	var glue_cost = glue * 13;
	
	var paint = Math.ceil(area / 250);
	var paint_cost = paint * 40;
	
	var filler = Math.ceil(sheets / 15);
	var filler_cost = filler * 7;
	
	//var frieght_cost =  boxes * 45;
	var frieght_cost =  (boxes) * 40;
	var delivery_cost = 150;
	
	// var tec = sheets_cost + tubes_cost + lots_cost + boxes_cost + strips_cost + glue_cost + paint_cost + filler_cost + frieght_cost + delivery_cost;
	// var tec = sheets_cost + tubes_cost + lots_cost + boxes_cost + glue_cost + paint_cost + filler_cost + frieght_cost + delivery_cost;

    var tec = sheets_cost + gluegal_cost + boxes_cost + glue_cost + paint_cost + filler_cost + frieght_cost + delivery_cost;
	var csf = Math.round((tec/area)*100)/100
	
	document.calculator.area.value = area_m;
	document.calculator.perimeter.value = perimeter_m;
	
	document.calculator.sheets.value = sheets;
	document.calculator.sheets_cost.value = "$"+sheets_cost;
	
	document.calculator.tubes.value = gluegal;
	document.calculator.tubes_cost.value = "$"+gluegal_cost;
	
	// document.calculator.lots.value = lots;
	// document.calculator.lots_cost.value = "$"+lots_cost;
	
	document.calculator.boxes.value = boxes;
	document.calculator.boxes_cost.value = "$"+boxes_cost;
	
	// document.calculator.strips.value = strips;
	// document.calculator.strips_cost.value = "$"+strips_cost;
	
	document.calculator.glue.value = glue;
	document.calculator.glue_cost.value = "$"+glue_cost;
	
	document.calculator.paint.value = paint;
	document.calculator.paint_cost.value = "$"+paint_cost;
	
	document.calculator.filler.value = filler;
	document.calculator.filler_cost.value = "$"+filler_cost;
	
	document.calculator.frieght_cost.value = "$"+frieght_cost;
	document.calculator.delivery_cost.value = "$"+delivery_cost;
	document.calculator.tec.value = "$"+tec;
	document.calculator.csf.value = "$"+csf;
	}