// required JavaScript functions for ThreadneedlePensions.co.uk

function img_act(imgName) {
        imgOn = eval(imgName + "on.src");
        document [imgName].src = imgOn;
}

function img_inact(imgName) {
        imgOff = eval(imgName + "off.src");
        document [imgName].src = imgOff;
}

function spawn(html,x,y,win) {
	remote=window.open(html, win, 'height='+y+',width='+x+',scrollbars=0,resizable=0,menubar=0,toolbar=0,status=0,location=0,directories=0,left=200,top=28');
	if (remote.opener==null) remote.opener=window;
}

function toggleVisibility(id, NNtype, IEtype, WC3type) {
    if (document.getElementById) {
        eval("document.getElementById(id).style.visibility = \"" + WC3type + "\"");
    } else {
        if (document.layers) {
            document.layers[id].visibility = NNtype;
        } else {
            if (document.all) {
                eval("document.all." + id + ".style.visibility = \"" + IEtype + "\"");
            }
        }
    }
}

function Recalculate(h, item, init_flag) {
	if (item == "output1") {
		calc_type = 2;
	} else {
		calc_type = 1;
	}
	
	old_output1 = document.Calculator.ee_cont_rate.value;
	if (validate(h, item)) {
		output1 = PIA_projection(calc_type, 4, document.Calculator.age.value*1, document.Calculator.sex.value, document.Calculator.salary.value*1, document.Calculator.fund.value*1, 0.5, document.Calculator.nra.value*1, document.Calculator.output1.value*1, document.Calculator.ee_cont_rate.value*1, document.Calculator.er_cont_rate.value*1);
		if (calc_type == 2) {
			// save value before calc again...
			document.Calculator.ee_cont_rate.value = output1;
		}
		output2 = PIA_projection(4, 4, document.Calculator.age.value*1, document.Calculator.sex.value, document.Calculator.salary.value*1, document.Calculator.fund.value*1, 0.5, document.Calculator.nra.value*1, document.Calculator.output1.value*1, document.Calculator.ee_cont_rate.value*1, document.Calculator.er_cont_rate.value*1);
		if (check_error(output1)) {
			if (check_error(output2)) {
				if (item == "output1") {
					if (isNaN(output1)) {
						output1 = 0;
					}
					// document.Calculator.ee_cont_rate.value = Math.round(output1);
					document.Calculator.ee_cont_rate.value = output1;
					document.Calculator.output2.value = "£" + output2;
				} else {
					if (isNaN(output1)) {
						output1 = 0;
					};
					// document.Calculator.output1.value = Math.round(output1);
					document.Calculator.output1.value = output1;
					document.Calculator.output2.value = "£" + output2;
				}
				// check if calc_type 2 and output1 = 0, don't allow pension to be moved anymore by returning false
				if ((calc_type == 2) && (output1 == 0)) {
					return false;
				}
				return true;
			} else {
				// if the init_flag is true then this calculation has been called for the fist time
				// if an error has occurred then take the user back to the 1st calculator page
				if (init_flag) { document.RedirectForm.submit(); }
				return false;
			}
		} else {
			// restore old value...
			document.Calculator.ee_cont_rate.value = old_output1;
		}
	} else {
		return false;
	}
}
