$(document).ready(function(){
	
	var kantoorKG = 0;
	var kantoorBedrag = 0;
	var kantoorAfr = 0;
	
	var gasKG = 0;
	var stroomKG = 0;
	var stadVerw = 0;
	
	var benzineKg = 0;
	var dieselKg = 0;
	var lpgKg = 0;
	var busKg = 0;
	var treinKg = 0;
	var vervoerKG = 0;
	var vervoerBedrag = 0;
	var vervoerAfr = 0;
	
	var bbusKg = 0;
	var vrachtKg = 0;
	var vrachtTotKg = 0;
	var vrachtTotBedrag = 0;
	var vrachtTotAfr = 0;
	
	
	var vluchtKG = 0;
	var vluchtBedrag = 0;
	var vluchtAfr = 0;
	
	var z1Kg = 0;
	var z2Kg = 0;
	var z3Kg = 0;
	var z4Kg = 0;
	var z5Kg = 0;
	
	$('.kantoor .element input').change(function(){
		if($(this).is('#gasverbr'))
		{
			gasKG = parseFloat($(this).attr('value')*1.78);
			if(isNaN(gasKG))
			{
				gasKG = 0;
				$(this).attr('value','');
			}
		} 
		if($(this).is('#stroomverbr'))
		{
			stroomKG = parseFloat($(this).attr('value')*0.55);
			if(isNaN(stroomKG))
			{
				stroomKG = 0;
				$(this).attr('value','');
			}
		}
		if($(this).is('#stadsverw'))
		{
			stadVerw = parseFloat($(this).attr('value')*26.5);
			if(isNaN(stadVerw))
			{
				stadVerw = 0;
				$(this).attr('value','');
			}
		}
		
		function calcStroomKg(){
			stroomKG = parseFloat($('#stroomverbr').attr('value')*0.554);
				if(isNaN(stroomKG))
				{
					stroomKG = 0;
					$('#stroomverbr').attr('value','');
				}
		}
		
		kantoorKG = gasKG + stroomKG + stadVerw;
		kantoorAfr = kgAfr(kantoorKG)
		kantoorBedrag = kgToEur(kantoorKG);
						
		$(this).parent().parent().parent().find('.kg').html(parseInt(kantoorKG)+' kg');
		$(this).parent().parent().parent().find('.bedrag').html('&euro; '+ kantoorBedrag);
		
		$('.kantoorTotaal .uitstoot').html(parseInt(kantoorKG)+' kg');
		$('.kantoorTotaal .bedrag').html('&euro; ' + kantoorBedrag);
		$('.kantoorTotaal .afronding').html(parseInt(kantoorAfr)+' kg');
		
		var ton = Math.round(kantoorKG/1000);
		ton = (ton == 0) ? 1 : ton;
		
		$('#qtyuitstoot_mijn_Kantoor829538044').attr('value', ton);
		$('#uitstoot_kantoor').attr('value', kantoorKG);
		
	});
	
	$('.bedrvervoer .element input').change(function(){
		if($(this).is('#kmBenzine'))
		{
			benzineKg = parseFloat($(this).attr('value')*0.218);
			if(isNaN(benzineKg))
			{
				benzineKg = 0;
				$(this).attr('value','');
			}
		} 
		if($(this).is('#kmDiesel'))
		{
			dieselKg = parseFloat($(this).attr('value')*0.2);
			if(isNaN(dieselKg))
			{
				dieselKg = 0;
				$(this).attr('value','');
			}
		}
		if($(this).is('#kmLPG'))
		{
			lpgKg = parseFloat($(this).attr('value')*0.2);
			if(isNaN(lpgKg))
			{
				lpgKg = 0;
				$(this).attr('value','');
			}
		}
		if($(this).is('#kmBus'))
		{
			busKg = parseFloat($(this).attr('value')*0.076);
			if(isNaN(busKg))
			{
				busKg = 0;
				$(this).attr('value','');
			}
		}
		if($(this).is('#kmTrein'))
		{
			treinKg = parseFloat($(this).attr('value')*0.042);
			if(isNaN(treinKg))
			{
				treinKg = 0;
				$(this).attr('value','');
			}
		}
				
		vervoerKG = benzineKg + dieselKg + lpgKg + busKg + treinKg;
		vervoerAfr = kgAfr(vervoerKG)
		vervoerBedrag = kgToEur(vervoerKG);
						
		$(this).parent().parent().parent().find('.kg').html(parseInt(vervoerKG)+' kg');
		$(this).parent().parent().parent().find('.bedrag').html('&euro; '+ vervoerBedrag);
		
		$('.vervoerTotaal .uitstoot').html(parseInt(vervoerKG)+' kg');
		$('.vervoerTotaal .bedrag').html('&euro; ' + vervoerBedrag);
		$('.vervoerTotaal .afronding').html(parseInt(vervoerAfr)+' kg');
		
		var ton = Math.round(vervoerKG/1000);
		ton = (ton == 0) ? 1 : ton;
		
		$('#qtyuitstoot_mijn_vervoer2051766082').attr('value', ton);
		$('#uitstoot_vervoer').attr('value', vervoerKG);
		
	});
	
	// vluchten is hetzelfde als particulier
	
	$('.bedrVluchten .element input').change(function(){
		if($(this).is('#z1'))
		{
			z1Kg = parseFloat($(this).attr('value')*509);
			if(isNaN(z1Kg))
			{
				z1Kg = 0;
				$(this).attr('value','');
			}
		} 
		
		if($(this).is('#z2'))
		{
			z2Kg = parseFloat($(this).attr('value')*872);
			if(isNaN(z2Kg))
			{
				z2Kg = 0;
				$(this).attr('value','');
			}
		} 
		
		if($(this).is('#z3'))
		{
			z3Kg = parseFloat($(this).attr('value')*2368);
			if(isNaN(z3Kg))
			{
				z3Kg = 0;
				$(this).attr('value','');
			}
		} 
		
		if($(this).is('#z4'))
		{
			z4Kg = parseFloat($(this).attr('value')*3857);
			if(isNaN(z4Kg))
			{
				z4Kg = 0;
				$(this).attr('value','');
			}
		} 
		
		if($(this).is('#z5'))
		{
			z5Kg = parseFloat($(this).attr('value')*5069);
			if(isNaN(z5Kg))
			{
				z5Kg = 0;
				$(this).attr('value','');
			}
		} 
			
		vluchtKG = z1Kg + z2Kg + z3Kg + z4Kg + z5Kg;
		vluchtAfr = kgAfr(vluchtKG)
		vluchtBedrag = kgToEur(vluchtKG);
						
		$(this).parent().parent().parent().find('.kg').html(parseInt(vluchtKG)+' kg');
		$(this).parent().parent().parent().find('.bedrag').html('&euro; '+ vluchtBedrag);
		
		$('.vluchtenTotaal .uitstoot').html(parseInt(vluchtKG)+' kg');
		$('.vluchtenTotaal .bedrag').html('&euro; ' + vluchtBedrag);
		$('.vluchtenTotaal .afronding').html(parseInt(vluchtAfr)+' kg');
		
		var ton = Math.round(vluchtKG/1000);
		ton = (ton == 0) ? 1 : ton;
		
		$('#qtyuitstoot_mijn_vluchten35444924').attr('value', ton);
		$('#uitstoot_vluchten').attr('value', vluchtKG);
		
	});
	
	$('.vracht .element input').change(function(){
		if($(this).is('#kmBbus'))
		{
			bbusKg = parseFloat($(this).attr('value')*0.260);
			if(isNaN(bbusKg))
			{
				bbusKg = 0;
				$(this).attr('value','');
			}
		} 
		if($(this).is('#kmVracht'))
		{
			vrachtKg = parseFloat($(this).attr('value')*0.630);
			if(isNaN(vrachtKg))
			{
				vrachtKg = 0;
				$(this).attr('value','');
			}
		}
				
		vrachtTotKg = bbusKg + vrachtKg;
		vrachtTotAfr = kgAfr(vrachtTotKg)
		vrachtTotBedrag = kgToEur(vrachtTotKg);
						
		$(this).parent().parent().parent().find('.kg').html(parseInt(vrachtTotKg)+' kg');
		$(this).parent().parent().parent().find('.bedrag').html('&euro; '+ vrachtTotBedrag);
		
		$('.vrachtTotaal .uitstoot').html(parseInt(vrachtTotKg)+' kg');
		$('.vrachtTotaal .bedrag').html('&euro; ' + vrachtTotBedrag);
		$('.vrachtTotaal .afronding').html(parseInt(vrachtTotAfr)+' kg');
		
		var ton = Math.round(vrachtTotKg/1000);
		ton = (ton == 0) ? 1 : ton;
		
		$('#qtyuitstoot_het_vrachtverkeer285186729').attr('value', ton);
		$('#uitstoot_vracht').attr('value', vrachtTotKg);
		
	});
});

function kgAfr(KG){
		KGwerkelijk = KG;
		
		if (KGwerkelijk != 0) { 
		
			KGafgerond = 1000*(Math.round(KG/1000))
			
			//bedr = Math.round(bedrRaw*Math.pow(10,2))/Math.pow(10,2);
			if(KGwerkelijk < 499){
				KGafgerond = 1000;
			} 
		
		} else {
			
			KGafgerond = 0;
		
		}
		
		return KGafgerond;
}
