/*
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="en-US" />
*/

/* $Id$ */

var wattageMapping = new Array();

/*
	kWhPerKg:
		Worldwide: 0.40 kg
		HK, PRC and rest of Asia: 0.997 kg
*/
var kWhPerKg = 0.40;
var daysPerYear = 365;

var maxRow = 11;
var ttlCostsPerYear = 0.00;
var ttlSavingsPerLampLifeWithMEGAMAN = 0.00;
var ttlReducedCO2EmissionsPerYear = 0.00;

function validateField ( fieldName, rowNum, roundDecimals ) {
	var fieldObj = document.getElementById(fieldName + '_' + rowNum);
	var tmpVal = fieldObj.value;

	if (isNaN(tmpVal)) {
		tmpVal = 0;
	}

	tmpVal = roundToNdp(tmpVal, roundDecimals);
	fieldObj.value = tmpVal;
	
	return tmpVal;
}


function doEnergySavingCalcRow ( theForm, rowNum ) {
	// [A] Number of lamps
	var numOfLamps = validateField('numOfLamps', rowNum, 0);

	// [B] Wattage (W)
	var presentWattagePerLamp = validateField('presentWattagePerLamp', rowNum, 0);

	// [C] Burning Hours per Day
	var burnHrsPerDay = validateField('burnHrsPerDay', rowNum, 0);

	// [D] Electricity Cost ($/kWh)
	var kWhPrice = validateField('kWhPrice', rowNum, 2);

	// [E] Compute: Electricity Consumption (kWh/Day)
	// Formula: SUM(A6/1000*B6*C6)
	var consumptionPerkWh = roundToNdp((numOfLamps / 1000 * presentWattagePerLamp * burnHrsPerDay), 1);
	document.getElementById('spnConsumptionPerkWh' + '_' + rowNum).innerText = printf("%0.1f", consumptionPerkWh);

	// [F] Compute: Running Costs ($/Year)
	// Formula: SUM(A6*B6/1000*C6*D6*365)
	var costsPerYear = roundToNdp((numOfLamps * presentWattagePerLamp / 1000 * burnHrsPerDay * kWhPrice * daysPerYear), 2);
	document.getElementById('spnCostsPerYear' + '_' + rowNum).innerText = formatCurrency('', costsPerYear);

	// [G] MEGAMAN - Wattage (W)
	var megamanWattagePerLamp = validateField('MEGAMANWattagePerLamp', rowNum, 0);

	// [H] MEGAMAN - Lamp Life (hrs)
	var megamanLampLife = validateField('MEGAMANLampLife', rowNum, 0);
	
	// [I] Compute: MEGAMAN - Electricity Consumption (kWh/Day)
	// Formula: SUM(A6/1000*G6*C6*365)
	var consumptionkWhWithMEGAMAN = roundToNdp((numOfLamps / 1000 * megamanWattagePerLamp * burnHrsPerDay), 1);
	document.getElementById('spnConsumptionkWhWithMEGAMAN' + '_' + rowNum).innerText = printf("%0.1f", consumptionkWhWithMEGAMAN);

	// [J] Compute: MEGAMAN - Running Cost ($/Year)
	// Formula: SUM(A6*G6/1000*C6*D6*365)
	var costsPerYearWithMEGAMAN = roundToNdp((numOfLamps * megamanWattagePerLamp / 1000 * burnHrsPerDay * kWhPrice * daysPerYear), 2);
	document.getElementById('spnCostsPerYearWithMEGAMAN' + '_' + rowNum).innerText = formatCurrency('', costsPerYearWithMEGAMAN);

	// [K] Compute: MEGAMAN - Savings ($/Year)
	// Formula: SUM(F6-J6)
	var savingsPerYearWithMEGAMAN = roundToNdp((costsPerYear - costsPerYearWithMEGAMAN), 2);
	document.getElementById('spnSavingsPerYearWithMEGAMAN' + '_' + rowNum).innerText = formatCurrency('', savingsPerYearWithMEGAMAN);

	// [L] Compute: MEGAMAN - Savings ($/Lamp Life)
	// Formula: SUM(H6/1000*B6*D6*A6)-(H6/1000*G6*D6*A6)
	var savingsPerLampLifeWithMEGAMAN = roundToNdp(((megamanLampLife / 1000 * presentWattagePerLamp * kWhPrice * numOfLamps) - (megamanLampLife / 1000 * megamanWattagePerLamp * kWhPrice * numOfLamps)), 2);
	document.getElementById('spnSavingsPerLampLifeWithMEGAMAN' + '_' + rowNum).innerText = formatCurrency('', savingsPerLampLifeWithMEGAMAN);

	// [M] Compute: Reduced CO2 Emissions (Kg/Year)
	// Formula: SUM(E6-I6)*(kWhPerKg)
	var reducedCO2EmissionsPerYear = roundToNdp(((consumptionPerkWh - consumptionkWhWithMEGAMAN) * kWhPerKg * daysPerYear), 2);
	document.getElementById('spnReducedCO2EmissionsPerYear' + '_' + rowNum).innerText = formatCurrency('', reducedCO2EmissionsPerYear);

	// Calculate column totals
	ttlCostsPerYear += costsPerYear;
	ttlSavingsPerLampLifeWithMEGAMAN += savingsPerLampLifeWithMEGAMAN;
	ttlReducedCO2EmissionsPerYear += reducedCO2EmissionsPerYear;
	
	return true;
}


function doEnergySavingCalc ( theForm ) {
	ttlCostsPerYear = 0.00;
	ttlSavingsPerLampLifeWithMEGAMAN = 0.00;
	ttlReducedCO2EmissionsPerYear = 0.00;

	for (var rowNo = 1; rowNo <= maxRow; rowNo++) {
		doEnergySavingCalcRow(theForm, rowNo);
	}

	// Update totals.
	document.getElementById('spnTtlCostsPerYear').innerText = formatCurrency('', ttlCostsPerYear);
	document.getElementById('spnTtlSavingsPerLampLifeWithMEGAMAN').innerText = formatCurrency('', ttlSavingsPerLampLifeWithMEGAMAN);
	document.getElementById('spnTtlReducedCO2EmissionsPerYear').innerText = formatCurrency('', ttlReducedCO2EmissionsPerYear);

	// Update grand totals.
	document.getElementById('spnGTtlSavingsPerLampLifeWithMEGAMAN').innerText = formatCurrency('', ttlSavingsPerLampLifeWithMEGAMAN);
	document.getElementById('spnGTtlReducedCO2EmissionsPerYear').innerText = formatCurrency('', ttlReducedCO2EmissionsPerYear);

	return true;
}


function doResetCalc ( theForm ) {

	theForm.reset();
	
	doEnergySavingCalc(theForm);
	
	return true;
}

function presentWattagePerLamp_onchange ( rowNum ) {
	var selectedPresentWattagePerLamp = document.getElementById('presentWattagePerLamp_' + rowNum).value;

	if (selectedPresentWattagePerLamp == 0) {
		var megamanWattage = 0;
	} else {
		try {
			var megamanWattage = wattageMapping[selectedPresentWattagePerLamp];
			if (isNaN(megamanWattage)) {
				megamanWattage = parseInt(selectedPresentWattagePerLamp / 5);
			}
		} catch (e) {}
	}
	
	document.getElementById('MEGAMANWattagePerLamp_' + rowNum).value = megamanWattage;

	return true;
}

