var sTotalValue = 0;
var v_total = 0;  // this will hold the greenhouse gas emissions value in metric tons

//*****************************************************************************************
// this FormValidation function is invoked by the "onsubmit" in
// the form tag
function FormValidation() 
{
var iRet;
var bError=0;

// get "theForm" this way instead of passing it into the FormValidation function - otherwise, it will not work in Firefox
theForm = document.forms["form1"];

//make sure data was entered in at least one input box
iRegionCount = 0;
if (theForm.AKGD.value != "") iRegionCount++;
if (theForm.AKMS.value != "") iRegionCount++;
if (theForm.AZNM.value != "") iRegionCount++;
if (theForm.CAMX.value != "") iRegionCount++;
if (theForm.ERCT.value != "") iRegionCount++;
if (theForm.FRCC.value != "") iRegionCount++;
if (theForm.HIMS.value != "") iRegionCount++;
if (theForm.HIOA.value != "") iRegionCount++;
if (theForm.MROE.value != "") iRegionCount++;
if (theForm.MROW.value != "") iRegionCount++;
if (theForm.NEWE.value != "") iRegionCount++;
if (theForm.NWPP.value != "") iRegionCount++;
if (theForm.NYCW.value != "") iRegionCount++;
if (theForm.NYLI.value != "") iRegionCount++;
if (theForm.NYUP.value != "") iRegionCount++;
if (theForm.RFCE.value != "") iRegionCount++;
if (theForm.RFCM.value != "") iRegionCount++;
if (theForm.RFCW.value != "") iRegionCount++;
if (theForm.RMPA.value != "") iRegionCount++;
if (theForm.SPNO.value != "") iRegionCount++;
if (theForm.SPSO.value != "") iRegionCount++;
if (theForm.SRMV.value != "") iRegionCount++;
if (theForm.SRMW.value != "") iRegionCount++;
if (theForm.SRSO.value != "") iRegionCount++;
if (theForm.SRTV.value != "") iRegionCount++;
if (theForm.SRVC.value != "") iRegionCount++;
if (iRegionCount==0 && theForm.NationalAverage.value=="")
	{
	alert("Please enter a value in at least one of the region boxes in Option 1, or enter a value in the National Average box in Option 2.");
	return (false);
	}
if (iRegionCount>0 && theForm.NationalAverage.value!="")
	{
	alert("If you would like to use the National Average box in Option 2, then you cannot also use the region boxes in Option 1. Please clear your entries from either Option 1 or Option 2.");
	return (false);
	}

// now make sure that any data that was entered is a number
bError = NumberValue(theForm.AKGD, "AKGD", bError);
bError = NumberValue(theForm.AKMS, "AKMS", bError);
bError = NumberValue(theForm.AZNM, "AZNM", bError);
bError = NumberValue(theForm.CAMX, "CAMX", bError);
bError = NumberValue(theForm.ERCT, "ERCT", bError);
bError = NumberValue(theForm.FRCC, "FRCC", bError);
bError = NumberValue(theForm.HIMS, "HIMS", bError);
bError = NumberValue(theForm.HIOA, "HIOA", bError);
bError = NumberValue(theForm.MROE, "MROE", bError);
bError = NumberValue(theForm.MROW, "MROW", bError);
bError = NumberValue(theForm.NEWE, "NEWE", bError);
bError = NumberValue(theForm.NWPP, "NWPP", bError);
bError = NumberValue(theForm.NYCW, "NYCW", bError);
bError = NumberValue(theForm.NYLI, "NYLI", bError);
bError = NumberValue(theForm.NYUP, "NYUP", bError);
bError = NumberValue(theForm.RFCE, "RFCE", bError);
bError = NumberValue(theForm.RFCM, "RFCM", bError);
bError = NumberValue(theForm.RFCW, "RFCW", bError);
bError = NumberValue(theForm.RMPA, "RMPA", bError);
bError = NumberValue(theForm.SPNO, "SPNO", bError);
bError = NumberValue(theForm.SPSO, "SPSO", bError);
bError = NumberValue(theForm.SRMV, "SRMV", bError);
bError = NumberValue(theForm.SRMW, "SRMW", bError);
bError = NumberValue(theForm.SRSO, "SRSO", bError);
bError = NumberValue(theForm.SRTV, "SRTV", bError);
bError = NumberValue(theForm.SRVC, "SRVC", bError);
bError = NumberValue(theForm.NationalAverage, "National Average", bError);
if (bError != 0) return (false);

// now do the calculations
sTotalValue = 0;
iRet = CalculateC02Equiv(theForm);

// convert the pounds to metric tons
v_total = sTotalValue * multiples[1];  

// display the value in the "sum of greenhouse gas emissions" box 
theForm.y0.value = commaSplit(roundIt(v_total));

// if the current unit is not metric tons, then do the conversion for the unit that is selected
if (theForm.resultUnits.selectedIndex != 2)
	{
	iRet = setResultUnits(form1);
	}

// display the equivalencies 
iRet = calculateEquivs();

}

//*****************************************************************************************
// this function calls the CalculateSubregionvalue function for the National Average box if
// it is filled in, or for each possible region if the National Average is not filled in.
function CalculateC02Equiv(theForm)
{
var iRet;

if (theForm.NationalAverage.value != "")
	{
	iRet = CalculateSubregionValue("NationalAverage", theForm.NationalAverage.value);
	}
else
	{
	iRet = CalculateSubregionValue("AKGD", theForm.AKGD.value);
	iRet = CalculateSubregionValue("AKMS", theForm.AKMS.value);
	iRet = CalculateSubregionValue("AZNM", theForm.AZNM.value);
	iRet = CalculateSubregionValue("CAMX", theForm.CAMX.value);
	iRet = CalculateSubregionValue("ERCT", theForm.ERCT.value);
	iRet = CalculateSubregionValue("FRCC", theForm.FRCC.value);
	iRet = CalculateSubregionValue("HIMS", theForm.HIMS.value);
	iRet = CalculateSubregionValue("HIOA", theForm.HIOA.value);
	iRet = CalculateSubregionValue("MROE", theForm.MROE.value);
	iRet = CalculateSubregionValue("MROW", theForm.MROW.value);
	iRet = CalculateSubregionValue("NEWE", theForm.NEWE.value);
	iRet = CalculateSubregionValue("NWPP", theForm.NWPP.value);
	iRet = CalculateSubregionValue("NYCW", theForm.NYCW.value);
	iRet = CalculateSubregionValue("NYLI", theForm.NYLI.value);
	iRet = CalculateSubregionValue("NYUP", theForm.NYUP.value);
	iRet = CalculateSubregionValue("RFCE", theForm.RFCE.value);
	iRet = CalculateSubregionValue("RFCM", theForm.RFCM.value);
	iRet = CalculateSubregionValue("RFCW", theForm.RFCW.value);
	iRet = CalculateSubregionValue("RMPA", theForm.RMPA.value);
	iRet = CalculateSubregionValue("SPNO", theForm.SPNO.value);
	iRet = CalculateSubregionValue("SPSO", theForm.SPSO.value);
	iRet = CalculateSubregionValue("SRMV", theForm.SRMV.value);
	iRet = CalculateSubregionValue("SRMW", theForm.SRMW.value);
	iRet = CalculateSubregionValue("SRSO", theForm.SRSO.value);
	iRet = CalculateSubregionValue("SRTV", theForm.SRTV.value);
	iRet = CalculateSubregionValue("SRVC", theForm.SRVC.value);
	}
}

//*****************************************************************************************
// this function does the following:
// - looks up the EGRID Output Emission Rate (lb/MWh) for the specified region
// - converts the input value from KWh to MWh (by dividing by 1000)
// - multiples the input value for the region by the EGRID Output Emission Rate for the region
// - adds the resulting product to the running total value 
function CalculateSubregionValue(sRegion, sInputValue)
{
if (sInputValue != "")
	{
	sRegionValue = LookupEGRIDValue(sRegion);
	sInputValue = sInputValue / 1000;        // convert the input value from KWh to MWh
	sTotalValue = sTotalValue + (sInputValue * sRegionValue);
	}
}

//*****************************************************************************************
// this function looks up the EGRID Output Emission Rate (lb/MWh) for the specified region
function LookupEGRIDValue(sRegion)
{
// find specified region in Subregion array
for (i=0; i < SubregionList.length; i++)
	{
	if (SubregionList[i] == sRegion) break;   // found a match
	}

// emission rate is in the corresponding array element of the EmissionList array
return(EmissionList[i]);  
}

//*****************************************************************************************
// this function displays a message if a field is not a number (unless it's blank)
function NumberValue(sValue, sMsg, bErrVal)
{
var num1;

// blank value for number - don't need to do any validation
if (sValue.value == "")
	{
	return(bErrVal);
	}
	
// there is already an error, so don't check for more errors	
if (bErrVal == 1)
     {
     return 1;
     }    
     
// is the value a number?     
num1 = parseFloat(sValue.value);
if (bErrVal==0 && sValue.value != num1)
     {
     alert("The " + sMsg + " value must be a number of 0 or greater. Do not enter commas.");
     sValue.focus();
     return 1; 
     }

return 0;     
 }