// Utilities {
	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			}
		}
	}
	
	function number_format(value, decimals){
		if (decimals === false){decimals = 2;}
		if (decimals == undefined) {decimals = 2;}
		multiplier = Math.pow(10, decimals);
		
		if (value < 0) {
			value = value * -1;
			negative = true;
		} else {
			negative = false;
		}
		
		nval = Math.round(value*multiplier)+'';
		len = nval.length;
		
		if (len == 1) {
			addzero = '0';
		} else {
			addzero = '';
		}
		
		deci = addzero + nval.substr(len-decimals,decimals);
		
		multiplier += '';
		deci = deci*1 ? deci : multiplier.substr(1);
	
		num = nval.substr(0,len-decimals);
		num = num ? num : '0';
	
		temp = (num.split('')).reverse();
		cnt = 3;
		while (temp[cnt]){
			if (temp[cnt] != '-'){
				temp[cnt] = temp[cnt]+',';
			}
			cnt = cnt+3;
		}
		num = (temp.reverse()).join('');
		
		nval = deci ? num+'.'+deci : num;
		
		if (negative) {
			nval = '-'+nval;
		}
	
		return nval;
	}
// }

// Setup {
	function hookElements() {
		document.getElementById("size").onchange=reCalculate;
		document.getElementById("electricitycost").onchange=reCalculate;
		document.getElementById("tvtype").onchange=reCalculate;
		document.getElementById("power").onchange=reCalculate;
		document.getElementById("eurating").onchange=reCalculate;
		document.getElementById("dailyuse").onchange=reCalculate;
	}
	
	addLoadEvent(hookElements);
// }

// Lookup Tables {
	function lookupPlasma(s) {
		s = parseInt(s);
		switch (s) {
			case 40	: return 270.192; break;
			case 42	: return 295.339; break;
			case 50	: return 395.925; break;
			case 52	: return 421.072; break;
			case 60	: return 521.658; break;
		}
	}
	function lookupCRT(s) {
		s = parseInt(s);
		switch (s) {
			case 15	: return 53.717; break;
			case 17	: return 56.583; break;
			case 19	: return 59.449; break;
			case 20	: return 60.882; break;
			case 22	: return 63.748; break;
			case 23	: return 65.181; break;
			case 26	: return 69.480; break;
			case 27	: return 70.913; break;
			case 32	: return 78.078; break;
		}
	}
	function lookupLCD(s) {
		s = parseInt(s);
		return 0.3137*Math.pow(s,1.7426);
	}
	
	function lookupEuro(s,r) {
		switch (r) {
			case "A": rv = 0.51; break;
			case "B": rv = 0.64; break;
			case "C": rv = 0.8; break;
			case "D": rv = 1; break;
			case "E": rv = 1.2; break;
			case "F": rv = 1.44; break;
		}
		return ((s*s*0.4273*0.2789)+20)*rv;
	}
	
	function lookupCost(watts,use,cost) {
		if (watts && use && cost) {
			return (parseFloat(watts)/1000)*365*parseFloat(use)*parseFloat(cost);
		} else {
			return false;
		}
	}
// }




function reCalculate() {
	// Get the elements {
		var size 						= document.getElementById("size");
		var electricitycost = document.getElementById("electricitycost");
		var tvtype					=	document.getElementById("tvtype");
		var power						= document.getElementById("power");
		var eurating				=	document.getElementById("eurating");
		var dailyuse				= document.getElementById("dailyuse");
		var saving					= document.getElementById("saving");
		var runningcost			= document.getElementById("runningcost");
		var consumption			= document.getElementById("consumption");
	// }
	
	if (eurating.value) {
		watts = lookupEuro(size.value,eurating.value);
	} else if (power.value) {
		watts = power.value;
	} else {
		switch (tvtype.value) {
			case "LCD" : watts = lookupLCD(size.value); break;
			case "Plasma" : watts = lookupPlasma(size.value); break;
			case "CRT" : watts = lookupCRT(size.value); break;
		}
	}
	
	consumption.innerHTML = "Not available";
	runningcost.innerHTML = "Not available";
	saving.innerHTML = "Not available";
	
	if (watts) {
		consumption.innerHTML = number_format(watts,0)+" watts";
		cost = lookupCost(watts,dailyuse.value,electricitycost.value);
		if (cost) {
			runningcost.innerHTML = "&pound;"+number_format(cost,2);
			saving.innerHTML = "&pound;"+number_format(cost - lookupCost(lookupEuro(size.value,"A"),dailyuse.value,electricitycost.value),2);
		} 
	} 
}







