// JavaScript Document

<!-- ;
function isNum(str) 

{

	// checks 'str' contains only digits and '.'

	for (var i = 0; i < str.length; i++) 
	{
		var ch = str.substring(i, i + 1)
		if ((ch < "0" || "9" < ch) && ch != '.') 
		{
			return false
		}

	}
	if ((str.length==0)) 
	{

        	return false
	}
	else 
	{
		return true
	}

}

function twodp(num) 

{

	num="$"+Math.round(100*num)/100;

	if (num.indexOf(".")==-1) num+=".00";

	if (num.indexOf(".")==num.length-2) num+="0";

	return num.substring(1,num.length);

}

function BlankForm() 

{

	document.repaycalc.principle.value=""
	document.repaycalc.apr.value=""
	document.repaycalc.nRepayments.value=""
	document.repaycalc.installment.value=""
	document.repaycalc.totalpb.value=""
	document.repaycalc.interest.value=""

}

function PayCalc() 

{

	msg=""
	ppstring=document.repaycalc.principle.value
	if (!isNum(ppstring)) 
	{
		msg+="The loan amount is not valid.\n"

	}

	aprstring=document.repaycalc.apr.value

	if (!isNum(aprstring)) 
	{
		msg+="The APR is not valid.\n"
	}


	nstring=document.repaycalc.nRepayments.value

	if (!isNum(nstring)) 

	{
		msg+="The number of months is not valid.\n"
	}

	if (msg.length==0)  
	{

		var p=parseFloat(ppstring)
		var n=parseFloat(nstring)
//		var n=parseFloat(nstring)*(365/12)


		var apr=parseFloat(aprstring)
		var r=Math.pow((1+apr/100),1/12)
//		var r=Math.pow((1+apr/100),1/365)
		

		if (apr>0) 

		{

			rn=Math.pow(r,n)
//			rn=Math.pow(r,n)

			
		a=p*(1-r)*rn/(1-rn)

		}

		else  
		{
			a=p/n

		}

		totp=twodp(a*n)
		interest=twodp(a*n-p)
		p=twodp(p)
		a=twodp(a)
		document.repaycalc.principle.value=p
		document.repaycalc.installment.value=a
		document.repaycalc.totalpb.value=totp
		document.repaycalc.interest.value=interest

	}

	else 

	{

		alert(msg+"\nPlease enter only digits or '.' into the left-hand boxes.\n")

	}

}


// end hide -->



