// <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
//
// $Id: products_led_savings.js 211 2010-06-24 11:01:06Z wah $
//

// Class for LED Calculator
var LEDCalc = function () {
	this.voltage = [
		[1, 'AC 220-240V'],
		[2, 'AC 127V'],
		[3, 'AC 110-120V'],
		[4, 'AC 100V']
	];

	this.COL_VoltageId = 0;				// Primary key
	this.COL_TypeOfReflectorLamp = 1;	// Primary key
	this.COL_HalogenWattage = 2;		// Primary key
	this.COL_HalogenLampLife = 3;
	this.COL_HalogenLampCost = 4;
	this.COL_HalogenReLampLaborCost = 5;
	this.COL_HalogenInitialUsageCost = 6;
	this.COL_HalogenBallastWattage = 7;
	this.COL_MegamanLEDWattage = 8;
	this.COL_MegamanLEDCost = 9;
	//
	this.voltageId = null;
	this.typeOfReflectorLamp = null;
	this.halogenWattage = null;
	this.halogenLampLife = null;
	this.halogenLampCost = null;
	this.halogenReLampLaborCost = null;
	this.halogenInitialUsageCost = null;
	this.halogenBallastWattage = null;
	this.megamanLedWattage = null;
	this.megamanLedCost = null;
	/*
	Fields:
	- *Voltage ID
	- *Type of Reflector
	- *Halogen Wattage (W)
	- Halogen Lamp Life (hr)
	- Halogen Lamp Cost (US$)
	- Halogen Relamping Labor Cost (US$)
	- Halogen Initial Usage Cost (US$)
	- Halogen Ballast Wattage (W)
	- MEGAMAN LED Wattage (W)
	- MEGAMAN LED Lamp Cost (US$)
	*/
		
	this.ledLampMapping = [
		[1, 'AR111', 35, 3000, 9.0, 10, 19.2, 4, 10, 85],
		[1, 'AR111', 50, 3000, 9.0, 10, 19.2, 4, 15, 100],
		[1, 'PAR16', 35, 2000, 7.0, 10, .0, 0, 7, 55],
		[1, 'PAR20', 50, 2000, 6.5, 10, .0, 0, 8, 60],
		[1, 'PAR30', 100, 2000, 7.0, 10, .0, 0, 15, 100],
//		[1, 'PAR38', 75, 3000, 7.0, 10, .0, 0, 15, TBC],

		[2, 'AR111', 35, 3000, 9.0, 10, 19.2, 4, 10, 85],
		[2, 'AR111', 50, 3000, 9.0, 10, 19.2, 4, 15, 100],
		[2, 'PAR16', 35, 2000, 7.0, 10, .0, 0, 7, 55],
		[2, 'PAR20', 50, 2500, 6.5, 10, .0, 0, 8, 60],
		[2, 'PAR30', 75, 2500, 7.0, 10, .0, 0, 15, 100],
//		[2, 'PAR38', 75, 3000, 7.0, 10, .0, 0, 15, TBC],

		[3, 'AR111', 35, 3000, 9.0, 10, 19.2, 4, 10, 85],
		[3, 'AR111', 50, 3000, 9.0, 10, 19.2, 4, 15, 100],
		[3, 'PAR16', 35, 3000, 7.0, 10, .0, 0, 7, 55],
		[3, 'PAR20', 50, 3000, 6.5, 10, .0, 0, 8, 60],
		[3, 'PAR30', 50, 2500, 7.0, 10, .0, 0, 10, 85],
		[3, 'PAR30', 75, 2500, 7.0, 10, .0, 0, 15, 100]
//		[3, 'PAR38', 75, 3000, 7.0, 10, .0, 0, 15, TBC]
	];

	//
	this.defaultRowNo = 4;
	//
	this.defaultVoltageId = this.ledLampMapping[this.defaultRowNo][this.COL_VoltageId];						// AC 220-240V
	this.defaultTypeOfReflectorLamp = this.ledLampMapping[this.defaultRowNo][this.COL_TypeOfReflectorLamp];	// PAR30
	this.defaulthalogenLampWattage = this.ledLampMapping[this.defaultRowNo][this.COL_HalogenWattage];		// 100W
	
	this.defaultNumOfLamps = 1;
	this.defaultBurnHoursPerDay = 6;
	this.defaultkWhPrice = 0.2;
	//
	this.daysPerYear = 365;
	this.kWhPerKg = 0.616;

	this.resetRec = function () {
		this.voltageId = null;
		this.typeOfReflectorLamp = null;
		this.halogenWattage = null;
		this.halogenLampLife = null;
		this.halogenLampCost = null;
		this.halogenReLampLaborCost = null;
		this.halogenInitialUsageCost = null;
		this.halogenBallastWattage = null;
		this.megamanLedWattage = null;
		this.megamanLedCost = null;

		return true;
	}

	this.retrieve = function ( voltageId, typeOfReflectorLamp, halogenLampWattage ) {
		var isFound = false;
		var cnt = this.ledLampMapping.length;
		var i = 0;
		
		this.resetRec();
		
		while ((!isFound) && (i < cnt)) {
			if ((this.ledLampMapping[i][this.COL_VoltageId] == voltageId)
				&& (this.ledLampMapping[i][this.COL_TypeOfReflectorLamp] == typeOfReflectorLamp)
				&& (this.ledLampMapping[i][this.COL_HalogenWattage] == halogenLampWattage)) {
				
				this.voltageId = parseInt(voltageId);
				this.typeOfReflectorLamp = typeOfReflectorLamp;
				this.halogenWattage = parseInt(halogenLampWattage);
				this.halogenLampLife = this.ledLampMapping[i][this.COL_HalogenLampLife];
				this.halogenLampCost = this.ledLampMapping[i][this.COL_HalogenLampCost];
				this.halogenReLampLaborCost = this.ledLampMapping[i][this.COL_HalogenReLampLaborCost];
				this.halogenInitialUsageCost = this.ledLampMapping[i][this.COL_HalogenInitialUsageCost];
				this.halogenBallastWattage = this.ledLampMapping[i][this.COL_HalogenBallastWattage];
				this.megamanLedWattage = this.ledLampMapping[i][this.COL_MegamanLEDWattage];
				this.megamanLedCost = this.ledLampMapping[i][this.COL_MegamanLEDCost];
				
				isFound = true;
			} else {
				i++;
			}
		}
	
		return isFound;
	}

	this.getTypeOfReflectorLamps = function ( voltageId ) {
		var retArray = [];
		var cnt = this.ledLampMapping.length;
		var j = 0;
		var lastVal = null;
		
		for (var i = 0; i < cnt; i++) {
			if (this.ledLampMapping[i][this.COL_VoltageId] == voltageId) {
				if ((lastVal == null) || (lastVal != this.ledLampMapping[i][this.COL_TypeOfReflectorLamp])) {
					retArray[j] = this.ledLampMapping[i][this.COL_TypeOfReflectorLamp];
					
					lastVal = this.ledLampMapping[i][this.COL_TypeOfReflectorLamp];
					
					j++;
				}
			}
		}
		
		return retArray;
	}

	this.getHalogenWattages = function ( voltageId, typeOfReflectorLamp ) {
		var retArray = [];
		var cnt = this.ledLampMapping.length;
		var j = 0;
		
		for (var i = 0; i < cnt; i++) {
			if ((this.ledLampMapping[i][this.COL_VoltageId] == voltageId)
				&& (this.ledLampMapping[i][this.COL_TypeOfReflectorLamp] == typeOfReflectorLamp)) {
				retArray[j] = this.ledLampMapping[i][this.COL_HalogenWattage];
				j++;
			}
		}
		
		return retArray;
	}

	this.doCalc = function ( voltageId, typeOfReflectorLamp, halogenLampWattage, numOfLamps, burnHoursPerDay, kWhPrice ) {
		var retArray = [];

		if (this.retrieve(voltageId, typeOfReflectorLamp, halogenLampWattage)) {
			// Do calculation
			var totalSavedkWPerYear = roundToNdp(parseFloat((parseInt(numOfLamps) * (parseInt(halogenLampWattage) - parseInt(this.megamanLedWattage)) * parseInt(burnHoursPerDay) * parseInt(this.daysPerYear)) / 1000), 2);
			var totalCostSavedPerYear = roundToNdp(parseFloat(parseFloat(totalSavedkWPerYear) * parseFloat(kWhPrice)), 2);
			var reducedCO2KgPerYear = roundToNdp(parseFloat(parseFloat(totalSavedkWPerYear) * parseFloat(this.kWhPerKg)), 2);

			var breakevenHours = roundToNdp((parseFloat(this.megamanLedCost) - parseFloat(this.halogenInitialUsageCost))
				/
				(
				 	(parseFloat(this.halogenLampCost) / parseInt(this.halogenLampLife) + parseFloat(this.halogenReLampLaborCost) / parseInt(this.halogenLampLife) + (parseInt(this.halogenWattage) + parseInt(parseInt(this.halogenBallastWattage))) * parseFloat(kWhPrice) / 1000)
					-
					(parseInt(this.megamanLedWattage) * 0.2 / 1000)
				), 0);

			var breakevenYears = roundToNdp(parseFloat((parseInt(breakevenHours) / parseInt(burnHoursPerDay)) / parseInt(this.daysPerYear)), 2);
			
			retArray = [
				totalSavedkWPerYear,
				totalCostSavedPerYear,
				reducedCO2KgPerYear,
				breakevenHours,
				breakevenYears
			];
		} else {
			alert('Oops! Please contact Web Master.');
		}

		return retArray;
	}
	
	this.resetTotals = function() {
		$('#totalSavedkWPerYear').empty().append('-');
		$('#totalCostSavedPerYear').empty().append('-');
		$('#reducedCO2KgPerYear').empty().append('-');
		//
		$('#breakevenHours').empty().append('-');
		$('#breakevenYears').empty().append('-');
	}
}


$(document).ready(function() {
//	try {
		var ledCalculator = new LEDCalc();
	
		// Bind "change" event
		$('#voltageId').change(function() {
			var typeOfReflectorLamps = ledCalculator.getTypeOfReflectorLamps($(this).val());
			
			populateSelectOptions('#typeOfReflectorLamp', typeOfReflectorLamps, 0);
			
			$('#typeOfReflectorLamp').trigger('change');
		});
		
		$('#typeOfReflectorLamp').change(function() {
			var halogenWattages = ledCalculator.getHalogenWattages($('#voltageId').val(), $(this).val());
	
			populateSelectOptions('#halogenLampWattage', halogenWattages, 0);
	
			$('#halogenLampWattage').trigger('change');
		});
		
		// Bind "change" event
		$('#halogenLampWattage').change(function() {
			//if (ledCalculator.retrieve($('#typeOfReflectorLamp').val(), $(this).val(), '#halogenLampLife')) {
			if (ledCalculator.retrieve($('#voltageId').val(), $('#typeOfReflectorLamp').val(), $(this).val())) {
	
				// Halogen
				$('#halogenLampLife').val(ledCalculator.halogenLampLife);
				$('#halogenLampCost').val(ledCalculator.halogenLampCost);
				$('#halogenLampRelampCost').val(ledCalculator.halogenReLampLaborCost);
	
				// Megaman LED
				$('#recommendedMegamanLampWattage').empty().append(ledCalculator.megamanLedWattage);
				$('#recommendedMegamanLampCost').empty().append(formatCurrency('', ledCalculator.megamanLedCost));
				
				ledCalculator.resetTotals();
			} else {
				alert('Oops! Please contact Web Master.');
			}
		});
	
		// Bind "click" event
		$('#btnCalc').click(function() {
			var kWhPrice = validateField('kWhPrice', 1);
	
			if (kWhPrice > 0) {
				var resultsArray = ledCalculator.doCalc(
					$('#voltageId').val(),
					$('#typeOfReflectorLamp').val(),
					$('#halogenLampWattage').val(),
					$('#numOfLamps').val(),
					$('#burnHoursPerDay').val(),
					kWhPrice
				);
				
				if (resultsArray.length > 0) {
					$('#totalSavedkWPerYear').empty().append(formatCurrency('', resultsArray[0]));
					$('#totalCostSavedPerYear').empty().append(formatCurrency('', resultsArray[1]));
					$('#reducedCO2KgPerYear').empty().append(formatCurrency('', resultsArray[2]));
					//
					$('#breakevenHours').empty().append(formatCurrency('', resultsArray[3]));
					$('#breakevenYears').empty().append(formatCurrency('', resultsArray[4]));
				}
			} else {
				alert('Value of "Unit Electricity Cost in your Region" is invalid!');
				$('#kWhPrice').focus();
			}
		});
		
		// Bind "click" event
		$('#btnReset').click(function() {
			$('#voltageId').val(ledCalculator.defaultVoltageId)
			
			$('#typeOfReflectorLamp').val(ledCalculator.defaultTypeOfReflectorLamp);
			$('#typeOfReflectorLamp').trigger('change');
			$('#numOfLamps').val(ledCalculator.defaultNumOfLamps);
			$('#burnHoursPerDay').val(ledCalculator.defaultBurnHoursPerDay);
			$('#kWhPrice').val(ledCalculator.defaultkWhPrice);
//			//
//			$('#totalSavedkWPerYear').empty().append('0');
//			$('#totalCostSavedPerYear').empty().append('0.00');
//			$('#reducedCO2KgPerYear').empty().append('0');
//			//
//			$('#breakevenHours').empty().append('0');
//			$('#breakevenYears').empty().append('0.00');
			//
			ledCalculator.resetTotals();
			//
			//$('#typeOfReflectorLamp').focus();
		});
		
		//
		$('#btnReset').trigger('click');
//	} catch(e) {alert(e);}
});
