var bShowBerekeningDetails = true;

function Bereken() {
	SluitHelp();
  var iLeeftijdKlasse = parseInt(document.getElementById('selectLeeftijdKlasse').value);
	if (document.getElementById("div65jaar").style.display == 'block'){
		if (document.getElementById('select65jaar').value == 65) {		
			iLeeftijdKlasse = 65;
		}	
	}
	
	
  var strTabel = document.getElementById('selectTabelKleur').value;
  if (document.getElementById("frmBerekening").chkLHKorting[0].checked == true){var bLHK = true} else{var bLHK = false}
	//var bAOK = false;	
	
	if (document.getElementById("divVakantiebonnen").style.display == 'block'){
		var iVakantiebonnen = document.getElementById('selectVakantiebonnen').value;
	} else {
		var iVakantiebonnen = 1;
	}
	
  var iTijdvak = document.getElementById('selectTijdvak').value;
  var iPeriode = parseInt(document.getElementById('selectPeriode').value);
  //var sTijdvak = document.getElementById('selectTijdvak').options[document.getElementById('selectTijdvak').selectedIndex].text;
	var sTijdvak = getSelText('selectTijdvak');
  var iTijdvakLoon = toRekenBedrag(document.getElementById('inpLoon').value);
	// komma wijzigen in punt	iTijdvakLoon = iTijdvakLoon 
	iTijdvakLoon = iTijdvakLoon.toString().replace(/\$|\,/g,'.');
	
	var iFactor = vulFactor(iTijdvak, iVakantiebonnen);
	
	var L = iTijdvakLoon * iFactor;
  if  (L <= iMaxAfronden){ 
		L =  parseInt((L / iVeelvoudJaarloon)) * iVeelvoudJaarloon ; 
	}
  
	var d = bepaalOHK(iLeeftijdKlasse, bLHK, L); //Overige heffingskortingen
	
	if (iLeeftijdKlasse == 65) {
		var aTmpTarief = aTabelTarief65;
	} else if ( iLeeftijdKlasse == 99 ) {
		var aTmpTarief = aTabelTariefUitfasering;
	} else {
		var aTmpTarief = aTabelTarief;
	}
	i = 0;	
  while (i < aTmpTarief.length -1 && aTmpTarief[i+1][0]  < L)  {i++;};
	var	a = aTmpTarief[i][1];
	var b = aTmpTarief[i][2];
	var c = aTmpTarief[i][3];

// begin berekenen k
  if (bLHK == true && strTabel == "wit"){
	  var i = 0;
  	while (aTabelAK[i][0] != (iLeeftijdKlasse==99?65:iLeeftijdKlasse) && i < aTabelAK.length){i++;};
		var m = aTabelAK[i][1];
		var n = aTabelAK[i][2];  	
		var p = aTabelAK[i][3];
		var m_Lmax = aTabelAK[i][4];
		var m_LnLpmax = aTabelAK[i][5];		
		var t = aTabelAK[i][6];
		var u = aTabelAK[i][7];
		var u_Ltmax = aTabelAK[i][8];		
		var k_max = aTabelAK[i][9];
		
		var tmpmL = m * L ;
		if (tmpmL > m_Lmax) {tmpmL = m_Lmax} 
		
		var tmpLp = L - p;
		if (tmpLp < 0 ) { tmpLp = 0}
		
		var nLP = n * tmpLp;
		var mLnLp = tmpmL + nLP;
		if (mLnLp > m_LnLpmax) {mLnLp = m_LnLpmax} 
					
		var tmpLt = L - t;
		if (tmpLt < 0 ) { tmpLt = 0}
			
		var tmpuLt = u * ( tmpLt);
		if (tmpuLt > u_Ltmax) {tmpuLt = u_Ltmax} 

		var k = parseInt(mLnLp - tmpuLt);
		
	} else {
  	var k = 0;  
  }
			
// einde berekenen k 	
	
  var zA = parseInt((L - a) * b / 100 + c);
  var zB = parseInt((iMaxAfronden - a) * b / 100 + c);
  
	var y = zB - (k + d);
	
  if (L <= iMaxAfronden){
		var x = zA - ( k + d);
  } else {
		var x = y + iTarBerekenX * ( L - iMaxAfronden);
  }
	if (x < 0 ) { x = 0}
	
	var tmpLB = x / iFactor;
	var LB = parseInt( (x / iFactor)* 100) / 100;
	
	var AK = parseInt( (k / iFactor)* 100) / 100;  
	
  if (sTijdvak == '4 weken'){
  	var sTijdvakTxt = sTijdvak;
	} else {     
  	var sTijdvakTxt = "een " + sTijdvak;  
  }
        
	document.getElementById('idResultTijdvakLB').innerHTML = sTijdvakTxt;
  document.getElementById('idResultTijdvakAK').innerHTML = sTijdvakTxt;
  
  document.getElementById('idResultLB').innerHTML = checkBedrag(LB,'', '', true, true, false, false );
  document.getElementById('idResultAK').innerHTML = checkBedrag(AK,'', '', true, true, false, false );  
	
	if (strTabel == "wit" && bLHK == true ){
		document.getElementById("divAK").style.display = 'block';
  } else {
		document.getElementById("divAK").style.display = 'none';
	}
	
	

  document.getElementById("divOpmAKGroen").style.display = 'none';
	document.getElementById("divOpmAKWitZonderLHK").style.display = 'none';
	if (strTabel == "groen" ){
		document.getElementById("divOpmAKGroen").style.display = 'block';
  } else if (strTabel == "wit" &&  bLHK == false){
		document.getElementById("divOpmAKWitZonderLHK").style.display = 'block';
  } 
                        
  doUitvoerResult('divResult');
  
     var tmpBerTotTxt = "<p>strTabel:  " + strTabel +
				"<br />iTijdvakLoon: " + iTijdvakLoon + 
			  "<br />iLeeftijdKlasse: " + iLeeftijdKlasse +
				"<br />a: " + a + 
				"<br />b: " + b +
				"<br />c: " + c +
				"<br />d: " + d + 
				"<br />iFactor: " + iFactor + 
				"<br />k: " + k + 
				"<br />k_max: " + k_max + 
				"<br />L: " + L + 
				"<br />m: " + m + 
				"<br />m_LnLpmax: " + m_LnLpmax +
				"<br />t: " + t +
				"<br />u: " + u +
				"<br />u_Ltmax: " + u_Ltmax +
				"<br />m_Lmax: " + m_Lmax +
				"<br />m_Lmax: " + m_Lmax +
				"<br />n: " + n + 
				"<br />p: " + p +
				"<br />x: " + x +
				"<br />zA: " + zA +
				"<br />zB: " + zB +        
				"<br />y: " + y +        
				"<br /><br />LB: " + LB +
				"<br />AK: " + AK +
				"<br /><br />tmpmL: " + tmpmL +
				"<br />tmpuLt: " + tmpuLt +
				"<br />nLP: " + nLP +
				"<br />mLnLp: " + mLnLp;
				+ "<p>";

				document.getElementById('divDebug').innerHTML = tmpBerTotTxt;
 
}

function vulFactor(iTijdvak, iVakantiebonnen) {
  var i = 0;	
  var tmpFactor = 0;
  while (aTabelFactor[i][0] != iTijdvak && i < aTabelFactor.length){ 
		i++;
	}
  tmpFactor = aTabelFactor[i][iVakantiebonnen];	
  return tmpFactor;	
}

function bepaalOHK(iLeeftijdKlasse, bLHK, L) {
var tmpOHK = 0;
//alert("iLeeftijdKlasse: " + iLeeftijdKlasse + "\nbLHK: " + bLHK + "\nbAOK: " + bAOK + "\nL: " + L);
	if (iLeeftijdKlasse == 65 || iLeeftijdKlasse == 99 ){
   	if (bLHK == true ){
      if (L < iGrensOHK65){
       	tmpOHK = aOHK[3][0];
      } else { 
        tmpOHK = aOHK[3][1]; 
      }
    } else {
    	tmpOHK = aOHK[2][0]
  	}
  } else {
  	if (bLHK == true ){
    	tmpOHK = aOHK[1][0]
    } else {
    	tmpOHK = aOHK[0][0]
  	}
  }
  return tmpOHK;
}



