var d = document;
var safari = (navigator.userAgent.toLowerCase().indexOf('safari') != -1) ? true : false;
var gebtn = function(parEl,child) { return parEl.getElementsByTagName(child); };
Event.observe(window, 'load', function() {
    if(!d.getElementById || !d.createTextNode) return;
    var ls = gebtn(d,'label');
    for (var i = 0; i < ls.length; i++) {
        var l = ls[i];
        if (l.className.indexOf('label_') == -1) continue;
        var inp = gebtn(l,'input')[0];
        if (l.className == 'label_check') {
            l.className = (safari && inp.checked == true || inp.checked) ? 'label_check c_on' : 'label_check c_off';
            l.onclick = check_it;
        };
        if (l.className == 'label_radio') {
            l.className = (safari && inp.checked == true || inp.checked) ? 'label_radio r_on' : 'label_radio r_off';
			l.onclick = turn_radio;
        };
    };
	maxOut();
}, false);

function roundNumber(number,decimal_points) {
	if(!decimal_points) return Math.round(number);
	if(number == 0) {
		var decimals = "";
		for(var i=0;i<decimal_points;i++) decimals += "0";
		return "0."+decimals;
	}

	var exponent = Math.pow(10,decimal_points);
	var num = Math.round((number * exponent)).toString();
	return num.slice(0,-1*decimal_points) + "." + num.slice(-1*decimal_points)
}

var check_it = function() {
    var inp = gebtn(this,'input')[0];
    if (this.className == 'label_check c_off' || (!safari && inp.checked)) {
        this.className = 'label_check c_on';
        if (safari) inp.checked = true;
    } else {
        this.className = 'label_check c_off';
        if (safari) inp.checked = false;
    };
};

var turn_radio = function() {
    var inp = gebtn(this,'input')[0];
    if (this.className == 'label_radio r_off' || inp.checked) {
        var ls = gebtn(this.parentNode,'label');
        for (var i = 0; i < ls.length; i++) {
            var l = ls[i];
            if (l.className.indexOf('label_radio') == -1)  continue;
            l.className = 'label_radio r_off';
        };
        this.className = 'label_radio r_on';
        if (safari) inp.checked = true;
    } else {
        this.className = 'label_radio r_off';
        if (safari) inp.checked = false;
    }
	maxOut();
};
function maxOut(){
	/*var val = $('HomeSize').value;
	houseslider.setValue(val,0);
	$('HomeSize').value = val;*/
	$('HomeSize').value = $('HomeSize').value.replace(/[^\d\.]/g,'');
	var insu = 0;
	if($('PoorInsulation').checked == true){
		insu = 0.110;
	}
	if($('NewInsulation').checked == true){
		insu = 0.055;
	}
	if($('GoodInsulation').checked == true){
		insu = 0.0385;
	}
	
	$('maxoutput').innerHTML = roundNumber($('HomeSize').value * insu,2);
	new Effect.Highlight('maxoutput',{startcolor:'#E8914A', endcolor: '#F0F0F0', restorecolor: '#F0F0F0'});
	$('boilerlist').style.display = 'none';
	calculateFuelQuantities();
}
function calculateFuelQuantities(){
	$('PelletCost').value = $('PelletCost').value.replace(/[^\d\.]/g,'');
	$('WoodChipCost').value = $('WoodChipCost').value.replace(/[^\d\.]/g,'');
	$('HoursPerDay').value = $('HoursPerDay').value.replace(/[^\d\.]/g,'');
	$('DaysPerYear').value = $('DaysPerYear').value.replace(/[^\d\.]/g,'');
	
	if($('HoursPerDay').value > 24){
		$('HoursPerDay').value = 24;
	}
	
	if($('DaysPerYear').value > 365){
		$('DaysPerYear').value = 365;
	}
	
	if($('MonthsPerYear').value > 12){
		$('MonthsPerYear').value = 12;
	}
	
	var maxkw = $('maxoutput').innerHTML;
	var totalhours = $('HoursPerDay').value * $('DaysPerYear').value;
	$('pellets-tonnes').innerHTML = roundNumber(maxkw*totalhours*0.80/5/1000/0.90,2);
	$('pellets-m3').innerHTML = roundNumber(maxkw*totalhours*0.80/0.90/3200,2);
	$('woodchip-tonnes').innerHTML = roundNumber(maxkw*totalhours*0.80/3.5/1000/0.90,2);
	$('woodchip-m3').innerHTML = roundNumber(maxkw*totalhours*0.80/0.90/880,2);
	$('pellets-cost').innerHTML = roundNumber($('pellets-tonnes').innerHTML * $('PelletCost').value,2);
	$('woodchip-cost').innerHTML = roundNumber($('woodchip-m3').innerHTML * $('WoodChipCost').value,2);
	calculateSavings();
}
function calculateSavings(){
	
	$('OilPrice').value = $('OilPrice').value.replace(/[^\d\.]/g,'');
	$('LPGPrice').value = $('LPGPrice').value.replace(/[^\d\.]/g,'');
	$('ElectricityPrice').value = $('ElectricityPrice').value.replace(/[^\d\.]/g,'');
	$('GasPrice').value = $('GasPrice').value.replace(/[^\d\.]/g,'');
	
	var maxkw = $('maxoutput').innerHTML;
	var totalhours = $('HoursPerDay').value * $('DaysPerYear').value;
	var oilprice = $('OilPrice').value / 10.2;
	var lpgprice = $('LPGPrice').value / 5.9;
	var electricityprice = $('ElectricityPrice').value;
	var gasprice = $('GasPrice').value;
	
	$('oil-pellets-saving').innerHTML = roundNumber((totalhours*0.80*maxkw/0.90*oilprice/100) - $('pellets-cost').innerHTML,2);
	$('oil-woodchip-saving').innerHTML = roundNumber((totalhours*0.80*maxkw/0.90*oilprice/100) - $('woodchip-cost').innerHTML,2);
	$('oil-carbon-saving').innerHTML = roundNumber(maxkw*totalhours*0.80*0.281,2);
	
	$('LPG-pellets-saving').innerHTML = roundNumber((totalhours*0.80*maxkw/0.90*lpgprice/100) - $('pellets-cost').innerHTML,2);
	$('LPG-woodchip-saving').innerHTML = roundNumber((totalhours*0.80*maxkw/0.90*lpgprice/100) - $('woodchip-cost').innerHTML,2);
	$('LPG-carbon-saving').innerHTML = roundNumber(maxkw*totalhours*0.80*0.225,2);
	
	$('electricity-pellets-saving').innerHTML = roundNumber((totalhours*0.80*maxkw/0.90*electricityprice/100) - $('pellets-cost').innerHTML,2);
	$('electricity-woodchip-saving').innerHTML = roundNumber((totalhours*0.80*maxkw/0.90*electricityprice/100) - $('woodchip-cost').innerHTML,2);
	$('electricity-carbon-saving').innerHTML = roundNumber(maxkw*totalhours*0.80*0.523,2);
	
	$('gas-pellets-saving').innerHTML = roundNumber((totalhours*0.80*maxkw/0.90*gasprice/100) - $('pellets-cost').innerHTML,2);
	$('gas-woodchip-saving').innerHTML = roundNumber((totalhours*0.80*maxkw/0.90*gasprice/100) - $('woodchip-cost').innerHTML,2);
	$('gas-carbon-saving').innerHTML = roundNumber(maxkw*totalhours*0.80*0.206,2);
	
}
function getBoilers(){
	$('boilerlist').style.display = '';
	$('boilerlist').innerHTML = '<img src="i/layout/loading.gif" alt="Loading..." />';
	var pars = 'KW='+$('maxoutput').innerHTML;
	var ajax = new Ajax.Request('getboilers.php',
								{asynchronous: true,
								method: 'get',
								onComplete: showBoilers,
								parameters: pars
								});
}
function showBoilers(req){
	$('boilerlist').innerHTML = req.responseText;
	reLightwindow = new lightwindow();
}