// JavaScript
// Mortgage Calculator Common functions
// --------------------------------------------
/*
IN:
	la = loan amount
	ir = interest rate
	yr = duration in years
RETVAL:
	payments per period
*/
function docalc(la, ir, yr) {
 BSMV = 0; // Tax on interest earned.
 
 if (ir==0) {
	return (la/yr)/12; 
 }
 
  var mi = ir/ 100  * (1+BSMV);
  var base = 1;
  var mbase = 1 + mi;
  
  for (i=0; i<yr * 12; i++) {
    base = base * mbase;
  }
  var payperperiod = la * mi / ( 1 - (1/base)) ;
  return payperperiod;
}

// --------------------------------------------
/*
IN:
	num - the number to format
	decimalNum - the number of decimal places to format the number to
	bolLeadingZero - true / false - display a leading zero for numbers between -1 and 1
	bolParens - true / false - use parenthesis around negative numbers
	bolCommas - put commas as number separators.
RETVAL:
	The formatted number
*/
function formatnumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas) { 
    var digitsep = '.';		// character used to separate thousands
	var decimalsep = ',';	// decimal point separator
	
    if (isNaN(parseInt(num))) return "0";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0) {
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
	}
	
	// See if we need to put in the thousands separators
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;
		iStart -= 3;
		while (iStart >= 1) {
		  tmpNumStr = tmpNumStr.substring(0,iStart) + digitsep + tmpNumStr.substring(iStart,tmpNumStr.length)
		  iStart -= 3;
		}		
	}
	
	// Replace the decimal point with the appropriate locale
	var iStart = tmpNumStr.indexOf(".");
	if (iStart >= 0) {
		tmpNumStr[iStart] = decimalsep;
	} 

	// See if we need to use parenthesis
	if (bolParens && num < 0) {
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";
    }

	return tmpNumStr;		// Return our formatted string!
}


/* Refinance Functions */
/* Unused */


// future value
function fv (pv,rate, term) {
  return pv * Math.pow(1+rate, term);
}

// present value
function pv (fv,rate, term) {
  return fv * Math.pow(1+rate, -term);
}


function loan (principal, term, rate) 
{ 
	this.principal = principal; 
	this.term = term; 
	this.rate = rate; 
	this.pay = principal * rate / ( 1 - Math.pow(1+rate, -term) );
	this.amortization =  getaTable(this.principal, this.term, this.rate, this.pay);


	function getaTable(principal, term, rate, pay) {
		
		function row (loanPre, loanPost, loanPostPay, payPrincipal, payInt, cumPayPrincipal, cumPayInt) { 
			this.loanPre = loanPre; 
			this.loanPost = loanPost; 
			this.loanPostPay = loanPostPay; 
			this.payPrincipal = payPrincipal; 
			this.payInt = payInt;  
			this.cumPayPrincipal = cumPayPrincipal; 
			this.cumPayInt = cumPayInt;
		} 
		
		aTable = new Array();
		for (i=0; i<term; i++) {
		   aTable[i] = new row();
		   aTable[i].loanPre = (i==0) ? principal : aTable[i-1].loanPostPay;
		   aTable[i].loanPost = aTable[i].loanPre * (1+rate);
		   aTable[i].loanPostPay = aTable[i].loanPost - pay;
		   aTable[i].payPrincipal = aTable[i].loanPre - aTable[i].loanPostPay;
		   aTable[i].cumPayPrincipal = (i==0) ? aTable[i].payPrincipal : aTable[i-1].cumPayPrincipal + aTable[i].payPrincipal;
		   aTable[i].payInt =  pay - aTable[i].payPrincipal;
		   aTable[i].cumPayInt = (i==0) ? aTable[i].payInt: aTable[i-1].cumPayInt + aTable[i].payInt ;
		}
		
		return(aTable);
	}
	
	
} 

/* Refinance functions - End */