
function doCalc()
	{
	var noerror = true;
	var errNum = 0;
	
	var errormsg = 'The repayment amount could not be calculated. \n\n'
		errormsg = errormsg + 'Please supply the following information and re-calculate - \n'
	
	if (document.frm.txtAmount.value == ''|| isNaN(document.frm.txtAmount.value))
		{
		errNum++
		errormsg = errormsg + errNum +'. Please enter a numeric value for the loan amount. \n'
		noerror = false;
		}
	
	if (document.frm.txtRate.value == ''|| isNaN(document.frm.txtRate.value))
		{
		errNum++
		errormsg = errormsg + errNum +'. Please enter a numeric value for the interest rate. \n'
		noerror = false;
		}
		
	if (document.frm.txtTerm.value == '' || isNaN(document.frm.txtTerm.value))
		{
		errNum++
		errormsg = errormsg + errNum +'. Please enter a numeric value for the loan term. \n'
		noerror = false;
		}

	if (noerror)
		{
		var rate = document.frm.txtRate.value
		var per = 12
		var nper = document.frm.txtTerm.value * 12
		var pv = - document.frm.txtAmount.value * 12
		var fv = 0
		
		if (document.frm.selType.options[document.frm.selType.selectedIndex].value == "pi") // principal & interest loan calculations
			{
			pmt(rate, per, nper, pv, fv);
			}
		else if (document.frm.selType.options[document.frm.selType.selectedIndex].value == "io") // for interest only loan calculations
			{
			var ipv = document.frm.txtAmount.value 
			ipmt = ipv * (rate / 100) / per
			document.frm.txtRepayment.value = "$" + displayCents(Math.round(ipmt*Math.pow(10,2))/Math.pow(10,2))
			}
		}
	else
		{
		alert(errormsg);
		}
	}	
						
function pmt(rate, per, nper, pv, fv)
	{	
	if (per == 0 || nper == 0)
		{
		pmt_value = 0
		}

		rate = rate/(per * 100)
		
	if (rate == 0)
		{ 
		pmt_value = - (fv + pv)/nper
		}
	else 
		{x = Math.pow((1 + rate),nper)
		pmt_value = -((rate * (fv + x * pv))/(-1 + x)) / 12
		}
	
	document.frm.txtRepayment.value = "$" + displayCents(Math.round(pmt_value*Math.pow(10,2))/Math.pow(10,2));
	
	}

function displayCents(amount) {
// returns the amount in the .99 format
    amount -= 0;
    return (amount == (Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount));
}
