
$(document).ready(function(){
						   
	 
	
	updateheadline();

    //run funtion to update headlines
	setInterval("updateheadline();",10000);	
	
	
	$("#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/submitbtnover.png"); 
		
      }, 
      function () {
       
	    $('#submitbtn').attr("src", "images/submitbtnnorm.png"); 
	   
      }
    );




$('#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 the age of 65 pay no National Insurance and may have a large tax free allowance</h3>");
   }
});


$('#widowlabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>An additional amount is added to your tax free allowance if you are a widow.</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 - 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 - 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>Have you been a student? 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>Have you been a student? 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>");
   }
});




$('#sexhelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Are You Female?</h3><p>If you are over age of 60 and Female you do not have to pay any NICs.</p>");
   }
});


$('#sexlabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Are You Female?</h3><p>If you are over age of 60 and Female you do not have to pay any NICs.</p>");
   }
});






$('#agehelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Please select your Age Group</h3><p>If you are over age of 60, there are additional allowances and National Insurance Reductions.</p>");
   }
});


$('#agelabel').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>Please select your Age Group</h3><p>If you are over age of 60, there are additional allowances and National Insurance Reductions.</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</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</h3><p>Make sure you have a valid code, if you on emergency tax, just enter BR</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>");
		}
});



$('#salarysacrificehelp').tooltip({
	delay: 0,
	showURL: false,
	bodyHandler: function() {
     return $("<h3>If you have entered a salary sacrifice scheme, please tick this box.</h3><p>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>");
		}
});



$('#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>");
		}
});



$('#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 sex = $("input#sex:checked").val();
	 if (sex == undefined) 
	 { 
	 var sex = "male"; 
	 }
	 

	 var age = $("select#age").val();
	 
	
	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)|(nt|NT)|(ot|OT))$/;     // allows BR or D0 or NT or OT 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 salarysacrifice = $("input#salarysacrifice:checked").val();
	 if (salarysacrifice == undefined) 
	 { 
	 var salarysacrifice = "No"; 
	 }
	
	
	var taxyear = $("select#taxyear").val();
	
	
	var payperiod = $("select#payperiod").val(); 
	
	
	
	
	
	var grossincome = $("input#grossincome").val(); 
	
	var bonus = $("input#bonus").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;  
     }
	}
	
	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 make sure the field is 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);

$('#floatingheads').css({backgroundPosition: '0 0'});


$("#floatingheads").show();

$('#floatingheads').animate({
    backgroundPosition: '(0 -160)'
	}, 1250);

 $('#floatingheads').animate({
    backgroundPosition: '(0 0)'
	}, 1250, function() {
    // Animation complete.
	
			
  
			$.ajax({  
		   type: "POST",  
		   url: "bin/addcomment.php",  
		   data: dataStringc,  
		   success: function(msg) {  //the msg container holds the xmlhttp response 
			 
			 $("input#submitcomment").fadeOut("slow");
			 
			$("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;  



}





	

	


