$(document).ready(function(){
	
	var altKG = 0;
	var altBedrag = 0;
	var altAfr = 0;
	
	$('.alternatief .element input').change(function(){
		if($(this).is('#numPpl') || $(this).is('#duration'))
		{
			altKG = parseFloat(($('#numPpl').attr('value') * 12) * $('#duration').attr('value'));
			if(isNaN(altKG))
			{
				altKG = 0;
				$(this).attr('value','');
			}
		} 
				
		//altKG = gasKG + stroomKG + dieselKG;
		altAfr = kgAfr(altKG)
		altBedrag = kgToEur(altKG);
						
		$(this).parent().parent().parent().find('.kg').html(parseInt(altKG)+' kg');
		$(this).parent().parent().parent().find('.bedrag').html('&euro; '+ altBedrag);
		
		$('.alternatiefTotaal .uitstoot').html(parseInt(altKG)+' kg');
		$('.alternatiefTotaal .bedrag').html('&euro; ' + altBedrag);
		$('.alternatiefTotaal .afronding').html(parseInt(altAfr)+' kg');
		
		var ton = Math.round(altKG/1000);
		ton = (ton == 0) ? 1 : ton;
		
		$('#qtyAlternatieve_methode_uitstoot_evenement1478919183').attr('value', ton);
		
	});
	
	var locatieKG = 0;
	var locatieBedrag = 0;
	var locatieAfr = 0;
	
	var gasKG = 0;
	var stroomKG = 0;
	var dieselKG = 0;
	
	$('.locatie .element input').change(function(){
		if($(this).is('#stroom'))
		{
			stroomKG = parseFloat($(this).attr('value')*0.55);
			if(isNaN(stroomKG))
			{
				stroomKG = 0;
				$(this).attr('value','');
			}
		} 
		if($(this).is('#gas'))
		{
			gasKG = parseFloat($(this).attr('value')*1.78);
			if(isNaN(gasKG))
			{
				gasKG = 0;
				$(this).attr('value','');
			}
		}
		if($(this).is('#diesel'))
		{
			dieselKG = parseFloat($(this).attr('value')*3.1);
			if(isNaN(dieselKG))
			{
				dieselKG = 0;
				$(this).attr('value','');
			}
		}
		
		locatieKG = gasKG + stroomKG + dieselKG;
		locatieAfr = kgAfr(locatieKG)
		locatieBedrag = kgToEur(locatieKG);
						
		$(this).parent().parent().parent().find('.kg').html(parseInt(locatieKG)+' kg');
		$(this).parent().parent().parent().find('.bedrag').html('&euro; '+ locatieBedrag);
		
		$('.locatieTotaal .uitstoot').html(parseInt(locatieKG)+' kg');
		$('.locatieTotaal .bedrag').html('&euro; ' + locatieBedrag);
		$('.locatieTotaal .afronding').html(parseInt(locatieAfr)+' kg');
		
		var ton = Math.round(locatieKG/1000);
		ton = (ton == 0) ? 1 : ton;
		
		$('#qtyuitstoot_van_de_locatie540381997').attr('value', ton);
		$('#uitstoot_locatie').attr('value', locatieKG);
		
	});
	
	var autoKg = 0;
	var treinKg = 0;
	var busKg = 0;
	var vliegKg = 0;
	
	var vervoerKg = 0;
	var vervoerAfr = 0;
	var vervoerBedrag = 0;
	
	$('.evtVervoer .element input').change(function(){
		if($(this).is('#numAuto') || $(this).is('#distAuto'))
		{
			autoKg =  parseFloat($('#numAuto').attr('value') * $('#distAuto').attr('value') * 0.218);
			if(isNaN(autoKg))
			{
				autoKg = 0;
				$('#numAuto').attr('value','');
				$('#distAuto').attr('value','');
			}
		}
		
		if($(this).is('#numTrein') || $(this).is('#distTrein'))
		{
			treinKg =  parseFloat($('#numTrein').attr('value') * $('#distTrein').attr('value') * 0.042);
			if(isNaN(treinKg))
			{
				treinKg = 0;
				$('#numTrein').attr('value','');
				$('#distTrein').attr('value','');
			}
		}
		
		if($(this).is('#numBus') || $(this).is('#distBus'))
		{
			busKg =  parseFloat($('#numBus').attr('value') * $('#distBus').attr('value') * 0.076);
			if(isNaN(busKg))
			{
				busKg = 0;
				$('#numBus').attr('value','');
				$('#distBus').attr('value','');
			}
		}
		
		if($(this).is('#numVlieg'))
		{
			vliegKg =  parseFloat($('#numVlieg').attr('value') * 1000);
			if(isNaN(busKg))
			{
				busKg = 0;
				$('#numVlieg').attr('value','');
			}
		}
		
		vervoerKg = autoKg + treinKg + busKg + vliegKg;
		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_van_het_vervoer_naar_het_evenement772325483').attr('value', ton);
		$('#uitstoot_vervoer').attr('value', vervoerKg);
	});
	
	var overigKG = 0;
	var overigBedrag = 0;
	var overigAfr = 0;
	
	var overigBbusKg = 0;
	var overigVrwKG = 0;
	var overigCateringKG = 0;
	var overigWasteKG = 0;
	var overigDrukwerkKG = 0;
	
	$('.evtOverig .element input').change(function(){
		if($(this).is('#kmBbus'))
		{
			overigBbusKg = parseFloat($(this).attr('value')*0.26);
			if(isNaN(overigBbusKg))
			{
				overigBbusKg = 0;
				$(this).attr('value','');
			}
		}
		
		if($(this).is('#kmVrw'))
		{
			overigVrwKG = parseFloat($(this).attr('value')*0.63);
			if(isNaN(overigVrwKG))
			{
				overigVrwKG = 0;
				$(this).attr('value','');
			}
		}
		
		if($(this).is('#catering'))
		{
			overigCateringKG = parseFloat($(this).attr('value')*0.33);
			if(isNaN(overigCateringKG))
			{
				overigCateringKG = 0;
				$(this).attr('value','');
			}
		}
		
		if($(this).is('#waste'))
		{
			overigWasteKG = parseFloat($(this).attr('value')*0.067);
			if(isNaN(overigWasteKG))
			{
				overigWasteKG = 0;
				$(this).attr('value','');
			}
		}
		
		if($(this).is('#drukwerk'))
		{
			overigDrukwerkKG = parseFloat($(this).attr('value')*0.01);
			if(isNaN(overigDrukwerkKG))
			{
				overigDrukwerkKG = 0;
				$(this).attr('value','');
			}
		}
		
		overigKG = overigBbusKg + overigVrwKG + overigCateringKG + overigWasteKG + overigDrukwerkKG;
		overigAfr = kgAfr(overigKG)
		overigBedrag = kgToEur(overigKG);
						
		$(this).parent().parent().parent().find('.kg').html(parseInt(overigKG)+' kg');
		$(this).parent().parent().parent().find('.bedrag').html('&euro; '+ overigBedrag);
		
		$('.overigTotaal .uitstoot').html(parseInt(overigKG)+' kg');
		$('.overigTotaal .bedrag').html('&euro; ' + overigBedrag);
		$('.overigTotaal .afronding').html(parseInt(overigAfr)+' kg');
		
		var ton = Math.round(overigKG/1000);
		ton = (ton == 0) ? 1 : ton;
		
		$('#qtyoverige_uitstoot_van_het_evenement853483878').attr('value', ton);
		$('#uitstoot_overig').attr('value', overigKG);
		
	});
	
});

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;
}
