// JavaScript Document
$(document).ready(function(){
	$("#special").change(function(){
		change_tc();
	});
	$("#code").change(function(){
		change_spec();
	});
	$("#suffix").change(function(){
		change_spec();
	});
	$("#period").change(function(){
		if($("#period").val() > 12){
			$('#period').val("");
		}
	});
});

function change_tc(){
	$('#code').val("");
	$('#suffix').val("");
}
function change_spec(){
	$('#special').val("");
}

function calculate () {
	var pay = $('#pay').val();
	var thispay = $('#thisPay').val();
	var code = $('#code').val();
	var suffix = $('#suffix').val();
	var wk1 = $("input[name='wk1']:checked").val();
	var special = $('#special').val();
	var period = $('#period').val();
	var freq = $("input[name='frequency']:checked").val();
	
	var opinion_msg = "";
	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(pay.length == 0 || pay.length == null)
	{
		msg += "<li>Please enter your taxable pay to date.</li>";
		res = false;
	}
	if(thispay.length == 0 || thispay.length == null)
	{
		msg += "<li>Please enter your taxable pay this period.</li>";
		res = false;
	}
	if(special.length == 0 && (code.length == 0 || suffix.length == 0))
	{
		msg += "<li>If you have a normal taxcode (i.e. 647L) please fill it in the taxcode area, otherwise please choose your special taxcode from the list provided.</li>";
		res = false;
	}
	if((code.length != 0 && suffix.length == 0) || (code.length == 0 && suffix.length != 0))
	{
		msg += "<li>You need to fill in your taxcode fully before you can continue.</li>";
		res = false;
	}
	if(period.length == 0 || period.length == null)
	{
		msg += "<li>Please enter your taxable pay this period.</li>";
		res = false;
	}
	if(freq == undefined)
	{
		msg += "<li>Please select how often you are paid.</li>";
		res = false;
	}

	msg += "</ul></p>";
	if(res == false)
	{
		$('#dialog').html(msg);
		$('#dialog').dialog('open');
		return false;
	}
	else
	{
		var taxFree = (code * 10) + 9;			
		if (wk1 == "yes") 
		{
			//change this to period one if this is week one, but set a temp variable so we can send back the right value to the page
			var tempPeriod = period;
			period  = 1; 
		}
		
		//find out if weekly or monthly paid. if there is no value, we need to set it to 1, so the page does not die with a "divide by zero" error
		if(freq == undefined)
		{
			freq = 1;
		}
		//rate bands
		var lrBand = 0;  						
		var brBand = 37400; 
		
		//work out tax free pay this period 
		var periodTaxFree = Math.round((period / freq * taxFree) + 0.499);			
					
		//and from there the net taxable this period
		var netPay = 0;
		netPay = Math.round(pay - periodTaxFree - 0.499);						

		//but redo it if this is week 1.
		//net pay is the taxable part of the pay this period
		if(wk1 == "yes")
		{
			netPay 	= thispay - periodTaxFree; 
		}
		
		//adjust for K Codes
		if (suffix == "K")
		{
			//first of all, make the tax free element zero
			taxFree = 0;
			//then add on the numeric part of the code to the taxable income in this period
			//but only after apportioning it for the period we are in
			var kTax = ((code * 10) + 9) / freq;
			/*alert((thispay) + (kTax));
			var kpay = (thispay*1) + (kTax*1);*/
			netPay = Math.round((thispay*1) + (kTax*1) + 0.499);
			//change to period one for the calcs,
			//but set a temp variable so we can send back the right value to the page
			
			tempPeriod = period;
			period  = 1; 
		}
					
		//work out proportion of rate bands available
		var lrAvail =  Math.round((period / freq * lrBand)+0.499);
		var brAvail =  Math.round((period / freq * brBand)+0.499);
			
		//work out how much tax is due
		//first, amount of income taxable at lower rate, lower of taxable income or lower rate available
		var lrTaxable = 0;
		if  (lrAvail < netPay) 
		{
			lrTaxable = lrAvail;
		} 
		else 
		{
			lrTaxable = netPay;
		}
		//and tax on that, at 10%
		lrTaxDue = Math.round(((lrTaxable * 0.1)*100)/100);
		
		//amount of income taxable at basic rate
		//nothing if all income taxable at 10%, or the lesser of the br available 


		//or the remainder of the taxable income
		var brTaxable = 0;
		if (lrTaxable < lrAvail) 
		{
			brTaxable = 0;
		} 
		else if ((netPay - lrTaxable) < brAvail) 
		{
			brTaxable = netPay - lrTaxable;
		} 
		else 
		{
			brTaxable = brAvail;
		}
		//and tax on that, at 22%
		var brTaxDue = Math.round(((brTaxable * 0.20)*100)/100);
		
		//and finally the amount taxable at 40%, if any
		//this is the taxable pay, less the lower and basic rate bands, but zero if this is negative.
		var hrTaxable = netPay - lrTaxable - brTaxable;
		if (hrTaxable < 0) 
		{
			hrTaxable = 0;
		}
		var hrTaxDue = Math.round(((hrTaxable * 0.4)*100)/100);
		
		//now we should have some results.
		//var result = number_format(lrTaxDue + brTaxDue + hrTaxDue,2);
		var result = Math.round(((lrTaxDue + brTaxDue + hrTaxDue)*100)/100);
		
		//redo the amount if a special code has been chosen		
		if (special == "br"){result = Math.round(((thispay * 0.20)*100)/100);}
		if (special == "d0"){result = Math.round(((thispay * 0.4)*100)/100);}
		if (special == "nt"){result = 0;}
		
		//reset the period signal because it may have been changed to 1 for the calcs
		if (tempPeriod){period = tempPeriod;}
		
		//and make it so the tax due is never less than zero
		if (result < 0){result = 0;}
		var kresult = thispay/2;
		
		
		if (wk1 == "yes" || suffix == "K"){
			if (result > kresult){
				opinion_msg = "The calculated result is: <strong>&pound;"+result+"</strong>.<br />The maximum tax that should be deducted in this pay period is: <strong>&pound;"+kresult+"</strong>.";	
			}
			else{
				opinion_msg = "It is only possible to calculate the tax paid in this period. <br />This should be : <strong>&pound;"+result+"</strong>.";
			}
		}
		else{
			opinion_msg = "The tax paid to date should be : <strong>&pound;"+result+"</strong>.";
		}
		
		$('#opinion').html(opinion_msg);
		$('#result_area').show('slow');		
	}	
	
}
