// JavaScript Document
//function for calculating the amortization
function getAmortization(a,n,p) {
	var i=0;
	var frm = document.MORTGAGE;
	var sATline="";
	var oAmortizationTable=document.getElementById("amortizationtable");
	var sCR=String.fromCharCode(13);
	var sTab=String.fromCharCode(27);

	/* Calculate amortization and write table to text area **/
	var payment = getPayment(a,n,p);
	document.getElementById('PAYMENT').value = currency(Math.round(payment*100)/100);
}

function getSpaces(n) {
	var i=0; 
	var sSpaces="";
	for (i=0;i<n;i++) {sSpaces += " ";}
	return sSpaces;
}

//function for getting the payment
function getPayment(a,n,p) {
	/* Calculates the monthly payment from annual percentage
	   rate, term of loan in months and loan amount. **/
	var acc=0;
	var base = 1 + p/1200;
	for (i=1;i<=n;i++) 
		{ acc += Math.pow(base,-i); }
	return a/acc;
}
	
//function for returning the currency value
function currency(pNum) {
  //-- Returns passed number as string in $xxx,xxx.xx format.
	var tRtnValue = "";
	if (pNum != "") {
		var n = pNum.toString().replace(/\$|\,/g,'');
		if (isNaN(n)) {n = "0";}
		var tSign = (n == (n = Math.abs(n)));
		n = Math.floor(n * 100 + 0.50000000001);
		var tCents = n % 100;
		if (tCents < 10) {tCents = "0" + tCents;}
		n = Math.floor(n / 100).toString();
		for (var i = 0; i < Math.floor((n.length - (1 + i)) / 3); i++) {
			n = n.substring(0, n.length - (4 * i + 3)) + ',' + n.substring(n.length - (4 * i + 3));
		}
		tRtnValue = (((tSign)?'':'-') + '$' + n + '.' + tCents);
	}else{
		tRtnValue = 0;
	}
	return tRtnValue;
}

//validation of the number
function valid_number(string) {
	var sTmp = "";
	var sValid = "0123456789.";

	for (var i=0; i< string.length; i++) {
 		if (sValid.indexOf(string.charAt(i)) != -1 )  
 			sTmp += string.charAt(i);
	}
	return(sTmp)
}
function showPopup(drp) 
{
	var url = drp;
	var w   = 620;
	var h   = 340; 
	if (arguments.length == 3){
		w   = width;
		h   = height;
	}
	
	
	if (document.all)
	var xMax = screen.width, yMax = screen.height;
	else
	{ 
	if (document.layers)
	var xMax = window.outerWidth, yMax = window.outerHeight;
	else
	var xMax = 640, yMax=480;
	}
	if (w>xMax) w = xMax * .9;
	if (h>yMax) h = yMax * .9;
	var l = (xMax - w)/2, t = (yMax-h)/2;
	
	window.open(url,"_blank",'screenX='+l+',left='+l+',screenY='+t+',top='+t+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,fullscreen=0,width='+w+',height='+h);
}

function replaceAll(oldStr,findStr,repStr) {
  var srchNdx = 0;  
  var newStr = ""; 
  while (oldStr.indexOf(findStr,srchNdx) != -1)  							
  {
	newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));							
	newStr += repStr;
	srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);           
  }
  newStr += oldStr.substring(srchNdx,oldStr.length);          
  return newStr;
}

function perform(){
	var newAmount = replaceAll(document.getElementById('amount').value,',','');
	newAmount = replaceAll(newAmount,'$','');
	getAmortization(newAmount,document.getElementById('term').value,document.getElementById('apr').value)
}


