/*
amortization.js
Copyright (c) 2007 Vertex42, LLC. All Rights Reserved.
*/


//You Need a Javascript Enabled Browser// <script> 
//<!--- Hide Script from Old Browsers

function isNum(x){
	filter = /(^\d+\.?$)|(^\d*\.\d+$)/;
	if (filter.test(x)) {
		return true;
	}
	return false;
}

//Remove the $ sign if you wish the parse number to NOT include it
function tocurrency(passedVal){
	prefix = "";
	wd = "w";
	var tempnum = passedVal.toString();
	for (i=0; i < tempnum.length; i++){
		if (tempnum.charAt(i)=="."){
			wd = "d";
			break;
		}
	}
	if (wd=="w") {
		return prefix+tempnum+".00";
	}
	else{
		if (tempnum.charAt(tempnum.length-2)=="."){
			return prefix+tempnum+"0";
		}
		else{
			tempnum = Math.round(tempnum*100)/100;
			return prefix+tempnum;
		}
	}
}

function amort_calc(p,r, n) {
	// Validate the P field
	if (!isNum(p)) {
		//alert("Please enter a valid number for the Loan Amount (P).")
		return false;
	}
	// Validate the r field
	if (!isNum(r)) {
		//alert("Enter an Interest Rate (r) as 0.01 for 1%.")
		return false;
	}
	// Validate the N field
	if (!isNum(n)) {
		//alert("Please enter the Total Number of Payments (N).")
		return false;
	}

	var P = parseFloat(p);
	var R = parseFloat(r);
	var N = parseFloat(n);
	var A = P*(r*Math.pow(1+r,N))/(Math.pow(1+r,N)-1);

	A = tocurrency(A);
	return A;
}

function PMT(rate, numberPeriods, presentValue, futureValue, isDueAtBigin) {
if (numberPeriods == 0)
return 0;

if (rate == 0)
return ((-futureValue - presentValue) / numberPeriods);

var auxiliaryRate1 = (isDueAtBigin ? 1 + rate : parseFloat(1));

var auxiliaryRate2 = Math.pow((rate + 1), numberPeriods);

return (((-futureValue - (presentValue * auxiliaryRate2)) / (auxiliaryRate1 * (auxiliaryRate2 - 1))) * rate);
}


// </script>
// End Hiding Script -->
