
	window.onload = init;		

	function init()
	{
		document.getElementById("contactform").style.visibility = "hidden";
		document.getElementById("answer").style.visibility = "hidden";
		document.calculateform.gross_pay.focus();

		document.getElementById("gross_pay").onkeydown=disableEnterKey;
		document.getElementById("tax_paid").onkeydown=disableEnterKey;
		document.getElementById("expenses_paid").onkeydown=disableEnterKey;
    		document.getElementById("net_interest").onkeydown=disableEnterKey;

		document.getElementById("firstname").onkeydown=disableEnterKey;
		document.getElementById("lastname").onkeydown=disableEnterKey;
		document.getElementById("address1").onkeydown=disableEnterKey;
		document.getElementById("address2").onkeydown=disableEnterKey;
		document.getElementById("postcode").onkeydown=disableEnterKey;
		document.getElementById("birthday").onkeydown=disableEnterKey;
		document.getElementById("nino").onkeydown=disableEnterKey;
		document.getElementById("email1").onkeydown=disableEnterKey;
		document.getElementById("email2").onkeydown=disableEnterKey;
		document.getElementById("phone").onkeydown=disableEnterKey;
		document.getElementById("net_interest").onkeydown=disableEnterKey;
				
	}

	function disableEnterKey()
	{
		if(event.keyCode == 13) 
		{
			event.keyCode=9; //return the tab key
			event.cancelBubble = true;
		}
	}




	function calculator()
	{

 
		var date1;
		var GrossPay;
		var TaxPaid;
		var NetBankInterest;
		var BankTax;
		var GrossBankInterest;
		var Expenses;
		var Exemption;
		var band1;
		var band2;
		var TaxRefund;
		var CorrectTax;
		var TaxableIncome;
		var GrossIncome;
		var temp1;

        if(formValidator() == true)
		{
			ind = document.calculateform.taxyear.selectedIndex;
			val = document.calculateform.taxyear.options[ind].value;

			switch(val)
			{

				case "2003-2004":
					Exemption = 4615;
					band1 = 1960;
					band2 = 30500;
					break;
				case "2004-2005":
					Exemption = 4745;
					band1 = 2020;
					band2 = 31400;
					break;
				case "2005-2006":
					Exemption = 4895;
					band1 = 2090;
					band2 = 32400;
					break;
				case "2006-2007":
					Exemption = 5035;
					band1 = 2150;
					band2 = 33300;
					break;
				case "2007-2008":
					Exemption = 5225;
					band1 = 2230;
					band2 = 34600;
					break;
				case "2008-2009":
				    Exemption = 6035;
					band1 = 2320;
					band2 = 34800;
					break;
				
			}

			date1 = parseInt(val);

			GrossPay = parseFloat(document.calculateform.gross_pay.value);
			TaxPaid = parseFloat(document.calculateform.tax_paid.value);
			NetBankInterest = parseFloat(document.calculateform.net_interest.value);
			Expenses = parseFloat(document.calculateform.expenses_paid.value);

			BankTax = NetBankInterest/4;
			TaxPaid = TaxPaid + BankTax;
			GrossBankInterest = NetBankInterest + BankTax;

			TaxRefund = 0.0;
			CorrectTax = 0.0;
			GrossIncome = GrossPay - Expenses + GrossBankInterest;
			if(GrossIncome <= Exemption)
			{
				if(TaxPaid > 0)
				{
					TaxRefund = TaxPaid;
				}
			}
			else
			{
				TaxableIncome = GrossIncome - Exemption;
				if(TaxableIncome <= band1)
				{
				    if(val == "2008-2009")
				    {
				        CorrectTax = GrossBankInterest * 0.1;
				        CorrectTax = CorrectTax + ((TaxableIncome-GrossBankInterest) * 0.2);
				    }
				    else
     					CorrectTax = TaxableIncome * 0.1;
				}
				else 
				{
				    
				    if(val == "2008-2009")
				    {
				        if(GrossBankInterest >= band1)
				        {
				    	   CorrectTax = band1 * 0.1;
				    	   GrossBankInterest = GrossBankInterest - band1;
				    	}
				    	else
				    	{
				    	   CorrectTax = GrossBankInterest * 0.1;
				    	   CorrectTax = CorrectTax + ((band1 - GrossBankInterest) * 0.2);
				    	   GrossBankInterest = 0;
				    	}
				    }
				    else
				        CorrectTax = band1 * 0.1;
				        
					TaxableIncome = TaxableIncome-band1;
					if(TaxableIncome <= band2)
					{
						if(GrossBankInterest >= TaxableIncome)
						{
						    CorrectTax = CorrectTax + (TaxableIncome*0.2);
						}
						else
						{
						    CorrectTax = CorrectTax + (GrossBankInterest * 0.2);
						    CorrectTax = CorrectTax + ((TaxableIncome-GrossBankInterest) * 0.22);
						    
						}
					}
					else
					{
						TaxableIncome = TaxableIncome - band2;
						CorrectTax = CorrectTax + (TaxableIncome * 0.4);
						if(GrossBankInterest < TaxableIncome)
						{
															
							CorrectTax = CorrectTax + (band2 * 0.22);							
						}
						else
						{
							GrossBankInterest = GrossBankInterest - TaxableIncome;
							CorrectTax = CorrectTax + (GrossBankInterest * 0.2);
							CorrectTax = CorrectTax + ((band2-GrossBankInterest) * 0.22);
						}
							
						
					}
				}
				if(TaxPaid > CorrectTax)
					TaxRefund = TaxPaid - CorrectTax;
			}

	
			var abc = document.getElementById("contactform");
			var def = document.getElementById("answer");			

			var sentence;
            var yourRefund;
			var ourCost;

				
			if (TaxRefund > 0)
			{
				
				ourCost = (TaxRefund * 0.15).toFixed(2);
				yourRefund = (TaxRefund-ourCost).toFixed(2);

				document.contform.cost.value = ourCost;
				document.contform.netrefund.value = yourRefund;
			
				abc.style.visibility = "visible";
				sentence = "Congratulations! Based on those figures, you are entitled to a refund of \u00A3" + TaxRefund.toFixed(2);
				sentence = sentence + "\n\nAt Tax Concern our fees are \u00A3" + ourCost + " and you pocket \u00A3" + yourRefund;
				sentence = sentence + " to assist your higher education.\n\nJust fill in the form below, and we will email you the UK Student Refund Pack customised for you within the next few minutes. We will process your claim within 1 week of receiving your signed documents.";

				def.style.visibility = "visible";				
			}
			else
			{
				def.style.visibility = "visible";
				abc.style.visibility = "hidden";
				sentence = "Unfortunately you are not due a tax refund.";
			
			}

			document.contform.taxrefund.value = TaxRefund;
			
			document.calculateform.answer.value = sentence;
			document.contform.results.value = document.getElementById('answer').value;
			document.contform.grosspay1.value = document.getElementById('gross_pay').value;
			document.contform.taxpaid1.value = document.getElementById('tax_paid').value;
			document.contform.netinterest1.value = document.getElementById('net_interest').value;
			document.contform.expensespaid1.value = document.getElementById('expenses_paid').value;
			document.contform.taxyear1.value = document.getElementById('taxyear').value;

			if (location.href == 'http://www.taxconcern.co.uk/student/' || location.href == 'http://www.taxconcern.co.uk/student/#rrr' || location.href == 'http://www.taxconcern.co.uk/student/index.htm' || location.href == 'http://www.taxconcern.co.uk/student/index.htm#rrr') {
				oStr = location.href;
				if (oStr.indexOf("index.htm",0) > 0) {
				    location.href = 'http://www.taxconcern.co.uk/student/index.htm#rrr';
				    } else {
				    location.href = 'http://www.taxconcern.co.uk/student/#rrr';
				    }
			}
			else {
			    location.href = 'http://www.taxconcern.co.uk/studentcalc_chinese.htm#rrr';
			}
		}
	}

	


	function formValidator(){
	// Make quick references to our fields
	var grosspay = document.getElementById('gross_pay');
	if(grosspay.value.length == 0)
	{
		document.calculateform.gross_pay.value = "0";
	}

	var taxpaid = document.getElementById('tax_paid');
	if(taxpaid.value.length == 0)
	{
		document.calculateform.tax_paid.value = "0";
	}

	var netinterest = document.getElementById('net_interest');
	if(netinterest.value.length == 0)
	{
		document.calculateform.net_interest.value = "0";
	}	
	
	var expensespaid = document.getElementById('expenses_paid');
	if(expensespaid.value.length == 0)
	{
		document.calculateform.expenses_paid.value = "0";
	}

	// Check each input in the order that it appears in the form!
	if(isNumeric(grosspay, "Invalid amount for gross pay")){
		if(isNumeric(taxpaid, "Invalid amount for tax paid")){
			if(isNumeric(netinterest, "Invalid amount for net interest")){
				if(isNumeric(expensespaid, "Invalid amount for expenses paid")){
					
							return true;
				}
			}
		}
	}
	
	
	return false;
	
}


function isNumeric(elem, helperMsg){
	var numericExpression = /^[.0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}


function CheckForm()
{

  

   var cname, caddrline1, cemail, cemailchk, cpostcode;
  

   	cname = document.contform.lastname;
	caddrline1 = document.contform.address1; 
	cemail = document.contform.email1;
	cemailchk = document.contform.email2; 
   	cpostcode = document.contform.postcode;


   if(trim(cname.value) == '')
   {
      alert('Please enter your surname');
      cname.focus();
      return false;
   }
   else if(trim(caddrline1.value) == '')
   {
      alert('Please enter the 1st line of your address');
      caddrline1.focus();
      return false;
   }
   else if(trim(cemail.value) == '')
   {
      alert('Please enter your email');
      cemail.focus();
      return false;
   }
   else if(trim(cpostcode.value) == '')
   {
      alert('Please enter your postcode');
      cpostcode.focus();
      return false;
   }
   else if(trim(cemail.value) != trim(cemailchk.value))
   {
      alert('Email check does not match');
      cemail.focus();
      return false;
   }
   else if(!isEmail(trim(cemail.value)))
   {
      alert('Email address is not valid');
      cemail.focus();
      return false;
   }
   
   else
   {
      cname.value    = trim(cname.value);
      cemail.value   = trim(cemail.value);
      caddrline1.value = trim(caddrline1.value);
      return true;
   }
 }

function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}

function isEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}


