//  Filename: calc_pay.js  //  Function: A PAYE and NI calculator for monthly salary  //  Author: Barry Hood -- design@delinear.co.uk  //  Date: 2005.05.06
function payroll_calculator(form_name)
{
	var monthly_gross_pay=0; 
	var numeric_tax_code=0; 
	var special_tax_code=0;
	monthly_gross_pay=strip_common_elements(document.getElementById(form_name).monthly_gross_pay.value);
	numeric_tax_code=strip_common_elements(document.getElementById(form_name).numeric_tax_code.value);
	special_tax_code=document.getElementById(form_name).special_tax_code.value;
	var gross_pay=0;
	var net_pay=0;
	var paye_deduction=0;
	var ni_employee_deduction=0;
	var monthly_pay=0;
	var monthly_tax_amount=0;
	var code_k_limit=0;
	var code_k_limit_multiplier=50;
	var lower_tax_threshold=20;  
	var middle_tax_threshold=20; 
	var higher_tax_threshold=40;
	var lower_tax_limit=2230/12;
	var middle_tax_limit=32770/12; 
	var combined_tax_limit=(lower_tax_limit+middle_tax_limit);
	var lower_monthly_gp_threshold=12;
	var lower_monthly_gp_limit=602.33; 
	var higher_monthly_gp_limit=3540.33;
	var lower_tax_amount=((lower_tax_limit/100)*lower_tax_threshold);
	var middle_tax_amount=((middle_tax_limit/100)*middle_tax_threshold);
	numeric_tax_code=(!numeric_tax_code?"489":numeric_tax_code);
	var error_array=new Array;
	var error_count=0;
	
	if(!check_number_valid(monthly_gross_pay))
	{
		error_array[error_count]="\tThe gross monthly pay";
		error_count++;
	}
	
	if(!check_number_valid(numeric_tax_code))
	{
			error_array[error_count]="\tThe numeric part of the tax code";
			error_count++;
	}
			
	if(error_array.length>0)
	{
		var error_message;
		error_message=(error_array.length>1)?"The following fields are incorrect. Please check the fields and try again:\n\n":"The following field is incorrect. Please check the fields and try again:\n\n";
			
		for(i=0;i<error_array.length;i++) 
		{
			error_message+=error_array[i];error_message+="\n";
		}

		document.getElementById(form_name).output_gross_monthly_pay.value='';
		document.getElementById(form_name).tax_amount.value='';
		document.getElementById(form_name).ni_employee_deduction.value='';
		document.getElementById(form_name).net_pay.value='';
		
		alert(error_message);
		return false;
	}
	
	monthly_tax_amount=parseFloat(numeric_tax_code);
	monthly_tax_amount=(((10*monthly_tax_amount)+9)/12);
	
	if(special_tax_code=="K")
	{
		monthly_pay=(parseFloat(monthly_gross_pay)+monthly_tax_amount);
		code_k_limit=((monthly_gross_pay/100)*code_k_limit_multiplier);
		paye_deduction=(monthly_pay<=lower_tax_limit?paye_deduction=(monthly_pay/100)*lower_tax_threshold:(monthly_pay<=combined_tax_limit?(((monthly_pay-lower_tax_limit)/100)*middle_tax_threshold)+lower_tax_amount:(((monthly_pay-combined_tax_limit)/100)*higher_tax_threshold)+middle_tax_amount+lower_tax_amount));
		paye_deduction=(paye_deduction>code_k_limit?code_k_limit:paye_deduction);
	}
	else if(special_tax_code=="NT")
	{
		paye_deduction=0;
	}else if(special_tax_code=="BR")
	{
		paye_deduction=((monthly_gross_pay/100)*middle_tax_threshold);
	}else if(special_tax_code=="D0")
	{
		paye_deduction=((monthly_gross_pay/100)*higher_tax_threshold);
	}else
	{
		monthly_pay=monthly_gross_pay;
		paye_deduction=(monthly_pay<=monthly_tax_amount?0:(monthly_pay<=(monthly_tax_amount+lower_tax_limit)?paye_deduction=((monthly_pay-monthly_tax_amount)/100)*lower_tax_threshold:(monthly_pay<=(monthly_tax_amount+combined_tax_limit)?paye_deduction=(((monthly_pay-monthly_tax_amount-lower_tax_limit)/100)*middle_tax_threshold)+lower_tax_amount:(((monthly_pay-monthly_tax_amount-combined_tax_limit)/100)*higher_tax_threshold)+middle_tax_amount+lower_tax_amount)));
		
		
	}
		
		ni_employee_deduction=(monthly_gross_pay<=lower_monthly_gp_limit?0:(monthly_gross_pay<=higher_monthly_gp_limit?((monthly_gross_pay-lower_monthly_gp_limit)/100)*lower_monthly_gp_threshold:(((higher_monthly_gp_limit-lower_monthly_gp_limit)/100)*lower_monthly_gp_threshold)+((monthly_gross_pay-higher_monthly_gp_limit)*0.02))); <!-- we made a change here before -->
		gross_pay=monthly_gross_pay;
		
		net_pay=(monthly_gross_pay-(paye_deduction+ni_employee_deduction));
		document.getElementById(form_name).output_gross_monthly_pay.value=format_number_output(gross_pay,2,',','£');
		document.getElementById(form_name).tax_amount.value=format_number_output(paye_deduction,2,',','£');
		document.getElementById(form_name).ni_employee_deduction.value=format_number_output(ni_employee_deduction,2,',','£');
		document.getElementById(form_name).net_pay.value=format_number_output(net_pay,2,',','£');
		return false;
}
