// this is a modified version of a file created by ICF called equivalency.js.
// ERG took the parts that were needed for the eGRID equivalency calculator, added comments, and made minor modifications 
// where needed to customize it for the calculator2.htm page.


/*
	equivalency factors; 
	i.e. 	4.62 metric ton of Carbon Dioxide Equivalent = 1 car off the road for one year
	*** these factors are used to display the values in the y1 to y7 equivalency text boxes ***
	(note: we do not display all of the items below, so some elements are commented out, and the boxes
	on the calculator2.htm page were renamed y1 to y7 to keep the numbers contiguous. Also, household
	electricity was moved to the end of the array). 
*/
equiv_factors = new Array(
5.46, 	//	Passenger cars not driven for one year
//11.33, 	// Household energy use
0.00881,	// Gallons of gasoline
0.43,	// Barrels of oil
//74.88,	// Tanker trucks filled with gasoline
//0.039, 	// Number of tree seedlings grown for 10 years
//4.4,		// Acres of pine or fir forests storing carbon for one year 
//143.37,	// Acres of forest preserved from deforestation
0.024,	// Propane cylinders used for home barbeques
191.5, 	// Coal burned/railcars of coal
//2.90,	// Tons of waste recycled instead of landfilled 
4643734, // coal fired power plant for one year
7.21	// Household electricity use for one year (# of households)
);

/*
Programming notes:
02/12/2009
Household electricity use for one year (# of households): change from 7.55 to 7.21
*/


/*** this function displays a greenhouse gas emissions total (the v_total value), in whatever unit
the user has chosen (tons, pounds, metric tons, kilograms).  The v_total variable always remains in metric tons,
so that the conversions work correctly (the "multiples" factors convert a metric ton value to another value).
****/ 
function calculateTotal()
{
	form_id = document.forms["form1"];
	form_id.y0.value = commaSplit(roundIt(v_total/multiples[finalResultUnits]));
	
}

/*** this function displays the equivalencies (i.e., "This is equivalent to one of the following...").
As with the calculateTotal function above, the equivalencies are calculated using the v_total variable,
which is in metric tons regardless of the current units that are selected.
****/ 
function calculateEquivs()
{
	form_id = document.forms["form1"];
	for (i=1; i<=equiv_factors.length; i++)
		{
		eval("form_id.y"+i+".value='"+ commaSplit(roundIt(v_total/equiv_factors[i-1]))+"'");
		}

	
}

multiples = new Array(0.9071847,0.00045359237,1,0.001); //multiples for converting into metric tons
// The order is: Tons, Pounds, Metric Tons, Kilograms
// 1 ton [metric] = 1.1023113 ton [short]
// 1 ton [short] = 907.18474 kilogram
// 1 ton [metric] = 2,204.6226218 pound
// 1 ton [short] = 2,000 pound
finalResultUnits = 2; // the default units for displaying the result is in Metric Tons

/*** this function is called when the user changes the greenhouse gas emissions results units (tons, pounds, metric tons, kilograms).
It recalculates the total value that is displayed, based on the units that were selected.  However, the actual v_total value does not change.
****/ 
function setResultUnits(){
	form_id = document.forms["form1"];
	finalResultUnits = parseFloat(form_id.resultUnits.options[form_id.resultUnits.selectedIndex].value);
	if (form_id.y0.value != "") calculateTotal();
}
		
/*** this function rounds a number ***/
function roundIt(number)
{
	var absolute = Math.abs(number);
	if (isNaN(number) || number == 0)
	{
		return number;
	}
	else
	{
		// If the result is above 100 let's just use a whole number. 
		if (absolute > 100)
		{
			number = Math.round(number);
		}
		// If it's lower than 100 but greater than 1, let's show it out to one decimal place (e.g., 10.1).
		else if ((absolute < 100) && (absolute > 1))
		{
			number = Math.round(number * 10) / 10;
		}
		// If it's below zero, let's show it out to two decimal places for numbers up to 1/100 (e.g., 0.95, or 0.09)
		else if ((absolute < 1) && (absolute > 0.001))
		{
			number = Math.round(number * 100) / 100;
		}
		//  and out to four decimal places for numbers smaller than that (e.g., 0.0097, 0.0030).
		else
		{
			number = Math.round(number * 10000) / 10000;
		}
		return number;
	}
}

/*** this function inserts commas into a number where needed ***/
function commaSplit(srcNumber)
{
	var txtNumber = new String(srcNumber);
	
	if (isNaN(srcNumber) || txtNumber == "") {
		return srcNumber;
	}
	else {
		var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
		var arrNumber = txtNumber.split('.');
		arrNumber[0] += '.';
		do {
			arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
		} while (rxSplit.test(arrNumber[0]));
		
		if (arrNumber.length > 1) {
			return arrNumber.join('');
		}
		else {
			return arrNumber[0].split('.')[0];
		}
    }
}



