function show_bles(what){

	if(what == '1'){
		document.getElementById("selfemployed").style.display = 'block';
		document.getElementById("p60").style.display = 'none';
		document.getElementById("sep60").style.display = 'none';
	} else if(what == '2'){
		document.getElementById("selfemployed").style.display = 'none';
		document.getElementById("p60").style.display = 'block';
		document.getElementById("sep60").style.display = 'none';
	} else if(what == '3'){
		document.getElementById("selfemployed").style.display = 'none';
		document.getElementById("p60").style.display = 'none';
		document.getElementById("sep60").style.display = 'block';
	}
}

function show_szczegoly(a){
	if(document.getElementById("szczegoly"+a).style.display == 'none'){
		document.getElementById("szczegoly"+a).style.display = 'block';
	} else {
		document.getElementById("szczegoly"+a).style.display = 'none';
	}
	if(document.getElementById("pokukr"+a).innerHTML == 'pokaż szczegóły'){
		document.getElementById("pokukr"+a).innerHTML = 'ukryj szczegóły';
	} else {
		document.getElementById("pokukr"+a).innerHTML = 'pokaż szczegóły';
	}
}

		var zwrot = 0;

		PA = new Array();
		PA[1] = 4895; //2005/2006
		PA[2] = 5035; //2006/2007
		PA[3] = 5225; //2007/2008
		PA[4] = 6035; //2008/2009
		PA[5] = 6475; //2009/2010
		PA[6] = 6475; //2009/2010
		PA[7] = 7475; //2010/2011
		
		rate = new Array();
		rate[1] = new Array(); //2005/2006
			rate[1][1] = 2090;
			rate[1][2] = 32400;
		rate[2] = new Array(); //2006/2007
			rate[2][1] = 2150;
			rate[2][2] = 33300;
		rate[3] = new Array (); //2007/2008
			rate[3][1] = 2230;
			rate[3][2] = 34600;
		rate[4] = new Array (); //2008/2009
			rate[4][1] = 0;
			rate[4][2] = 34800;
		rate[5] = new Array (); //2009/2010
			rate[5][1] = 0;
			rate[5][2] = 37400;
		rate[6] = new Array (); //2009/2010
			rate[6][1] = 0;
			rate[6][2] = 37400;
		rate[7] = new Array (); //2010/2011
			rate[7][1] = 0;
			rate[7][2] = 35000;			
			
			
		tax = new Array();
		tax[1] = new Array(); //2005/2006
			tax[1][1] = 0.1;
			tax[1][2] = 0.22;
			tax[1][3] = 0.4;
		tax[2] = new Array(); //2006/2007
			tax[2][1] = 0.1;
			tax[2][2] = 0.22;
			tax[2][3] = 0.4;
		tax[3] = new Array(); //2007/2008
			tax[3][1] = 0.1;
			tax[3][2] = 0.22;
			tax[3][3] = 0.4;
		tax[4] = new Array(); //2008/2009
			tax[4][1] = 0;
			tax[4][2] = 0.2;
			tax[4][3] = 0.4;
		tax[5] = new Array();//2009/2010
			tax[5][1] = 0;
			tax[5][2] = 0.2;
			tax[5][3] = 0.4;
		tax[6] = new Array();//2010/2011
			tax[6][1] = 0;
			tax[6][2] = 0.2;
			tax[6][3] = 0.4;
		tax[7] = new Array();//2010/2011
			tax[7][1] = 0;
			tax[7][2] = 0.2;
			tax[7][3] = 0.4;			
		

function count_tax(rok, worksuma, taxsuma){
	podatek = 0;
	if(rok > 0){	
		x = worksuma - PA[rok];
			if(x < 0){ // Po odliczeniu Kwoty Wolnej Od Podatku
				podatek = taxsuma;
			} else {
				
				if(x < rate[rok][1]) {
					prog = tax[rok][1];
					podatek = x * prog;
				} else if ( x < rate[rok][2] ) {
					prog = tax[rok][2];
					podatek = rate[rok][1] * tax[rok][1];
					podatek += (x - rate[rok][1])* prog;
					} else {
					prog = tax[rok][3];
					podatek = rate[rok][1] * tax[rok][1];
					podatek += (rate[rok][2] - rate[rok][1]) * tax[rok][2];
					podatek += (x -  rate[rok][2]) * prog;					
				}
				podatek -= taxsuma;
				podatek = Math.round(podatek*100)/100;	
			}
			}
	return podatek;
}	

		NICfree = new Array();
		NICfree[1] = 4895; //2005/2006
		NICfree[2] = 5035; //2006/2007
		NICfree[3] = 5225; //2007/2008
		NICfree[4] = 5435; //2008/2009
		NICfree[5] = 5715; //2009/2010
		NICfree[6] = 5715; //2009/2010
		NICfree[7] = 7225; //2010/2011		
		
		 ratenic = new Array();
		 taxnic1 = new Array();
		 taxnic2 = new Array();
		 ratenic[1] = new Array();
		ratenic[1][1] = 32760;
		taxnic1[1] = 0.11;	
        taxnic2[1] = 0.01;	
		ratenic[2] = new Array();		
		ratenic[2][1] = 33540;
		taxnic1[2] = 0.11;	
        taxnic2[2] = 0.01;		
		ratenic[3] = new Array();		
		ratenic[3][1] = 34840;
		taxnic1[3] = 0.11;	
        taxnic2[3] = 0.01;		
		ratenic[4] = new Array();
		ratenic[4][1] = 40040;
		taxnic1[4] = 0.11;	
        taxnic2[4] = 0.01;
		ratenic[5] = new Array();
		ratenic[5][1] = 43875;
		taxnic1[5] = 0.11;	
        taxnic2[5] = 0.01;	
		ratenic[6] = new Array();		
		ratenic[6][1] = 43875;
		taxnic1[6] = 0.11;	
        taxnic2[6] = 0.01;
		ratenic[7] = new Array();
		ratenic[7][1] = 42475;
		taxnic1[7] = 0.12;	
        taxnic2[7] = 0.02;
		//taxnic1 = 0.11;
		//taxnic2 = 0.01;
		
function count_nic(rok, profit){
	var nic = 0;
	if(rok > 0){	
		//alert('proit:'+profit+ 'free:' + NICfree[rok]);
		var x = profit - NICfree[rok];
		
			if(x < 0){ // Po odliczeniu Kwoty Wolnej Od Podatku
				nic = 0;
			} else {
				 if(x < ratenic[rok][1]) {
					prog = taxnic1[rok];
					nic = x * prog;
					//alert('kwota: '+x+',  prog: '+prog);
				} else {
					nic = (ratenic[rok][1]-NICfree[rok]) * taxnic1[rok];
					a = ratenic[rok][1] - NICfree[rok];
					nic += (x - a)* taxnic2[rok];
				} 

			
			}
				nic =Math.round(nic*100)/100;	
			}
			
	return nic;
}			
function zwrot_p60() {
	var podatek = count_tax(document.getElementById("yearp60").value, document.getElementById("incomesp60").value,'0');
	podatek -= parseFloat(document.getElementById("taxpaidp60").value);
		if(podatek > 0){
		document.getElementById("wyniczek").innerHTML = '<div class="clear">&nbsp;</div><div class="szczegolypodl">Podatek do zapłacenia:</div><div class="szczegolypodr"><p class="cenaszczegoly">&pound;'+podatek+' </p></div>';
	} else {
		document.getElementById("wyniczek").innerHTML = '<div class="clear">&nbsp;</div><div class="szczegolypodl">Zwrot nadpłaconego podatku:</div><div class="szczegolypodr"><p class="cenaszczegoly">&pound;'+podatek*(-1)+' </p></div>';
	}
}

function zwrot_se(){
	document.getElementById("profit").value = parseFloat(document.getElementById("incomes").value) - parseFloat(document.getElementById("koszty").value);
	var podatek = count_tax(document.getElementById("year").value, parseFloat(document.getElementById("profit").value), '0');
	var nic = count_nic(document.getElementById("year").value, parseFloat(document.getElementById("profit").value));
	document.getElementById("alreadypaid").value = parseFloat(document.getElementById("taxcis").value);
	document.getElementById("nic").value = nic;
	document.getElementById("tax").value = podatek;
	document.getElementById("razem").value = parseFloat(document.getElementById("nic").value) + parseFloat(document.getElementById("tax").value);
	//var alltax = Math.round(100*(parseFloat(document.getElementById("razem").value) - parseFloat(document.getElementById("taxcis").value)))/100;
	var alltax = (100*(parseFloat(document.getElementById("razem").value) - parseFloat(document.getElementById("taxcis").value)))/100;
	document.getElementById("alltax").value = parseFloat(document.getElementById("razem").value);
	document.getElementById("roznica").value = alltax;
if(alltax > 0){
		document.getElementById("wynik").innerHTML = '<div class="clear">&nbsp;</div><div class="szczegolypodl">Podatek do zapłacenia:</div><div class="szczegolypodr"><p class="cenaszczegoly">&pound;'+alltax+' </p><a style="cursor: pointer;" id="pokukr1" onclick="show_szczegoly(1)">pokaż szczegóły</a></div><div class="clear">&nbsp;</div>';
	} else {
		document.getElementById("wynik").innerHTML = '<div class="clear">&nbsp;</div><div class="szczegolypodl">Zwrot nadpłaconego podatku:</div><div class="szczegolypodr"><p class="cenaszczegoly">&pound;'+alltax*(-1)+' </p><a style="cursor: pointer;" id="pokukr1" onclick="show_szczegoly(1)">pokaż szczegóły</a></div><div class="clear">&nbsp;</div>';
	}
	document.getElementById("wynik").innerHTML += '';
	
	}

function zwrot_p60ise(){
	var f = parseFloat(document.getElementById("aincomes").value) - parseFloat(document.getElementById("akoszty").value);
	document.getElementById("aprofit").value = f + parseFloat(document.getElementById("aincomesp60").value)
	var e = parseFloat(document.getElementById("ataxcis").value) + parseFloat(document.getElementById("ataxpaidp60").value);
	var podatek = count_tax(document.getElementById("ayear").value, parseFloat(document.getElementById("aprofit").value), '0');
	document.getElementById("atax").value = podatek;
	var nic = count_nic(document.getElementById("ayear").value, f);
	document.getElementById("anic").value = nic;
	document.getElementById("arazem").value = parseFloat(document.getElementById("atax").value) + parseFloat(document.getElementById("anic").value);
	document.getElementById("aalltax").value = parseFloat(document.getElementById("arazem").value);
	document.getElementById("aalreadypaid").value = e;
	var aalltax = Math.round(100*(parseFloat(document.getElementById("arazem").value) - parseFloat(document.getElementById("aalreadypaid").value)))/100;
	document.getElementById("aroznica").value = aalltax;
	if(aalltax > 0){
		document.getElementById("awynik").innerHTML = '<div class="clear">&nbsp;</div><div class="szczegolypodl">Podatek do zapłacenia:</div><div class="szczegolypodr"><p class="cenaszczegoly">&pound;'+aalltax+' </p><a style="cursor: pointer;" id="pokukr2" onclick="show_szczegoly(2)">pokaż szczegóły</a></div><div class="clear">&nbsp;</div>';
	} else {
		document.getElementById("awynik").innerHTML = '<div class="clear">&nbsp;</div><div class="szczegolypodl">Zwrot nadpłaconego podatku:</div><div class="szczegolypodr"><p class="cenaszczegoly">&pound;'+aalltax*(-1)+' </p><a style="cursor: pointer;" id="pokukr2" onclick="show_szczegoly(2)">pokaż szczegóły</a></div><div class="clear">&nbsp;</div>';
	}
	document.getElementById("awynik").innerHTML += '';
}
