/* 	Author: 	Yassen Bakalov (ybakalov@icfconsulting.com) | ICF Consulting
	Created: 	June, 2004
	Code based on RECON.xls, developed by ICF Consulting
*/
/* --------------------------------------------------------------------------------------------------- */
// do not modify the JavaScript code below this line
/* --------------------------------------------------------------------------------------------------- */
var g_DisplayResultsInPopup = true; 		// display the results in a pop-up window (need html & body tags)
	
/* expand the factors into a two-dimensional array */
for (i=0; i<g_GHG_Factors.length; i++){
	g_GHG_Factors[i] = g_GHG_Factors[i].split("~");
	g_Energy_Factors[i] = g_Energy_Factors[i].split("~");
}
/* build the HTML code for the input sheet */
g_Materials 			= g_Materials.split("~");
g_Materials_Examples 	= g_Materials_Examples.split("~");
g_Tbl_Headers1 			= g_Tbl_Headers1.split("~");
g_Tbl_Headers2 			= g_Tbl_Headers2.split("~");
g_Recycled_Range 		= g_Recycled_Range.split("~");
var ReCon1HTML = ""; // html for section 1
var ReCon2HTML = ""; // html for section 2
// build table headers
for (i=0; i<g_Tbl_Headers1.length; i++){
	ReCon1HTML += '<th class="tblHeader" valign="bottom">'+g_Tbl_Headers1[i]+'</th>';
}
ReCon1HTML = '<table cellpadding="4" cellspacing="0" border="1" bordercolor="'+g_Tbl_Color+'" bordercolordark="white" bordercolorlight="'+g_Tbl_Color+'"><tr>'+ReCon1HTML+'</tr>';
for (i=0; i<g_Tbl_Headers2.length; i++){
	ReCon2HTML += '<th class="tblHeader" valign="bottom">'+g_Tbl_Headers2[i]+'</th>';
}
ReCon2HTML = '<table cellpadding="4" cellspacing="0" border="1" bordercolor="'+g_Tbl_Color+'" bordercolordark="white" bordercolorlight="'+g_Tbl_Color+'"><tr>'+ReCon2HTML+'</tr>';
// build table content
for (i=0; i<g_Materials.length; i++){
	ReCon1HTML += '<tr>'; // table content for section 1
	ReCon1HTML += '	<td class="tblCellCalc" valign="bottom"><input type="text" size="15" name="materials_purchased" title="'+g_Materials[i]+'" onblur="ProcessInput(this,'+i+')" class="inputField"></td>';
	ReCon1HTML += '	<td class="tblCellCalc" valign="bottom">'+g_Materials[i]+'</td>';
	ReCon1HTML += '	<td class="tblCellCalc" valign="bottom" align="center"><input type="text" size="10" name="lbs_purchased" title="'+g_Materials[i]+' Amount Purchased (lbs)" onblur="ProcessInput(this,'+i+')" class="inputField"></td>';
	ReCon1HTML += '	<td class="tblCellCalc" valign="bottom" align="center"><input type="text" size="10" name="tons_purchased" title="'+g_Materials[i]+' Amount Purchased (tons)" class="outputField" readonly></td>';
	ReCon1HTML += '	<td class="tblCellCalc" valign="bottom">'+g_Materials_Examples[i]+'</td>';
	ReCon1HTML += '</tr>';
	
	ReCon2HTML += '<tr>'; // table content for section 2
	ReCon2HTML += '	<td class="tblCellCalc" valign="bottom"><input type="text" size="15" name="materials_purchased_display" title="'+g_Materials[i]+'" class="outputField" readonly></td>';
	ReCon2HTML += '	<td class="tblCellCalc" valign="bottom">'+g_Materials[i]+'</td>';
	ReCon2HTML += '	<td class="tblCellCalc" valign="bottom" align="center"><input type="text" size="4" name="baseline_content" title="'+g_Materials[i]+' Baseline Recycled Content (%)" onblur="ProcessInput(this,'+i+')" class="inputField"></td>';
	ReCon2HTML += '	<td class="tblCellCalc" valign="bottom" align="center"><input type="text" size="4" name="alternate_content" title="'+g_Materials[i]+' Alternate Recycled Content (%)" onblur="ProcessInput(this,'+i+')" class="inputField"></td>';
	ReCon2HTML += '	<td class="tblCellCalc" valign="bottom" align="center"><input type="checkbox" name="use_default" value="1" onclick="SetDefault(this,'+i+')" title="'+g_Materials[i]+' Default"></td>';
	ReCon2HTML += '	<td class="tblCellCalc" valign="bottom" align="center">'+g_Recycled_Range[i]+'</td>';
	ReCon2HTML += '</tr>';
}
ReCon1HTML += '</table>';
ReCon2HTML += '</table>';
/* ----------------------------------------------------------------------------------- */
/* functionality for handling the calculations and results */
			
var ReConMTCEResult = '';					// the MTCE result in HTML format
var ReConMMBtuResult = '';					// the MMBtu result in HTML format	
function GenerateResult(formID,MTCEorMMBtu){
	ReConMTCEResult = '';					// initialize MTCE result in HTML format
	ReConMMBtuResult = '';					// initialize MMBtu result in HTML format	
	var MTCETotals = new Array(0,0,0);		// keeps track of baseline, alternate, and difference totals
	var MMBtuTotals = new Array(0,0,0);
	
	ReConMTCEResult += g_Output_CSS + '<p align="center"><strong>GHG Emissions</strong></p><p class="epaLtSans">(Version 3, 8/06)</p><p>This page provides the user with an estimate of the GHG emissions impact of the materials purchased or manufactured based on the information entered in the Input page.  The emission factors include upstream manufacturing emissions, carbon sequestration, and avoided disposal emissions.</p><p><strong>Results:  The table below provides emission factors and GHG emissions impacts for purchasing and manufacturing activities in the baseline and alternate recycled content scenarios.  Column f provides an estimate of the impact of shifting manufacturing processes to include more recycled inputs.  Negative results in this column indicate an emission savings.</strong></p>';
	ReConMMBtuResult += g_Output_CSS + '<p align="center"><strong>Energy Consumption</strong><p class="epaLtSans">(Version 3, 8/06)</p><p>This page provides the user with an estimate of the energy impact of the materials purchased or manufactured based on the information entered in the Input page.  The energy factors include upstream manufacturing as well as avoided disposal impacts.</p><p><strong>Results:  The table below provides energy consumption factors and total energy consumption impacts for manufacturing activities in the baseline and alternate recycled content scenarios.  Column f provides an estimate of the impact of shifting manufacturing processes to include increased recycled content. Negative results in this column indicate an energy savings.</strong></p>';
	ReConMTCEResult += '<table cellpadding="4" cellspacing="0" border="1" bordercolor="'+g_Tbl_Color+'" bordercolordark="white" bordercolorlight="'+g_Tbl_Color+'">';
	ReConMMBtuResult += '<table cellpadding="4" cellspacing="0" border="1" bordercolor="'+g_Tbl_Color+'" bordercolordark="white" bordercolorlight="'+g_Tbl_Color+'">';
	ReConMTCEResult += '<tr><th class="tblHeader">Material</th><th class="tblHeader">Surrogate Material</th><th class="tblHeader">(a)<br>Amount<br>(tons)</th><th class="tblHeader">(b)<br>Baseline 30-Yr. Total GHG Emission Factor<br>(MTCE/ton)</th><th class="tblHeader">(c)<br>Baseline 30-Yr. Total GHG Emissions (MTCE)<br>(=a x b)</th><th class="tblHeader">(d)<br>Alternate 30-Yr. Total GHG Emission Factor<br>(MTCE/ton)</th><th class="tblHeader">(e)<br>Alternate 30-Yr. Total GHG Emissions (MTCE)<br>(=a x d)</th><th class="tblHeader">(f)<br>Differential Between Baseline and Alternate (MTCE)<br>(=e - c)</th></tr>';
	ReConMMBtuResult += '<tr><th class="tblHeader">Material</th><th class="tblHeader">Surrogate Material</th><th class="tblHeader">(a)<br>Amount<br>(tons)</th><th class="tblHeader">(b)<br>Baseline 30-Yr. Total Energy Consumption<br>(MMBTus/ton)</th><th class="tblHeader">(c)<br>Baseline 30-Yr. Total Energy (MMBTus)<br>(=a x b)</th><th class="tblHeader">(d)<br>Alternate 30-Yr. Total Energy Consumption<br>(MMBTus/ton)</th><th class="tblHeader">(e)<br>Alternate 30-Yr. Total Energy Consumption (MMBTus)<br>(=a x d)</th><th class="tblHeader">(f)<br>Differential Between Baseline and Alternate (MMBTus)<br>(=e - c)</th></tr>';
	
	var flag_Results_Generated = false;
	for (i=0; i<g_Materials.length; i++){
		var v_lbs_purchased = formID.lbs_purchased[i].value;			// Amount Purchased (pounds)
		var v_baseline_content = formID.baseline_content[i].value;		// Baseline Recycled Content (percent)
		var v_alternate_content = formID.alternate_content[i].value;	// Alternate Recycled Content (percent)
				
		if (v_lbs_purchased != "") {
			if (v_baseline_content == "") v_baseline_content = ""+Math.round(g_GHG_Factors[i][3]); // set to default
			flag_Results_Generated = true;
			v_lbs_purchased = parseFloat(RemoveCommas(v_lbs_purchased),10);
			v_baseline_content = parseFloat(RemoveCommas(v_baseline_content),10);
			v_alternate_content = parseFloat(RemoveCommas(v_alternate_content),10);
			if (isNaN(v_alternate_content)) v_alternate_content = v_baseline_content;
			
			/* RMAM Emissions & Energy	*/
			// Baseline EF Calc
			var baseEFCalc = ((v_baseline_content/100)*g_GHG_Factors[i][1])+((1-(v_baseline_content/100))*g_GHG_Factors[i][0]);
			// Alternate EF Calc
			var altEFCalc = ((v_alternate_content/100)*g_GHG_Factors[i][1])+((1-(v_alternate_content/100))*g_GHG_Factors[i][0]);
			// Baseline EnF Calc (MMBtu/ton)
			var baseEnFCalc = ((v_baseline_content/100)*g_Energy_Factors[i][1])+((1-(v_baseline_content/100))*g_Energy_Factors[i][0]);
			// Alternate EnF Calc (MMBtu/ton)
			var altEnFCalc = ((v_alternate_content/100)*g_Energy_Factors[i][1])+((1-(v_alternate_content/100))*g_Energy_Factors[i][0]);
			
			/* Forest C Sequestration 	*/
			// Baseline C Seq Calc (MTCE/ton)
			var baseSeqMTCE = ((v_baseline_content/g_GHG_Factors[i][7])*g_GHG_Factors[i][2]);
			// Alternate C Seq Calc (MTCE/ton)
			var altSeqMTCE = ((v_alternate_content/g_GHG_Factors[i][7])*g_GHG_Factors[i][2]);
			
			/* Avoided Disposal Adjustment	*/
			// Baseline Composite Disposal EF (MTCE/ton)
			var baseEFDisp = ((v_baseline_content/g_GHG_Factors[i][4])/100)*((g_GHG_Factors[i][5]*
				g_Disposal_Scenario[0]/100)+(g_GHG_Factors[i][6]*g_Disposal_Scenario[1]/100));
			// Alternate Composite Disposal EF (MTCE/ton)
			var altEFDisp = ((v_alternate_content/g_GHG_Factors[i][4])/100)*((g_GHG_Factors[i][5]*
				g_Disposal_Scenario[0]/100)+(g_GHG_Factors[i][6]*g_Disposal_Scenario[1]/100));
			// Baseline Composite Disposal EnF (MMBtu/ton)
			var baseEnFDisp = ((v_baseline_content/g_Energy_Factors[i][4])/100)*((g_Energy_Factors[i][5]*
				g_Disposal_Scenario[0]/100)+(g_Energy_Factors[i][6]*g_Disposal_Scenario[1]/100));
			// Alternate Composite Disposal EnF (MMBtu/ton)
			var altEnFDisp = ((v_alternate_content/g_Energy_Factors[i][4])/100)*((g_Energy_Factors[i][5]
				*g_Disposal_Scenario[0]/100)+(g_Energy_Factors[i][6]*g_Disposal_Scenario[1]/100));
			
			var v_a = v_lbs_purchased/2000;
			var v_b = baseEFCalc + baseSeqMTCE - baseEFDisp;
			/*
			var alerting;
			alerting = "baseEFCalc is " + baseEFCalc + "; ";
			alerting += " baseSeqMTCE is " + baseSeqMTCE + "; ";
			alerting += " baseEFDisp is " + baseEFDisp + "; ";
			alerting += " v_b is " + v_b + "; ";
		    alert(alerting); //DBH Testing
			*/
			var v_c = v_a * v_b; MTCETotals[0] += v_c;
			var v_d = altEFCalc + altSeqMTCE - altEFDisp;
			var v_e = v_a * v_d; MTCETotals[1] += v_e;
			var v_f = v_e - v_c; MTCETotals[2] += v_f;
			
			ReConMTCEResult += '<tr>';
			ReConMTCEResult += '<td class="tblCellCalc">'+formID.materials_purchased[i].value+'&nbsp;</td>'
			ReConMTCEResult += '<td class="tblCellCalc">'+g_Materials[i]+'</td>';
			ReConMTCEResult += '<td class="tblCellCalc">'+AddCommas(RoundIt(v_a))+'</td>';
			ReConMTCEResult += '<td class="tblCellCalc">'+AddCommas(RoundIt(v_b))+'</td>';
			ReConMTCEResult += '<td class="tblCellCalc">'+AddCommas(RoundIt(v_c))+'</td>';
			ReConMTCEResult += '<td class="tblCellCalc">'+AddCommas(RoundIt(v_d))+'</td>';
			ReConMTCEResult += '<td class="tblCellCalc">'+AddCommas(RoundIt(v_e))+'</td>';
			ReConMTCEResult += '<td class="tblCellCalc">'+AddCommas(RoundIt(v_f))+'</td>';
			ReConMTCEResult += '</tr>';
			
			var v_a = v_lbs_purchased/2000;
			var v_b = baseEnFCalc - baseEnFDisp;
			var v_c = v_a * v_b; MMBtuTotals[0] += v_c;
			var v_d = altEnFCalc - altEnFDisp;
			var v_e = v_a * v_d; MMBtuTotals[1] += v_e;
			var v_f = v_e - v_c; MMBtuTotals[2] += v_f;
			
			ReConMMBtuResult += '<tr>';
			ReConMMBtuResult += '<td class="tblCellCalc">'+formID.materials_purchased[i].value+'&nbsp;</td>'
			ReConMMBtuResult += '<td class="tblCellCalc">'+g_Materials[i]+'</td>';
			ReConMMBtuResult += '<td class="tblCellCalc">'+AddCommas(RoundIt(v_a))+'</td>';
			ReConMMBtuResult += '<td class="tblCellCalc">'+AddCommas(RoundIt(v_b))+'</td>';
			ReConMMBtuResult += '<td class="tblCellCalc">'+AddCommas(RoundIt(v_c))+'</td>';
			ReConMMBtuResult += '<td class="tblCellCalc">'+AddCommas(RoundIt(v_d))+'</td>';
			ReConMMBtuResult += '<td class="tblCellCalc">'+AddCommas(RoundIt(v_e))+'</td>';
			ReConMMBtuResult += '<td class="tblCellCalc">'+AddCommas(RoundIt(v_f))+'</td>';
			ReConMMBtuResult += '</tr>';
		}
	}
	alerting = "";//DBH Testing
	if (!flag_Results_Generated) {
		ReConMTCEResult += '<tr><td class="tblCellCalc" colspan="8" align="center"> - - - <strong>No inputs were provided</strong> - - - </td></tr>';
		ReConMMBtuResult += '<tr><td class="tblCellCalc" colspan="8" align="center"> - - - <strong>No inputs were provided</strong> - - - </td></tr>';
	
	}
	
	// GHG Output
	ReConMTCEResult += '<tr><td class="tblCellCalc" colspan="3"><strong>Total</strong></td>';
	ReConMTCEResult += '<td class="tblCellCalc"><strong>Baseline</strong></td>';
	ReConMTCEResult += '<td class="tblCellCalc"><strong>'+AddCommas(RoundIt(MTCETotals[0]))+'</strong></td>';
	ReConMTCEResult += '<td class="tblCellCalc"><strong>Alternate</strong></td>';
	ReConMTCEResult += '<td class="tblCellCalc"><strong>'+AddCommas(RoundIt(MTCETotals[1]))+'</strong></td>';
	ReConMTCEResult += '<td class="tblCellCalc"><strong>'+AddCommas(RoundIt(MTCETotals[2]))+'</strong></td></tr>';
	ReConMTCEResult += '</table>';
	
	ReConMTCEResult += '<p class="tblCellCalc">Notes:<br>&middot; Values may not sum due to independent rounding.<br>&middot; Emission factors taken from the EPA\'s WAste Reduction Model (WARM).<br>&middot; Miscellaneous Metals: Average of aluminum and steel.<br>&middot; Miscellaneous Plastics: Average of PET, HDPE, LDPE.</p>';
	ReConMTCEResult += '<table cellpadding="4" cellspacing="0" border="1" bordercolor="'+g_Tbl_Color+'" bordercolordark="white" bordercolorlight="'+g_Tbl_Color+'" width="100%">';
	ReConMTCEResult += '<tr><td class="tblCellCalc">&nbsp;</td><td class="tblHeader"><strong>MTCE</strong></td><td class="tblHeader"><strong>MTCO2 Eq</strong></td></tr>';
	ReConMTCEResult += '<tr><td class="tblCellCalc">The life-cycle greenhouse gas emissions for the baseline manufacturing scenario are:</td><td class="tblCellCalc" align="center"><strong>'+AddCommas(RoundIt(MTCETotals[0]))+'</strong></td><td class="tblCellCalc" align="center"><strong>'+AddCommas(RoundIt(MTCETotals[0]*44/12))+'</strong></td></tr>';
	ReConMTCEResult += '<tr><td class="tblCellCalc">The life-cycle greenhouse gas emissions for the alternate manufacturing scenario are:</td><td class="tblCellCalc" align="center"><strong>'+AddCommas(RoundIt(MTCETotals[1]))+'</strong></td><td class="tblCellCalc" align="center"><strong>'+AddCommas(RoundIt(MTCETotals[1]*44/12))+'</strong></td></tr>';
	ReConMTCEResult += '<tr><td class="tblCellCalc"><strong>The greenhouse gas benefit associated with increasing the fraction of recycled inputs is:</strong><br>Note: negative value indicates GHG emission reductions, i.e., benefit.</td><td class="tblCellCalc" align="center"><strong>'+AddCommas(RoundIt(MTCETotals[2]))+'</strong></td><td class="tblCellCalc" align="center"><strong>'+AddCommas(RoundIt((MTCETotals[1]-MTCETotals[0])*44/12))+'</strong></td></tr>';
	ReConMTCEResult += '<tr><td class="tblCellCalc"><strong>The greenhouse gas benefit in terms of passengar cars not driven for one year:</strong></td><td class="tblCellCalc" colspan="2" align="center"><strong>'+AddCommas(RoundIt(MTCETotals[2]/(-1.26)))+' cars</strong></td></tr>';
	ReConMTCEResult += '</table>';
	// Energy Output
	ReConMMBtuResult += '<tr><td class="tblCellCalc" colspan="3"><strong>Total</strong></td>';
	ReConMMBtuResult += '<td class="tblCellCalc"><strong>Baseline</strong></td>';
	ReConMMBtuResult += '<td class="tblCellCalc"><strong>'+AddCommas(RoundIt(MMBtuTotals[0]))+'</strong></td>';
	ReConMMBtuResult += '<td class="tblCellCalc"><strong>Alternate</strong></td>';
	ReConMMBtuResult += '<td class="tblCellCalc"><strong>'+AddCommas(RoundIt(MMBtuTotals[1]))+'</strong></td>';
	ReConMMBtuResult += '<td class="tblCellCalc"><strong>'+AddCommas(RoundIt(MMBtuTotals[2]))+'</strong></td></tr>';
	ReConMMBtuResult += '</table>';
	
	ReConMMBtuResult += '<p class="tblCellCalc">Notes:<br>&middot; Values may not sum due to independent rounding.<br>&middot; Energy factors taken from the EPA\'s WAste Reduction Model (WARM).<br>&middot; Miscellaneous Metals: Average of aluminum and steel.<br>&middot; Miscellaneous Plastics: Average of PET, HDPE, LDPE.</p>';
	ReConMMBtuResult += '<table cellpadding="4" cellspacing="0" border="1" bordercolor="'+g_Tbl_Color+'" bordercolordark="white" bordercolorlight="'+g_Tbl_Color+'" width="100%">';
	ReConMMBtuResult += '<tr><td class="tblCellCalc">The life-cycle energy consumption for the baseline manufacturing scenario is:</td><td class="tblCellCalc" align="right"><strong>'+AddCommas(RoundIt(MMBtuTotals[0]))+' MMBtu</strong></td></tr>';
	ReConMMBtuResult += '<tr><td class="tblCellCalc">The life-cycle energy consumption for the alternate manufacturing scenario is:</td><td class="tblCellCalc" align="right"><strong>'+AddCommas(RoundIt(MMBtuTotals[1]))+' MMBtu</strong></td></tr>';
	ReConMMBtuResult += '<tr><td class="tblCellCalc"><strong>The energy benefit associated with increasing the fraction of recycled inputs is:</strong><br>Note: negative value indicates energy savings, i.e., benefit.</td><td class="tblCellCalc" align="right"><strong>'+AddCommas(RoundIt(MMBtuTotals[2]))+' MMBtu</strong></td></tr>';
	ReConMMBtuResult += '<tr><td class="tblCellCalc"><strong>The energy benefit in terms of gallons of gasoline not consumed:</strong></td><td class="tblCellCalc" align="right"><strong>'+AddCommas(RoundIt(MMBtuTotals[2]/(-0.124047619)))+' Gallons</strong></td></tr>';
	ReConMMBtuResult += '</table>';
	
	DisplayResults(MTCEorMMBtu);
}
var reNumeric = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/ // regular expression for a number
function ProcessInput(objID,arrayIndex){	// handle the onblur event for every input field
	var fieldName = objID.name;
	var fieldValue = RemoveCommas(objID.value);
	objID.value = AddCommas(fieldValue); 	// add formatting to the input
		
	if (fieldValue == "") return true; 		// do not process empty fields
	
	if (fieldName == "lbs_purchased") {
		if (!reNumeric.test(fieldValue)) return InvalidEntry(objID,"Invalid number...");
		var inTons = "" + fieldValue/2000; inTons = RoundIt(inTons);
		objID.form.tons_purchased[arrayIndex].value = AddCommas(inTons);
	}
	else if (fieldName == "baseline_content" || fieldName == "alternate_content"){
		if (!reNumeric.test(fieldValue)) return InvalidEntry(objID,"Invalid number...");
		else if (fieldValue > 100) return InvalidEntry(objID,"Invalid percentage...");
	}
	else if (fieldName == "materials_purchased"){
		objID.form.materials_purchased_display[arrayIndex].value = fieldValue;
	}
}
function InvalidEntry(objID,errorMsg){		// display alerts when user enters invalid numbers
	alert(errorMsg);
	objID.focus();
	objID.select();
	return false;
}
function RemoveCommas(strValue) {			// remove commas from a string
	var objRegExp = /,/g;
	return strValue.replace(objRegExp,'');
}
function AddCommas(strValue) {				// add commas as a thousands' separator
	strValue = strValue + "";
	var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');
	while(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, '$1,$2');
	}
	return strValue;
}
function RoundIt(numResult){				// provide custom level of rounding
	if (isNaN(numResult) || numResult == 0) return numResult;
	else {
		if (Math.abs(numResult) < 0.01) numResult = Math.round(numResult*1000)/1000;
		else if (Math.abs(numResult) < 10) numResult = Math.round(numResult*100)/100;
		else numResult = Math.round(numResult*100)/100;
		return numResult;
	}
}
function SetDefaultAll(formID){				// populate a Baseline Recycled Content field with a default value
	for (i=0; i<g_Materials.length; i++){
		if (formID.use_default[i].checked) formID.use_default[i].checked = false;
		else formID.use_default[i].checked = true;
		SetDefault(formID.use_default[i],i);
	}
}
function SetDefault(objID,arrayIndex){		// populate all Baseline Recycled Content fields with default values
	if (objID.form.baseline_content[arrayIndex].value == "" && objID.checked)
		objID.form.baseline_content[arrayIndex].value = Math.round(g_GHG_Factors[arrayIndex][3]); // Default Recycled Content
	else if (objID.form.baseline_content[arrayIndex].value == Math.round(g_GHG_Factors[arrayIndex][3]) && !objID.checked)
		objID.form.baseline_content[arrayIndex].value = "";
	return true;
}
var MaterialSelected = "";
function PopulateUnit(formID, MaterialID){		// populare the unit picklist of the Unit Converter interface
	var oUnit = formID.Unit;
	oUnit.options[0].selected = true;
	formID.WeightPerUnit.value = "";
	formID.TotalWeight.value = "";
	if (MaterialID < 1 || isNaN(MaterialID)) {
		oUnit.options.length = 1;
		MaterialSelected = "";
		return true;
	}
	else MaterialID--; // decrease the index (the first field is "Select a material..."
	oUnit.options.length = ConverterValues[MaterialID].length;
	for (i=0; i<ConverterValues[MaterialID].length-1; i++){
		oUnit[i+1].text = ConverterValues[MaterialID][i][0] + " (" + ConverterValues[MaterialID][i][2] + ")";
		oUnit[i+1].value = ConverterValues[MaterialID][i][1];
	}
	MaterialSelected = ConverterMaterials[MaterialID];
}
function CalcTotalWeight(formID, UnitValue){	// calculate total weight for the material unit converter
	if (!isNaN(UnitValue)) formID.WeightPerUnit.value = UnitValue;
	var vUnits = RemoveCommas(formID.nUnits.value);
	var vWigth = RemoveCommas(formID.WeightPerUnit.value);
	var vTotal = 0;
	vTotal = vUnits * vWigth;
	if (vTotal == "0" || isNaN(vTotal)) formID.TotalWeight.value = "";
	else formID.TotalWeight.value = AddCommas(RoundIt(vTotal));
}
function CopyToInputs(formID,openerFormName){					// copy results from the Unit Converter pop-up to the opener
	var MatchFound = false;
	if (MaterialSelected == "") {
		alert("Material is not selected.")
		return false;
	}
	else if (formID.TotalWeight.value == "" || isNaN(RemoveCommas(formID.TotalWeight.value))){
		alert("No weight is available to copy.")
		window.focus();
		return false;
	}
	for (i=0; i<g_Materials.length; i++) if (g_Materials[i]==MaterialSelected) {MatchFound = true; break;}
	
	if (!MatchFound) alert(MaterialSelected + " is not avaliable on the input page.\nPlease copy and paste the value manually.");
	else {
		if (openerFormName == "") {alert("Error has occured.\nForm is undefined."); return false;}
		var fieldValue = RemoveCommas(formID.TotalWeight.value);
		document.forms[openerFormName].lbs_purchased[i].value = AddCommas(fieldValue);
		ProcessInput(document.forms[openerFormName].lbs_purchased[i],i)
		document.forms[openerFormName].lbs_purchased[i].focus();
		document.forms[openerFormName].lbs_purchased[i].select();
		//alert(MaterialSelected + " was updated.");
	}
}
function CalcTotalPaperWeight(formID){			// calculate the total weight of paper for the printer/office paper ream weight calculator 
	var vWidth = RemoveCommas(formID.pWidth.value);
	var vHeight = RemoveCommas(formID.pHeight.value);
	var vGrade = RemoveCommas(formID.pGrade.value);
	var vPurchase = RemoveCommas(formID.pPurchase.value);
	
	var vReam = (vWidth*vHeight)/(8.5*11)*(vGrade/20)*5;
	var vTotalWeight = vReam * vPurchase;
	
	if (isNaN(vReam)) vReam = "N/A"
	if (isNaN(vTotalWeight)) vTotalWeight = "N/A"
	
	formID.pReam.value = AddCommas(RoundIt(vReam));
	formID.pTotalWeight.value = AddCommas(RoundIt(vTotalWeight));
}
var ConverterMaterials = new Array();
var ConverterValues = new Array();
function DisplayConverter(formID){				// write the unit converter content
	v_Unit_Converter = g_Unit_Converter.split("@");// expand the unit converter table 
	var ConvResults = "";
	ConvTools = g_Output_CSS+'<p align="center"><strong>Unit Converter</strong></p><p class="epaLtSans">(Version 3, 8/06)</p><table align="center" width="600"><tr><td>This sheet provides a calculator based on sample weights for some typical materials and an office paper weight calculator.  The unit weight values may be used to determine the weight of materials purchased or manufactured when only information on the number of units is available.  Please note that these values should be considered as an approximation only, and the user is encouraged to utilize actual material weight values where possible.  The user can select the drop-down windows below to choose a material type and example material from the Material Unit Weight Table, and then enter the number of units in the input cell (shaded) to get an approximate weight estimate.  The weight estimates created in this sheet can then be used in the Input Sheet.</td></tr></table>';
	ConvResults += '<p align="center"><strong>Material Unit Weight Table</strong></p><table cellpadding="4" cellspacing="0" border="1" bordercolor="'+g_Tbl_Color+'" bordercolordark="white" bordercolorlight="'+g_Tbl_Color+'" align="center">';
	
	ConvResults += '<tr><th class="tblHeader">Material Type</th><th class="tblHeader">Example Material</th><th class="tblHeader">Weight (lbs)</th><th class="tblHeader">Unit Explanation/Notes</th></tr>';
	for (i=0; i<v_Unit_Converter.length; i++){
		v_Unit_Converter[i] = v_Unit_Converter[i].split("~"); // split into each cell
		ConvResults += '<tr><td class="tblCellCalc" rowspan="'+(v_Unit_Converter[i].length-1)/3+'">'+v_Unit_Converter[i][0]+'</td>';
		var tmpConverterIndex = 0;
		var tmpConverterSubIndex = 0;
		ConverterMaterials[i] = v_Unit_Converter[i][0];
		ConverterValues[i] = new Array();
		ConverterValues[i][0] = new Array();
		for (j=1; j<v_Unit_Converter[i].length; j++){
			ConverterValues[i][tmpConverterIndex][tmpConverterSubIndex++] = v_Unit_Converter[i][j];
			ConvResults += '<td class="tblCellCalc">'+v_Unit_Converter[i][j]+'</td>'
			if (j%3 == 0) {ConvResults += '</tr><tr>'; ConverterValues[i][++tmpConverterIndex] = new Array(); tmpConverterSubIndex=0;}
		}
		ConvResults += '</tr>';
	}
	ConvResults += "</table><div align='center' class='footnote'>Please note that weights of these materials may vary depending on design specifications and other factors.</div>";
	
	ConvTools += '<form name="ReCon_Converter">';
	ConvTools += '<table cellpadding="0" cellspacing="0" border="0" align="center" width="600"><tr><td align="left">';
	ConvTools += '	<select name="Material" onchange="window.opener.PopulateUnit(this.form,this.selectedIndex)">';
	ConvTools += '		<option>Select a material...</option>';
	for (i=0; i<ConverterMaterials.length; i++){
		ConvTools += '		<option value="'+i+'">'+ConverterMaterials[i]+'</option>';
	}
	ConvTools += '	</select>';
	ConvTools += '	<select name="Unit" onchange="window.opener.CalcTotalWeight(this.form,this.value)">';
	ConvTools += '		<option>Select a unit...</option>';
	ConvTools += '	</select>';
	ConvTools += '	</td></tr>';
	ConvTools += '	<tr><td class="tblCellCalc"><strong>Material Unit Converter</strong></td></tr><tr><td>';
	ConvTools += '	<table cellpadding="4" cellspacing="0" border="1" bordercolor="'+g_Tbl_Color+'" bordercolordark="white" bordercolorlight="'+g_Tbl_Color+'" width="100%">';
	ConvTools += '	<tr><th class="tblHeader">Number of units (cans, feet, cubic yards, etc.)</th><th class="tblHeader">Weight per Unit Selected</th><th class="tblHeader">Total Weight (pounds)</th></tr>';
	ConvTools += '	<tr><td class="tblCellCalc" align="center"><input type="text" name="nUnits" size="10" class="inputField" onblur="window.opener.CalcTotalWeight(this.form)"></td><td class="tblCellCalc" align="center"><input type="text" name="WeightPerUnit" size="10" class="outputField" readonly></td><td class="tblCellCalc" align="center"><input type="text" name="TotalWeight" size="10" class="outputField" readonly></td></tr>';
	ConvTools += '	<tr><td colspan="3" align="center"><input type="button" name="CopyWeightToInputPage" onclick="if (window.opener.MaterialSelected == \'\') alert(\'Material is not selected.\'); else if (this.form.TotalWeight.value == \'\' || isNaN(window.opener.RemoveCommas(this.form.TotalWeight.value))) alert(\'No weight is available to copy.\'); else window.opener.CopyToInputs(this.form,\''+formID.name+'\')" value="Copy to Input Page">&nbsp;<input type="button" name="BackToCalc" value="Return to User Input" onclick="window.close()"></td></tr>';
	ConvTools += '	</table>';
	ConvTools += '</td></tr></table>';
	ConvTools += '<br>';
	ConvTools += '<table cellpadding="0" cellspacing="0" border="0" align="center" width="600"><tr><td class="tblCellCalc">';
	ConvTools += '	<strong>Printer/Office Paper Ream Weight Calculator</strong>';
	ConvTools += '	</td></tr><tr><td>';
	ConvTools += '	<table cellpadding="4" cellspacing="0" border="1" bordercolor="'+g_Tbl_Color+'" bordercolordark="white" bordercolorlight="'+g_Tbl_Color+'">';
	ConvTools += '	<tr><td colspan="2" class="tblCellCalc">This calculator will determine the approximate weight of one ream (500 sheets) of office paper based on specifications entered by the user. The user must enter the dimensions and grade of paper (lb). The shaded fields are input fields. Note that standard office paper is considered to be 8.5" x 11", 20 lb grade.</td></tr>';
	ConvTools += '	<tr><td>';
	ConvTools += '	<table cellpadding="0" cellspacing="2" border="0" align="center">';
	ConvTools += '	<tr><td class="tblCellCalc">Paper Dimensions (inches)</td><td class="tblCellCalc">Width<br><input type="text" name="pWidth" size="10" class="inputField" onblur="window.opener.CalcTotalPaperWeight(this.form)" value="8.5"></td><td class="tblCellCalc">Height<br><input type="text" name="pHeight" size="10" class="inputField" onblur="window.opener.CalcTotalPaperWeight(this.form)" value="11"></td></tr>';
	ConvTools += '	<tr><td class="tblCellCalc">Paper Grade (lb)</td><td class="tblCellCalc" colspan="2"><input type="text" name="pGrade" size="10" class="inputField" onblur="window.opener.CalcTotalPaperWeight(this.form)" value="20"></td></tr>';
	ConvTools += '	<tr><td class="tblCellCalc">Weight of One Ream (lbs)</td><td class="tblCellCalc" colspan="2"><input type="text" name="pReam" size="10" class="outputField" readonly value="5"></td></tr>';
	ConvTools += '	<tr><td class="tblCellCalc">Reams Purchased</td><td class="tblCellCalc" colspan="2"><input type="text" name="pPurchase" size="10" class="inputField" onblur="window.opener.CalcTotalPaperWeight(this.form)" value="0"></td></tr>';
	ConvTools += '	<tr><td class="tblCellCalc">Total Weight of Paper (lbs)</td><td class="tblCellCalc" colspan="2"><input type="text" name="pTotalWeight" size="10" class="outputField" readonly value="0"></td></tr>';
	ConvTools += '	</table></td></tr>';
	ConvTools += '	</table>';
	ConvTools += '</td></tr></table>';
	ConvTools += '</form>';
	
	ConvResults = ConvTools + ConvResults;
	
	if (g_DisplayResultsInPopup) {
		ConvResults += '<p align="center"><a href="javascript:void(window.close)" onclick="window.close(); return false;">Close Window</a></p>';
		ConvResults = '<html><head><title>ReCon Unit Converter</title></head><body>' + ConvResults + '</body></html>';
	}
	
	//return ConvResults;
	results_window = openWindow('','unit_converter');
	results_window.document.open();
	results_window.document.write(ConvResults);
	results_window.document.close();
}
function DisplayInputs(ContentID){			// write the input tables
	if (ContentID == 1) {
		document.write(g_Output_CSS + ReCon1HTML);
	}
	else if (ContentID == 2) {
		document.write(g_Output_CSS + ReCon2HTML);
	}
	else document.write(g_Output_CSS + ReCon1HTML + ReCon2HTML);
}
	
function DisplayResults(MTCEorMMBtu){		// write the output/result tables
	var ReConResult = "";
	if (MTCEorMMBtu == "mtce") ReConResult += ReConMTCEResult + '<p align="center"><a href="javascript:void(opener.DisplayResults(\'mmbtu\'))" onclick="opener.DisplayResults(\'mmbtu\'); return false;">View Energy Output</a>';
	else if (MTCEorMMBtu == "mmbtu") ReConResult += ReConMMBtuResult + '<p align="center"><a href="javascript:void(opener.DisplayResults(\'mtce\'))" onclick="opener.DisplayResults(\'mtce\'); return false;">View GHG Output</a>';
	else ReConResult += ReConMTCEResult +'<hr>'+ ReConMMBtuResult;
	if (g_DisplayResultsInPopup) {
		ReConResult += ' &nbsp;&nbsp; <a href="javascript:void(window.close)" onclick="window.close(); return false;">Close Window</a></p>';
		ReConResult = '<html><head><title>ReCon Output Page</title></head><body>' + ReConResult + '</body></html>';
	}
			
	results_window = openWindow('','results');
	results_window.document.open();
	results_window.document.write(ReConResult);
	results_window.document.close();
}
var popUpWin = null;
function openWindow(url,windowname,width,height){	// generic function for opening a pop-up window
	width=(width)?width:screen.width*3/4;
	height=(height)?height:screen.height*2/3;
	var screenX = 50; //(screen.width/3 - width/3);
	var screenY = 50; //(screen.height/2 - height/2);
	var features= "width=" + width + ",height=" + height;
	features += ",screenX=" + screenX + ",left=" + screenX;
	features += ",screenY=" + screenY  +",top=" + screenY;
	features += ",resizable=yes,scrollbars=yes,menubar=yes"
	popUpWin=window.open(url, windowname, features);
	if (popUpWin) popUpWin.focus();
	return popUpWin;
}
