// Drinkwater Savings Calculator
// 2009 Yve Verstrepen, Controlled Soft Water

window.onload = function (evt) { calc(); };

var calcCooler = true;

// Everything
function calc()
{
	// Calculate current situation
	var curMonth = (document.getElementById("txtContAantal").value * document.getElementById("txtContKost").value);
	
	if (calcCooler)
		curMonth += (document.getElementById("txtKoelHuur").value * document.getElementById("txtKoelAantal").value);
	
	document.getElementById("curTotaal").innerHTML = roundDec(curMonth, 2);
	
	// Calculate CSW situation
	var tapEq = (document.getElementById("txtContAantal").value * getCheckedValue(document.forms[0].elements['optType'])) * (3 / 1000);
	document.getElementById("waterKost").innerHTML = roundDec(tapEq, 2);
	
	var cswMonth = document.getElementById("txtToestAantal").value * document.getElementById("toestKost").innerHTML
					+ tapEq;
	
	document.getElementById("cswTotaal").innerHTML = roundDec(cswMonth, 2);
	
	// Calculate savings
	var saves = curMonth - cswMonth;
	
	var savings = "<UL>"
				+ "  <LI>&euro; " + roundDec(saves, 2) + " per maand. </LI>"
				+ "  <LI>&euro; " + roundDec((saves * 12), 2) + " per jaar. </LI><BR />"
				+ "  <LI>&euro; " + roundDec((saves * 36), 2) + " op 3 jaar. </LI>"
				+ "  <LI>&euro; " + roundDec((saves * 60), 2) + " op 5 jaar. </LI>"
				+ "  <LI>&euro; " + roundDec((saves * 120), 2) + " op 10 jaar. </LI>"
				+ "</UL>";
	
	document.getElementById("besparing").innerHTML = savings;
	
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function switchToBidons()
{
	calcCooler = true;
	document.getElementById("txtContKost").value = 7.5;
	document.getElementById('hider').style.visibility = 'visible'; 
	calc();
}
function switchToFles1l()
{
	calcCooler = false;
	document.getElementById("txtContKost").value = 0.65;
	document.getElementById('hider').style.visibility = 'hidden'; 
	calc();
}
function switchToFles50cl()
{
	calcCooler = false;
	document.getElementById("txtContKost").value = 0.5;
	document.getElementById('hider').style.visibility = 'hidden'; 
	calc();
}

function roundDec(number, decimals)
{
	return Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
}
