﻿	function calc() 
	{
		
		var sel = document.getElementById('year').value;
		//alert( sel );
		var arr = new Array(7);
		
		arr[10] = new Array(4);
		arr[10]['free']  = '6475';
		arr[10]['tax10'] = '0';
		arr[10]['tax22'] = '37400';
		arr[10]['tax40'] = '37400';
		arr[10]['rate2'] ='20';
		arr[10]['rate1'] ='0';
		
		arr[9] = new Array(4);
		arr[9]['free']  = '6035';
		arr[9]['tax10'] = '0';
		arr[9]['tax22'] = '34800';
		arr[9]['tax40'] = '34800';
		arr[9]['rate2'] ='20';
		arr[9]['rate1'] ='0';
		
		arr[8] = new Array(4);
		arr[8]['free']  = '5225';
		arr[8]['tax10'] = '2230';
		arr[8]['tax22'] = '32370';
		arr[8]['tax40'] = '34600';
		arr[8]['rate2'] ='22';
		arr[8]['rate1'] ='10';
		
		arr[6] = new Array(4);
		arr[6]['free']  = '5035';
		arr[6]['tax10'] = '2150';
		arr[6]['tax22'] = '31150';
		arr[6]['tax40'] = '33300';
		arr[6]['rate2'] ='22';
		arr[6]['rate1'] ='10';
		
		arr[5] = new Array(4);
		arr[5]['free']  = '4895';
		arr[5]['tax10'] = '2090';
		arr[5]['tax22'] = '30310';
		arr[5]['tax40'] = '32400';
		arr[5]['rate2'] ='22';
		arr[5]['rate1'] ='10';
		
		arr[4] = new Array(4);
		arr[4]['free']  = '4745';
		arr[4]['tax10'] = '2020';
		arr[4]['tax22'] = '29380';
		arr[4]['tax40'] = '31400';
		arr[4]['rate2'] ='22';
		arr[4]['rate1'] ='10';
		
		arr[3] = new Array(4);
		arr[3]['free']  = '4615';
		arr[3]['tax10'] = '1960';
		arr[3]['tax22'] = '28540';
		arr[3]['tax40'] = '30500';	
		arr[3]['rate2'] ='22';		
		arr[3]['rate1'] ='10';
		re = /^(\d+)(\.*)(\d+)$/;
		
		if( document.getElementById('gross').value=="" ) {
			alert("Please, fill gross pay");
			return false;
		}
		else if( document.getElementById('paid').value!="" && !document.getElementById('paid').value.match(re) ) {
			alert('Please, fill paid tax with digits and .');
			return false;
		}
		else 
		{
			
			re = /^(\d+)(\.*)(\d+)$/;
			if( document.getElementById('gross').value.match(re) ) 
			{
				
				var gross = document.getElementById('gross').value;
				var tax = 0;
				var paid = document.getElementById('paid').value;
				
				var free = arr[sel]['free'];
				var rest = 0;
				var tax = 0;
				var lastTax = 0;
				var rate2=(arr[sel]['rate2']*1)/100;
				var rate1=(arr[sel]['rate1']*1)/100;
				//alert(rate2);
								
				if( gross > arr[sel]['free'] ) 
				{
					//alert("sdfg");
					//gross = gross - arr[sel]['free'];
					//rest = gross;
					//alert(rest);
				}
				
				
				var act = gross;
				rest = gross - free;
				if(rest < 0) rest=0;
				
				if( free < (act*1) ) 
				{
					//alert(1);
					act = act - arr[sel]['free'];
					tax += 0;
				}
				else {
					act = 0;
				}
				
				if( act < arr[sel]['tax10'] )
				{
					//alert(2);
					tax += act * rate1;
					act = 0;
					lastTax = rate2;
				}
				
				if( act >= arr[sel]['tax10']  ) 
				{
					//alert(3);
					tax += arr[sel]['tax10'] * rate1;
					act -= arr[sel]['tax10'];
					//alert( tax + " - " + act );
					lastTax = rate2;
				}
				
				
				if( act > arr[sel]['tax10'] && act < arr[sel]['tax22'] )
				{
					//alert(4);
					tax += act * rate2;
					act = 0;
					lastTax = 0.4;
				}
				
				if( act >= arr[sel]['tax22']  ) 
				{
					//alert(5);
					tax += arr[sel]['tax22'] * rate2;
					act -= arr[sel]['tax22'];
					//alert( tax + " - " + act );
					lastTax = 0.4;
				}
				
				
				if( act > 0 )
				{
					//alert(6);
					tax += act * lastTax;
				}
	
				
				
				
				/*if( gross > arr[sel]['tax10']  )
				{
					alert( 10 );
					//gross = gross - arr[sel]['tax10'];
					tax += arr[sel]['tax10'] * 0.10;	
					alert(tax);
				}
				
				if( (gross-arr[sel]['tax10']) > arr[sel]['tax22'] )
				{
					alert( 22 );
					//gross = gross - arr[sel]['tax22'];
					tax += arr[sel]['tax22'] * rate2;	
				}
				
				if( (gross - arr[sel]['tax10'] - arr[sel]['tax22']) > arr[sel]['tax40'] )
				{
					alert( 40 );
					tax += ( gross - arr[sel]['tax10'] - arr[sel]['tax22'] ) * 0.4;
				}*/
				
				
				//var out = 4745.00;
				//var rest = gross - out;
				//var total = (rest - 2020 - 29380)*0.4;
				//var paid = 0;
				
				if( document.getElementById('paid').value!="" ) 
				{
					if( document.getElementById('paid').value.match(re) )
					{
						paid = document.getElementById('paid').value;
						//var tax = 202+6463.6+total;
						var tax2 = tax - paid;
					}
					else {
						alert('Please, fill paid tax with digits and .');
					}
				}
				else {
					var paid = 0;
					//var tax = 202+6463.6+total;
					var tax2 = tax;
				}

				//alert(tax);
				gross = gross*1;
				free = free*1;
				paid = paid*1;
				rest = rest*1;
				document.getElementById("r_gross").innerHTML = gross.toFixed(2);
				document.getElementById("r_free").innerHTML = free.toFixed(2);
				//document.getElementById("r_free").innerHTML = document.getElementById('gross').value;
				document.getElementById("r_total").innerHTML = rest.toFixed(2);
				document.getElementById("r_tax").innerHTML = tax.toFixed(2);
				document.getElementById("r_paid").innerHTML = paid.toFixed(2);
				document.getElementById("r_result").innerHTML = tax2.toFixed(2);
				document.getElementById("results").style.display="block";
				return false;
			}
			else {
				alert('Please, fill gross pay with digits and .');
			}
		}
		return false;
	}

