/*
 * CO2 Calculator support for whatsmyco2.com
 *
 * Copyright (C) 2007 What's My CO2, Inc.
 */

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function updateValue(id, newValue) {
	var text = $(id).firstChild
	var prefix = text.nodeValue.charAt(0);
	if (newValue == null) {
		text.nodeValue = prefix;
	} else {
		text.nodeValue = prefix + ' ' + addCommas(newValue.toFixed(0));
	}
}

function updateValuePercent(id, newValue, total) {
	var text = $(id).firstChild
	var prefix = text.nodeValue.charAt(0);
	if (newValue == null || total == null) {
		text.nodeValue = prefix + ' / %';
	} else {
		text.nodeValue = prefix + ' ' + addCommas(newValue.toFixed(0) + ' / ' + (newValue * 100 / total).toFixed(0) + '%');
	}
}

function valueIfAll(anArray, value) {
	for (var i=0; i < anArray.length; i++) {
		if (anArray[i] == null || anArray[i] === '' || isNaN(anArray[i]) || anArray[i] == Infinity) return null;
	}
	return value;
}

function valueOfRadioButton(aRadioButtonKey) {
	var yesKey = aRadioButtonKey + 'Yes';
	var noKey = aRadioButtonKey + 'No';
	if ($F(yesKey) != null || $F(noKey) != null) return $(yesKey).checked; 
	return null;
}
	
function recalculate()
{
	var familySize = $F('familySize');
	var heatSource = $F('heatSource');

	var milesNumerator = $F('milesNumerator');
	var milesMultiplier = $F('milesMultiplier');
	var milesPerGallon = $F('milesPerGallon');

	var monthlyElectricBill = $F('monthlyElectricBill');
	var monthlyGasBill = $F('monthlyGasBill');
	var monthlyOilBill = $F('monthlyOilBill');

	var recycleNewspaper = valueOfRadioButton('recycleNewspaper');
	var recycleGlass = valueOfRadioButton('recycleGlass');
	var recyclePlastic = valueOfRadioButton('recyclePlastic');
	var recycleCans = valueOfRadioButton('recycleCans');
	
	// Reductions
	var moreMilesPerGallon = $F('moreMilesPerGallon');
	var fewerMiles = $F('fewerMiles');
	
	var lowerDegreesWinter = $F('lowerDegreesWinter');
	var higherDegreesSummer = $F('higherDegreesSummer');
	var replacedLightBulbs = $F('replacedLightBulbs');

	var newRefrigerator = valueOfRadioButton('newRefrigerator');
	var newFurnace = valueOfRadioButton('newFurnace');
	var newWindows = valueOfRadioButton('newWindows');
	
	var recycleMore = valueOfRadioButton('recycleMore');
	var recycleMagazines = valueOfRadioButton('recycleMagazines');
	
	var teTransportation = valueIfAll([milesNumerator, milesMultiplier, milesPerGallon, 1 / milesPerGallon], milesNumerator * milesMultiplier / milesPerGallon * 19.4 * 100 / 95);
	updateValue('totalEmissionsFromTransportation', teTransportation);

	var eElectricity = valueIfAll([monthlyElectricBill], monthlyElectricBill / 0.1 * 1.37 * 12);
	var eGas = valueIfAll([monthlyGasBill], monthlyGasBill / 13.83 * 120.61 * 12);
	var eOil = valueIfAll([monthlyOilBill], monthlyOilBill / 2.37 * 22.28 * 12);
	updateValue('emissionsFromElectricity', eElectricity);
	updateValue('emissionsFromGas', eGas);
	updateValue('emissionsFromOil', eOil);

	var teWasteBeforeRecycling = valueIfAll([familySize], familySize * 1018);
	updateValue('totalEmissionsFromWasteBeforeRecycling', teWasteBeforeRecycling);

	var eletotalEmissionsFromAirTravel = document.getElementById('totalEmissionsFromAirTravel');
	var inttotalEmissionsFromAirTravel = 0;
	var strtotalEmissionsFromAirTravel = "";
	
	if( IsMSIE() ) {
		if(eletotalEmissionsFromAirTravel.innerText != '') {
			strtotalEmissionsFromAirTravel = eletotalEmissionsFromAirTravel.innerText;
			strtotalEmissionsFromAirTravel = strtotalEmissionsFromAirTravel.replace(",","");
			inttotalEmissionsFromAirTravel = parseInt(strtotalEmissionsFromAirTravel);
		}
	} else {
		if(eletotalEmissionsFromAirTravel.textContent != '') {
			strtotalEmissionsFromAirTravel = eletotalEmissionsFromAirTravel.textContent;
			strtotalEmissionsFromAirTravel = strtotalEmissionsFromAirTravel.replace(",","");
			inttotalEmissionsFromAirTravel = parseInt(strtotalEmissionsFromAirTravel);
		}		
	}
	var rPotentialRecyclingNewspaper = valueIfAll([familySize, recycleNewspaper], familySize * 184.3);
	var rPotentialRecyclingGlass = valueIfAll([familySize, recycleGlass], familySize * 25.6);
	var rPotentialRecyclingPlastic = valueIfAll([familySize, recyclePlastic], familySize * 46.58);
	var rPotentialRecyclingCans = valueIfAll([familySize, recycleCans], familySize * 165.79);

	var rPotentialRecycling = rPotentialRecyclingNewspaper + rPotentialRecyclingGlass + rPotentialRecyclingPlastic + rPotentialRecyclingCans;
	
	var rRecyclingNewspaper = valueIfAll([rPotentialRecyclingNewspaper], recycleNewspaper ? rPotentialRecyclingNewspaper : 0);
	var rRecyclingGlass = valueIfAll([rPotentialRecyclingGlass], recycleGlass ? rPotentialRecyclingGlass : 0);
	var rRecyclingPlastic = valueIfAll([rPotentialRecyclingPlastic], recyclePlastic ? rPotentialRecyclingPlastic : 0);
	var rRecyclingCans = valueIfAll([rPotentialRecyclingCans], recycleCans ? rPotentialRecyclingCans : 0)
	
	updateValue('reductionFromRecyclingNewspaper', rRecyclingNewspaper);	
	updateValue('reductionFromRecyclingGlass', rRecyclingGlass);	
	updateValue('reductionFromRecyclingPlastic', rRecyclingPlastic);	
	updateValue('reductionFromRecyclingCans', rRecyclingCans);
	
	var rRecycling = rRecyclingNewspaper + rRecyclingGlass + rRecyclingPlastic + rRecyclingCans;
	
	var teWasteAfterRecycling = valueIfAll([teWasteBeforeRecycling], teWasteBeforeRecycling - rRecycling);
	updateValue('totalEmissionsFromWasteAfterRecycling', teWasteAfterRecycling);
	
	var totalEmissions = teTransportation + eElectricity + eGas + eOil + teWasteAfterRecycling + inttotalEmissionsFromAirTravel;
	updateValue('totalEmissions', totalEmissions);

	var deltaMPG = parseInt(moreMilesPerGallon);
	var improvedMPG = parseInt(milesPerGallon) + (isNaN(deltaMPG) ? 0 : deltaMPG);
	var rMoreMilesPerGallon = valueIfAll([teTransportation, moreMilesPerGallon], teTransportation - (milesNumerator * milesMultiplier / improvedMPG * 19.4 * 100 / 95));
	updateValuePercent('reductionFromMoreMilesPerGallon', rMoreMilesPerGallon, totalEmissions);

	var fewerMilesClamped = Math.min(fewerMiles, milesNumerator);
	var rFewerMiles = valueIfAll([fewerMiles, milesPerGallon, milesMultiplier], fewerMilesClamped * milesMultiplier / improvedMPG * 19.4 * 100 / 95);
	updateValuePercent('reductionFromFewerMiles', rFewerMiles, totalEmissions);
	
	var heatFactor;
	var heatEnergyEmissions;
	var iWindowReplacement;
	var iFurnaceReplacement;
	if (heatSource == 'Natural Gas') {
		heatFactor = 0.42;
		heatEnergyEmissions = eGas;
		iWindowReplacement = 3320;
		iFurnaceReplacement = 3011;
	} else if (heatSource == 'Electric') {
		heatFactor = 0.33;
		heatEnergyEmissions = eElectricity;
		iWindowReplacement = 3884;
		iFurnaceReplacement = 0;
	} else if (heatSource == 'Oil') {
		heatFactor = 0.42;
		heatEnergyEmissions = eOil;
		iWindowReplacement = 4577;
		iFurnaceReplacement = 4151;
	}
	var rLowerDegreesWinter = valueIfAll([lowerDegreesWinter, heatFactor], heatEnergyEmissions * heatFactor * 0.01 * lowerDegreesWinter);
	updateValuePercent('reductionFromLowerDegreesWinter', rLowerDegreesWinter, totalEmissions);
	
	var rHigherDegreesSummer = valueIfAll([eElectricity, higherDegreesSummer], eElectricity * 0.16 * 0.05 * 0.42 * higherDegreesSummer);
	updateValuePercent('reductionFromHigherDegreesSummer', rHigherDegreesSummer, totalEmissions);
	
	var rLightBulbs = valueIfAll([replacedLightBulbs], replacedLightBulbs * 73 * 1.37);
	updateValuePercent('reductionFromReplacedLightBulbs', rLightBulbs, totalEmissions);
	
	var rNewRefrigerator = valueIfAll([newRefrigerator], newRefrigerator ? 521 : 0);
	updateValuePercent('reductionFromNewRefrigerator', rNewRefrigerator, totalEmissions);
	
	var rNewFurnace = valueIfAll([newFurnace, iFurnaceReplacement], newFurnace ? iFurnaceReplacement : 0);
	updateValuePercent('reductionFromNewFurnace', rNewFurnace, totalEmissions);
	
	var rNewWindows = valueIfAll([newWindows, iWindowReplacement], newWindows ? iWindowReplacement : 0);
	updateValuePercent('reductionFromNewWindows', rNewWindows, totalEmissions);
	
	var rRecycleMore = valueIfAll([recycleMore, rPotentialRecyclingNewspaper, rPotentialRecyclingGlass, rPotentialRecyclingPlastic, rPotentialRecyclingCans], recycleMore ? rPotentialRecycling - rRecycling : 0);
	updateValuePercent('reductionFromRecyclingMore', rRecycleMore, totalEmissions);
	
	var rRecycleMagazines = valueIfAll([recycleMagazines, familySize], recycleMagazines ? familySize * 47.66 : 0);
	updateValuePercent('reductionFromRecyclingMagazines', rRecycleMagazines, totalEmissions);
	
	var rTotal = rMoreMilesPerGallon + rFewerMiles + rLowerDegreesWinter + rHigherDegreesSummer + rLightBulbs + rNewRefrigerator + rNewFurnace + rNewWindows + rRecycleMore + rRecycleMagazines;
	updateValuePercent('reductionTotal', rTotal, totalEmissions);
	var assuagedGuiltTotal = valueIfAll([totalEmissions, rTotal], totalEmissions - rTotal)
	updateValue('totalAfterChanges', assuagedGuiltTotal);
}

