
$(document).ready(function(){
						   
	 
	
	updateheadline();

    //run funtion to update headlines
	setInterval("updateheadline();",100000);	
	
	
	$("#grossincome").focus().select();
	$('#grossincome').keypress(function(e) 
		{
			if (e.which == 13) 
			{
				$('#taxcalc').submit();
				return false;
			}
		});


	

var taxyeartext = $("select#taxyear option:selected").val();

$("#knowntaxedstuff").text(taxyeartext);




$("#submitbtn").hover(
      function () {
 		
		$('#submitbtn').attr("src", "images/submitbtnover2.png"); 
		
      }, 
      function () {
       
	    $('#submitbtn').attr("src", "images/submitbtnnorm2.png"); 
	   
      }
    );




$("#tbandicon").hover(
      function () {
 		
		$('#tbandicon').attr("src", "images/togglebandover.png");
		$('.bandbreakdown').show(); 
		
      }, 
      function () {
       
	    $('#tbandicon').attr("src", "images/togglebandnorm.png"); 
		$('.bandbreakdown').hide();
	   
      }
    );


$('#nistatushelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Please tick any of these options that apply to you.</h3>");
   }
});

$('#marriedlabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Married Couples over state pension age pay no National Insurance and may have a large tax free allowance. NOTE. Only tick this option if you or your spouse were born before 6 April 1935.</h3>");
   }
});



$('#blindlabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Additional allowances are provided for blind taxpayers.</h3>");
   }
});


$('#nonatinslabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Tick this box if you do not have to pay any National Insurance Contributions.</h3>");
   }
});


$('#cislabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>If you are CIS Subcontractor/Self Employed, tick this so we can include your CLASS 2 and 4 NI's from CIS/Self Employment work - for CIS these would not have been taken off your statements/vouchers but are due on your tax return.</h3>");
   }
});

$('#cishelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>If you are CIS Subcontractor/Self Employed, tick this so we can include your CLASS 2 and 4 NI's from CIS/Self Employment work - for CIS these would not have been taken off your statements/vouchers but are due on your tax return.</h3>");
   }
});


$('#studentloanhelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Did you take out a Student Loan?</h3><p>If paid via PAYE or Self Employed and declaring your earnings via a Self Assessment Tax Return you will pay 9% of any income above &pound;15000 toward repaying your loan.</p><p>Tick this box to include this calculation.</p>");
   }

});


$('#studentloanlabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Did you take out a Student Loan?</h3><p>If paid via PAYE or Self Employed and declaring your earnings via a Self Assessment Tax Return you will pay 9% of any income above &pound;15000 toward repaying your loan.</p><p>Tick this box to include this calculation.</p>");
   }
});





$('#agesexspahelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Age/Sex/State Pension Age</h3><p>Enter your sex and current age for us to calculate the correct allowances for you. Tick the SPA box if you are at, or will be at, your state pension age within the selected tax year. State pension ages are no longer the default 60 for females, 65 for males and are changing depending upon your date of birth over the next decade. Our state pension age calculator will take your sex and date of birth and let you know when you will reach state pension.</p>");
   }
});

 

$('#agesexspalabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Age/Sex/State Pension Age</h3><p>Enter your sex and current age for us to calculate the correct allowances for you. Tick the SPA box if you are at, or will be at, your state pension age within the selected tax year. State pension ages are no longer the default 60 for females and are changing depending upon your date of birth over the next decade. Our state pension age calculator will take your sex and date of birth and let you know when you will reach state pension.</p>");
   }
});






$('#spalabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>State Pension Age (SPA)</h3><p>If you are state pension age anytime within the selected tax year, tick the SPA box. You can use our state pension age calculator to find out what your SPA is - use the menu above to select from our list of tax calculators.</p>");
   }
});





$('#otherallowshelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Do you have an additional tax allowance or deduction?</h3><p>Please enter any additional allowance that you have on top of your regular tax free allowance, if you have a deduction please enter it with a minus symbol in front e.g. 1500 for an allowance or -1500 for a deduction.</p>");
   }
});


$('#otherallowsdeductslabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Do you have an additional tax allowance or deduction?</h3><p>Please enter any additional allowance that you have on top of your regular tax free allowance, if you have a deduction please enter it with a minus symbol in front e.g. 1500 for an allowance or -1500 for a deduction.</p>");
	}
});


$('#taxcodehelp').tooltip({
	delay: 0,
	showURL: false,
		bodyHandler: function() {
     return $("<h3>If you know your tax code, please enter it here and we will calculate based upon that code (Include the letter - i.e. 747L)</h3><p>Make sure you have a valid code, if you on emergency tax, just enter BR</p>");
		}
});

 


$('#taxcodelabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>If you know your tax code, please enter it here and we will calculate based upon that code (Include the letter - i.e. 747L)</h3><p>Make sure you have a valid code, if you on emergency tax, just enter BR</p>");
		}
	
});



$('#nicletterhelp').tooltip({
	delay: 0,
	showURL: false,
		bodyHandler: function() {
     return $("<p>Letter Based NIC Calculation is only available for tax year 2009 onwards - otherwise A will be used. If you know your NIC Letter, please enter it so your calculation we be based upon it. We currently accept the A,B,C,D,E,F,G,J,L,S and X letters. The Standard Rate Letter is A, The various contracted out options are letters D, E and L Salary Related, or Money Purchase S, G and F.</p>");
		}
});

 


$('#nicletterlabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<p>Letter Based NIC Calculation is only available for tax year 2009 onwards - otherwise A will be used. If you know your NIC Letter, please enter it so your calculation we be based upon it. We currently accept the A,B,C,D,E,F,G,J,L,S and X letters. The Standard Rate Letter is A, The various contracted out options are letters D, E and L Salary Related, or Money Purchase S, G and F.</p>");
		}
	
});



 





$('#pensionhelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>If you have a pension, please enter the amount or percentage you pay regularly into this box</h3><p>If you enter 5% a month, enter 5%, if you pay &pound;100, please enter 100.</p>");
		}
});


$('#pensionlabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>If you have a pension, please enter the amount or percentage you pay regularly into this box</h3><p>If you enter 5% a month, enter 5%, if you pay &pound;100, please enter 100.</p>");
		}
});


$('#pensionperiodlabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Please select the frequency of which you make the stated pension contributions</h3>");
		}
});



$('#pensiontypehelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Select the type of Pension you have</h3><p><b>Salary Sacrifice</b> - Your employer will reduce your Gross Salary by the Pension amount. This will in turn reduce the amount of tax, national insurance and employers national insurance payable. There is no tax relief to claim as your employer makes the pension contribution for you.</p><p><b>Employers Scheme</b> - Your employer takes your pension contribution from your salary before calculating tax so you receive full tax relief at source - but you still pay NICs on the full amount.</p><p><b>Private Pension</b> - You make payments from your salary to a private pension plan - you are still taxed on the pension payments and pay NICs but can receive tax relief automatically at basic rate into your pension by the pension provider. You will need to claim any higher rate relief via a tax return or by adjusting your tax code.</p>");
		}
});



$('#pensiontypelabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Select the type of Pension you have</h3><p><b>Salary Sacrifice</b> - Your employer will reduce your Gross Salary by the Pension amount. This will in turn reduce the amount of tax, national insurance and employers national insurance payable. There is no tax relief to claim as your employer makes the pension contribution for you.</p><p><b>Employers Scheme</b> - Your employer takes your pension contribution from your salary before calculating tax so you receive full tax relief at source - but you still pay NICs on the full amount.</p><p><b>Private Pension</b> - You make payments from your salary to a private pension plan - you are still taxed on the pension payments and pay NICs but can receive tax relief automatically at basic rate into your pension by the pension provider. You will need to claim any higher rate relief via a tax return or by adjusting your tax code.</p>");
		}
});



$('#taxyearhelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Select the Tax Year to calculate, Remember the tax year starts 6th April and ends 5th April.</h3>");
		}
});



$('#taxyearlabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Select the Tax Year to calculate, Remember the tax year starts 6th April and ends 5th April.</h3>");
		}
});



$('#grossincomehelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Please select the period and amount you were paid, i.e. If you select month and enter 1000, we will calculate based upon 12000 per year gross income.</h3>");
		}
});





$('#childcarevouchersamounthelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Childcare Vouchers Salary Sacrifice</h3><p>If you have sacrificed some of your salary to allow your employer to purchase childcare vouchers for you, please enter the amount you sacrifice per month in this box. We will calculate the amount of the sacrifice that is allowable as a tax deductible and reduce the tax and national insurance amounts accordingly. For 2011/2012 the calculator assumes you registered for vouchers on or after 6th April 2011.</p>");
		}
});



$('#childcarevouchersamountlabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Childcare Vouchers Salary Sacrifice</h3><p>If you have sacrificed some of your salary to allow your employer to purchase childcare vouchers for you, please enter the amount you sacrifice per month in this box. We will calculate the amount of the sacrifice that is allowable as a tax deductible and reduce the tax and national insurance amounts accordingly. For 2011/2012 the calculator assumes you registered for vouchers on or after 6th April 2011.</p>");
		}
});





$('#bonushelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>If you receive an annual bonus - please enter the figure here. Bonuses for PAYE are calculated in the same manner as normal PAYE - although we will only calculate the tax and ni due on them.</h3>");
		}
});


$('#payperiodlabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Please select the period and amount you were paid, i.e. If you select month and enter 1000, we will calculate based upon 12000 per year gross income.</h3>");
		}
});




$('#knowntaxhelp').tooltip({
	delay: 0,
	showURL: false,
		bodyHandler: function() {
     return $("<h3>If you have your P60, Final Payslip, P45 or know your total tax paid for the tax year you have chosen, please enter the amount in this box. If you have overpaid tax and a refund is available we will provide the exact figures for you.</h3>");
		}
});





$('#knowntaxedlabel').tooltip({
	delay: 0,
	showURL: false,
		bodyHandler: function() {
     return $("<h3>If you have your P60, Final Payslip, P45 or know your total tax paid for the tax year you have chosen, please enter the amount in this box. If you have overpaid tax and a refund is available we will provide the exact figures for you.</h3>");
		}
});








$("#taxman").hover(
				
				//mouseenter
				function (){  $('#taxman').animate({backgroundPosition: '(20 20)'},400);  },   
				   
				   //mouseleave
				   function () {  $('#taxman').animate({backgroundPosition: '(20 125)'},400);	}
				   
				   
				   
				   
				   );



 

 


}); //end of document ready code	














function calc(){
	
/* Validate Form Fields */


     $("#errorlbl").show();
	 $("input#otherallowsdeducts").css({border: '1px solid #AAAAAA'});
	 $("input#taxcode").css({border: '1px solid #AAAAAA'});
	 $("input#pension").css({border: '1px solid #AAAAAA'});
	 $("input#grossincome").css({border: '1px solid #AAAAAA'});
	 $("input#bonus").css({border: '1px solid #AAAAAA'});
	 $("input#knowntaxed").css({border: '1px solid #AAAAAA'});

     
	 var married = $("input#married:checked").val();
	 if (married == undefined) 
	 { 
	 var married = "not married"; 
	 }
	 
	 
	 var widow = $("input#widow:checked").val();
	 if (widow == undefined) 
	 { 
	 var widow = "not widow"; 
	 }
	 
	 
	 var blind = $("input#blind:checked").val();
	 if (blind == undefined) 
	 { 
	 var blind = "not blind"; 
	 }
	 
	 
	 var nonatins = $("input#nonatins:checked").val();
	 if (nonatins == undefined) 
	 { 
	 var nonatins = "does pay ni"; 
	 }
	 
	 
	 var cis = $("input#cis:checked").val();
	 if (cis == undefined) 
	 { 
	 var cis = "not cis"; 
	 }
	 
	 
	 var studentloan = $("input#studentloan:checked").val();
	 if (studentloan == undefined) 
	 { 
	 var studentloan = "not student loan"; 
	 }
	 
	  
	 var age = $("select#age").val();
	 
	 var sex = $("select#sex").val();
	 
	  var spa = $("input#spa:checked").val();
		 if (spa == undefined) 
		 { 
		 var spa = "no"; 
		 }
	
	var posneg = /^[-]?[1-9]\d*\.?[0]*$/; // allows -10 or 10, nothing else.
	  var otherallowsdeducts = $("input#otherallowsdeducts").val();  
         
		 if (otherallowsdeducts != "")
		 {
		 if (posneg.test(otherallowsdeducts)== false) {  
       $("#errorlbl").text("Your other allowances or deductions figure must be a whole number, either 1000 or -1000 if it is a deduction, leave blank if nothing.");
	   $("input#otherallowsdeducts").css({border: '6px solid #1e73b7'});
       $("input#otherallowsdeducts").focus();  
       return false;  
     }
		 }



	 
	 var taxcodereg2 = /^((br|BR)|(d0|D0)|(d1|D1)|(nt|NT)|(0t|0T))$/;     // allows BR or D0 or D1 or or NT or 0T in upper or lower case formats.
	  var taxcodereg3 = /^[0-9]{0,1}[0-9]{2,3}[LPTYVlptvy]{1}$/;     // allows 3 or 4 digits followed by L P T V or Y
	  var taxcodereg4 = /^[Kk]{1}[0-9]{2,5}$/;     // allows K or k followed 3 or 4 digits	 
	 
	 var taxcode = $("input#taxcode").val();  
        
	if (taxcode != "")
	{
		
		if (taxcodereg2.test(taxcode)== false && taxcodereg3.test(taxcode)== false && taxcodereg4.test(taxcode)== false) {  
       $("#errorlbl").text("Please enter a valid UK Tax Code, or Make Sure The Field Is Blank");
	   $("input#taxcode").css({border: '6px solid #1e73b7'});
       $("input#taxcode").focus();  
       return false;  
     }
	}
	
	
	
	
	
	//var pensionreg = /(^\£(\d{1,3},?(\d{3},?)*\d{3}(\.\d{1,3})?|\d{1,3}(\.\d{2})?)$|^\d{1,2}(\.\d{1,2})? *%$|^100%$)/;
	
	var pensionreg = /(^(\d{1,3},?(\d{3},?)*\d{3}(\.\d{1,3})?|\d{1,3}(\.\d{2})?)$|^\d{1,2}(\.\d{1,2})? *%$|^100%$)/;
	
	var pension = $("input#pension").val();
	
	if (pension != "")
	{
		
		if (pensionreg.test(pension)== false) {  
       $("#errorlbl").text("Please enter either a whole amount eg. 100 or Percentage eg. 12%");
	   $("input#pension").css({border: '6px solid #1e73b7'});
       $("input#pension").focus();  
       return false;  
     }
	}
	
	var pensionperiod = $("select#pensionperiod").val(); 
	
		 
	 
	 var pensiontype = $("input#pensiontype:checked").val();
	 
	
	var taxyear = $("select#taxyear").val();
	
	
	var payperiod = $("select#payperiod").val(); 
	
	
	
	
	
	var grossincome = $("input#grossincome").val(); 
	
	var bonus = $("input#bonus").val();
	
	var childcarevouchersamount = $("input#childcarevouchersamount").val();
	
	var wholenumber = /^\d+(?:\.\d{0,2})?$/;  //regex to check for whole positive numbers with max 2 decimal points like 251.33
	
	if (wholenumber.test(grossincome)== false) {  
       $("#errorlbl").text("Please enter your gross income in the following format 100.99");
	   $("input#grossincome").css({border: '6px solid #1e73b7'});
       $("input#grossincome").focus();  
       return false;  
     }
	
	if(bonus != ""){
	if (wholenumber.test(bonus)== false) {  
       $("#errorlbl").text("Please enter your bonus income in the following format 100.99");
	   $("input#bonus").css({border: '6px solid #1e73b7'});
       $("input#bonus").focus();  
       return false;  
     }
	}
	
	
	if(childcarevouchersamount != ""){
	if (wholenumber.test(childcarevouchersamount)== false) {  
       $("#errorlbl").text("Enter childcare vouchers amount in the format 100.99");
	   $("input#childcarevouchersamount").css({border: '6px solid #1e73b7'});
       $("input#childcarevouchersamount").focus();  
       return false;  
     }
	}
	
	var knowntaxed = $("input#knowntaxed").val(); 
	
	if (knowntaxed != "")
	{
	if (wholenumber.test(knowntaxed)== false) {  
       $("#errorlbl").text("If you know how much you were taxed, please enter it in the following format: 100.99, otherwise leave blank");
	   $("input#knowntaxed").css({border: '6px solid #1e73b7'});
       $("input#knowntaxed").focus();  
       return false;  
     }
	}
	
	
		 
$.galog.pageView('calculation/taxcalculation'); //send trackinfo to analytics


}



















// script gets the updated figures from the database and displays them into the info bar
function updateheadline(){
	
 //put the loading animations into the spans where the figures will be going	
 $("#totref").html("<img src='images/ajax-loader-2.gif' />");
 
 $("#recentref").html("<img src='images/ajax-loader-2.gif' />");


 // run an ajax function to get the info needed, then parse it.
 $.ajax({  
   type: "POST",  
   url: "bin/populateheadline.php",   
   success: function(msg) {  //the msg container holds the xmlhttp response 
                             
	 $("#response").html(msg);
     var $response=$(msg);
     var totalrefunds = " " + $response.filter('#totalrefunds').text() + " ";
     var recentrefund = " " + $response.filter('#recentrefund').text() + " ";
                  
	 //put the figures in	
 	 $("#totref").html(totalrefunds);
 
 	 $("#recentref").html(recentrefund);


   }
 });  



}



function placeit(){
	
var taxyeartext = $("select#taxyear option:selected").val();

$("#knowntaxedstuff").text(taxyeartext);

}





function addcomment(){
	
$("input#commentname").css({border: '0'});
$("input#commentemail").css({border: '0'});
$("input#commenttext").css({border: '0'});

var commentname = $("input#commentname").val();
var commentemail = $("input#commentemail").val();
var commenttext = $("input#commenttext").val();

if(commentname == ""){ $("input#commentname").css({border: '6px solid #1e73b7'}).val("Name..."); setTimeout('$("input#commentname").val("");',500);$("input#commentname").focus();return false; }
if(commentemail == ""){ $("input#commentemail").css({border: '6px solid #1e73b7'}).val("Email..."); setTimeout('$("input#commentemail").val("");',500);$("input#commentemail").focus();return false; }
if(commenttext == ""){ $("input#commenttext").css({border: '6px solid #1e73b7'}).val("Comment...");setTimeout('$("input#commenttext").val("");',500); $("input#commenttext").focus();return false; }

if(commenttext.length > 125) { alert("Your comment cannot be more than 125 characters!"); return false; }

var dataStringc = "commentname="+ escape(commentname) + "&commentemail=" + escape(commentemail) + "&commenttext=" + escape(commenttext);

$("input#submitcomment").fadeOut("fast");			
  
			$.ajax({  
		   type: "POST",  
		   url: "bin/addcomment.php",  
		   data: dataStringc,  
		   success: function(msg) {  //the msg container holds the xmlhttp response 
			 
			 
			 
			$("input#submitcomment").val("Sent..Thanks!");
			$("input#submitcomment").fadeIn("slow");
			
			
			
			$("input#commentname").val("");
			$("input#commentemail").val("");
			$("input#commenttext").val("");
			
			$("#floatingheads").fadeOut("fast");
			setTimeout('$("input#submitcomment").val("Add Comment");',2250);
		
		   }
		 });
	
	



   
 return false;  



}





	

	



