	function Round(number,X) {
	// Rounds number to X decimal places, defaults to 2
	X = (!X ? 2 : X);
	return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

// Change these variables if any tax or NI bands change
// This will affect ALL calculations including all figures above 0
// which are shown on first loading the page.
 
var consMaxVouchers = 2916;
var consNIAllowance = 5035;
var consTaxAllowance = 5035;
var consUpperTaxBand = 33300;
var consLowerTaxBand = 2150;
var consUpperNIBand = 33540;
var consUpperTaxRate = 40;
var consMiddleTaxRate = 22;
var consLowerTaxRate = 10;
var consLowerNIIn = 11;
var consLowerNIOut = 9.4;
var consUpperNIRate = 1;


function Calculate() {
	// Declares the variables used in the calculations
	var intSalary = 0;
	var intVouchers = 0;
	var intOption = 0;
	var intChoice = 0;
	var TooGreat = false;
	var intNIWorkings1 = 0;
	var intNIWorkings2 = 0;
	var intTaxWorkings1 = 0;
	var intTaxWorkings2 = 0;
	var strInformation = '';
	
	// These variables are used to store the results
	var intNISavings = 0;
	var intTaxSavings = 0;

	// Captures the input from the user
	intSalary = document.getElementById('Salary').value;
	intVouchers = document.getElementById('Vouchers').value;
	intOption = document.getElementById('Choice').value;
	
	// Checks the number of vouchers entered.
	// Sets to max voucher number if too high both on screen and in calculations
	if (intVouchers > consMaxVouchers) {
		intVouchers=consMaxVouchers;
		TooGreat=true;
	}
	
	// Checks that they've purchased too many vouchers or have too low a salary
	// for the calculations to work correctly
	if ((intSalary-intVouchers)<consTaxAllowance) {
		alert('Salary less vouchers must remain over \u00A3'+consNIAllowance+' to pay minimum NIC!');
		document.getElementById('Salary').value='0';
		document.getElementById('Vouchers').value='0';
	}
	
	else {
		// Saves salary after lower tax band and after vouchers
		intChoice = intSalary - consTaxAllowance;
	
		// Loops from payments after tax to before and saves results
		// Checks to see what tax band the user is in
		if (intChoice >=consUpperTaxBand) {
			intTaxWorkings1 = (intChoice-consUpperTaxBand)

			//Displays test information
			strInformation += 'Upper Band<br>'
			
			if (intTaxWorkings1<intVouchers) {
				//Displays test information
				strInformation += 'and Middle Band<br>'

				// Calculates upper and middle tax band savings if not all voucher's used
				intTaxWorkings2 = ((intVouchers-intTaxWorkings1)*consMiddleTaxRate)/100;
				intTaxSavings = intTaxWorkings2+(intTaxWorkings1*consUpperTaxRate)/100;
				
				// Displays test information
				strInformation += '<br>Tax Workings 1: '+intTaxWorkings1+'<br>';
				strInformation += 'Tax Workings 2: '+intTaxWorkings2+'<br>';
				strInformation += 'Upper Tax Savings: '+((intTaxWorkings1*consUpperTaxRate)/100)+'<br>';
				strInformation += 'Middle Tax Savings: '+intTaxWorkings2+'<br>';
			}
			else {
				// Calculates upper savings only if all vouchers used for this
				intTaxSavings = (intVouchers*consUpperTaxRate)/100;
			}
		}
		
		else if (intChoice > consLowerTaxBand) {
			intTaxWorkings1 = (intChoice-consMiddleTaxRate)
			
			//Displays test information
			strInformation += 'Middle Band<br>'
			
			if (intTaxWorkings1<intVouchers) {
				//Displays test information
				strInformation += 'and Lower Band<br>'
				
				// Calculates middle and lower tax band savings if not all voucher's used
				intTaxWorkings2 = 0		//((intVouchers-intTaxWorkings1)*consLowerTaxBand)/100;
				intTaxSavings = intTaxWorkings2+(intTaxWorkings1*consMiddleTaxRate)/100;
				
				//Displays test information									
				strInformation += '<br>Tax Workings 1: '+intTaxWorkings1+'<br>';
				strInformation += 'Tax Workings 2: '+intTaxWorkings2+'<br>';
				strInformation += 'Middle Tax Savings: '+((intTaxWorkings1*consMiddleTaxRate)/100)+'<br>';
				strInformation += 'Lower Tax Savings: '+intTaxWorkings2+'<br>';
			}
			else {
				// Calculates middle savings only if all vouchers used for this
				intTaxSavings = (intVouchers*consMiddleTaxRate)/100;
			}
		}
		
		else if (intChoice < consLowerTaxBand && intChoice > consTaxAllowance) {
			intTaxWorkings1 = (intChoice-consLowerTaxBand)

			//Displays test information
			strInformation += 'Lower Band<br>'
			
			if (intTaxWorkings1<intVouchers) {
				//Displays test information
				strInformation += 'and Free Band<br>'

				// Calculates lower and free band savings if not all voucher's used
				intTaxWorkings2 = 0;
				intTaxSavings = (intTaxWorkings1*consLowerTaxBand)/100;
													
				//Displays test information
				strInformation += '<br>Tax Workings 1: '+intTaxWorkings1+'<br>';
				strInformation += 'Tax Workings 2: '+intTaxWorkings2+'<br>';
				strInformation += 'Middle Tax Savings: '+intTaxWorkings2+'<br>';
			}
			else {
				// Calculates lower savings only if all vouchers used for this
				intTaxSavings = (intVouchers*consLowerTaxBand)/100;
			}

		}
		else {
			intTaxSavings = 0;
			strInformation += 'Free Band'
		}
										
		// Decides NI rate for user
		// NOTE:  NI is charges on how much is below and how much is above the band
		// 				with the vouchers!
		// 				Make sure not all vouchers are being used
		if (intSalary >= consUpperNIBand) {
			intNIWorkings1 = (intSalary-consUpperNIBand);
			
			// Checks to make sure all the vouchers have not already been used
			// (Else you get negative values)
			if (intNIWorkings1 < intVouchers) {
				intNIWorkings2 = ((intVouchers-intNIWorkings1)*intOption)/100;
				intNISavings = intNIWorkings2+((intNIWorkings1*consUpperNIRate)/100);
				
				// Saves inner workings for later display									
				 strInformation += 'NI Workings 1: '+intNIWorkings1+'<br>';
				 strInformation += 'Upper NI Savings: '+((intNIWorkings1*consUpperNIRate)/100)+'<br>';
				 strInformation += 'Lower NI Savings: '+intNIWorkings2+'<br>';
			}
			else {
				intNISavings = (intVouchers*consUpperNIRate)/100;
			}
			
		}
		else {
			intNISavings = (intVouchers*intOption)/100;
		}
		
		// Checks if the user has entered too mny vouchers
		if (TooGreat==true) {
			// Alerts the user if they've entered more then the maximum number of vouchers and resets the TooGreat variable
			alert('Any vouchers over the limit of  \u00A3'+consMaxVouchers+' will not count towards any more Tax or NI deductions');
			// Sets the value onscreen to the maximum voucher limit.
			document.getElementById('Vouchers').value=consMaxVouchers;
		}

		// Performs remaining calculations and assigns variables to elements
		document.getElementById('NISavings').innerHTML= '£'+Math.round(intNISavings);
		document.getElementById('TaxSavings').innerHTML= '£'+Math.round(intTaxSavings);
		document.getElementById('Total').innerHTML= '£'+Math.round(intTaxSavings+intNISavings);
	
		// Displays calculations: Commenting this out stops them being displayed
		// Also comment out the last div on this page which is where they're displayed!
		//document.getElementById('Info').innerHTML= strInformation;
	}
}


