function taxrebatecalc() {
	var amount = 0;
	$('#result').empty();
	$('span#gross_pay_error').empty();
	$('span#tax_paid_error').empty();

	var taxId = $('#taxYear').val();
	var taxdata = taxtable[taxId];

	var taxable_pay = $('#gross_pay').val();
	var tax_paid = $('#tax_paid').val();

	var valid = true;
	if(taxable_pay.length == 0) {
		$('span#gross_pay_error').text('You must enter an amount.');
		return false;
	}
	if(isNaN(taxable_pay)) {
		$('span#gross_pay_error').text('Not a valid amount.');
		return false;
	}
	
	if(tax_paid.length == 0) {
		$('span#tax_paid_error').text('You must enter an amount.');
		return false;
	}
	if(isNaN(tax_paid)) {
		$('span#tax_paid_error').text('Not a valid amount.');
		return false;
	}

	var total_tax_due = 0;
	if($('#married').val() == 1)
               taxable_pay = taxable_pay < taxdata.married_allowance ? 0 : taxable_pay - taxdata.married_allowance;
	else
               taxable_pay = taxable_pay < taxdata.single_allowance ? 0 : taxable_pay - taxdata.single_allowance;
	
	if(taxable_pay >  taxdata.st_tax_amount) {
		total_tax_due += taxdata.st_tax_amount * (taxdata.st_tax_rate / 100);
		taxable_pay -= taxdata.st_tax_amount;
	}
	else {
		total_tax_due += taxable_pay * (taxdata.st_tax_rate / 100);
		taxable_pay = 0;
	}

	if(taxable_pay > taxdata.nd_tax_amount - taxdata.st_tax_amount) {
		total_tax_due += (taxdata.nd_tax_amount - taxdata.st_tax_amount) * (taxdata.nd_tax_rate / 100);
		taxable_pay -= taxdata.nd_tax_amount - taxdata.st_tax_amount;
	}
	else {
		total_tax_due += taxable_pay * (taxdata.nd_tax_rate / 100);
		taxable_pay = 0;
	}

	if(taxable_pay > 0) {
		total_tax_due += taxable_pay * (taxdata.remaining_rate / 100);
	}
	
	amount = $('#tax_paid').val() - total_tax_due;
	amount = Math.round(amount);
	
	if(amount > 0) {
		$('#result').html('<span class="over">Overpaid by &pound;' + amount + '</span>');
	}
	else if(amount < 0){
		$('#result').html('<span class="under">Underpaid by &pound;' + Math.abs(amount) + '</span>');
	}
	else {
		$('#result').html('<span class="correct">Correct Amount</span>');
	}
}

