<!--
var consPerYear = 0; // consumption per year (Jahresverbrauch)
var start = 3;

// do some initial work
function initialize() {
    // check java script
    checkJavaScript();
	//     table
	build();
	// calculate table values
	calculate();
	
	// display first page
	display("page1");
}

// this function calculates the values of the table "Einfache Berechnung nach Wohnfläche".
// it will be called, if the value of the area ("beheizte Wohnfläche") changes
function calculate() {
	var table = getById("table1"); // get table
	var f = getById("area"); // textfield ("beheizte Wohnfläche")
	var consum = getById("consumption"); // drop-down menu (Auswahlliste, "gewählter Jahresverbrauch")
	var oilPrice = getPrice(1); // oil is in row 1
	var pelletPrice = getPrice(8); // pellets are in row 8
    
	if(!isNaN(f.value)) {
		for(var i=start; i < 10; i++) { // skips empty and header rows
			var val = Math.round(f.value*ratio[i-start]/100)*100; // lookup for "ratio" in properties.js!
			table.rows[i].cells[2].innerHTML=formatGermanNumber(val); // geschätzter/errechneter Jahresverbrauch
			table.rows[i].cells[3].innerHTML=formatGermanNumber(Math.round(val/10));
			table.rows[i].cells[4].innerHTML=formatGermanNumber(Math.round(val/4.9));
			table.rows[i].cells[5].innerHTML=formatEuroNumber(formatGermanNumber(Math.round(((val/10)*oilPrice-(val/4.9)*pelletPrice)*15)));
		}
		updateUsage();
	}
}

// this function builds the conversion tab (Umrechnung Verbrauch)
function build() {
	var tfWidth = 50;
	if(units.length == type.length && type.length == fuelValue.length 
		&& fuelValue.length == price.length && price.length==fuelType.length) 
	{
		var conversionDiv = getById("conversion");
		var html = "<p>beheizte Wohnfl&auml;che [m<sup>2</sup>]: <input type='textfield' name='area2' id='area2' class='TFConversion' onchange='calculateConversion2()'></p><p>&nbsp;</p><table width='100%'><tr><td><table id='con1' border=1>";
		html += "<th>Brennstoffart</th><th>Letztj&auml;hriger Verbrauch</th><th>Einheit</th><th>Kesselart</th><th>Heizwert<br>"
			+ "in kWh/Einheit</th><th>Preis<br>[Euro/Einheit]<p><select id='country' onchange='showCountryPrices()'><option value='1'>&Ouml;sterreich</option><option value='2'>Deutschland</option></select></p></th>"
			+ "<th>Cent/<br>kWh</th><th>W&auml;rme-<br>menge<br>kWh</th>";
			
		var color =  "#fbb900";
		var old =  "#f29c12";
		//alert ("Length of field Type: " +  type.length);
		for(var i=0; i < type.length; i++) {	
			
			// alternate background color (color changes, if the "Brennstoffart" changes)
			if(fuelType[i] != "&nbsp;" && fuelType[i] != "" && fuelType[i] != "G50") {
				var tmp = color;
				color = old;
				old = tmp;
			}
			html += "<tr bgcolor='" + color + "'>";
			if(fuelType[i] == "G50") { // "G50" align must be right ;-)
				html +=  "<td style='text-align: right;'><nobr>" + fuelType[i] + "</nobr></td>";
			}
			else {
				html +=  "<td><nobr>" + fuelType[i] + "</nobr></td>";
			}
			html +=  "<td><input type='textfield' name='amount" + i + "' class='TFConversion'  onchange='calculateConversion()'></td>";
			html +=  "<td>" + units[i] + "</td>";
			html +=  "<td>" + type[i] + "</td>";
			html +=  "<td>" + formatGermanNumber(fuelValue[i]) + "</td>";
			html +=  "<td align='center'>&euro;&nbsp;<input type='textfield' id='price" + i + "' value='" + formatGermanNumber(price[i]) + "' class='TFConversion'  onchange='calculateConversion()'></td>";
			html +=  "<td>" + formatGermanNumber(Math.round(price[i]/fuelValue[i]*1000)/1000) + "</td>";
			html +=  "<td>&nbsp;</td>";
			html +=  "</tr>";
		}
		html +=  "</table></td>";
		
		html += "<td align='right' valign='top'><table id='con2'>";
		html += "<tr><td>&nbsp;</td><td>kWh Jahresenergieverbrauch</td></tr>";
		html += "<tr><td width='120'nowrap ><select id='efficiency' onchange='calculateConversion2()'>";
		html += "<option value='90'>&lt; 10 Jahre: 90</option><option value='80'>&gt;= 10 Jahre: 80</option><option value='70'>&gt;= 20 Jahre: 70</option><option value='65'>&gt;= 30 Jahre: 65</option>";
		html += "</select></td><td nowrap>% Kesselwirkungsgrad (alter Kessel) Bestand</td></tr>";
		html += "<tr><td>&nbsp;</td><td>kWh werden momentan genutzt</td></tr>";
		html += "<tr><td><input type='textfield' name='effUse' id='effUse' class='TFConversion' onchange='calculateConversion2()' value=5></td><td><nobr>% Wirkungsgradverb.-NEUER Kessel</nobr></td></tr>";
		html += "<tr><td></td><td></td></tr>";
		html += "<tr><td>&nbsp;</td><td><nobr>kW errechnete Gebäudeheizlast</nobr></td></tr>";
		html += "<tr><td>&nbsp;</td><td><nobr>kW erforderliche Kesselgr&ouml;&szlig;e</nobr></td></tr>";
		html += "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
		html += "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
		html += "<tr><td>&nbsp;</td><td><nobr>W/m<sup>2</sup>&nbsp;&nbsp;&nbsp; fl&auml;chenm&auml;ssig bezogene Geb&auml;udeheizlast</nobr></td></tr>";
		html += "<tr><td>&nbsp;</td><td><nobr>kWh/m<sup>2</sup> jahresbezogener Energiebedarf</nobr></td></tr>";
		html += "</table></td></tr></table>";
		
		conversionDiv.innerHTML=html;
		conversionDiv.appendChild(getById("foot").cloneNode(true));
	}
	updateHeatingArea();
}

// this function recalculates the values of the first conversion table (con1)
function calculateConversion() {
	var con1 = getById("con1");
	consPerYear = 0; // consumption per year (Jahresverbrauch); global var!
	// start at 1, because at 0 is the table header!!
	for(var i=1; i < con1.rows.length; i++) {
		var amount = 0;
		var price = 0;
		var heatingValue = getHeatingValue(i); // Heizwert
		
		amount = getNeededFuelAmount(i); // "eingegebener letztjähriger Verbrauch"
		if(isNaN(amount)) {
			amount = 0;
		}
		
		price = getPrice(i); // price per unit (Preis pro Einheit)
                
		if(isNaN(price)) {
			price = 0;
		}
		setPricePerkWh(i, (price/heatingValue)); // cent per kWh
		var heatingAmount = amount * heatingValue;
		setHeatingAmount(i, heatingAmount); // heating amount; "Wärmemenge"
		consPerYear += amount * heatingValue;
	}
	if(consPerYear==0) {
		var temp = getById("consumption").value;
		if(!isNaN(temp)) {
			consPerYear=temp;
		}
	}
	
	// if con1 changes, con2 changes too and must be recalculated
	calculateConversion2();
}

// this function recalculates the values of the second conversion table (con2)
function calculateConversion2() {
	var boilerEfficiency = getBoilerEfficiency(); // Kesselwirkungsgrad
	var usedPower = 0; // genutzte Heizkraft (kWh werden genutzt)
	var bHeatLoad = 0; // Gebäude Heizlast
	var boilerSize = 0; // Kesselgröße
	var effUse = getEffUse(); // Wirkungsgradverbesserung NEUER Kessel
	var heatingLoadPerM2 = 0; // Heizlast pro Quadratmeter
	var area = 0; // beheizte Fläche
	var energyDemand = 0; // Energiebedarf
	
	// check user input
	area = getById("area2").value;
	if(isNaN(area)) {
		area = 1;
	}
	//if (!area) {
	//   alert("Es fehlt die Angabe der Wohnfläche.");
        //}
        //alert ("area: " + area);	
	// calculate values
	usedPower = consPerYear*boilerEfficiency;
	bHeatLoad = usedPower/divisorBHL;
	boilerSize = (bHeatLoad/boilerEfficiency)/(1+effUse/100);
	heatingLoadPerM2 = bHeatLoad/area*1000;
	energyDemand = boilerSize*divisorBHL/area;
	//alert("heating= " + bHeatLoad + "area: " + area);
	
	// display values
	setConsumptionPerYear(consPerYear);
	setUsedPower(usedPower);
	setBuildingHeatLoad(bHeatLoad);
	setBoilerSize(boilerSize);
	setHeatingLoadPerM2(heatingLoadPerM2);
	setEnergyDemand(energyDemand);
}

// this method ensures that the two area text fields (2x"beheizte Wohnfläche") 
// are synchron. the first textfield overrides the second one
function updateHeatingArea() {
	var value = getById("area").value;
	if(!isNaN(value)) {
		var area2 = getById("area2");
		area2.value = value;
	}
	
	calculateConversion2();
}

// 
function updateUsage() {
    clearCon1(); // first clear table con1 and recalculate
    var value = parseInt(getById("consumption").value); // get value of drop-down menu
    var table1rows = getById("table1").rows; // get table
	if(!isNaN(value)) {
		consPerYear = parseGermanNumber(parseEuroNumber(table1rows[value+start-1].cells[2].innerHTML)); // global var
		setConsumptionPerYear(value);
	}
    else {
		consPerYear = 0; // global var
		setConsumptionPerYear(0);
    }
	
	calculateConversion2(); // recalculate table con2
}

// this function clears the table con1 and recalculates it
function clearCon1() {
	var rows = getById("con1").rows;
	getById("area2").value = "";
    for(var i=1; i < rows.length; i++) {
        setNeededFuelAmount(i, "");
    }
    calculateConversion();
}

// this function is called, if a tab is clicked on. the function displays
// the clicked form or diagram
function display(tab) {
	var index = parseInt(tab.charAt(4)) - 1;
	var page1 = getById("page1");
	var page2 = getById("page2");
	
	// show div or diagram
	switch(index) {
	case 0: 
		// display first form
		setInActive(page2);
		setActive(page1);
		getById("conversion").style.visibility = "hidden";
		getById("estimation").style.visibility = "visible";
		break;
	case 1:
		// display second form
		setInActive(page1);
		setActive(page2);
		getById("estimation").style.visibility = "hidden";
		getById("conversion").style.visibility = "visible";
		getById("area").value="";
		getById("consumption").value=0;
		break;
	case 2:
		// display "Brennstroffvergleich" diagram
		drawFuelTypeDiagram();
		break;
	case 3:
		// display "15 Jahre" diagram
		draw15yearsDiagram();
		break;
	case 4:
		// display "1 Jahr" diagram
		draw1yearDiagram();
		break;
	case 5:
		// display "Menge Jahr" diagram
		drawAmountYearDiagram();
		break;
	case 6:
		// display "CO2 Jahr" diagram
		drawCO2YearDiagram();
		break;
	}
}

// this function changes the style of a tab (sets to inactive)
function setInActive(node) {
	node.style.backgroundImage="";
	node.firstChild.style.backgroundImage="";
	node.firstChild.style.color="";
	node.firstChild.style.paddingBotton="";
}

// this function changes the style of a tab (sets to active)
function setActive(node) {
	node.style.backgroundImage="url('fileadmin/bilder/left_on.gif')";
	node.firstChild.style.backgroundImage="url('fileadmin/bilder/right_on.gif')";
	node.firstChild.style.color="#333";
	node.firstChild.style.paddingBotton="5px";
}

// this function displays the fuel prices per unit of the selected country.
// 1=Österreich, 2=Deutschland
function showCountryPrices() {
    var p;
    var sel = getById("country"); // get drop-down menu
    
    switch(parseInt(sel.value)) {
    case 1: // Austria; Österreich
        p = price;
        break;
    case 2: // Germany; Deutschland
        p = germanPrice;
        break;
    }
    
    var rows = getById("con1").rows;
    for(var i=1; i<rows.length; i++) {
        setPrice(i, p[i-1]);
    }
    calculateConversion(); // recalculate table
}

// this function provides browser independency
function getById(id) {
    var el; // the wanted element
    if(document.all) {
        // Code for Internet Explorer 5.5 and higher
        el = eval("document.all." + id);
    }
    else if(document.getElementById) {
        // Code for Netscape compatible Browsers (e.g. Mozilla)
        el = document.getElementById(id);
    }
    return el;
}

// this function checks, whether javascript is enabled or not
function checkJavaScript() {
    var alert = getById("alert");
    var nav = getById("navigation");
    var div = getById("estimation");
    alert.style.visibility="hidden";
    nav.style.visibility="visible";
    div.style.visibility="visible";
    
}

// dummy-function for the navigation links
function showTabView() { }
// -->

