	
function submitCalculator() {

	if (checkAllResponses()) {
		// do something here
		var output1 = PIA_projection(1, 4, document.Calculator.age.value*1, document.Calculator.sex.value, document.Calculator.salary.value*1, document.Calculator.fund.value*1, document.Calculator.spouse_frac.value*1, document.Calculator.nra.value*1, document.Calculator.target.value*1, document.Calculator.ee_cont_rate.value*1, document.Calculator.er_cont_rate.value*1);
		var output2 = PIA_projection(4, 4, document.Calculator.age.value*1, document.Calculator.sex.value, document.Calculator.salary.value*1, document.Calculator.fund.value*1, document.Calculator.spouse_frac.value*1, document.Calculator.nra.value*1, document.Calculator.target.value*1, document.Calculator.ee_cont_rate.value*1, document.Calculator.er_cont_rate.value*1);

		if (check_error(output1)) {
		if (check_error(output2)) {	
			document.Calculator.output1.readOnly = false;
			document.Calculator.output1.value = Math.round(output1);
			document.Calculator.output1.readOnly = true;
		
			document.Calculator.output2.readOnly = false;
			document.Calculator.output2.value = "£" + output2;
			document.Calculator.output2.readOnly = true;

			//set all input boxes to read only
			document.Calculator.age.disabled = true;
			document.Calculator.nra.readOnly = true;
			document.Calculator.fund.disabled = true;
			document.Calculator.sex.disabled = true;
			document.Calculator.ee_cont_rate.readOnly = true;
			document.Calculator.er_cont_rate.disabled = true;
			document.Calculator.salary.disabled = true;
			

			//swap out images
			document.img_nra_up.src = "images/arrow_up.gif"
			document.img_nra_down.src = "images/arrow_down.gif"
			document.img_ee_cont_rate_up.src = "images/arrow_up.gif"
			document.img_ee_cont_rate_down.src = "images/arrow_down.gif"
			document.img_output1_up.src = "images/arrow_up.gif"
			document.img_output1_down.src = "images/arrow_down.gif"
			
			document.getElementById("info_txt").style.visibility = "visible";
			
			//set hyperlinks
			//document.nra_up.href = ""
			//document.ee_cont_rate_down.href = "javascript:control('ee_cont_rate', '-');"
		}
		}
	}
}

function checkAllResponses() {
	var h = document.Calculator;

	//check responses for all inputs
	if (h.age.value == "") {
			alert("Please enter your age");
			h.age.focus();
			return false;
	}
	else if (h.nra.value == "") {
			alert("Please enter your retirement age");
			h.nra.focus();
			return false;
	}
	else if (h.fund.value == "") {
			//alert("Please enter the current value of your existing pension fund");
			//h.fund.focus();
			//return false;
			h.fund.value = 0;
	}
	else if (h.ee_cont_rate.value == "") {
			//alert("Please enter the % of your salary you contribute to your pension");
			//h.ee_cont_rate.focus();
			//return false;
			h.ee_cont_rate.value = 0;
	}
	else if (h.er_cont_rate.value == "") {
			//alert("Please enter the % of your salary your employer contributes to your pension");
			//h.er_cont_rate.focus();
			//return false;
			h.er_cont_rate.value = 0;
	}
	else if (h.salary.value == "") {
			alert("Please enter your current salary");
			h.salary.focus();
			return false;
	}
	//validate all inputs again
	var inputs = new Array("age", "nra", "fund", "er_cont_rate", "ee_cont_rate", "salary");
	for (i=0; i < inputs.length; i++) {
		h = eval("document.Calculator." + inputs[i]);
		if (!validate(h, inputs[i])) { return false; }
	}

	//if everything's valid then return true
	return true;
}

function validate(h, question) {

	// clear the output1 box
//	document.Calculator.output1.readOnly = false;
//	document.Calculator.output1.value = "";
//	document.Calculator.output1.readOnly = true;


	if (question == "age") {
		if (!Number(h.value) || Number(h.value) < 16 || Number(h.value) > 74) {
			alert("Please enter an age between 16-74 years");
			return false;
		}
		// convert to an integer
		else {
			h.value = parseInt(String(h.value));
		}
	}
	else if (question == "nra") {
		if (!Number(h.value) || Number(h.value) < 55 || Number(h.value) > 75) {
			alert("Please enter an age between 55-75 years");
			return false;
		}
		// convert to an integer
		else {
			h.value = parseInt(String(h.value));

			// check if age is within 2 years of retirment age
			if (Number(document.Calculator.age.value)) {
				if (h.value <= Number(document.Calculator.age.value)) {
					alert("Please enter a retirment age greater than your age");
					//document.location = "kitbag.asp?pg=3";
					return false;
				}
				else if (h.value - Number(document.Calculator.age.value) <= 2) {
					alert("You are within two years of retirement. This calculator is not suitable for you.\n We recommend you seek independent financial advice");
					//document.location = "kitbag.asp?pg=3";
					return false;
				}
			}
		}
	}
	else if (question == "fund") {
		if (h.value == "0" || h.value == "") {
			h.value = 0;
			return true;
		}
		else if (!Number(h.value) || (Number(h.value) > Number(10000000)) || (h.value < 0)) {
			alert("Please enter a pension value between £0-10,000,000");
			return false;
		}
		// convert to an integer
		else {
			h.value = parseInt(String(h.value));
		}
	}
	else if (question == "ee_cont_rate") {
		if (h.value == "0" || h.value == "") {
			h.value = 0;
			return true;
		}
		else if (!Number(h.value) || (h.value < 0) || (Number(h.value) > 100)) {
			alert("Please enter your contribution between 0-100%");
			return false;
		}
	}
	else if (question == "er_cont_rate") {
		if (h.value == "0" || h.value == "") {
			h.value = 0;
			return true;
		}
		/*if (!Number(h.value) || (h.value < 0) || (Number(h.value) > 25)) {
			alert("Please enter an employers contribution between 0-25%");
			return false;
		}*/
	}
	else if (question == "salary") {
		if (!Number(h.value) || Number(h.value) <0) {
			alert("Please enter a salary value more than £0");
			return false;
		}
		// convert to an integer
		else {
			h.value = parseInt(String(h.value));
		}
	}
	else if (question == "output1") {
		if (!Number(h.value) && (!h.value == 0)) {
			alert("Please enter a number for your final pension %");
			return false;
		}
		else if (Number(h.value) < 0) {
			return false;
		}
		/*else if (Number(h.value) > 66) {
			alert("Maximum pension is 66% of your final salary");
			return false;
		}*/
		// convert to an integer
		else {
			h.value = parseInt(String(h.value));
		}

	}


	return true;
}

function control(item, operator) {
	var h = eval("document.Calculator." + item);
	var curr_val = Number(h.value);
	var bReadOnly = false;

	bReadOnly = h.readOnly;
	h.readOnly = false;
	
	// round down or up to the nearest integer depending on operator
	if (operator == "+") {
		h.value = eval("Math.floor(curr_val) + 1");
	} else {
		h.value = eval("Math.ceil(curr_val) - 1");
	}

	//if new value is not valid then don't change it back
	if (!validate(h, item)) {
		h.value = curr_val;
	}

	//if recalculate returns an error and the operator was plus then don't change it back
	//we need to let the value be reduced if recalculate still returns an error

	if (!Recalculate(h, item) && (operator == "+" || item == "output1")) {
		h.value = curr_val;
	}
	if (!validate(h, "output1")){
		h.value = curr_val;
	}
	h.readOnly = bReadOnly;

}

function check_error(result) {

	//check result for errors
	//Error1	:	Contribution rate and/or current fund produces benefit above IR limits 
	//Error2a	:	Contributions required to fund target are in excess of 15% of maximum allowable salary
	//Error2b	:	Your contribution rate exceeds 15% of the Earnings Cap before your reach your selected retirement age
	//Error3	:	Benefit chosen leads to an an NRA outside allowable range

	//The following error codes should not occur since being trapped by the calculator :-
	//Error4	:	PIA option chosen does not exist
	//Error5	:	Incorrect salary entered
	//Error6	:	Member within 2 years of retirement
	//Error7	:	Calculation option chosen does not exist
	//Error8	:	NRA entered is outside permissable range

	if (String(result).substring(0, 5) == "Error") {
		if (result == "Error1") {
			alert("On the basis of information entered, it is likely that you \nwill exceed the Inland Revenue limits. \nWe recommend you seek independent financial advice.");
			return false;
		}
		else if (result == "Error2a") {
			alert("On the basis of information entered, it is likely that you \nwill exceed the Inland Revenue limits. \nWe recommend you seek independent financial advice.");
			return false;
		}
		else if (result == "Error2b") {
			alert("On the basis of information entered, it is likely that you \nwill exceed the Inland Revenue limits. \nWe recommend you seek independent financial advice.");
			return false;
		}
	}
	return true;
}
