/*
* JSON Objects containing rates and max ceilings
*/

var pensionRates = {"rates": [  
	{"age" : 30 ,  "rate"   :   15}, 
     {"age" :40  ,  "rate"  :   20 }, 
     {"age" :50  ,  "rate"  :   25}, 
     {"age" :55 ,  "rate"   :   30}, 
     {"age" :60  ,  "rate"  :   35}, 
     {"age" :81  ,  "rate"  :   40} 
     ]
};

var maxContribution = { "maxrates": [ 
	{ "year": "2008" ,    "max" :   115000},
    { "year": "2009",   "max" : 115000}
    ]
};




function init()
{

	calculateContribution();
}

function calculateContribution()
{

	//for formatting numbers
	var format = new Format();
	var maxReached08 = false;
	var maxReached09 = false;
	
	//reset
	document.getElementById('maxreached08').style.display="none";
	document.getElementById('maxreached09').style.display="none";
	document.getElementById('errorincome').style.display="none";
	document.getElementById('errorage').style.display="none";
	
	

	//check the year 
	var taxYear = "2009";
	document.getElementById('ageYear').innerHTML = taxYear;	
	document.getElementById('totalYear').innerHTML = taxYear;	
	
	
	
				
	var amount = document.getElementById('annualIncome').value;
	
	//remove comma and dots if any
		//fix up the number
	amount = format.removeComma(amount)
	
	
	if(amount.lenght == 0 || amount == "" || isNaN(amount))
	{
	
		document.getElementById('totalTax').innerHTML = "";
		document.getElementById('errorincome').style.display="block";
		return;
	}
	
	var age = document.getElementById('age').value;
	
	if(age.lenght == 0 || age == "" || isNaN(age))
	{
	
		document.getElementById('totalTax').innerHTML = "";
		document.getElementById('errorage').style.display="block";
		return;
	}
	
	// get the max amount
	var year = null;
	var max = null;
	 
	for(var key in maxContribution.maxrates)
	{
	
		year = maxContribution.maxrates[key].year;
	
		if(year == taxYear)
		{
				max = maxContribution.maxrates[key].max;
				break;
		}
		
	}
	
	//var max = eval("maxContribution.maxrates[0]."+taxYear);
	
	
	
	
	if(amount > max && year==2008)
	{
		//show the message too
		amount = max;
		maxReached08 = true;
	}	
	
	if(amount > max && year==2009)
	{
		//show the message too
		amount = max;
		maxReached09 = true;
	}
	
	
	//calculate
	var rate = null;
	

	
	for(var key2 in pensionRates.rates)
	{	
		//alert(pensionRates.rates[key2].age)
	
		if( age < pensionRates.rates[key2].age)
		{
			rate = pensionRates.rates[key2].rate;
			break;
		}
	}


	var totalContribution = (amount) * (rate/100); 
	
	//round it
	totalContribution = Math.round(totalContribution);
	
	document.getElementById('totalTax').innerHTML = format.addComma(totalContribution);	
	document.getElementById('totalTax2').innerHTML = format.addComma(totalContribution);	
    
	
	if(maxReached08)
	{
		document.getElementById('maxreached08').style.display="block";	
	}
   if(maxReached09)
	{
		document.getElementById('maxreached09').style.display="block";	
	}
	
	
	
}
