function calculateSalary(frm) {
    var result_2010 = calculate2010(frm);
    var result_2011 = calculate2011(frm);

    //build output
	var out = '<h2>Summary</h2><table class="grid"><tr><th>&nbsp;</th><th width="100">Yearly</th><th width="100">Monthly</th><th width="100">Weekly</th></tr>';
	out += '<tr><td class="smlDetail"><strong>Total Pay</strong></td>';
	out += '<td>' + result_2011.grossincome.toFixed(2) + '</td>';
	out += '<td>' + result_2011.monthlygrossincome.toFixed(2) + '</td>';
	out += '<td>' + result_2011.weeklygrossincome.toFixed(2) + '</td></tr>';
	out += '<tr><td><strong>Tax Free Allowance</strong></td>';
	out += '<td>' + result_2011.taxfreeallowance.toFixed(2) + '</td>';
	out += '<td>' + result_2011.monthlytaxfreeallowance.toFixed(2) + '</td>';
	out += '<td>' + result_2011.weeklytaxfreeallowance.toFixed(2) + '</td></tr>';
	out += '<tr><td><strong>Tax</strong></td>';
	out += '<td>' + result_2011.tax.toFixed(2) + '</td>';
	out += '<td>' + result_2011.monthlytax.toFixed(2) + '</td>';
	out += '<td>' + result_2011.weeklytax.toFixed(2) + '</td></tr>';

	if( result_2011.studentloan ) {
		out += '<tr><td><strong>Student Loan</strong></td>';
		out += '<td>' + result_2011.repayment.toFixed(2) + '</td>';
		out += '<td>' + result_2011.monthlyrepayment.toFixed(2) + '</td>';
		out += '<td>' + result_2011.weeklyrepayment.toFixed(2) + '</td></tr>';
	}

	if( result_2011.payni ) {
		out += '<tr><td><strong>National Insurance</strong></td>';
        out += '<td>' + result_2011.ni.toFixed(2) + '</td>';
        out += '<td>' + result_2011.monthlyni.toFixed(2) + '</td>';
        out += '<td>' + result_2011.weeklyni.toFixed(2) + '</td></tr>';
	}

	out += '<tr><td><strong>Total Deductions</strong></td>';
	out += '<td>' + result_2011.totaldeductions.toFixed(2) + '</td>';
	out += '<td>' + result_2011.monthlytotaldeductions.toFixed(2) + '</td>';
	out += '<td>' + result_2011.weeklytotaldeductions.toFixed(2) + '</td></tr>';
    
    var yearly = (result_2011.yearlyearnings - result_2010.yearlyearnings);
    var monthly = (result_2011.monthlyearnings - result_2010.monthlyearnings);
    var weekly = (result_2011.weeklyearnings - result_2010.weeklyearnings);
    
    var yearly_style = yearly < 0 ? ' style="color: red;"' : '';
    var monthly_style = monthly < 0 ? ' style="color: red;"' : '';
    var weekly_style = weekly < 0 ? ' style="color: red;"' : '';
    
    out += '<tr><td style="font-size: 9pt;"><strong>Difference from 2010/11 tax year</strong></td>';
    out += '<td'+yearly_style+'><strong>&pound;' + yearly.toFixed(2) + '</strong></td>';
    out += '<td'+monthly_style+'><strong>&pound;' + monthly.toFixed(2) + '</strong></td>';
    out += '<td'+weekly_style+'><strong>&pound;' + weekly.toFixed(2) + '</strong></td></tr>';
    
	out += '<tr class="highlightRow"><td><strong>Take home wage</strong></td>';
	out += '<td>&pound;' + result_2011.yearlyearnings.toFixed(2) + '</td>';
	out += '<td>&pound;' + result_2011.monthlyearnings.toFixed(2) + '</td>';
	out += '<td>&pound;' + result_2011.weeklyearnings.toFixed(2) + '</td></tr>';
    out += '</table>';

	document.getElementById('salary_calculator_results').innerHTML = out;
}

function calculate2010(frm) {
    var grossincome = parseInt(frm.income.value);
    var studentloan = frm.studentloan.checked;
    var payni = !(frm.payni.checked);

    var repayment = 0;
    var ni = 0;

    var taxfreeallowance = 6475;
    var taxband1 = 43875;

    // in the 20% band the limit you have to pay in tax is �7,200
    var taxband1_limit = 7480;

    var ni_limit = 4197.60;
    var nifreeallowance = 5715;
    var niband1 = 43875;
    var tax = 0;

    if(grossincome <= taxfreeallowance) {
        tax = 0;
    }

    if (grossincome > 150000) {

        basicAmt = 37400; // first 37,400 goes to basic bracket
        higherAmt = 112600; // next 112,600 goes to second bracket
        additionalAmt = grossincome - 150000; // anything over 150,000 goes into highest bracket

        tax = (basicAmt * 0.20) + (higherAmt * 0.40) + (additionalAmt * 0.50);

    }

    if (grossincome > 100000 && grossincome <= 150000) {
        basicAmt = 37400; // first 37,400 goes to basic bracket
        higherAmt = grossincome - basicAmt;

        // Personal Allowance reduced by 1 for ever 2 of income above 100,000
        taxfreeallowance = taxfreeallowance - (grossincome - 100000)/2;
        if (taxfreeallowance < 0) taxfreeallowance = 0;
        tax = (basicAmt * 0.20) + ((higherAmt-taxfreeallowance) * 0.40);
    }

    if (grossincome <= 100000) {

        if( (grossincome >= taxfreeallowance) && ((grossincome-taxfreeallowance) <= taxband1) ) {

            tax = (grossincome-taxfreeallowance) * 0.2;

            //writeoutput("20% band");
            if( tax >= taxband1_limit ) {
                tax = taxband1_limit;
            }
        }

        if( (grossincome) > taxband1 ) {
            //alert("40% band");
            tax = ( (grossincome - taxfreeallowance) - (taxband1-taxfreeallowance) ) * 0.4 + taxband1_limit;
        }
    }

    //National Insurance
    if( payni ) {

        if( grossincome <= nifreeallowance  ) {
            ni = 0;
        }

        if( (grossincome >= nifreeallowance) && ((grossincome - nifreeallowance) <= niband1) ) {
            //alert("11% band");
            ni = (grossincome - nifreeallowance) * 0.11;

            if( ni >= ni_limit ) {
                ni = ni_limit;
            }
        }

        if( grossincome > niband1 ) {

            //alert("upper earnings limit");
            ni = ( (grossincome - nifreeallowance) - (niband1-nifreeallowance)) * 0.01 + ni_limit;
        }
    }

    if( studentloan ) {
        //Student Loan
        var studentloanallowance = 15000;

        if( grossincome <= studentloanallowance ) {
            repayment = 0;
        } else {
            repayment = (grossincome - studentloanallowance) * 0.09;
        }
    }

    var yearlyearnings = ((((grossincome-taxfreeallowance) - tax) + taxfreeallowance) - ni) - repayment;
    //yearlyearnings = yearlyearnings - ((grossincome-nifreeallowance) - ni) + nifreeallowance;

    var monthlyearnings = yearlyearnings / 12;
    var weeklyearnings = yearlyearnings / 52;

    var monthlytax = tax / 12;
    var weeklytax = tax / 52;

    var monthlyni = ni / 12;
    var weeklyni = ni / 52;

    var monthlygrossincome = grossincome / 12;
    var weeklygrossincome = grossincome / 52;

    var monthlytaxfreeallowance = taxfreeallowance / 12;
    var weeklytaxfreeallowance = taxfreeallowance / 52;

    var totaldeductions = tax + ni + repayment;
    var monthlytotaldeductions = totaldeductions / 12;
    var weeklytotaldeductions = totaldeductions / 52;

    var monthlyrepayment = repayment / 12;
    var weeklyrepayment = repayment / 52;
    
    return {
        'grossincome'               : grossincome,
        'monthlygrossincome'        : monthlygrossincome,
        'weeklygrossincome'         : weeklygrossincome,
        'taxfreeallowance'          : taxfreeallowance,
        'monthlytaxfreeallowance'   : monthlytaxfreeallowance,
        'weeklytaxfreeallowance'    : weeklytaxfreeallowance,
        'tax'                       : tax,
        'monthlytax'                : monthlytax,
        'weeklytax'                 : weeklytax,
        'repayment'                 : repayment,
        'monthlyrepayment'          : monthlyrepayment,
        'weeklyrepayment'           : weeklyrepayment,
        'ni'                        : ni,
        'monthlyni'                 : monthlyni,
        'weeklyni'                  : weeklyni,
        'totaldeductions'           : totaldeductions,
        'weeklytotaldeductions'     : weeklytotaldeductions,
        'yearlyearnings'            : yearlyearnings,
        'monthlyearnings'           : monthlyearnings,
        'weeklyearnings'            : weeklyearnings,
        'studentloan'               : studentloan,
        'payni'                     : payni,
        'monthlytotaldeductions'    : monthlytotaldeductions
    };
}

function calculate2011(frm) {
    var grossincome = parseInt(frm.income.value);
    var studentloan = frm.studentloan.checked;
    var payni = !(frm.payni.checked);

    var repayment = 0;
    var ni = 0;

    var taxfreeallowance = 7475;
    var taxband1 = 42475;

    // in the 20% band the limit you have to pay in tax is �7,000
    var taxband1_limit = 7000;

    var ni_limit = 4230;
    var nifreeallowance = 7225;
    var niband1 = 42475;
    var tax = 0;

    if(grossincome <= taxfreeallowance) {
        tax = 0;
    }

    if (grossincome > 150000) {

        basicAmt = 35000; // first 35,000 goes to basic bracket
        higherAmt = 115000; // next 115,000 goes to second bracket
        additionalAmt = grossincome - 150000; // anything over 150,000 goes into highest bracket

        tax = (basicAmt * 0.20) + (higherAmt * 0.40) + (additionalAmt * 0.50);

    }

    if (grossincome > 100000 && grossincome <= 150000) {
        basicAmt = 35000; // first 35,000 goes to basic bracket
        higherAmt = grossincome - basicAmt;

        // Personal Allowance reduced by 1 for ever 2 of income above 100,000
        taxfreeallowance = taxfreeallowance - (grossincome - 100000)/2;
        if (taxfreeallowance < 0) taxfreeallowance = 0;
        tax = (basicAmt * 0.20) + ((higherAmt-taxfreeallowance) * 0.40);
    }

    if (grossincome <= 100000) {

        if( (grossincome >= taxfreeallowance) && ((grossincome-taxfreeallowance) <= taxband1) ) {

            tax = (grossincome-taxfreeallowance) * 0.2;

            //writeoutput("20% band");
            if( tax >= taxband1_limit ) {
                tax = taxband1_limit;
            }
        }

        if( (grossincome) > taxband1 ) {
            //alert("40% band");
            tax = ( (grossincome - taxfreeallowance) - (taxband1-taxfreeallowance) ) * 0.4 + taxband1_limit;
        }
    }

    //National Insurance
    if( payni ) {

        if( grossincome <= nifreeallowance  ) {
            ni = 0;
        }

        if( (grossincome >= nifreeallowance) && ((grossincome - nifreeallowance) <= niband1) ) {
            //alert("12% band");
            ni = (grossincome - nifreeallowance) * 0.12;

            if( ni >= ni_limit ) {
                ni = ni_limit;
            }
        }

        if( grossincome > niband1 ) {

            //alert("upper earnings limit");
            ni = ( (grossincome - nifreeallowance) - (niband1-nifreeallowance)) * 0.02 + ni_limit;
        }
    }

    if( studentloan ) {
        //Student Loan
        var studentloanallowance = 15000;

        if( grossincome <= studentloanallowance ) {
            repayment = 0;
        } else {
            repayment = (grossincome - studentloanallowance) * 0.09;
        }
    }

    var yearlyearnings = ((((grossincome-taxfreeallowance) - tax) + taxfreeallowance) - ni) - repayment;
    //yearlyearnings = yearlyearnings - ((grossincome-nifreeallowance) - ni) + nifreeallowance;

    var monthlyearnings = yearlyearnings / 12;
    var weeklyearnings = yearlyearnings / 52;

    var monthlytax = tax / 12;
    var weeklytax = tax / 52;

    var monthlyni = ni / 12;
    var weeklyni = ni / 52;

    var monthlygrossincome = grossincome / 12;
    var weeklygrossincome = grossincome / 52;

    var monthlytaxfreeallowance = taxfreeallowance / 12;
    var weeklytaxfreeallowance = taxfreeallowance / 52;

    var totaldeductions = tax + ni + repayment;
    var monthlytotaldeductions = totaldeductions / 12;
    var weeklytotaldeductions = totaldeductions / 52;

    var monthlyrepayment = repayment / 12;
    var weeklyrepayment = repayment / 52;
    
    return {
        'grossincome'               : grossincome,
        'monthlygrossincome'        : monthlygrossincome,
        'weeklygrossincome'         : weeklygrossincome,
        'taxfreeallowance'          : taxfreeallowance,
        'monthlytaxfreeallowance'   : monthlytaxfreeallowance,
        'weeklytaxfreeallowance'    : weeklytaxfreeallowance,
        'tax'                       : tax,
        'monthlytax'                : monthlytax,
        'weeklytax'                 : weeklytax,
        'repayment'                 : repayment,
        'monthlyrepayment'          : monthlyrepayment,
        'weeklyrepayment'           : weeklyrepayment,
        'ni'                        : ni,
        'monthlyni'                 : monthlyni,
        'weeklyni'                  : weeklyni,
        'totaldeductions'           : totaldeductions,
        'weeklytotaldeductions'     : weeklytotaldeductions,
        'yearlyearnings'            : yearlyearnings,
        'monthlyearnings'           : monthlyearnings,
        'weeklyearnings'            : weeklyearnings,
        'studentloan'               : studentloan,
        'payni'                     : payni,
        'monthlytotaldeductions'    : monthlytotaldeductions
    };
}
