<!--

function Calculate(){
var contFlag = 0;
var fldApp1 = document.CalcForm.txtApp1.value;
var fldApp2 = document.CalcForm.txtApp2.value;
fldApp1 = fldApp1.replace("£","");
fldApp1 = fldApp1.replace(",","");
fldApp1 = parseFloat(fldApp1);
fldApp2 = fldApp2.replace("£","");
fldApp2 = fldApp2.replace(",","");
fldApp2 = parseFloat(fldApp2);


if (fldApp1=='') {
	alert('Please enter Applicant 1\'s gross annual salary');
	document.CalcForm.txtApp1.focus();
	contFlag = 1;
}
if (isNaN(fldApp1)) {
	alert('Applicant 1\'s gross annual salary must be a number');
	document.CalcForm.txtApp1.focus();
	contFlag = 1;
}
if (!fldApp2==''){
	if (isNaN(fldApp2)) {
		alert('Applicant 2\'s gross annual salary must be a number');
		document.CalcForm.txtApp2.focus();
		contFlag = 1;
	}
}

if (contFlag == 0){
	var App1, App2, Lend1, Lend2, AppTotal,App1R,App2R;
	App1R = document.CalcForm.txtApp1.value.replace(",","");
	App1R = App1R.replace("£","");
	App2R = document.CalcForm.txtApp2.value.replace(",","");
	App2R = App2R.replace("£","");
	App1 = parseFloat(App1R);
	App2 = parseFloat(App2R);
	
	if (document.CalcForm.txtApp2.value==''){
		App2 = 0;
	}
	AppTotal = App1 + App2;
	//work out both min values
	Lend1a = AppTotal * 3;
	Lend1b = (App1 * 3.5) + App2;
	//work out both max values
	Lend2a = AppTotal * 4;
	Lend2b = (App1 * 4.5) + App2;
	
	if (Lend1a >= Lend1b){
		Lend1 = Lend1a;
	}else{
		Lend1 = Lend1b;
	}
	
	if (Lend2a >= Lend2b){
		Lend2 = Lend2a;
	}else{
		Lend2 = Lend2b;
	}
	
	Lend1 = "£" + Lend1;
	Lend2 = "£" + Lend2;
	
	
	document.CalcForm.txtLend1.value = Lend1;
	document.CalcForm.txtLend2.value = Lend2;  
}
return false;
}

function checkNumber(input, min, max, msg) {

		msg = msg + " is invalid. ";

//check that entry is a number
		var str = input;
		for (var i = 0; i < str.length; i++) {
			var ch = str.substring( i, i + 1)
			if ((ch < "0" || "9" < ch) && ch != '.') {
				alert(msg);
				return false;
			}
		}

//check entry is in a sensible range
		var num = 0 + str
		if (num < min || max < num) {
			alert(msg + " Please enter a value in the range [" + min + ".." + max + "]");
			return false;
		}
		input.value = str;
		return true;
	}

	function computeFieldCol1(input) {
		if (input.value != null && input.value.length != 0)
		{
			input.value = "" + eval(input.value);
		}
		computeFormCol1(input.form);
	}

	function computeFieldCol2(input) {
		if (input.value != null && input.value.length != 0)
		{
			input.value = "" + eval(input.value);
		}
		computeFormCol2(input.form);
	}

	function computeFieldCol3(input) {
		if (input.value != null && input.value.length != 0)
		{
			input.value = "" + eval(input.value);
		}
		computeFormCol3(input.form);
	}


	function replacestring(){
	var A1=document.RepayForm.A1.value;
	
	A1 = A1.replace("£","");
	A1 = A1.replace(",","");
	A1 = parseInt(A1);
	
	document.RepayForm.A1.value = A1;
	
	
	}


	function computeFormCol1(form) {
		var A1=document.RepayForm.A1.value;
		var R1=document.RepayForm.R1.value;
		var T1=document.RepayForm.T1.value;
		var goFlag = 0;
		
		A1 = A1.replace("£","");
		A1 = A1.replace(",","");
		A1 = parseFloat(A1);
		
		
		if (A1==''){
		alert('Please enter the mortgage amount');
		document.RepayForm.A1.focus();
		goFlag = 1;
		return false;
		}
		if (isNaN(A1)){
		alert('Mortgage amount must be a number');
		document.RepayForm.A1.focus();
		goFlag = 1;
		return false;
		}
		
		if (R1==''){
		alert('Please enter the interest rate');
		document.RepayForm.A1.focus();
		goFlag = 1;
		return false;
		}
		if (isNaN(R1)){
		alert('Interest rate must be a number');
		document.RepayForm.R1.focus();
		goFlag = 1;
		return false;
		}
		
		if (goFlag==0){
		

		//making sure that an entry has been made in each field.
		if ((A1 == null || A1.length == 0) ||
			(R1 == null || R1.length == 0) ||
			(T1 == null || T1.length == 0)) 
		{
			return false;
		}

		 //making sure that entries are valid by using check number
		if (!checkNumber(A1, 1, 999999, "Amount") ||
			!checkNumber(R1, .001, 100, "Interest Rate") ||
			!checkNumber(T1, 1, 35, "Term")) 
		{
			form.Cm1.value = "Invalid";
			return false;
		}

		// maths et al to be computed
		R1 = R1 / 100;
		var P = 1 + (((A1*R1)/12*12) * (1/(1-(Math.pow(1/(1+R1/12),T1*12)))))/12;
		document.RepayForm.Cm1.value = poundsPence( P );
		P = 1 + (A1*R1)/12;
		document.RepayForm.CI1.value = poundsPence( P );
		}
		return false;
	}

	function poundsPence( N ) {
		if ((navigator.appName.indexOf('Microsoft')>-1)
			&& (navigator.appVersion.indexOf('3.0')>-1) )
		{
			return N;
		}
		S = new String( N );
		var i = S.indexOf('.');
		if (i != -1) {
			S = S.substr( 0, i );
			if (S.length-i < 0)
				S = S + '0';
		}
		S = "£" + S;
//		S = S+1;
		return S;
	}

	//clears form
	function clearFormCol1(form) {
		form.A1.value = "";
		form.T1.value = "";
		form.R1.value = "";
		form.Cm1.value = "";
		form.CI1.value = "";
	}
//-->
