function calculate()
{
	var year 		= $('#taxyear').val();
	var married		=  $("input[name='married']:checked").val();	
	var age 		=  $('#age').val();
	var spouseage 	=  $('#spouseage').val();	
	//set the limit and all the allowances
	var limit = 0;
	var pa = 0; 		//minimum personal allowance for the year
	var paa = 0; 		//aged related personal allowance for the year
	var pae= 0;		//higher aged related personal allowance for the year
	var ma= 0; 		//minimum married allowance, if qualify
	var maa= 0;		//aged related married allowance, if qualify
	var mae = 0;		//higher aged related married allowance, if qualify
    var totalIncome  = 0;
	var pay1 	 = $('#pay1').val();
	pay1 = to_num(pay1);
	var pay2 	 = $('#pay2').val();
	pay2 = to_num(pay2);
	var pension	 = $('#pension').val();
	pension = to_num(pension);
	var other	 = $('#other_inc').val();
	other = to_num(other);
	var expenses = $('#expenses').val();
	expenses = to_num(expenses);
	var psubs = $('#psubs').val();
	psubs = to_num(psubs);
	
	var msg = "<p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span>Please check the following errors:-<ul>";
	var res = true;
	
	if(year.length == 0)
	{
		msg += "<li>Please select the taxable year.</li>";
		res = false;
	}
	if(age.length == 0)
	{
		msg += "<li>Please select the year you were born.</li>";
		res = false;		
	}
	if(married.length == 0 || married == undefined)
	{
		msg += "<li>Please tell us whether you are married or not?</li>";
		res = false;
	}
	if(married == "yes")
	{
		if(spouseage.length == 0)
		{
			msg += "<li>Please select the year your spouse were born.</li>";
			res = false;					
		}
	}
	if(pay1 == 0 && pay2 == 0 && pension == 0  && other == 0)
	{
		msg += "<li>Please provide at least one taxable income.</li>";
		res = false;		
	}
	msg += "</ul></p>";
	if(res == false)
	{
		$('#dialog').html(msg);
		$('#dialog').dialog('open');
		return false;
	}	
	else
	{
			switch (year){
			case "2010":
				limit = 22900;
				pa = 6475;
				paa = 9490;
				pae = 9640;
				ma = 2670;//minimum marriage allowance
				maa = 6965;
				mae = 6965;	
				break;
			case "2009":
				limit = 21800;
				pa = 6035;
				paa = 9030;
				pae = 9180;
				ma = 2540;
				maa = 6535;
				mae = 6625;
				break;
			case "2008":
				 limit = 20900;
				 pa = 5225;
				 paa = 7550;
				 pae = 7690;
				 ma = 2440;
				 maa = 6285;
				 mae = 6365;
				break;
			case "2007":
				 limit = 20100;
				 pa = 5035;
				 paa = 7280;
				 pae = 7420;
				 ma = 2350;
				 maa = 6065;
				 mae = 6135;
				break;
			case "2006":
				 limit = 19500;
				 pa = 4895;
				 paa = 7090;
				 pae = 7220;
				 ma = 2280;
				 maa = 5905;
				 mae = 5975;
				break;
			case "2005":
				 limit = 18900;
				 pa = 4745;
				 paa = 6830;
				 pae = 6950;
				 ma = 2210;
				 maa = 5725;
				 mae = 5795;
				break;
			case "2004":
				 limit = 18300;
				 pa = 4615;
				 paa = 6610;
				 pae = 6720;
				 ma = 2150;
				 maa = 5565;
				 mae = 5635;
				break;			
			}
	
	  totalIncome =  pay1 + pay2 + pension + other - expenses - psubs ;
		
		//determine which allowances we are going to use, and set the personal and married accordingly
		var ageCalc 			=  age; //set a temporary version of the age so we can mess about with it in the calcs
		var spouseageCalc		=  spouseage; //and a temporary version of the spouseage so we can mess about with it in the calcs
		var p = 0 ;//personal allowance
		var m = 0; //married allowance, which is zero by default
		var excess = 0;
		var reduction = 0;
		var netReduction = 0; 
		var pp = 0;
		
		//work out how old this person was in the year we are looking at
		//we will take off one from the age for each year back. ie
		//in 2003 the person was two years younger than now. 
		//every year it is updated, we need to add an additional switch value, and change the minus amounts. 
		//the current tax year we don't touch) 
		
		switch (year){

			case "2009":
				 ageCalc -= 1;
				 spouseageCalc -= 1;
				 break;
			case "2008":
				 ageCalc -= 2;
				 spouseageCalc -= 2;
				 break;
			case "2007":
				 ageCalc -= 3;
				 spouseageCalc -= 3;
				break;
			case "2006":
				 ageCalc -= 4;
	
				 spouseageCalc -= 4;
				break;
			case "2005":
				 ageCalc -= 5;
				 spouseageCalc -= 5;
				break;
			case "2004":
				 ageCalc -= 6;
				 spouseageCalc -= 6;
				break;
			case "2003":
				 ageCalc -= 7;
				 spouseageCalc -= 7;
				break;
		}
		
		//get the relevant age allowance band using this age.
		var client_check = (year-1935)- ageCalc;
		if(client_check > 0)
		{
			//they are not entitled to marriage allowance
			ageBand = "4";
		}
		ageBand = "1";
		if ( ageCalc >= "65")  ageBand = "2";
		if ( ageCalc >= "75")  ageBand = "3";
		
		
		
		
		var spouse_check = (year-1935)-spouseageCalc;
		var year_check = year-1935;
		var spouseageBand ="0";
		if(spouse_check <= 0)
		{
			//not entitled to marriage allowance unless client is over 65 in 05/04/2002
			spouseageBand = "1";
			if ( spouseageCalc >= "65") spouseageBand = "2";	
			if ( spouseageCalc >= "75") spouseageBand = "3";
		}
		
		if(spouse_check > 0)
		{
			//they are not entitled to marriage allowance
			spouseageBand = "4";
		}
		
		switch (ageBand)
		{
				case "1":
						 p =  pa;
						if ( married == "yes" && spouseageBand == "2")  m =  maa;
						if ( married == "yes" && spouseageBand == "3")  m =  mae;
						break;
				case "2":					
						 p =  paa;
						 m =  maa;
						if (married == "yes" && spouseageBand == "3")  m =  mae;
						break;
				case "3":
						 p =  pae;
						if ( married == "yes")  m =  mae;
						break;
		}
		//add up all the allowances
		 var totalAllowances =  p +  m;
		//work out the excess of income over the annual limit
		if ( totalIncome >=  limit) 
			{
			//the excess is the total income less the annual limit
			  excess =  totalIncome -  limit;
			 //divide this by two to get the total allowances reduction
				 reduction =  excess / 2;
			 }		 
		
		//work out personal allowance first, taking the reduction away from the allowance
		//net off the reduction, taking the pa used away from the total reduction
			if ( reduction <=  p -  pa)
			{
			 pp =  p;
			 p =  p -  reduction;
			 netReduction =  reduction - ( pp -  p);
			}
			else 
			{
			 pp =  p;
			 p =  pa;
			 netReduction =  reduction - ( pp -  p);
			}
		//work out married allowance using the rest of net reduction left, but only if it is available
		//and also do some adjustments if both partners are in band one
			switch (married)
		{
				case "yes":
				 m =  m -  netReduction;
				if 	( m <  ma)  m =  ma;
				//if ( ageBand == "1" &&  spouseageBand == "1")  m = "0";
				//if ( ageBand == "2" &&  spouseageBand == "2")  m = "0";
				if ( spouseageBand == "4" && (client_check > 0))  m = "0";
				break;
				case("no"):
				 m = 0;
				break;
				default:
				 m = 0;
				break;			
		}
		$('#res_pa').html(to_amount(p));
		$('#res_ma').html(to_amount(m));		

		$('#result_area').show('slow');				
		return res;
	}
} // function calculate ends here.. 