/* 

##
## Rekenschema (c) 2010 Uitgeverij Nieuwezijds
##

*/

var gewijzigd = false;

$(function() {
	$(".tabs").tabs();
});

function download()
{
	gewijzigd = false;
	return true;
}


function waarschuwing()
{
    if (!gewijzigd) window.onbeforeunload = function() {    
    	if ( gewijzigd)	return confirm( 'Na deze actie gaan alle ingevulde gegevens verloren.\n\nWeet u het zeker?');
    };
	gewijzigd = true;
	if ( c('3C5') > c('3B5') ) alert('De restwaarde is hoger dan de aanschafwaarde.');
	if ( c('3C6') > c('3B6') ) alert('De restwaarde is hoger dan de aanschafwaarde.');
	if ( c('3C7') > c('3B7') ) alert('De restwaarde is hoger dan de aanschafwaarde.');
	if ( c('3C8') > c('3B8') ) alert('De restwaarde is hoger dan de aanschafwaarde.');
	
	
}


$(function(){
	//$("#accordion").accordion({ header: "h3", collapsible: true, active: 'none'  });

	$('#topmenu li a').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
	
	$('.schema').blur(
		function() { 
			$(this).removeClass('untouched'); 
			$(this).val( $(this).val().replace(/[^0-9-]/g,'') );
			$(this).format({format:"#,###"});
			waarschuwing();
		} 
	);
	
	$('.schema').focus(
		function() { 
			$(this).val( $(this).val().replace(/[^0-9-]/g,'') );
		} 
	);
	
	$('.schema').keyup(
		function () {	
			$(this).removeClass('untouched'); 
			bereken();
		}
	);
	
	
	$('.schema3').blur(
		function() { 
			$(this).removeClass('untouched'); 
			$(this).val( $(this).val().replace(/[^0-9-]/g,'') );
			$(this).format({format:"#,###"});
			waarschuwing();
		} 
	);
	
	$('.schema3').focus(
		function() { 
			$(this).val( $(this).val().replace(/[^0-9-]/g,'') );
		} 
	);
	
	$('.schema3').keyup(
		function () {	
			$(this).removeClass('untouched'); 
			bereken();
		}
	);
	
	// Dikke fake voor het idee
	$('.schema2').blur(
		function () {	
			$(this).removeClass('untouched'); 
			bereken();
		}
	);

});

$(document).ready(function () {
    bereken();
});


function elkemaand( rij )
{
	setValue('LC' + rij , c('LB' + rij));
	setValue('LD' + rij , c('LB' + rij));
	setValue('LE' + rij , c('LB' + rij));
	setValue('LF' + rij , c('LB' + rij));
	setValue('LG' + rij , c('LB' + rij));
	setValue('LH' + rij , c('LB' + rij));
	setValue('LI' + rij , c('LB' + rij));
	setValue('LJ' + rij , c('LB' + rij));
	setValue('LK' + rij , c('LB' + rij));
	setValue('LL' + rij , c('LB' + rij));
	setValue('LM' + rij , c('LB' + rij));
}

function telop( col )
{
	return( 
		c('L' + col + '5') +
		c('L' + col + '7') +
		c('L' + col + '10') +
		c('L' + col + '11') +
		c('L' + col + '12') +
		c('L' + col + '13') + 
		c('L' + col + '14') + 
		c('L' + col + '15') + 
		c('L' + col + '16') + 
		c('L' + col + '18')	
	);
}


function bereken()
{
	// Liquiditeit 
	
	//RIJEN 
	elkemaand( 11 );
	elkemaand( 12 );
	elkemaand( 13 );
//	elkemaand( 14 );
	elkemaand( 15 );
//	elkemaand( 18 );
	// OVERNEMEN
	setValue( 'LB20', telop('B'));
	setValue( 'LC5', c('LB20'));
	setValue( 'LC20', telop('C'));
	setValue( 'LD5', c('LC20'));
	setValue( 'LD20', telop('D'));
	setValue( 'LE5', c('LD20'));
	setValue( 'LE20', telop('E'));
	setValue( 'LF5', c('LE20'));
	setValue( 'LF20', telop('F'));
	setValue( 'LG5', c('LF20'));
	setValue( 'LG20', telop('G'));
	setValue( 'LH5', c('LG20'));
	setValue( 'LH20', telop('H'));
	setValue( 'LI5', c('LH20'));
	setValue( 'LI20', telop('I'));
	setValue( 'LJ5', c('LI20'));
	setValue( 'LJ20', telop('J'));
	setValue( 'LK5', c('LJ20'));
	setValue( 'LK20', telop('K'));
	setValue( 'LL5', c('LK20'));
	setValue( 'LL20', telop('L'));
	setValue( 'LM5', c('LL20'));
	setValue( 'LM20', telop('M'));


	// Afschrijvingen
	
	setValue( '3E5', (c('3B5')-c('3C5'))/  c('3D5')   ); 
	setValue( '3E6', (c('3B6')-c('3C6'))/  c('3D6')   ); 
	setValue( '3E7', (c('3B7')-c('3C7'))/  c('3D7')   ); 
	setValue( '3E8', (c('3B8')-c('3C8'))/  c('3D8')   ); 
	setValue( '3E9', c('3E5') + c('3E6') + c('3E7') + c('3E8'));
	setValue( 'B11', - c('3E9'));

	// Explotatiebegroting
//	setValue( 'B5', c('B4') * -0.05 );
	setValue( 'C6', c('B4') + c('B5') );
//	setValue( 'E5', c('E4') * -0.05 );
	setValue( 'F6', c('E4') + c('E5') );
	setValue( 'E7', c('B7'));
	setValue( 'E8', c('B8'));
	setValue( 'E9', c('B9'));
	setValue( 'E10', c('B10'));
	setValue( 'E11', c('B11'));
	
	setValue( 'C12', c('B7') + c('B8') + c('B9') + c('B10') + c('B11') );
	setValue( 'F12', c('E7') + c('E8') + c('E9') + c('E10') + c('E11') );
	
	setValue( 'C14', c('C12') + c('C6') );
	setValue( 'F14', c('F12') + c('F6') );

	// Autokosten
	setValue( '4B18', c('4B16') + c('4B17') );
	setValue( '4B21', (c('4B4') -c('4B6'))/c('4B5'));
	setValue( '4B22', c('4B10') );
	setValue( '4B23', c('4B11') );
	setValue( '4B24', c('4B18') * c('4B9')/100 );
	setValue( '4B25', c('4B18') / c('4B7') * c('4B8')/100 );

	setValue( '4B26', c('4B21') + c('4B22') + c('4B23') + c('4B24') + c('4B25') );
	setValue( '4B28', c('4B26') );
	setValue( '4B29', -c('4B13') * c('4B12') /100);
	setValue( '4B30', c('4B28') + c('4B29') );
	setValue( '4B32', c('4B17') * 0.19 );
	setValue( '4B34', c('4B32') - c('4B30') );

	
	// Investeringsbegroting
	setValue( '2C8', c('2B3') + c('2B4') + c('2B5') + c('2B6') + c('2B7') ); 
	setValue( '2E7', c('2B5') );
	setValue( '2F8', c('2E3') + c('2E4') + c('2E5') + c('2E6') + c('2E7') ); 

	setValue( '2E10', c('2B6')/3);
	setValue( '2B11', 0.19 *( c('2B6') + c('2B10') + c('2B16') + c('2B17')));
	
	setValue( '2C14', c('2B10') + c('2B11') + c('2B12') + c('2B13') ); 
	setValue( '2F13', c('2E10') + c('2E11') + c('2E12') ); 


	setValue( '2C18', c('2B16') + c('2B17') ); 
	setValue( '2F18', c('2E15') + c('2E16') + c('2E17') ); 
	
	setValue( '2C19', c('2C8') + c('2C14') + c('2C18') ); 
	setValue( '2F19', c('2F8') + c('2F13') + c('2F18') ); 
	

	
	// Bezettingsgraad
	
	setValue( '5B5', c('5B3') * c('5B4') );
	setValue( '5B9', c('5B7') / c('5B8') );
	setValue( '5B11', (c('5B10') /100) *  c('5B9') );
	setValue( '5B13',  (c('5B11')/ c('5B5'))*100 );
	
	// Benodigd inkomen
	
	setValue( 'BIA', c('BI1') + c('BI2') + c('BI3') + c('BI4') + c('BI5') + c('BI6') + c('BI7') + c('BI8') );
	setValue( 'BIB', c('BI9') + c('BI10') + c('BI11') + c('BI12') + c('BI13') );
	setValue( 'BIC', c('BI14') + c('BI15') + c('BI16') + c('BI17') );
	setValue( 'BIM', c('BIA') + c('BIB') + c('BIC') );
	setValue( 'BI', 12 * c('BIM') );
	
	
	// Heffingskorting
	
	setValue( 'HK', c('HK1') + c('HK2') + c('HK3') + c('HK4') + c('HK5') + c('HK6') + c('HK7') + c('HK8') + c('HK9') + c('HK10') );
	
	setValue( '9D14',c('HK') );
	
	// Belastingvrijstelling
	
	setValue( 'BV', c('BV1') + c('BV2') + c('BV3') + c('BV4') + c('BV5') + c('BV6') + c('BV7') + c('BV8') + c('BV9')  );
	
	setValue( '9C4' , c('BV'));
	
	// Besteedbaar inkomen
	// setValue ('9C4' = uit ander blad
	setValue ('9C5', (c('9C3') - c('BV1') -c('BV2')) * 0.12);
	setValue( '9D8', c('9C3') - c('9C4') - c('9C5') - c('9C6') + c('9C7') );
	if ( c('9D8') < 0 ) setValue('9D8', 0);
	
	if (c( '9D8' ) > 18218 )
	{
		setValue( '9C9', 6094 );
	} else 
	{
		setValue( '9C9', c( '9D8' ) * 0.3345);
	}
	if (c('9D8') >(18218+14519) )
	{
		setValue( '9C10', 14519*0.4195 );
	} else  if (c('9D8') >18218){
		setValue( '9C10',(c('9D8')-18218)*0.4195);
	} else {
		setValue ('9C10',0);
	}

	if (c('9D8') >(18218+14519+21628) )
	{
		setValue( '9C11', 21628*0.42 );
	} else  if (c('9D8') >(18218+14519)){
		setValue( '9C11',(c('9D8')-18218-14519)*0.42);
	} else {
		setValue ('9C11',0);
	}

	if (c('9D8') >(18218+14519+21628) )
	{
		setValue( '9C12',(c('9D8')-18218-14519-21628)*0.52);
	} else {
		setValue ('9C12',0);
	}	
	
	setValue( '9D13', c('9C9') + c('9C10') + c('9C11') + c('9C12') );
	
	// 	setValue( '9D14') HEFFINGSKORTING
	
	if ((c('9D13') - c('9D14')) >0 )
	{
		setValue( '9D15', c('9D13') - c('9D14'));
	} else {
	
		setValue( '9D15', 0);
	}
 
	
	if ( (c('9D8')+ c('9C6')) < 33189 )
	{
		setValue( '9D16', 0.0495* ( c('9D8')+ c('9C6') ) );
	} else {
		setValue( '9D16', 0.0495*33189 );
	}		
	
	setValue( '9D18', c('9C3') - c('9C6') - c('9D15') - c('9D16') );
	
}



function c( veld )
{
	var value=0;
	if (  $('#' + veld).val())
	{
		value =	$('#' + veld).val().replace(/[^0-9-]/g,'');
	}
	else
	{
		value = $('#' + veld).text().replace(/[^0-9-]/g,'');
	}
	return Math.ceil(Number(value));
}

function setValue(veld, value)
{
	$('#'+ veld).text( parseInt(value) );
	$('#'+ veld).format({format:"#,###"}); 
}