// JavaScript Document

function checkvalue(fvalue, fobject) {
	var newvalue = 0;
	if(!isNaN(eval(fvalue)) && fvalue != "''") {
		newvalue = eval(fobject) + 0;
	} 
	return newvalue;
}
function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function calcRafterlen() {
	df = document.frmRafterlen;
	//	df.lengthFt.value
	rafterFt = "'" + trim(df.rafterFt.value) + "'";
	rafterIn = "'" + trim(df.rafterIn.value) + "'";
	overhangFt = "'" + trim(df.overhangFt.value) + "'";
	overhangIn = "'" + trim(df.overhangIn.value) + "'";
	pitch = "'" + trim(df.pitch.value) + "'";
	
	if(!isNaN(eval(rafterFt)) && rafterFt != "''") {
		rafterFt = eval(df.rafterFt.value) + 0;
	} else {
		rafterFt = 0;
	}

	if(!isNaN(eval(rafterIn)) && rafterIn != "''") {
		rafterIn = eval(df.rafterIn.value) + 0;
	} else {
		rafterIn = 0;
	}

	if(!isNaN(eval(overhangFt)) && overhangFt != "''") {
		overhangFt = eval(df.overhangFt.value) + 0;
	} else {
		overhangFt = 0;
	}

	if(!isNaN(eval(overhangIn)) && overhangIn != "''") {
		overhangIn = eval(df.overhangIn.value) + 0;
	} else {
		overhangIn = 0;
	}

	if(!isNaN(eval(pitch)) && pitch != "''") {
		pitch = eval(df.pitch.value) + 0;
	} else {
		pitch = 0;
	}

	rafterLength = rafterFt + (rafterIn /12);
	overhangLength = overhangFt + (overhangIn /12);
	if (pitch < 3){ //pitch less than 3
		plumb = 0;
	} else{
		if (pitch > 12){ //pitch greater than 12
			plumb = 1;			
			} else { //pitch between 3 and 12
			plumb = .5;
			}
			
	}
	pitchMultiplier = (Math.sqrt(144 + Math.pow(pitch,2))) / 12;
	rafterFlat = rafterLength + overhangLength + plumb;
	rafterActualLen = rafterFlat * pitchMultiplier;
	stockSize = Math.ceil(rafterActualLen);
	//Math.ceil(a)
	
	resultsvalue = "This job requires a stock size of " + stockSize+".";
	document.getElementById('answer').innerHTML = resultsvalue;
	return false;
}
function calcblowninsulationin() { //v3.0
	df = document.BlownInsulation;
	//	df.lengthFt.value
	lengthFt = "'" + trim(df.lengthFt.value) + "'";
	lengthIn = "'" + trim(df.lengthIn.value) + "'";
	widthFt = "'" + trim(df.widthFt.value) + "'";
	widthIn = "'" + trim(df.widthIn.value) + "'";
	RValue = "'" + trim(df.RValue.value) + "'";

	if(!isNaN(eval(lengthFt)) && lengthFt != "''") {
		lengthFt = eval(df.lengthFt.value) + 0;
	} else {
		lengthFt = 0;
	}

	if(!isNaN(eval(lengthIn)) && lengthIn != "''") {
		lengthIn = eval(df.lengthIn.value) + 0;
	} else {
		lengthIn = 0;
	}

	if(!isNaN(eval(widthFt)) && widthFt != "''") {
		widthFt = eval(df.widthFt.value) + 0;
	} else {
		widthFt = 0;
	}

	if(!isNaN(eval(widthIn)) && widthIn != "''") {
		widthIn = eval(df.widthIn.value) + 0;
	} else {
		widthIn = 0;
	}

	if(!isNaN(eval(RValue)) && RValue != "''") {
		RValue = eval(df.RValue.value) + 0;
	} else {
		RValue = 0;
	}

	areavalue = ((lengthFt + (lengthIn/12))*(widthFt + (widthIn/12)));
	DepthValue = Math.round((RValue * .307)*100)/100;
	LbsValue =  Math.round((areavalue * .0341 * RValue)*10)/10;

	resultsvalue = "This job requires " + LbsValue + " lbs of insulation, " + DepthValue + "\" deep.";
	document.getElementById('answer').firstChild.nodeValue = resultsvalue;
	return false;
}

function calcrolledinsulationin() { //v3.0
	df = document.RolledInsulation;
	//	df.lengthFt.value
	lengthFt = "'" + trim(df.lengthFt.value) + "'";
	lengthIn = "'" + trim(df.lengthIn.value) + "'";
	widthFt = "'" + trim(df.widthFt.value) + "'";
	widthIn = "'" + trim(df.widthIn.value) + "'";

	if(!isNaN(eval(lengthFt)) && lengthFt != "''") {
		lengthFt = eval(df.lengthFt.value) + 0;
	} else {
		lengthFt = 0;
	}

	if(!isNaN(eval(lengthIn)) && lengthIn != "''") {
		lengthIn = eval(df.lengthIn.value) + 0;
	} else {
		lengthIn = 0;
	}

	if(!isNaN(eval(widthFt)) && widthFt != "''") {
		widthFt = eval(df.widthFt.value) + 0;
	} else {
		widthFt = 0;
	}

	if(!isNaN(eval(widthIn)) && widthIn != "''") {
		widthIn = eval(df.widthIn.value) + 0;
	} else {
		widthIn = 0;
	}

	areavalue = ((lengthFt + (lengthIn/12))*(widthFt + (widthIn/12)));
	areavalue = Math.round((areavalue)*100)/100;
	
	labor16 = Math.round((areavalue * .007)*100)/100;
	labor24 = Math.round((areavalue * .005)*100)/100;
	resultsvalue = "This job requires " + areavalue + " square feet of insulation. <br /> Estimated Labor: <br /> 16\" O\/C: " + labor16 + " hours. <br /> 24\" O\/C: " + labor24 + " hours.";
	document.getElementById('answer').innerHTML = resultsvalue;
	return false;
}

function calcrvalue() { //v3.0
	df = document.RValue;
	//	df.lengthFt.value
	DepthValue = "'" + trim(df.DepthValue.value) + "'";

	if(!isNaN(eval(DepthValue)) && DepthValue != "''") {
		DepthValue = eval(df.DepthValue.value) + 0;
	} else {
		DepthValue = 0;
	}

	RVal = Math.round((DepthValue * 3.6)*10)/10;

	resultsvalue = "This job has an estimated R-value of " + RVal + ".";
	document.getElementById('answer').firstChild.nodeValue = resultsvalue;
	return false;
}

function calcconcreteholes(){
	df = document.ConcreteHoles;
	var my_pie=Math.PI;
	HoleDiameter = "'" + trim(df.HoleDiameter.value) + "'";
	HoleDepth = "'" + trim(df.HoleDepth.value) + "'";
	HoleNum = "'" + trim(df.HoleNum.value) + "'";
	
	if(!isNaN(eval(HoleDiameter)) && HoleDiameter != "''") {
		HoleDiameter = eval(df.HoleDiameter.value) + 0;
	} else {
		HoleDiameter = 0;
	}
	
	if(!isNaN(eval(HoleDepth)) && HoleDepth != "''") {
		HoleDepth = eval(df.HoleDepth.value) + 0;
	} else {
		HoleDepth = 0;
	}
	
	if(!isNaN(eval(HoleNum)) && HoleNum != "''") {
		HoleNum = eval(df.HoleNum.value) + 0;
	} else {
		HoleNum = 0;
	}
	
	cirArea = (((HoleDiameter / 12) / 2) * ((HoleDiameter / 12) / 2)) * my_pie
	cirVolume = cirArea * (HoleDepth / 12)
	totalCirVolume = cirVolume * HoleNum
	totalCirVolume = Math.round((totalCirVolume)*100)/100;
	estHours = (totalCirVolume / 27) * .875;
	estHours = Math.round((estHours)*100)/100;
	
	resultsvalue = "This job requires " + totalCirVolume + " cubic feet of concrete. <br /> Estimated Labor:  " + estHours + " hours.";
	document.getElementById('answer').innerHTML = resultsvalue;
	return false;
}

function calcconcretecalculator(CalcVer) { //v3.0
	//var CalcVer = 'beams';
	df = document.concretecalculator;
	//	df.lengthFt.value
	lengthFt = "'" + trim(df.lengthFt.value) + "'";
	lengthIn = "'" + trim(df.lengthIn.value) + "'";
	widthFt = "'" + trim(df.widthFt.value) + "'";
	widthIn = "'" + trim(df.widthIn.value) + "'";
	DepthFt = "'" + trim(df.DepthFt.value) + "'";
	DepthIn = "'" + trim(df.DepthIn.value) + "'";
	
	if(!isNaN(eval(lengthFt)) && lengthFt != "''") {
		lengthFt = eval(df.lengthFt.value) + 0;
	} else {
		lengthFt = 0;
	}

	if(!isNaN(eval(lengthIn)) && lengthIn != "''") {
		lengthIn = eval(df.lengthIn.value) + 0;
	} else {
		lengthIn = 0;
	}

	if(!isNaN(eval(widthFt)) && widthFt != "''") {
		widthFt = eval(df.widthFt.value) + 0;
	} else {
		widthFt = 0;
	}

	if(!isNaN(eval(widthIn)) && widthIn != "''") {
		widthIn = eval(df.widthIn.value) + 0;
	} else {
		widthIn = 0;
	}

	if(!isNaN(eval(DepthFt)) && DepthFt != "''") {
		DepthFt = eval(df.DepthFt.value) + 0;
	} else {
		DepthFt = 0;
	}

	if(!isNaN(eval(DepthIn)) && DepthIn != "''") {
		DepthIn = eval(df.DepthIn.value) + 0;
	} else {
		DepthIn = 0;
	}
	lengthFtValue = lengthFt + (lengthIn / 12);
	widthFtValue = widthFt + (widthIn / 12);
	depthFtValue = DepthFt + (DepthIn / 12);
	
	areaValue = lengthFtValue * widthFtValue;
	
	volumeValue = Math.round(((areaValue * depthFtValue))*100)/100;
	volumeValueyard = volumeValue /27;
	
	
	ColmunFootings = Math.round((volumeValueyard * .875)*10)/10;
	ContinuousGrade = Math.round((volumeValueyard * 1.8)*10)/10;
	Haunches = Math.round((volumeValueyard * 1.8)*10)/10;
	walls8x4 = Math.round((areaValue * .026)*10)/10;
	walls8x4_8 = Math.round((areaValue * .03)*10)/10;
	walls8x4_12 = Math.round((areaValue * .033)*10)/10;
	walls8x16 = Math.round((areaValue * .036)*10)/10;
	Slabs = Math.round((areaValue * .024)*10)/10;
	bagsNeeded80 =  Math.ceil(volumeValueyard / .022);
	bagsNeeded60 =  Math.ceil(volumeValueyard / .0165);

	switch(CalcVer) {
	case 'footing': {
		outString = ": " + ColmunFootings + " hours. "; 
		break;
	}
	case 'beams': {
		outString = " (no forming or finishing required): " + ContinuousGrade + " hours.  "; 
		break;
	}
	case 'walls': {
//		outString = "<dl><dt>Continuous grade beams or footings cast directly against earth (no forming or finishing required) </dt><dd>" + ContinuousGrade + " Hours. </dd></dl> "; 
		if (widthFtValue < 4) {
			outString = ": <dl><dt>8\" walls to 4\' high, direct from chute </dt><dd>" + walls8x4 + " hours. </dd></dl> ";
		}
		if ((widthFtValue >= 4) && (widthFtValue <= 8)) {
		outString = ": <dl><dt>8\" walls to 4\' to 8\' high, pumped </dt><dd>" + walls8x4_8 + " hours. </dd></dl> ";
		}
		if ((widthFtValue >= 8) && (widthFtValue <= 12)) {
		outString = ": <dl><dt>8\" walls to 8\' to 12\' high, pumped</dt><dd>" + walls8x4_12 + " hours. </dd></dl> ";
		}
		if (widthFtValue > 12) {
		outString = ": <dl><dt>8\" walls to 16\' high, pumped</dt><dd>" + walls8x16 + " hours. </dd></dl> ";
		}
		break;
	}
	case 'slabs': {
		outString = ": " + Slabs + " hours."; 
		break;
	}
	//case 'Colmun': break;
	//case 'Colmun': document.write("4"); break;
	//default: document.write("?"); break;
	}

	resultsvalue = "You will need enough concrete to fill " + volumeValue + " cubic feet <br/>(" + bagsNeeded80 + " 80lbs. Bags of Quikrete&reg;). <br /> ";
	resultsvalue = resultsvalue + "Estimated Labor" + outString ;
//	resultsvalue = resultsvalue + "<dl><dt>Colmun Footings (non-continuous in forms)</dt><dd>" + ColmunFootings + " Hours.</dd> ";
//	resultsvalue = resultsvalue + "<dt>Continuous grade beams or footings cast directly against earth (no forming or finishing required) </dt><dd>" + ContinuousGrade + " Hours. </dd> ";
//	resultsvalue = resultsvalue + "<dt>Haunches </dt><dd>" + Haunches + " Hours. </dd> ";
//	resultsvalue = resultsvalue + "<dt>8\" walls to 4\' high, direct from chute </dt><dd>" + walls8x4 + " Hours. </dd> ";
//	resultsvalue = resultsvalue + "<dt>8\" walls to 4\' to 8\' high, pumped </dt><dd>" + walls8x4_8 + " Hours. </dd> ";
//	resultsvalue = resultsvalue + "<dt>8\" walls to 8\' to 12\' high, pumped</dt><dd>" + walls8x4_12 + " Hours. </dd> ";
//	resultsvalue = resultsvalue + "<dt>Slabs </dt><dd>" + Slabs + " Hours. </dd></dl> ";
	document.getElementById('answer').innerHTML = resultsvalue;
	return false;
}




function calcFlooring(CalcVer) { //v3.0
	df = document.FlooringCalc;
	//	df.lengthFt.value
	
	lengthFt = checkvalue("'" + trim(df.lengthFt.value) + "'", df.lengthFt.value);
	lengthIn = checkvalue("'" + trim(df.lengthIn.value) + "'", df.lengthIn.value);
	widthFt = checkvalue("'" + trim(df.widthFt.value) + "'", df.widthFt.value);
	widthIn = checkvalue("'" + trim(df.widthIn.value) + "'", df.widthIn.value);

	lengthFtValue = lengthFt + (lengthIn / 12);
	widthFtValue = widthFt + (widthIn / 12);
	
	areaValue = lengthFtValue * widthFtValue;
	areaValueYards = areaValue / 9;
	//alert(areaValue);
	//alert(areaValueYards);
	resilientFlooring = Math.round((areaValueYards * .25)*100)/100;
	carpet = Math.round((areaValueYards * .114)*100)/100;
	tile = Math.round((areaValue * .16)*10)/10;
	wood = Math.round((areaValue * .062)*10)/10;

	areaValue = Math.round(areaValue*10)/10;
	areaValueYards = Math.round(areaValueYards*10)/10;

	switch(CalcVer) {
	case 'wood': {
		outString = "This job requires " + areaValue + " square feet of wood. <br />  Estimated Labor: " + wood + " hours. </dd></dl> "; 
		break;
	}
	case 'tile': {
		//outString = "<dl><dt>Tile </dt><dd>" + tile + " Hours. </dd></dl> ";
		outString = "This job requires " + areaValue + " square feet of tile. <br />  Estimated Labor: " + tile + " hours. </dd></dl> "; 
		break;
	}
	case 'resilient': {
		//outString = "<dl><dt></dt><dd>" + resilientFlooring + " hours.</dd></dl> ";
		outString = "This job requires " + areaValueYards + " square yards of resilient flooring. <br />  Estimated Labor: " + resilientFlooring + " hours. </dd></dl> "; 
		break;
	}
	case 'carpet': {
		//outString = "<dl><dt>Carpet </dt><dd>" + carpet + " hours. </dd></dl> ";
		outString = "This job requires " + areaValueYards + " square yards of carpet. <br />  Estimated Labor: " + carpet + " hours. </dd></dl> "; 
		break;
	}
	}



	resultsvalue = "" //"You will need enough to concrete to fill " + volumeValue + " cubic feet. <br /> ";
	resultsvalue = resultsvalue + outString;
//	resultsvalue = resultsvalue + "<dl><dt>Resilient flooring (sheet vinyl)</dt><dd>" + resilientFlooring + " Hours.</dd> ";
//	resultsvalue = resultsvalue + "<dt>Carpet </dt><dd>" + carpet + " Hours. </dd> ";
//	resultsvalue = resultsvalue + "<dt>Tile </dt><dd>" + tile + " Hours. </dd> ";
//	resultsvalue = resultsvalue + "<dt>Wood </dt><dd>" + wood + " Hours. </dd></dl> ";
	document.getElementById('answer').innerHTML = resultsvalue;
	return false;
}



function calcwallpaper() { //v3.0
	df = document.wallpaper;
	//	df.lengthFt.value
	wall1Ft = checkvalue("'" + trim(df.wall1Ft.value) + "'", df.wall1Ft.value);
	wall1In = checkvalue("'" + trim(df.wall1In.value) + "'", df.wall1In.value);
	wall2Ft = checkvalue("'" + trim(df.wall2Ft.value) + "'", df.wall2Ft.value);
	wall2In = checkvalue("'" + trim(df.wall2In.value) + "'", df.wall2In.value);
	wall3Ft = checkvalue("'" + trim(df.wall3Ft.value) + "'", df.wall3Ft.value);
	wall3In = checkvalue("'" + trim(df.wall3In.value) + "'", df.wall3In.value);
	wall4Ft = checkvalue("'" + trim(df.wall4Ft.value) + "'", df.wall4Ft.value);
	wall4In = checkvalue("'" + trim(df.wall4In.value) + "'", df.wall4In.value);
	roomHeightFt = checkvalue("'" + trim(df.roomHeightFt.value) + "'", df.roomHeightFt.value);
	roomHeightIn = checkvalue("'" + trim(df.roomHeightIn.value) + "'", df.roomHeightIn.value);
	numDoors = checkvalue("'" + trim(df.numDoors.value) + "'", df.numDoors.value);
	numWindows = checkvalue("'" + trim(df.numWindows.value) + "'", df.numWindows.value);

	wall1FtValue = 		wall1Ft + (wall1In / 12);
	wall2FtValue = 		wall2Ft + (wall2In / 12);
	wall3FtValue = 		wall3Ft + (wall3In / 12);
	wall4FtValue = 		wall4Ft + (wall4In / 12);
	roomHeightFtValue = roomHeightFt + (roomHeightIn / 12);
	
	totalArea = (wall1FtValue * roomHeightFtValue) + (wall2FtValue * roomHeightFtValue) + (wall3FtValue * roomHeightFtValue) + (wall4FtValue * roomHeightFtValue);
	doorsArea = (numDoors * 16.66);
	windowsArea = (numWindows * 15);
	
	wallPaperRolls =  Math.ceil(((totalArea) - (doorsArea + windowsArea))/56);
	laborEst = Math.round((wallPaperRolls * 1.768)*10)/10;
	
	resultsvalue = "This job requires " + wallPaperRolls + " rolls of paper. <br /> ";
	resultsvalue = resultsvalue + "Estimated Labor: " + laborEst + " hours.";
	document.getElementById('answer').innerHTML = resultsvalue;
	return false;
}


function calcpaint() { //v3.0
	df = document.paintcalc;
	//	df.lengthFt.value
	lengthFt = checkvalue("'" + trim(df.lengthFt.value) + "'", df.lengthFt.value);
	lengthIn = checkvalue("'" + trim(df.lengthIn.value) + "'", df.lengthIn.value);
	widthFt = checkvalue("'" + trim(df.widthFt.value) + "'", df.widthFt.value);
	widthIn = checkvalue("'" + trim(df.widthIn.value) + "'", df.widthIn.value);
	heightFt = checkvalue("'" + trim(df.heightFt.value) + "'", df.heightFt.value);
	heightIn = checkvalue("'" + trim(df.heightIn.value) + "'", df.heightIn.value);
	numDoors = checkvalue("'" + trim(df.numDoors.value) + "'", df.numDoors.value);
	numWindows = checkvalue("'" + trim(df.numWindows.value) + "'", df.numWindows.value);
	moldingIn = checkvalue("'" + trim(df.moldingIn.value) + "'", df.moldingIn.value);
	paintCoverPrimer = checkvalue("'" + trim(df.paintCoverPrimer.value) + "'", df.paintCoverPrimer.value);
	paintCoverFinish = checkvalue("'" + trim(df.paintCoverFinish.value) + "'", df.paintCoverFinish.value);

	lengthFtValue = 	lengthFt + (lengthIn / 12);
	widthFtValue = 		widthFt + (widthIn / 12);
	heightFtValue = 	heightFt + (heightIn / 12);
	moldingft = 		(moldingIn / 12);
	
	grossWallArea = ((lengthFtValue * heightFtValue) * 2) + ((widthFtValue * heightFtValue) * 2);
	grossCeilingArea = (lengthFtValue * widthFtValue);
	doorsArea = (numDoors * 16.66);
	windowsArea = (numWindows * 15);
	totalMoldingLen = (numDoors * 17) + (doorsArea + windowsArea) + ((lengthFtValue + widthFtValue) * 2) - (numDoors * 3);
	netWallArea = grossWallArea - (doorsArea + windowsArea);

	wallPrimer = Math.round(((netWallArea) / paintCoverPrimer)*10)/10;
	wallFinish = Math.round(((netWallArea) / paintCoverFinish)*10)/10;
	ceilingPrimer = Math.round((grossCeilingArea / paintCoverPrimer)*10)/10;
	ceilingFinish = Math.round((grossCeilingArea / paintCoverFinish)*10)/10;
	doorsAndWindowsPrimer = Math.round(((doorsArea + (windowsArea * .33)) / paintCoverPrimer)*10)/10;
	doorsAndWindowsFinish = Math.round(((doorsArea + (windowsArea * .33)) / paintCoverFinish)*10)/10;
	moldingsPrimer = Math.round(((totalMoldingLen * moldingft) / paintCoverPrimer)*10)/10;
	moldingsFinish = Math.round(((totalMoldingLen * moldingft) / paintCoverFinish)*10)/10;
	
	wallPrimerLabor = Math.round((netWallArea * .002)*10)/10;
	wallFinishLabor = Math.round((netWallArea * .003)*10)/10;
	ceilingPrimerLabor = Math.round((grossCeilingArea * .004)*10)/10;
	ceilingFinishLabor = Math.round((grossCeilingArea * .005)*10)/10;
	doorsLabor = Math.round((numDoors * .4)*10)/10;
	WindowsLabor = Math.round((numWindows * .225)*10)/10;
	moldingsLabor = Math.round(Math.ceil((totalMoldingLen * .008)*100))/100;

//	laborEst = (wallPaperRolls * 1.768;
	
	resultsvalue = "" //"You will need " + wallPaperRolls + " rolls of paper. <br /> ";
	resultsvalue = resultsvalue + "<dl><dt>Walls </dt><dd>This job requires " + wallPrimer + " gallons of primer (estimated labor: " + wallPrimerLabor + " hours) and " + wallFinish + " gallons of finish (estimated labor: " + wallFinishLabor + " hours). </dd> ";
	resultsvalue = resultsvalue + "<dt>Ceiling </dt><dd>This job requires " + ceilingPrimer + " gallons of primer (estimated labor: " + ceilingPrimerLabor + " hours) and " + ceilingFinish + " gallons of finish (estimated labor: " + ceilingFinishLabor + " hours). </dd> ";
	resultsvalue = resultsvalue + "<dt> Doors and Windows</dt><dd>This job requires " + doorsAndWindowsPrimer + " gallons of primer and " + doorsAndWindowsFinish +" gallons of finish (estimated labor for doors: " + doorsLabor + " hours; estimated labor for windows " + WindowsLabor +" hours. </dd> ";
	resultsvalue = resultsvalue + "<dt>Moldings </dt><dd>This job requires " + moldingsPrimer + " gallons of primer and " + moldingsFinish + " gallons of finish; estimated labor: " + moldingsLabor + " hours. </dd></dl> ";
	document.getElementById('answer').innerHTML = resultsvalue;
	return false;
}