// JavaScript Document

function getInterstPayments()
{
	var outstandingBalance = document.f.amount.value.replace(",","");
	var currentInterestRate = document.f.apr.value;
  
  if (outstandingBalance == "") {
    alert( "Please enter the outstanding balance of your loan" );
    return false ;
  }
  if (currentInterestRate == "") {
    alert( "Please enter the your interest rate" );
    return false ;
  }
 
  displayInterstPayments(outstandingBalance, currentInterestRate);
}

function  displayInterstPayments(outstandingBalance, currentInterestRate){

var B2,B4,B6;

B2 = outstandingBalance;
B4 = currentInterestRate/100;

document.f.intonly.value = (Math.round(B2*B4*100/12))/100;

}

function calculate()
{
	show('results');
	var d=document.f;
	amount=d.amount.value;
	apr=d.apr.value;
	n=d.n.value;
	npy=d.npy.value;
	
	if( (amount!='') && (n!='') && (apr!='') && (npy!='') )
	{
		tmp=Math.pow((1+(apr/100/npy)), (n*npy));
		payment=(amount*tmp*(apr/100/npy))/(tmp-1);
		if((!isNaN(payment))&&(payment!=Number.POSITIVE_INFINITY)&&(payment!=Number.NEGATIVE_INFINITY))
		{
			d.payment.value=round(payment);
			d.totpaid.value=round(payment*n*npy);
			d.intpaid.value=round((payment*n*npy)-amount);
		}
		else
		{
			alert('Error: One or more fields contain data\nwhich cannot be used in the calculation. \n\nCheck you have only used numbers and \ndecimal points in the text fields.');
		}
	}
	else
	{
		alert('Error:\nYou did not provide enough data.');
	}
}

function round(val)
{
	tmp=Math.round(val*100)/100+'';
	if(tmp.indexOf('.')==-1)tmp+='.00';
	else if(tmp.length-tmp.indexOf('.')==2)tmp+='0';
	return tmp;
}

function resetall()
{
	var d=document.f;
	d.amount.value='';
	d.apr.value='';
	d.n.value='';
	d.npy.value=12;
	d.payment.value='';
	d.totpaid.value='';
	d.intpaid.value='';
	d.amount.focus();
}

function printer()
{
	if((navigator.appVersion.indexOf("4.") != -1) && (navigator.appName.indexOf("Netscape") != -1)){
	print();}
}

function buildtable()
{
	tablebuilt=true;
	txt='<html><head><title>Payment Schedule</title></head><body bgcolor="#FFFFFF"><center><form>';
	txt+='<input type="button" value="Close" onClick="window.close()"><br><br><em>to print this table, right-click with your mouse and<br>select print from the context menu...</em><br><br>';
	txt+='<table bgcolor="#FFFFFF" border="0" cellpadding="4" cellspacing="3">';
	txt+='<tr><strong><td align="center">Payment<br>Number</td><td align="center">Monthly<br>payment</td><td align="center">Principal<br>amount</td><td align="center">Interest<br>amount</td><td align="center">Remaining<br>balance</td></strong></tr>';
	amount=eval(amount);
	for(i=1;i<=n*npy;i++)
	{
		tbldata='<td bgcolor="'+((i%2!=0) ? '#F3E3E8' : '#EBD0D9')+'" align="right">';
		interest=amount*apr/npy/100;
		amount+=interest;
		principle=payment-interest;
		amount-=payment;
		txt+='<tr>'+tbldata+i+':</td>'+tbldata+round(payment)+'</td>'+tbldata+round(principle)+'</td>'+tbldata+round(interest)+'</td>'+tbldata+round(amount)+'</td></tr>';
	}
	txt+='</table><br><br><input type="button" value="Close" onClick="self.close()"></center></form></body></html>';
	var psch=window.open('', 'viewsch' ,'top=0,left=0,toolbar=no,scrollbars=yes,resizable=yes,width=500,height=450,menubar=no,status=no');
	psch.document.write(txt);
}

