function isNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   var dot=false;

 
  	for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      	if (ValidChars.indexOf(Char) == -1){
         	IsNumber = false;
        }
		if (ValidChars.indexOf(Char) == 10){
			if(dot==true)
				return false;
         	dot = true;
        }
		
      }
   	return IsNumber;
   
}

function compute() {
	var  f  = document.getElementById('first').value;
	var  s  = document.getElementById('second').value;

	if(!isNumeric(s) || !isNumeric(f)){
		alert('Please enter a valid number');
		return false;
	}
	
	if(s != 0) {
		borrowamt = f*3.99 + s*3.99;
	} else {
		borrowamt = 4.49 * f;
	}

	if(0==Math.round(borrowamt)) {
		document.getElementById('result').innerHTML = '';
	} else {
		document.getElementById('result').innerHTML = "<b><br>You can borrow &pound;"+addCommas(borrowamt.toFixed(2))+"</b><br><br>The mortgage amount shown is intended as a guide only. The actual amount you will be able to borrow could be more or less than this figure and depends upon your personal circumstances.";
		document.getElementById('loan').value = borrowamt.toFixed(2);
 	}
	return false;
}

function compute1() {
	var  l  = document.getElementById('loan').value;
	var  t  = document.getElementById('term').value;
	var  r  = document.getElementById('rate').value;

	if(!isNumeric(l) || !isNumeric(t) || !isNumeric(r)){
		alert('Please enter a valid number');
		return false;
	}

	repayment = (l*r/100)/((1-(1/Math.pow(1+r/100/12,(t*12)/1)))*12);
	interest = (l*r/100)/12;

	if(l<=0 || t<=0 || r<=0)	
		document.getElementById('result1').innerHTML = '';
	else
		document.getElementById('result1').innerHTML = "<br><b>Interest Rate : "+r+"%<br><br>Repayment: &pound;"+addCommas(repayment.toFixed(2))+"<br>Interest Only: &pound;"+addCommas(interest.toFixed(2))+"</b><br><br>The monthly payment shown is intended as a guide only. The actual amount you will have to pay each month could be more or less than this figure.<br><br>The figures shown for repayment of an interest only mortgage only refer to repayment of the interest. The cost of repayment of the amount borrowed is not included.";

		
	return false;
}

function addCommas( strValue ) {
/************************************************
DESCRIPTION: Inserts commas into numeric string.

PARAMETERS:
strValue - source string containing commas.

RETURNS: String modified with comma grouping if
source was all numeric, otherwise source is
returned.

REMARKS: Used with integers or numbers with
2 or less decimal places.
*************************************************/
var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');

//check for match to search criteria
while(objRegExp.test(strValue)) {
//replace original string with first group match,
//a comma, then second group match
strValue = strValue.replace(objRegExp, '$1,$2');
}
return strValue;
}
