$().ready(function(){
	$(".backCalc").click(function(){
		$("p#pararesults2").hide();
		$("#calcFrm").show();
		return false;
	});
	
// compute
	$("input#calculate, input#calculate2").click(function(){
		if ( isNaN($("#hourlyRate").val()) || isNaN($("#hoursWeek").val()) ) {
			alert("Invalid data! Please use only numbers!");
		} else {
			$("p#pararesults").show();
			$("p#pararesults2").show();
			$("p#pararesults2").next().hide();
			$("p#pararesults").next().hide();
			$("#calcFrm").hide();
			var hourlyRate = $("#hourlyRate").val();
            if(!hourlyRate)
                hourlyRate = $("#hourlyRateaa").val();
			var hoursPerWeek = $("#hoursWeek").val();
            if(!hoursPerWeek)
                hoursPerWeek = $("#hoursWeek2").val();
			var gross = hourlyRate * hoursPerWeek;
			var subsistence = -26 * 5;
			var travel = -gross * 0.15;
			var deminimis;
			var t_deminimis = gross + subsistence + travel;
			if (t_deminimis / hoursPerWeek < 6) {
				deminimis = hoursPerWeek * 6;
			} else {
				deminimis = t_deminimis;
			}
			var employers_ni = (+(deminimis - 100) / 1.128) * 0.128;
			var taxable;
			if (employers_ni > 0) {
				taxable = deminimis - employers_ni;
			} else {
				taxable = deminimis;
			}
			var const_allowances = 5225;
			var const_lower_rate_band = 2230;
			var const_basic_rate_band = 32370;
			var e11 = taxable * 52;
			var e17;
			if ((e11 - const_allowances) > const_lower_rate_band) {
				e17 = const_lower_rate_band * 0.10;
			} else {
				e17 = (e11 - const_allowances) * 0.10;
			}
			var e18 = e17 < 0 ? -e17 : 0;
			var e20;
			if ((const_allowances + const_lower_rate_band) > e11) {
				e20 = 0;
			} else {
				if ((e11 - (const_allowances + const_lower_rate_band)) > const_basic_rate_band){
					e20 = const_basic_rate_band * 0.22;
				} else {
					e20 = (e11-(const_allowances + const_lower_rate_band))*0.22;
				}
			}
			var e23;
			if (e11 > (const_allowances + const_lower_rate_band + const_basic_rate_band))
			{
				e23 = (e11 - (const_allowances + const_lower_rate_band + const_basic_rate_band)) * 0.40;
			} else {
				e23 = 0;
			}
			var bal_of_general_income;
			var bal_of_basic_rate_band;
			var bal_of_brb_after_interest;
			if (e11 > (const_allowances + const_lower_rate_band + const_basic_rate_band))
			{
				bal_of_general_income = e11 - (const_allowances + const_lower_rate_band + const_basic_rate_band);
			} else {
				bal_of_general_income = 0;
			}
			if ((const_allowances + const_lower_rate_band + const_basic_rate_band) > e11)
			{
				bal_of_basic_rate_band = (const_allowances + const_lower_rate_band + const_basic_rate_band) - e11;
			} else {
				bal_of_basic_rate_band = 0;
			}
			if ((const_allowances + const_lower_rate_band + const_basic_rate_band) > (e11)) {
				bal_of_brb_after_interest = (const_allowances + const_lower_rate_band + const_basic_rate_band) - e11;
			} else {
				bal_of_brb_after_interest = 0;
			}
			var total_per_anum = e17 + e18 + e20 + e23;
			var total_per_month = total_per_anum / 12;
			var total_per_week = total_per_anum / 52;
			var paye = -total_per_anum / 52;
			var h40 = e11;
			var h41 = h40 / 12;
			var h42 = h40 / 52;
			var c44 = 100;
			var d44 = 0;
			var e44 = c44 * 52;
			var h44 = h42 < d44 ? 0 : c44 * d44;
			var h46 = h42 < 670 && h42 > 100 ? (h42 - c44) * 0.11 : 0;
			var h47 = h42 >= 670 ? (((670 - 100.01) * 0.11) + (h46 - 670) * 0.01) : 0;
			var ees_ni = -(h44+h46+h47);
			var net_pay = + taxable + paye + ees_ni;
			var expenses = + gross - deminimis;
			var take_home = net_pay + expenses;
			$("#home2").html(take_home.toFixed(2));
			$("#home22").html(take_home.toFixed(2));
			$("#hourlyRate2").html(hourlyRate);
			$("#hourlyRate22").html(hourlyRate);
			$("#hoursWeekDisplay").html(hoursPerWeek);
			$("#hoursWeekDisplay2").html(hoursPerWeek);
		}
		return false;
	});
    
    $("input#calculate2").click(function(){
        $("#hourlyRate").val($("#hourlyRateaa").val());
        $("#hoursWeek").val($("#hoursWeek2").val());
        $("input#calculate").click();
        return false;
    });
	
	
// fade
	$('.fade').innerfade({

		speed: 'slow',

		timeout: 12000,

		type: 'sequence'

	});
	
// toggle next paragraph
$('a.toggle').click(function(){
	$('p.'+$(this).attr('rel')+':visible').hide('slow');
	$('p.'+$(this).attr('rel')+':hidden').show('slow');
	return false;
});	

$(".magicbox p").hide();

$(".magicbox span").click(function(){//toggle

	$("p", $(this).parent()).slideToggle("slow");

	//$("p:visible", $(this).parent()).hide("slow");

}).css("cursor","pointer");



$(".magicbox a.close").click(function(){

$("p",$(this).parent().parent()).slideUp("slow");

return false;

});
});