window.onload = updateScreen;
var backsimileEvent = window.setInterval(checkHashUpdate, 500);
var currentPage;
var submenuActive = null;
var subsubmenuActive = null;


	function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=660,height=150');");
	}

	function hideScreen(id) { if (document.getElementById(id)) { document.getElementById(id).style.display = 'none'; } }
	function showScreen(id) { if(document.getElementById(id)) { document.getElementById(id).style.display = 'block'; } }

	function updateScreen() { 
		
		firstLoc = location.hash.replace("#",""); 
		
		if (firstLoc.length == 0) { firstLoc = 'introduction'; } 
		if (!document.getElementById(firstLoc)) { firstLoc = 'introduction'; } 
		
		if (document.getElementById(firstLoc)) {
			document.getElementById(firstLoc).style.display = 'block'; currentPage = firstLoc;
		}
		 }
	
	
	function hideAll() { 
		
		
		
		var div = document.getElementsByTagName('div');
		
		for (var i = 0; i < div.length; i++) { 
			
			if (div[i].className == 'screen') { div[i].style.display = 'none'; } 
			
			} 
		
		}
	
	
	function checkHashUpdate() { 
		loc = location.hash.replace("#",""); if (loc != currentPage) { hideAll(); updateScreen(); } 
		}
	
	
	function showMenu(id) {
			document.getElementById(id + 'Menu').style.display = 'block';
			document.getElementById(id + 'Tab').className = 'active';
	}
	
	
	function hideAllMenus() {
			document.getElementById('generalMenu').style.display = 'none';
			document.getElementById('generalTab').className = '';
			
			document.getElementById('energyMenu').style.display = 'none';
			document.getElementById('energyTab').className = '';
			
			document.getElementById('industrialMenu').style.display = 'none';	
			document.getElementById('industrialTab').className = '';
						
			document.getElementById('wasteMenu').style.display = 'none';	
			document.getElementById('wasteTab').className = '';
						
			document.getElementById('agricultureMenu').style.display = 'none';	
			document.getElementById('agricultureTab').className = '';
			
			document.getElementById('energySecondGenerationMenu').style.display = 'none';
			document.getElementById('energyIndustryMenu').style.display = 'none';
			document.getElementById('energyTransportMenu').style.display = 'none';
			document.getElementById('energyFugitiveMenu').style.display = 'none';
			document.getElementById('energyOtherMenu').style.display = 'none';
		}
		
		
	function showEnergyMenu(id) {
			document.getElementById(id + 'Menu').style.display = 'block';
	}
		
	function hideEnergyMenus() {
			document.getElementById('energySecondGenerationMenu').style.display = 'none';
			document.getElementById('energyIndustryMenu').style.display = 'none';
			document.getElementById('energyTransportMenu').style.display = 'none';
			document.getElementById('energyFugitiveMenu').style.display = 'none';
			document.getElementById('energyOtherMenu').style.display = 'none';
		}
		
		
	function makeActive(id) {
			if (submenuActive != null) {
			submenuActive.className = ''
			} else {
				document.getElementById('introductionMenuLi').className = '';
				}
			submenuActive = id;
			id.className = 'active';
			
			
			}
			
	function makesubActive(id) {
if (subsubmenuActive != null) {			subsubmenuActive.className = ''; }
			subsubmenuActive = id;
			id.className = 'active';
			
			
			}
			
			
			
	function setValue(el, val) {

if (el == 'basicInformation5' || el == 'basicInformation8') {
	var els = document.getElementsByName(el);
	var i = 0;
	while ((els[0].options[i].text != val) && (i < els[0].options.length)) {i++;}
	if (i < els[0].options.length) {els[0].selectedIndex = i;}

} else { 	
				var els = document.getElementsByName(el);
				if (els[0]) els[0].value = val;
}
			}
			
			
	function populateElectricity(source, co2, ch4, n20) {
				co2.value = source.value * document.getElementById('publicElectricityAndHeatProduction3').value;
				ch4.value = source.value * document.getElementById('publicElectricityAndHeatProduction4').value;
				n20.value = source.value * document.getElementById('publicElectricityAndHeatProduction5').value;
			}
			
	function populateHeat(source, co2, ch4, n20) {
				co2.value = source.value * document.getElementById('publicElectricityAndHeatProduction8').value;
				ch4.value = source.value * document.getElementById('publicElectricityAndHeatProduction9').value;
				n20.value = source.value * document.getElementById('publicElectricityAndHeatProduction10').value;
			}
			


	function sumBoxes(sources, target) {
		var total = 0;
		for(var box in sources) {
			var val = parseFloat(document.getElementById(sources[box]).value);
			if (!isNaN(val)) {total += val;}
		}
		document.getElementById(target).value = total;
	}


	function divideBoxes(source, divisor, target) {
		var sourceval = parseFloat(document.getElementById(source).value);
		var divisorval = parseFloat(document.getElementById(divisor).value);
		document.getElementById(target).value = total;
	}
			
			
	function updateEmissionsFactors() {

		var sourceval = parseFloat(document.getElementById('publicElectricityAndHeatProduction22').value);
		var divisorval = parseFloat(document.getElementById('publicElectricityAndHeatProduction35').value);
		document.getElementById('publicElectricityAndHeatProduction27').value = (document.getElementById('publicElectricityAndHeatProduction25').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
		document.getElementById('publicElectricityAndHeatProduction32').value = (document.getElementById('publicElectricityAndHeatProduction30').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
		document.getElementById('publicElectricityAndHeatProduction37').value = (document.getElementById('publicElectricityAndHeatProduction36').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';

		var sourceval = parseFloat(document.getElementById('publicElectricityAndHeatProduction23').value);
		var divisorval = parseFloat(document.getElementById('publicElectricityAndHeatProduction35').value);
		document.getElementById('publicElectricityAndHeatProduction28').value = (document.getElementById('publicElectricityAndHeatProduction25').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
		document.getElementById('publicElectricityAndHeatProduction33').value = (document.getElementById('publicElectricityAndHeatProduction30').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
		document.getElementById('publicElectricityAndHeatProduction38').value = (document.getElementById('publicElectricityAndHeatProduction36').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';

		var sourceval = parseFloat(document.getElementById('publicElectricityAndHeatProduction24').value);
		var divisorval = parseFloat(document.getElementById('publicElectricityAndHeatProduction35').value);
		document.getElementById('publicElectricityAndHeatProduction29').value = (document.getElementById('publicElectricityAndHeatProduction25').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
		document.getElementById('publicElectricityAndHeatProduction34').value = (document.getElementById('publicElectricityAndHeatProduction30').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
		document.getElementById('publicElectricityAndHeatProduction39').value = (document.getElementById('publicElectricityAndHeatProduction36').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';

		var sourceval = parseFloat(document.getElementById('publicElectricityAndHeatProduction122').value);
		var divisorval = parseFloat(document.getElementById('publicElectricityAndHeatProduction135').value);
		document.getElementById('publicElectricityAndHeatProduction127').value = (document.getElementById('publicElectricityAndHeatProduction125').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
		document.getElementById('publicElectricityAndHeatProduction132').value = (document.getElementById('publicElectricityAndHeatProduction130').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
		document.getElementById('publicElectricityAndHeatProduction137').value = (document.getElementById('publicElectricityAndHeatProduction136').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';

		var sourceval = parseFloat(document.getElementById('publicElectricityAndHeatProduction123').value);
		var divisorval = parseFloat(document.getElementById('publicElectricityAndHeatProduction135').value);
		document.getElementById('publicElectricityAndHeatProduction128').value = (document.getElementById('publicElectricityAndHeatProduction125').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
		document.getElementById('publicElectricityAndHeatProduction133').value = (document.getElementById('publicElectricityAndHeatProduction130').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
		document.getElementById('publicElectricityAndHeatProduction138').value = (document.getElementById('publicElectricityAndHeatProduction136').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';

		var sourceval = parseFloat(document.getElementById('publicElectricityAndHeatProduction124').value);
		var divisorval = parseFloat(document.getElementById('publicElectricityAndHeatProduction135').value);
		document.getElementById('publicElectricityAndHeatProduction129').value = (document.getElementById('publicElectricityAndHeatProduction125').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
		document.getElementById('publicElectricityAndHeatProduction134').value = (document.getElementById('publicElectricityAndHeatProduction130').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
		document.getElementById('publicElectricityAndHeatProduction139').value = (document.getElementById('publicElectricityAndHeatProduction136').value.length > 0 && divisorval > 0) ? sourceval / divisorval : '';
	}

	
	function populateDefaultScreenNationalFactors(screenId) {

		var electricityenergy = parseFloat(document.getElementById(screenId + '27').value);
		var electricityco2factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction27').value);
		var electricitych4factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction28').value);
		var electricityn20factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction29').value);

		var heatenergy = parseFloat(document.getElementById(screenId + '32').value);
		var heatco2factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction32').value);
		var heatch4factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction33').value);
		var heatn20factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction34').value);

		var coolingenergy = parseFloat(document.getElementById(screenId + '37').value);
		var coolingco2factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction37').value);
		var coolingch4factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction38').value);
		var coolingn20factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction39').value);

		document.getElementById(screenId + '28').value = Math.round((electricityenergy * electricityco2factor)*1000)/1000;
		document.getElementById(screenId + '29').value = electricityenergy * electricitych4factor;
		document.getElementById(screenId + '30').value = electricityenergy * electricityn20factor;

		document.getElementById(screenId + '33').value = Math.round((heatenergy * heatco2factor)*1000)/1000;
		document.getElementById(screenId + '34').value = heatenergy * heatch4factor;
		document.getElementById(screenId + '35').value = heatenergy * heatn20factor;

		document.getElementById(screenId + '38').value = Math.round((coolingenergy * coolingco2factor)*1000)/1000;
		document.getElementById(screenId + '39').value = coolingenergy * coolingch4factor;
		document.getElementById(screenId + '40').value = coolingenergy * coolingn20factor;

		if (document.getElementById(screenId + '28').value == 'NaN') document.getElementById(screenId + '28').value = '0';
		if (document.getElementById(screenId + '29').value == 'NaN') document.getElementById(screenId + '29').value = '0';
		if (document.getElementById(screenId + '30').value == 'NaN') document.getElementById(screenId + '30').value = '0';

		if (document.getElementById(screenId + '33').value == 'NaN') document.getElementById(screenId + '33').value = '0';
		if (document.getElementById(screenId + '34').value == 'NaN') document.getElementById(screenId + '34').value = '0';
		if (document.getElementById(screenId + '35').value == 'NaN') document.getElementById(screenId + '35').value = '0';

		if (document.getElementById(screenId + '38').value == 'NaN') document.getElementById(screenId + '38').value = '0';
		if (document.getElementById(screenId + '39').value == 'NaN') document.getElementById(screenId + '39').value = '0';
		if (document.getElementById(screenId + '40').value == 'NaN') document.getElementById(screenId + '40').value = '0';

		sumBoxes(new Array(screenId + '28',screenId + '33',screenId + '38'), screenId + '43');
		sumBoxes(new Array(screenId + '29',screenId + '34',screenId + '39'), screenId + '44');
		sumBoxes(new Array(screenId + '30',screenId + '35',screenId + '40'), screenId + '45');

	}

	function populateDefaultScreenRegionalFactors(screenId) {

		var electricityenergy = parseFloat(document.getElementById(screenId + '47').value);
		var electricityco2factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction127').value);
		var electricitych4factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction128').value);
		var electricityn20factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction129').value);

		var heatenergy = parseFloat(document.getElementById(screenId + '51').value);
		var heatco2factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction132').value);
		var heatch4factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction133').value);
		var heatn20factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction134').value);

		var coolingenergy = parseFloat(document.getElementById(screenId + '55').value);
		var coolingco2factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction137').value);
		var coolingch4factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction138').value);
		var coolingn20factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction139').value);

		document.getElementById(screenId + '48').value = Math.round((electricityenergy * electricityco2factor)*1000)/1000;
		document.getElementById(screenId + '49').value = electricityenergy * electricitych4factor;
		document.getElementById(screenId + '50').value = electricityenergy * electricityn20factor;

		document.getElementById(screenId + '52').value = Math.round((heatenergy * heatco2factor)*1000)/1000;
		document.getElementById(screenId + '53').value = heatenergy * heatch4factor;
		document.getElementById(screenId + '54').value = heatenergy * heatn20factor;

		document.getElementById(screenId + '56').value = Math.round((coolingenergy * coolingco2factor)*1000)/1000;
		document.getElementById(screenId + '57').value = coolingenergy * coolingch4factor;
		document.getElementById(screenId + '58').value = coolingenergy * coolingn20factor;

		if (document.getElementById(screenId + '48').value == 'NaN') document.getElementById(screenId + '48').value = '0';
		if (document.getElementById(screenId + '49').value == 'NaN') document.getElementById(screenId + '49').value = '0';
		if (document.getElementById(screenId + '50').value == 'NaN') document.getElementById(screenId + '50').value = '0';

		if (document.getElementById(screenId + '52').value == 'NaN') document.getElementById(screenId + '52').value = '0';
		if (document.getElementById(screenId + '53').value == 'NaN') document.getElementById(screenId + '53').value = '0';
		if (document.getElementById(screenId + '54').value == 'NaN') document.getElementById(screenId + '54').value = '0';

		if (document.getElementById(screenId + '56').value == 'NaN') document.getElementById(screenId + '56').value = '0';
		if (document.getElementById(screenId + '57').value == 'NaN') document.getElementById(screenId + '57').value = '0';
		if (document.getElementById(screenId + '58').value == 'NaN') document.getElementById(screenId + '58').value = '0';

		sumBoxes(new Array(screenId + '48',screenId + '52',screenId + '56'), screenId + '60');
		sumBoxes(new Array(screenId + '49',screenId + '53',screenId + '57'), screenId + '61');
		sumBoxes(new Array(screenId + '50',screenId + '54',screenId + '58'), screenId + '62');

	}

	function populateTransportScreenNationalFactors(screenId) {

		var electricityenergy = parseFloat(document.getElementById(screenId + '27').value);
		var electricityco2factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction27').value);
		var electricitych4factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction28').value);
		var electricityn20factor = parseFloat(document.getElementById('publicElectricityAndHeatProduction29').value);

		document.getElementById(screenId + '28').value = Math.round((electricityenergy * electricityco2factor)*1000)/1000;
		document.getElementById(screenId + '29').value = electricityenergy * electricitych4factor;
		document.getElementById(screenId + '30').value = electricityenergy * electricityn20factor;

		if (document.getElementById(screenId + '28').value == 'NaN') document.getElementById(screenId + '28').value = '0';
		if (document.getElementById(screenId + '29').value == 'NaN') document.getElementById(screenId + '29').value = '0';
		if (document.getElementById(screenId + '30').value == 'NaN') document.getElementById(screenId + '30').value = '0';

	}



