//Manual J Short Form Calculator
//Copyright 1999 by James R. Leichter MrHVAC.com All Rights Reserved


function ClearWorksheetTotals() { // not used anymore 
return;
}

function Round(N) {	// Round a number 3 decimal places
	return Math.round(N*1000)/1000;
}

function IsValidPosNeg(V) {	// If V is a valid number return true, else false
	if (V == "") return 0;	  
	GotDecimalPt = 0;
	GotMinus = 0;
	GotDigit = 0;

	for (i=0; i<V.length; i++) {
		if ((V.charAt(i) < "0") || (V.charAt(i) > "9")) {
			if (V.charAt(i) == ".") {
				if (!GotDecimalPt)
					GotDecimalPt = 1;
				else
					return 0;	// More than one decimal pt
			} else if (V.charAt(i) == "-") {
				if (GotDigit || GotDecimalPt || GotMinus)
					return 0; // Minus sign in wrong place
				else
					GotMinus = 1;	
			} else {
				return 0; // Not decimal pt and not a 0 to 9
			}
		}

		GotDigit = 1;
	}

	return 1;	// Good numeric value
}

function Numeric(V) {	// If V is numeric return its value, else return 0
	if (V == "") return 0;	// Nothing entered in this field	  
	if (!IsValidPosNeg(V)) return 0; // Not a valid number

	return parseFloat(V);	// Return the numeric value
}

TableEFactors=3; // Factors per selection - 15/20/25

var WallTable = new Array(
	5.0,6.4,7.8,
	1.7,2.1,2.6,
	1.5,1.9,2.3,
	1.3,1.7,2.0,
	1.1,1.4,1.7,
	0.9,1.2,1.4,
	5.8,8.3,10.9,
	1.6,2.3,3.1,
	0.9,1.3,1.6,
	0.0,0.0,0.0,
	0.0,0.0,0.0,
	0.0,0.0,0.0
);

var CeilingTable = new Array(
	17.0,19.2,21.4,
	4.4,4.9,5.5,
	3.2,3.7,4.1,
	2.1,2.3,2.6,
	1.9,2.1,2.4,
	1.3,1.5,1.6,
	1.0,1.1,1.3,
	0.9,1.0,1.1,
	11.2,12.6,14.1,
	2.8,3.2,3.5,
	1.9,2.2,2.4,
	1.8,2.0,2.2,
	1.6,1.8,2.0
);

var FloorTable = new Array (
	0.0,0.0,0.0,
	0.0,0.0,0.0,
	0.0,0.0,0.0,
	3.9,5.8,7.7,
	0.8,1.3,1.7,
	0.5,0.8,1.1,
	0.0,0.0,0.0,
	0.0,0.0,0.0,
	0.0,0.0,0.0,
	0.0,0.0,0.0,
	0.0,0.0,0.0,
	0.0,0.0,0.0,
	0.0,0.0,0.0
);

// TABLE D

TableDFactors = 4;

var InfiltrationLossTable = new Array (
	0.4,0.4,0.3,0.3,
	1.2,1.0,0.8,0.7,
	2.2,1.5,1.2,1.0
);

var InfiltrationGainTable = new Array (
	0.2,0.2,0.2,0.2,
	0.5,0.5,0.4,0.4,
	0.8,0.7,0.6,0.5
);

// Table F

TableFFactors = 2;
SupplyTempFactors=12 * TableFFactors;

var DuctLossMultiplierTable = new Array (
//Copyright 1999 by James R. Leichter MrHVAC.com All Rights Reserved
// Supply Air Temp < 120
	1.3,1.25,
	1.2,1.15,
	1.15,1.10,
	1.10,1.05,

	1.20,1.15,
	1.15,1.10,
	1.10,1.05,
	1.05,1.00,

	1.25,1.20,
	1.15,1.10,
	1.10,1.05,
	1.05,1.00,

// Supply Air Temp > 120
	1.35,1.30,
	1.25,1.20,
	1.20,1.15,
	1.15,1.10,

	1.25,1.20,
	1.20,1.15,
	1.15,1.10,
	1.10,1.05,

	1.30,1.25,
	1.20,1.15,
	1.15,1.10,
	1.10,1.05
); 
	
//		WORKSHEET FUNCTIONS

function WorksheetError(Msg) {
	alert (Msg);
	ClearWorksheetTotals();
}

function CalcWorksheet() {
	with (document.Worksheet) {
		// COPY CUSTOMER INFO TO LOCAL VARS

		fName = Name.value;
		fPhone = Phone.value;
		fAddress = Address.value;
		fCity = City.value;
		fState = State.value;
		fZip = Zip.value;
		fJob = Job.value;

		// CHECK GROSS WALL ENTERED

		GWSqFt = GrossWallSqFt.value;
		if (Numeric(GWSqFt) == 0)
			return WorksheetError("Please fill in Gross Wall Sq Ft");

		// CHECK DESIGN TEMPERATURE ENTRIES

		if (
		 (!IsValidPosNeg(WinterInsideDesignTemp.value) ) ||
		 (!IsValidPosNeg(WinterOutsideDesignTemp.value) ) ||
		 (!IsValidPosNeg(SummerInsideDesignTemp.value) ) ||
		 (!IsValidPosNeg(SummerOutsideDesignTemp.value) ) )
			return WorksheetError("Please enter Design Temperature information");

		WIDT = WinterInsideDesignTemp.value;
		WODT = WinterOutsideDesignTemp.value;
		WTD =  WIDT - WODT;

		SIDT = SummerInsideDesignTemp.value;
		SODT = SummerOutsideDesignTemp.value;
		STD =  SODT - SIDT;

		// CHECK WINDOW FORMS FILLED OUT

		DWLoss = GetHeatingDoorsAndWindowsBTUHLoss();

		if (DWLoss == "")
			return WorksheetError("Please complete Heating Doors And Windows. Be sure to click the Calculate button.");

		DWGain = GetCoolingDoorsAndWindowsBTUHGain();

		if (DWGain == "")
			return WorksheetError("Please complete Cooling Doors And Windows. Be sure to click the Calculate button.");

		DWSqFt = GetHeatingDoorsAndWindowsArea();
		DWSqFtx = GetCoolingDoorsAndWindowsArea();

		if (DWSqFtx != DWSqFt)
			return WorksheetError("Please correct differnece in area between Heating and Cooling Windows and Doors");


		//	NET WALL CALCULATIONS

		NWSqFt = GWSqFt - DWSqFt;
		Index = WallConstruction.selectedIndex;

		if (Index <= 0)
			return WorksheetError("Please select Wall Construction");

		CoolingIndex = NetWallTempDiff.selectedIndex;

		if (CoolingIndex <= 0)
			return WorksheetError("Please select Wall Temp Difference");

		// Wall heating is factor is contained in the form option values

		NWHF = WallConstruction.options[Index].value;
		NWLoss = NWHF * NWSqFt;

		// Wall cooling factor is contained in the wall table

		TableIndex = (Index-1) * TableEFactors + CoolingIndex-1;
		NWCF = WallTable[TableIndex];
		NWGain = Round(WallTable[TableIndex] * NWSqFt);


		 // A1 CALCULATION - USER DEFINABLE ENTRY

		A1Desc = A1Description.value;

		A1HF = "";
		A1Loss = "";
		if (A1BTUHLoss.value != "") {
			A1Loss = Numeric(A1BTUHLoss.value);
			if (A1Loss <= 0)
				return WorksheetError("Please enter A1 Heating BTUHLoss");
			else
				A1HF = A1HeatingFactor.value;
		}

		A1CF = "";
		A1Gain = "";
		if (A1BTUHGain.value != "") {
			A1Gain = Numeric(A1BTUHGain.value);
			if (A1Gain <= 0)
				return WorksheetError("Please enter A1 Cooling BTUHGain");
			else
				A1CF = A1CoolingFactor.value;
		}


		 // A2 CALCULATION - USER DEFINABLE ENTRY


		A2Desc = A2Description.value;

		A2Loss = "";
		A2HF = "";
		if (A2BTUHLoss.value != "") {
			A2Loss = Numeric(A2BTUHLoss.value);
			if (A2Loss <= 0)
				return WorksheetError("Please enter A2 Heating BTUHLoss");
			else
				A2HF = A2HeatingFactor.value;
		}

		A2CF = "";
		A2Gain = "";
		if (A2BTUHGain.value != "") {
			A2Gain = Numeric(A2BTUHGain.value);
			if (A2Gain <= 0)
				return WorksheetError("Please enter A2 Cooling BTUHGain");
			else
				A2CF = A2CoolingFactor.value;
		}

		//	CEILING CALCULATIONS	

		CeilingIndex = CeilingConstruction.selectedIndex;
		if (CeilingIndex <= 0)
			return WorksheetError("Please select Ceiling Construction");

		CoolingIndex = CeilingTempDiff.selectedIndex;
		if (CoolingIndex <= 0)
			return WorksheetError("Please select Ceiling Temp Difference");

		CSqFt = Numeric(CeilingSqFt.value);
		if (CSqFt <= 0)
			return WorksheetError("Please enter Ceiling Area");

		CLoss = Round(CeilingConstruction.options[CeilingIndex].value * CSqFt);
		CHF = CeilingConstruction.options[CeilingIndex].value;
		TableIndex = (CeilingIndex-1) * TableEFactors + CoolingIndex-1;
		CCF = CeilingTable[TableIndex];
		CGain = Round(CeilingTable[TableIndex] * CSqFt);


		//	FLOOR CALCULATIONS

		FloorIndex = FloorConstruction.selectedIndex;
		if (FloorIndex <= 0)
			return WorksheetError("Please select Floor Construction");

		CoolingIndex = FloorTempDiff.selectedIndex;
		if (CoolingIndex <= 0)
			return WorksheetError("Please select Floor Temp Difference");

		FSqFt = Numeric(FloorSqFt.value);
		if (FSqFt <= 0)
			return WorksheetError("Please enter Floor Area");

		FHF = FloorConstruction.options[FloorIndex].value;
		FLoss = Round(FloorConstruction.options[FloorIndex].value * FSqFt);
		TableIndex = (FloorIndex-1) * TableEFactors + CoolingIndex-1;
		FCF = FloorTable[TableIndex];
		FGain = Round(FloorTable[TableIndex] * FSqFt);

		// INFILTRATION CALCULATIONS

		if (FSqFt <= 900) {
			TableDIndex = 0;
		} else if (FSqFt <= 1500) {
			TableDIndex = 1;
		} else if (FSqFt <= 2100) {
			TableDIndex = 2;
		} else { // over 2100 sq ft
			TableDIndex = 3;
		}

		if (InfiltrationQualityFactor.selectedIndex <= 0)
			return WorksheetError("Please select Infiltration Quality");

		if (Numeric(InfiltrationVol.value) <= 0)
			return WorksheetError("Please enter House Volume");

		TableDIndex = TableDIndex + TableDFactors * (InfiltrationQualityFactor.selectedIndex-1);

		IHF = InfiltrationLossTable[TableDIndex];
		ILoss = Round(
			IHF *	InfiltrationVol.value * 11 / 60);

		ICF = InfiltrationGainTable[TableDIndex];
		IGain = Round(ICF * STD * InfiltrationVol.value * 1.1 / 60);

		// SUB TOTAL BTUH LOSS

		STLoss = Round(
			1 * DWLoss +
			1 * NWLoss + 
			1 * A1Loss +
			1 * CLoss +
			1 * A2Loss +
			1 * FLoss +
			1 * ILoss);
		  
		// ADJUSTMENT FACTOR

		AHF = Math.round(WTD/10);

		// TOTAL BTUH LOSS

		TLoss = Math.round(STLoss * AHF);

		// PEOPLE COOLING FACTOR

		if (Numeric(PeopleCount.value) <= 0)
			return WorksheetError("Please enter the number of People");

		PCF = 300;
		PGain = PCF * PeopleCount.value;

		AGain = AppliancesBTUHGain.value;

		// SUB TOTAL COOLING GAIN - ROOM SENSIBLE ONLY

		STRSGain = Round(
			1 * DWGain + 
			1 * NWGain + 
			1 * A1Gain +
			1 * CGain + 
			1 * A2Gain +
			1 * FGain +
			1 * IGain + 
			1 * PGain +
			1 * AGain);

		// DUCT LOSS GAIN FACTOR

		DuctSupplyIndex = DuctLossGainSupplyTemp.selectedIndex;
		DuctIndex = DuctLossGainMultiplier.selectedIndex;

		if (DuctSupplyIndex <= 0)
			return WorksheetError("Please select Duct Supply Temp");

		if (DuctIndex <= 0)
			return WorksheetError("Please select Duct Location and Insulation");

		if (DuctIndex == 1) { // If in conditioned space
			DHF = 0; // Was in conditioned space
			DCF = 0;
		} else { // Not in conditioned space, must use calculations
			DHF = DuctLossMultiplierTable[
				(DuctIndex-2) * TableFFactors +
				(DuctSupplyIndex-1) * SupplyTempFactors +
				(WODT < 15 ? 0 : 1)];


			DCF = DuctLossGainMultiplier.options[DuctIndex].value;
		}

		STSGain = Round(DCF * STRSGain);

		MCF = 1.3;

		TLGLoss = Round(DHF * TLoss);

		TLGGain = Round(STSGain * MCF);

	} // with document.....

		// Rewrite the window with the calculated values

	document.write(
		"<center>" +

		"<table border=7>" +
		"<td colspan=4 align=\"center\"><font size=+2><b>Customer Info<b></font></td>" +
		"</tr><tr>" +
		"<th>Name</th><td>" + fName + "</td>" +
		"<th>Phone</th><td>" + fPhone + "</td>" +
		"</tr><tr>" +
		"<th>Address</th><td>" + fAddress + "</td>" +
		"<th>City</th><td>" + fCity + "</td>" +
		"</tr><tr>" +
		"<th>State</th><td>" + fState + "</td>" +
		"<th>Zip</th><td>" + fZip + "</td>" +
		"</tr><tr>" +
		"<th>Job</th><td colspan=3>" + fJob + "</td>" +
		"</tr>" +
		"</table>" +

		"<br><br><br>" +

		"<table border=7>" +

		"<tr>" +
		"<th colspan=4>Design Temperature</th>" +
		"</tr>" +

		"<tr>" +
		"<td></td>" +
		"<th>Inside</th>" +
		"<th>Outside</th>" +
		"<th>Difference</th>" +
		"</tr>" +

		"<tr>" +
		"<th>Winter</th>" +
		"<td>" + WIDT + "</td>" +
		"<td>" + WODT + "</td>" +
		"<td>" + WTD  + "</td>" +
		"</tr>" +

		"<tr>" +
		"<th>Summer</th>" +
		"<td>" + SIDT + "</td>" +
		"<td>" + SODT + "</td>" +
		"<td>" + STD  + "</td>" +
		"</tr>" +

		"</table>" +

		"<br><br><br>" +

		"<table border=7>" +

		"</tr>" +
		"<tr>" +
		"<td align=center colspan=2><font size=+1><b>HEATING<b></font></td>" +
		"<td align=center colspan=2><font size=+1><b>COMMON DATA SECTION<b></font></td>" +
		"<td align=center colspan=2><font size=+1><b>COOLING<b></font></td>" +
		"</tr>" +

		"<tr>" +
		"<th>BTUH LOSS</th>" +
		"<th>HEATING<br>FACTOR</th>" +
		"<th>SUBJECT</b></font></th>" +
		"<th>AREA<br>Sq Ft</th>" +
		"<th>COOLING<br>FACTOR</th>" +
		"<th>BTUH GAIN</th>" +
		"</tr>" +

		"<tr>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td>GROSS WALL</td>" +
		"<td>" + GWSqFt + "</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"</tr>" +

		"<tr>" +
		"<td>" + DWLoss+ "</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td>DOORS & WINDOWS</td>" +
		"<td>" + DWSqFt + "</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td>" + DWGain + "</td>" +
		"</tr>" +

		"<tr>" +
		"<td>" + NWLoss + "</td>" +
		"<td>" + NWHF + "</td>" +
		"<td>NET WALL</td>" +
		"<td>" + NWSqFt + "</td>" +
		"<td>" + NWCF + "</td>" +
		"<td>" + NWGain + "</td>" +
		"</tr>" +

		"<tr>" +
		"<td>" + CLoss + "</td>" +
		"<td>" + CHF + "</td>" +
		"<td>CEILING</td>" +
		"<td>" + CSqFt + "</td>" +
		"<td>" + CCF+ "</td>" +
		"<td>" + CGain+ "</td>" +
		"</tr>" +

		"<tr>" +
		"<td>" + A1Loss + "</td>" +
		"<td>" + A1HF + "</td>" +
		"<td colspan=2>" + A1Desc + "</td>" +
		"<td>" + A1CF + "</td>" +
		"<td>" + A1Gain + "</td>" +
		"</tr>" +

		"<tr>" +
		"<td>" + A2Loss + "</td>" +
		"<td>" + A2HF + "</td>" +
		"<td colspan=2>" + A2Desc + "</td>" +
		"<td>" + A2CF + "</td>" +
		"<td>" + A2Gain + "</td>" +
		"</tr>" +

		"<tr>" +
		"<td>" + FLoss + "</td>" +
		"<td>" + FHF + "</td>" +
		"<td>FLOORS</td>" +
		"<td>" + FSqFt + "</td>" +
		"<td>" + FCF + "</td>" +
		"<td>" + FGain + "</td>" +
		"</tr>" +

		"<tr>" +
		"<td>" + ILoss + "</td>" +
		"<td>" + IHF + "</td>" +
		"<td colspan=2>INFILTRATION FACTOR BTU Hr</td>" +
		"<td>" + ICF + "</td>" +
		"<td>" + IGain + "</td>" +
		"</tr>" +

		"<tr>" +
		"<td>" + STLoss+ "</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td colspan=2>SUB-TOTAL BTUH LOSS (per 10 deg F)</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"</tr>" +

		"<tr>" +
		"<td>X " + AHF + "</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td colspan=2>ADJUSTMENT FACTOR</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"</tr>" +

		"<tr>" +
		"<td>" + TLoss + "</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td colspan=2>TOTAL BTUH LOSS</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"</tr>" +

		"<tr>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td colspan=2>PEOPLE(Assume 2 persons per bedroom)</td>" +
		"<td>" + PCF + "</td>" +
		"<td>" + PGain + "</td>" +
		"</tr>" +

		"<tr>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td colspan=2>APPLIANCES</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td>" + AGain + "</td>" +
		"</tr>" +

		"<tr>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td colspan=2>SUB TOTAL BTUH GAIN(Room sensible only)</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td>" + STRSGain + "</td>" +
		"</tr>" +

		"<tr>" +
		"<td> X</td>" +
		"<td>" + DHF + "</td>" +
		"<td colspan=2>DUCT LOSS / GAIN FACTOR</td>" +
		"<td>" + DCF + "</td>" +
		"<td> X</td>" +
		"</tr>" +

		"<tr>" +
		"<td bgcolor=gray>&nbsp</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td colspan=2>SUB TOTAL BTUH (Sensible Gain)</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td>" + STSGain + "</td>" +
		"</tr>" +

		"<tr>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td colspan=2>MOISTURE REMOVAL</td>" +
		"<td>" + MCF + "</td>" +
		"<td> X</td>" +
		"</tr>" +

		"<tr>" +
		"<td>" + TLGLoss + "</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td colspan=2>TOTAL BTUH LOSS</td>" +
		"<td bgcolor=gray>&nbsp;</td>" +
		"<td>" + TLGGain + "</td>" +
		"</tr>" +
		"</table>"
	);

}
		

//		Heating Doors And Windows FUNCTIONS

function GetHeatingDoorsAndWindowsBTUHLoss() {
	return document.HeatingDoorsAndWindows.BTUHLossTotal.value;
}

function GetHeatingDoorsAndWindowsArea() {
	return document.HeatingDoorsAndWindows.AreaTotal.value;
}


HeatingDoorsAndWindowsLines = 13;	// Number of lines containing form items
HeatingDoorsAndWindowsItemsPerLine = 3;	// Includes form items in one line

HeatingDoorsAndWindowsFrameIndex = 0;	// Index within a line
HeatingDoorsAndWindowsAreaIndex = 1;
HeatingDoorsAndWindowsBTUHLossIndex=2;

function CalcHeatingDoorsAndWindowsTotals() {

 with (document.HeatingDoorsAndWindows) {

	for (Line=0;Line<HeatingDoorsAndWindowsLines;Line++) {
		LineIndex = Line * HeatingDoorsAndWindowsItemsPerLine;
		FrameIndex = elements[LineIndex + HeatingDoorsAndWindowsFrameIndex].selectedIndex;
		FrameValue = elements[LineIndex + HeatingDoorsAndWindowsFrameIndex].options[FrameIndex].value;
		Area = Numeric(elements[LineIndex + HeatingDoorsAndWindowsAreaIndex].value);
		BTUHLoss = FrameValue * Area;

		if (BTUHLoss == 0) { // User entered only one item on this line - need two
			elements[LineIndex + HeatingDoorsAndWindowsBTUHLossIndex].value = "";
			if ( (Area == 0) && (FrameValue != 0) ) {
				alert("Please enter Area, Heating Doors And Windows Line " + (Line+1));
				return;
			}
			if ( (Area != 0) && (FrameValue == 0) ) {
				alert("Please select Frame, Heating Doors And Windows Line " + (Line+1));
				return;
			}
		} else { // Good values for this line

			elements[LineIndex + HeatingDoorsAndWindowsBTUHLossIndex].value = Round(BTUHLoss);
			AreaTotal.value = 1 * AreaTotal.value + Area;
			BTUHLossTotal.value = Numeric(BTUHLossTotal.value) + Numeric(BTUHLoss);
		}
		// Round off totals
			AreaTotal.value = Round(Numeric(AreaTotal.value));
			BTUHLossTotal.value = Round(Numeric(BTUHLossTotal.value));
	}

 }
}

function ClearHeatingDoorsAndWindowsTotals() {
	with (document.HeatingDoorsAndWindows) {
		AreaTotal.value = "";		
		BTUHLossTotal.value = "";
	}
}
//Copyright 1999 by James R. Leichter MrHVAC.com All Rights Reserved
function NewHeatingDoorsAndWindowsEntry(BTUHLoss) {
	BTUHLoss.value = "";
	ClearHeatingDoorsAndWindowsTotals();
}

	// Cooling Doors And Windows Functions

var CoolingDoorsAndWindowsFactors = new Array(
	 18,22,26,	14,16,18,	11,12,13,
	 37,41,45,	31,33,35,	26,27,28,
	 52,56,60,	44,46,48,	38,39,40,
	 45,48,53,	39,41,43,	33,34,35,
	 28,32,36,	23,25,27,	19,20,21,
	164,168,172,	141,143,145,	132,136,140,
	8.6,10.9,13.2,	8.6,10.9,13.2,	8.6,10.9,13.2,
	3.5,4.5,5.4,	3.5,4.5,5.4,	3.5,4.5,5.4
);

CoolingDoorsAndWindowsLineItems = 8;	// Number of calculable line items
CoolingDoorsAndWindowsItemsPerLine = 4;	// Number of form items on one form line

// Indexes into a form line to get the data
CoolingDoorsAndWindowsPanesIndex = 0;
CoolingDoorsAndWindowsTempDiffIndex = 1;
CoolingDoorsAndWindowsAreaIndex = 2;
CoolingDoorsAndWindowsBTUHGainIndex = 3;	// Index to the gain on a given line

function GetCoolingDoorsAndWindowsBTUHGain() {
	return document.CoolingDoorsAndWindows.BTUHGainTotal.value;
}

function GetCoolingDoorsAndWindowsArea() {
	return document.CoolingDoorsAndWindows.AreaTotal.value;
}

function CalcCoolingDoorsAndWindowsTotals() {
	ClearCoolingDoorsAndWindowsTotals();

	with (document.CoolingDoorsAndWindows) {
		for (Line=0;Line<CoolingDoorsAndWindowsLineItems;Line++) { // Calculate values for each line
			// Use index variables to make things more readable
			LineIndex = Line * CoolingDoorsAndWindowsItemsPerLine; // Address the 1st form item on a line
			PanesIndex = Numeric(elements[LineIndex + CoolingDoorsAndWindowsPanesIndex].selectedIndex) - 1;
			TempDiffIndex = Numeric(elements[LineIndex + CoolingDoorsAndWindowsTempDiffIndex].selectedIndex) - 1;
			Area = Numeric(elements[LineIndex + CoolingDoorsAndWindowsAreaIndex].value);
			elements[LineIndex + CoolingDoorsAndWindowsBTUHGainIndex].value = "";

			if ( (TempDiffIndex == -1) && ( (PanesIndex != -1) || (Area != 0) ) ) {
				alert("Please select Temp Difference, Cooling Doors And Windows line " + (1 * Line+1));
				return;
			}
			if ( (PanesIndex == -1) && ( (TempDiffIndex != -1) || (Area != 0) ) ) {
				alert("Please select Pane, Cooling Doors And Windows line " + (1 * Line+1));
				return;
			}
			if ( (Area == 0) && ( (PanesIndex != -1) || (TempDiffIndex != -1) ) ) {
				alert("Please enter Area, Cooling Doors And Windows line " + (1 * Line+1));
				return;
			}

			if (Area != 0) { // If so, line is properly filled in	
				Factor = CoolingDoorsAndWindowsFactors[(Line * 9 + PanesIndex * 3 + TempDiffIndex)];
				Gain = Factor * Area;

				elements[LineIndex + CoolingDoorsAndWindowsBTUHGainIndex].value = Round(Gain);
				BTUHGainTotal.value = 1 * BTUHGainTotal.value + Gain;
				AreaTotal.value = 1 * AreaTotal.value + Area;
			}
		}
		// Round off the results
		BTUHGainTotal.value = Round(BTUHGainTotal.value);
		AreaTotal.value = Round(AreaTotal.value);
	}
}

function ClearCoolingDoorsAndWindowsTotals() {
	with (document.CoolingDoorsAndWindows) {
		AreaTotal.value = "";		
		BTUHGainTotal.value = "";
	}
}

//Copyright 1999 by James R. Leichter MrHVAC.com All Rights Reserved
function NewCoolingDoorsAndWindowsEntry(G) {
	G.value = "";
	ClearCoolingDoorsAndWindowsTotals();
}
