var tblValues;

function BodyLoad()
 {
	tblValues = new Array();

	tblValues[tblValues.length] = ",Deg F,,,,,Result is #H2O/MMSCF,,,,,,,,,,Compliments of  GLYTECH SERVICES INC.,,,,,,,,";
	tblValues[tblValues.length] = "PSI,120,115,110,105,100,95,90,85,80,75,70,65,64,61,57,54,50,46,43,39,36,32,28,25";
	tblValues[tblValues.length] = "0,5517,4793,4153,3588,3091,2655,2273,1940,1651,1400,1183,996,975,859,755,663,580,507,442,385,334,289,245,207";
	tblValues[tblValues.length] = "5,4405,3827,3316,2865,2468,2120,1816,1550,1319,1118,945,796,779,686,603,530,464,405,353,308,267,231,196,166";
	tblValues[tblValues.length] = "10,3292,2861,2479,2142,1845,1585,1358,1159,986,836,707,595,583,513,451,396,347,303,264,230,200,173,146,124";
	tblValues[tblValues.length] = "15,2821,2452,2125,1836,1582,1359,1164,994,846,717,606,510,500,440,387,340,298,260,227,197,172,149,126,106";
	tblValues[tblValues.length] = "20,2350,2042,1770,1530,1318,1132,970,828,705,598,505,425,416,367,323,283,248,217,189,164,143,124,105,89";
	tblValues[tblValues.length] = "25,2090,1816,1574,1361,1173,1007,863,737,627,532,450,379,371,327,288,252,221,193,168,146,127,110,94,79";
	tblValues[tblValues.length] = "30,1830,1590,1378,1191,1027,882,756,645,549,466,394,332,325,286,252,221,193,169,147,128,111,97,82,70";
	tblValues[tblValues.length] = "35,1665,1447,1254,1084,935,803,688,587,500,424,359,302,296,261,229,201,176,154,134,117,101,88,75,63";
	tblValues[tblValues.length] = "40,1500,1303,1130,977,842,723,620,529,450,382,323,272,266,235,206,181,159,139,121,105,92,80,68,57";
	tblValues[tblValues.length] = "55,1386,1204,1044,903,778,669,573,489,416,353,299,252,246,217,191,168,147,129,112,97,85,74,58,53";
	tblValues[tblValues.length] = "50,1271,1105,958,828,714,614,526,449,382,324,274,231,226,199,175,154,135,118,103,90,78,68,48,49";
	tblValues[tblValues.length] = "62.5,1098,954,827,715,617,530,455,388,330,280,237,200,196,172,152,133,117,102,89,78,68,59,45,42";
	tblValues[tblValues.length] = "75,924,803,696,602,519,446,383,327,278,236,200,169,165,145,128,112,99,87,76,66,57,50,42,36";
	tblValues[tblValues.length] = "87.5,826,718,623,539,464,399,343,293,249,212,179,151,148,130,115,101,89,78,68,59,51,45,38,32";
	tblValues[tblValues.length] = "100,727,633,549,475,409,352,302,258,220,187,158,133,130,115,101,89,78,69,60,52,45,39,33,28";
	tblValues[tblValues.length] = "125,602,523,454,393,339,291,250,214,182,155,131,111,108,96,84,74,65,57,50,43,38,33,28,24";
	tblValues[tblValues.length] = "150,514,447,388,336,290,249,214,183,156,133,112,95,93,82,72,64,56,49,43,37,33,28,24,20";
	tblValues[tblValues.length] = "175,449,391,339,294,254,218,187,160,137,116,99,83,82,72,63,56,49,43,38,33,29,25,21,18";
	tblValues[tblValues.length] = "200,400,348,302,262,226,194,167,143,122,104,88,74,73,64,57,50,44,38,34,29,26,22,19,16";
	tblValues[tblValues.length] = "225,360,314,273,236,204,176,151,129,110,94,80,67,66,58,51,45,40,35,31,27,23,20,17,15";
	tblValues[tblValues.length] = "250,329,286,249,215,186,160,138,118,101,86,73,62,60,53,47,41,36,32,28,24,21,19,16,13";
	tblValues[tblValues.length] = "275,302,263,229,198,171,148,127,109,93,79,67,57,56,49,43,38,34,30,26,23,20,17,15,12";
	tblValues[tblValues.length] = "300,280,244,212,184,159,137,118,101,86,73,62,53,52,46,40,36,31,28,24,21,18,16,14,12";
	tblValues[tblValues.length] = "350,245,214,186,161,139,120,103,89,76,65,55,46,46,40,36,31,28,24,21,19,16,14,12,10";
	tblValues[tblValues.length] = "400,218,190,166,144,124,107,92,79,68,58,49,42,41,36,32,28,25,22,19,17,15,13,11,9";
	tblValues[tblValues.length] = "450,197,172,150,130,113,97,84,72,62,52,45,38,37,33,29,26,23,20,17,15,13,12,10,9";
	tblValues[tblValues.length] = "500,180,158,137,119,103,89,77,66,56,48,41,35,34,30,27,24,21,18,16,14,12,11,9,8";
	tblValues[tblValues.length] = "600,155,135,118,102,89,77,66,57,49,42,36,30,33,26,23,21,18,16,14,12,11,10,8,7";
	tblValues[tblValues.length] = "700,137,119,104,91,79,68,59,51,43,37,32,27,26,23,21,18,16,14,13,11,10,9,7,6";
	tblValues[tblValues.length] = "800,123,107,94,82,71,61,53,46,39,34,29,24,24,21,19,17,15,13,12,10,9,8,7,6";
	tblValues[tblValues.length] = "900,112,98,86,75,65,56,49,42,36,31,26,23,22,20,17,15,14,12,11,9,8,7,6,5";
	tblValues[tblValues.length] = "1000,103,90,79,69,60,52,45,39,33,29,25,21,20,18,16,14,13,11,10,9,8,7,6,5";
	tblValues[tblValues.length] = "1200,90,79,69,60,53,46,40,34,29,25,22,19,18,16,14,13,11,10,9,8,7,6,5,5";
	tblValues[tblValues.length] = "1400,81,71,62,54,47,41,36,31,27,23,20,17,17,15,13,12,10,9,8,7,6,6,5,4";
	tblValues[tblValues.length] = "1600,74,65,57,50,43,38,33,28,25,21,18,16,15,14,12,11,10,9,8,7,6,5,5,4";
	tblValues[tblValues.length] = "1800,68,60,53,46,40,35,30,26,23,20,17,15,14,13,11,10,9,8,7,6,6,5,4,4";
	tblValues[tblValues.length] = "2000,64,56,49,43,38,33,29,25,22,19,16,14,14,12,11,10,9,8,7,6,5,5,4,4";
	tblValues[tblValues.length] = "2200,60,53,47,41,36,31,27,24,20,18,15,13,13,12,10,9,8,7,7,6,5,5,4,3";
	tblValues[tblValues.length] = "2400,57,50,44,39,34,30,26,23,20,17,15,13,12,11,10,9,8,7,6,6,5,4,4,3";
	tblValues[tblValues.length] = "2600,55,48,42,37,33,29,25,22,19,16,14,12,12,11,10,9,8,7,6,5,5,4,4,3";
	tblValues[tblValues.length] = "2800,52,46,41,36,31,27,24,21,18,16,14,12,12,10,9,8,7,7,6,5,5,4,4,3";
	tblValues[tblValues.length] = "3000,50,45,39,35,30,27,23,20,18,15,13,11,12,10,9,8,7,6,6,5,5,4,4,3";

	for (i = 0; i < tblValues.length; i++)
	{
		tblValues[i] = tblValues[i].split(",");
	};

	var	defGasFlow;
	var	defPressure;
	var	defTemp;
	var	defAllowableH2O;
	var	defDesignRate;
	var	defPumpModel;

	defGasFlow = "20.0";
	defPressure = "1000.0";
	defTemp = "100.0";
	defAllowableH2O = "7.0";
	defDesignRate = "3.0";
	defPumpModelIndex = 0;

	if (document.cookie != "")
	{
		arCookieKeys = new Array();
		arCookieKeys = document.cookie.split(";");
		for (i = 0; i < arCookieKeys.length; i++)
		{
			arCookieKey = new Array();
			arCookieKey = arCookieKeys[i].split("=");
			if (arCookieKey[0] == "Values")
			{
				arCookieValues = new Array();
				arCookieValues = arCookieKey[1].split(",");
				for (i = 0; i < arCookieValues.length; i++)
				{
					arCookieVal = new Array();
					arCookieVal = arCookieValues[i].split("$");
					if (arCookieVal[0] == "GasFlow")
					{
						defGasFlow = arCookieVal[1];
					}
					else if (arCookieVal[0] == "Pressure")
					{
						defPressure = arCookieVal[1];
					}
					else if (arCookieVal[0] == "Temp")
					{
						defTemp = arCookieVal[1];
					}
					else if (arCookieVal[0] == "AllowableH2O")
					{
						defAllowableH2O = arCookieVal[1];
					}
					else if (arCookieVal[0] == "DesignRate")
					{
						defDesignRate = arCookieVal[1];
					}
					else if (arCookieVal[0] == "PumpModelIndex")
					{
						defPumpModelIndex = arCookieVal[1];
					};
				};
			};
		};
	};

	txtGasFlow.value = defGasFlow;
	txtPressure.value = defPressure;
	txtTemp.value = defTemp;
	txtAllowableH2O.value = defAllowableH2O;
	txtDesignRate.value = defDesignRate;
	cmbPumpModel.selectedIndex = defPumpModelIndex;
}

function CalculateClick()
 {
	for (i = 0; i < tblValues.length; i++)
	{
		for (j = 0; j < tblValues[i].length; j++)
		{
			DebugLog(i + "," + j + ":" + tblValues[i][j]);
		};
	};
 }

function CalculateData(GasFlow,Pressure,Temp,AllowableH2O,PumpValues,PumpModelIndex,DesignRate)
 {

	DebugLog("GasFlow=" + GasFlow);
	DebugLog("Pressure=" + Pressure);
	DebugLog("Temp=" + Temp);
	DebugLog("AllowableH2O=" + AllowableH2O);
	DebugLog("PumpValues=" + PumpValues);
	DebugLog("DesignRate=" + DesignRate);

	arPumpValues = new Array();
	arPumpValues = PumpValues.split(";");

	var PumpSlope;
	var PumpMaxGalPerHr;

	PumpSlope = arPumpValues[0];
	PumpMaxGalPerHr = arPumpValues[1];


	var dtExpires = new Date();
	dtExpires.setTime(dtExpires.getTime() + (365 * 24 * 60 * 60 * 1000));

	document.cookie = "Values=GasFlow$" + GasFlow + ",Pressure$" + Pressure + ",Temp$" + Temp + ",AllowableH2O$" + AllowableH2O + ",PumpSlope$" + PumpSlope + ",DesignRate$" + DesignRate + ",PumpModelIndex$" + PumpModelIndex + ";expires=" + dtExpires.toGMTString;

	// Pressure must be between 0 and 3000
	Pressure = Math.round(Pressure);
	if (isNaN(Pressure))
	{
		alert("Pressure must be a number between 0 and 3000");
		return;
	}
	else
	{
		if ((Pressure < 0) || (Pressure > 3000))
		{
			alert("Pressure must be a number between 0 and 3000");
			return;
		};
	};
	
	// Temp must be between 25 and 120
	Temp = Math.round(Temp);
	if (isNaN(Temp))
	{
		alert("Temperature must be a number between 25 and 120");
		return;
	}
	else
	{
		if ((Temp < 25) || (Temp > 120))
		{
			alert("Temperature must be a number between 25 and 120");
			return;
		};
	};

	H2OToBeRemoved = H2O(Pressure,Temp) - AllowableH2O;
	TotalH2OToRemove = H2OToBeRemoved * GasFlow;
	GlycolCirculationPerDay = TotalH2OToRemove * DesignRate;
	GlycolCirculationPerHr = GlycolCirculationPerDay / 24.0;
	GlycolCirculationPerMin = GlycolCirculationPerHr / 60.0;
	PumpStrokesPerMin = GlycolCirculationPerHr * PumpSlope;

	var strErrorMsg;
	var strColor;
	if (GlycolCirculationPerHr > PumpMaxGalPerHr)
	{
		strErrorMsg = " *** Above Pump Max ***";
		strColor = "red";
	}
	else
	{
		strErrorMsg = "";
		strColor = "lightgreen";
	}; 

	DebugLog("H2OToBeRemoved=" + H2OToBeRemoved);
	DebugLog("TotalH2OToRemove=" + TotalH2OToRemove);
	DebugLog("GlycolCirculationPerDay=" + GlycolCirculationPerDay);
	DebugLog("GlycolCirculationPerHr=" + GlycolCirculationPerHr);
	DebugLog("GlycolCirculationPerMin=" + GlycolCirculationPerMin);
	DebugLog("PumpStrokesPerMin=" + PumpStrokesPerMin);

	var	strResult;
	
	strResult = ""

	strResult = strResult + "<TABLE border=0 cellPadding=0 cellSpacing=0 NOF=\"LY\">";
	strResult = strResult + "<TBODY>";
	strResult = strResult + "<TR vAlign=top>";
	strResult = strResult + "<td width=150 align=\"right\">H<sub>2</sub>O to be removed&nbsp</td>";
	strResult = strResult + "<td width=250 align=\"left\"><input type=\"text\" size=\"6\" align=\"right\" style=\"background-color=lightgreen\" readonly=\"true\" value=\"" + Math.round(H2OToBeRemoved) + "\">pounds per mmscf per day</td>";
	strResult = strResult + "<td width=150 align=\"left\">&nbsp</td>";
	strResult = strResult + "</TR>";
	strResult = strResult + "<TR vAlign=top>";
	strResult = strResult + "<td align=\"right\">Total H<sub>2</sub>O to be removed&nbsp</td>";
	strResult = strResult + "<td align=\"left\"><input type=\"text\" size=\"6\" align=\"right\" style=\"background-color=lightgreen\" readonly=\"true\" value=\"" + Math.round(TotalH2OToRemove) + "\">pounds per day</td>";
	strResult = strResult + "</TR>";
	strResult = strResult + "<TR vAlign=top>";
	strResult = strResult + "<td align=\"right\">Glycol circulation&nbsp</td>";
	strResult = strResult + "<td align=\"left\"><input type=\"text\" size=\"6\" align=\"right\" style=\"background-color=" + strColor + "\" readonly=\"true\" value=\"" + Math.round(GlycolCirculationPerDay) + "\">gallons per day,</td>";
	strResult = strResult + "<td align=\"left\" style=\"color=red\">" + strErrorMsg + "</td>";
	strResult = strResult + "</TR>";
	strResult = strResult + "<TR vAlign=top>";
	strResult = strResult + "<td align=\"right\">&nbsp</td>";
	strResult = strResult + "<td align=\"left\"><input type=\"text\" size=\"6\" align=\"right\" style=\"background-color=" + strColor + "\" readonly=\"true\" value=\"" + Math.round(GlycolCirculationPerHr) + "\">gallons per hour,</td>";
	strResult = strResult + "<td align=\"left\" style=\"color=red\">" + strErrorMsg + "</td>";
	strResult = strResult + "</TR>";
	strResult = strResult + "<TR vAlign=top>";
	strResult = strResult + "<td align=\"right\">&nbsp</td>";
	strResult = strResult + "<td align=\"left\"><input type=\"text\" size=\"6\" align=\"right\" style=\"background-color=" + strColor + "\" readonly=\"true\" value=\"" + Math.round(GlycolCirculationPerMin) + "\">gallons per minute</td>";
	strResult = strResult + "<td align=\"left\" style=\"color=red\">" + strErrorMsg + "</td>";
	strResult = strResult + "</TR>";
	strResult = strResult + "<TR vAlign=top>";
	strResult = strResult + "<td align=\"right\">Pump strokes&nbsp</td>";
	strResult = strResult + "<td align=\"left\"><input type=\"text\" size=\"6\" align=\"right\" style=\"background-color=" + strColor + "\" readonly=\"true\" value=\"" + Math.round(PumpStrokesPerMin) + "\">per minute for a " + cmbPumpModel[PumpModelIndex].text + "</td>";
	strResult = strResult + "<td align=\"left\" style=\"color=red\">" + strErrorMsg + "</td>";
	strResult = strResult + "</TR>";
	strResult = strResult + "</TBODY>";
	strResult = strResult + "</TABLE>";

	strResult = strResult + "<br><br>Gly-Tech has provided this feature for the convenience of the site users.  It in no way warrants its accuracy.";

	txtResult.innerHTML = strResult;
}

function ClearResults()
 {
	txtResult.innerHTML = "";
 }

function DebugLog(Data)
 {
	//docResult.write(Data + "<br>");
 }

function H2O(Pressure,Temp)
 {
	// Find indices PressureH and PressureL
	PressureH = 0;
	PressureL = 0;
	for (i = 2; i < tblValues.length; i++)
	{
		DebugLog("Pressure=" + Pressure + ", tblValues=" + tblValues[i][0]);
		if (Pressure <= eval(tblValues[i][0]))
		{
			PressureH = i;
			break;
		};
	};

	if (PressureH == 0)
	{
		alert("Error calculating PressureL: Pressure off chart");
	}
	else
	{
		if (Pressure == eval(tblValues[PressureH][0]))
		{
			PressureL = PressureH;
		}
		else if (PressureH == 2)
		{
			PressureL = PressureH;
			PressureH = PressureL + 1;
		}
		else
		{
			PressureL = PressureH - 1;
		};
	};

	// Find indices TempH and TempL
	TempH = 0;
	TempL = 0;
	for (i = 2; i < tblValues.length; i++)
	{
		DebugLog("Temp=" + Temp + ", tblValues=" + tblValues[1][i]);
		if (Temp >= eval(tblValues[1][i]))
		{
			TempL = i;
			break;
		};
	};

	if (TempL == 0)
	{
		alert("Error calculating TempL: Temp off chart");
	}
	else
	{
		if (Temp == eval(tblValues[1][TempL]))
		{
			TempH = TempL;
		}	
		else if (TempL == (tblValues[1].length - 1))
		{
			TempH = TempL;
			TempL = TempH - 1;
		}
		else
		{
			TempH = TempL + 1;
		};
	};

	// Calculate interim values H2OpLtH, H2OpHtH, H2OpLtL, H2OpHtL
	H2OpLtH = tblValues[PressureL][TempH];
	H2OpHtH = tblValues[PressureH][TempH];
	H2OpLtL = tblValues[PressureL][TempL];
	H2OpHtL = tblValues[PressureH][TempL];

	DebugLog(H2OpLtH + "/"  + H2OpHtH + "/"  + H2OpLtL + "/"  + H2OpHtL + "/");
	DebugLog(PressureL + "/"  + PressureH + "/"  + TempL + "/"  + TempH + "/");

	// Calculate H2O
	PressureFactor = (PressureH - PressureL) / PressureH;
	TempFactor = (TempH - TempL) / TempH;
	H2OH = H2OpLtH - ((H2OpLtH - H2OpHtH) * PressureFactor);
	H2OL = H2OpLtL - ((H2OpLtL - H2OpHtL) * PressureFactor);
	H2ORet = H2OH - ((H2OH - H2OL) * TempFactor);

	DebugLog("H2O=" + H2ORet);

	return H2ORet;
 }

function PumpModelOnChange()
 {
	ClearResults();
 }


