var num      = 0;
var amount   = 0;
var period   = 0;
var months   = 0;
var br = "<BR>";
var tblBeg = "<TD ALIGN='CENTER' VALIGN='TOP'><FONT SIZE='1' FACE='arial, helvetica'>";
var tblEnd = "</FONT></TD>";
var blank    = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
var blank1    = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
var dashes   = "-----------------------------------------------------------------";
var dollar   = "$";

function iA(){
	this.length = iA.arguments.length;
	for( var index = 0; index < this.length; index++ )
		this[index] = iA.arguments[index];
	}

var nPower   = new iA(10);
var fDecimal = new iA(16);

nPower[0] = 1;
	for( var index = 0; index < 9; index++ )
		nPower[index+1] = nPower[index] * 10;
	
fDecimal[0] = .1;
for( index = 1; index <= 15; index++ )
	fDecimal[index] = fDecimal[index-1] * .1;
/*	
fDecimal[1]  = .01;
fDecimal[2]  = .001;
fDecimal[3]  = .0001;
fDecimal[4]  = .00001;
fDecimal[5]  = .000001;
fDecimal[6]  = .0000001;
fDecimal[7]  = .00000001;
fDecimal[8]  = .000000001;
fDecimal[9]  = .0000000001;
fDecimal[10] = .00000000001;
fDecimal[11] = .000000000001;
fDecimal[12] = .0000000000001;
fDecimal[13] = .00000000000001;
fDecimal[14] = .000000000000001;
fDecimal[15] = .0000000000000001;
*/   
var numerals = "01234567890";
var sTmp    = "";
var sString = "";

function stn(){
	num = 0;
	position = sString.indexOf(".");
	sSuffix  = "";
	
	if( position > -1){
		sSuffix = sString.substring(position + 1, sString.length);
		sString = sString.substring(0, position);
	}
	
	strLen = sString.length;
	
	for( var index = strLen - 1; index > -1; index-- ){
		sTmp = sString.substring(index, index + 1);
		position = numerals.indexOf(sTmp);
		num += position * nPower[strLen - index - 1];
	}
	
	if( sSuffix != "" && sSuffix.length > dp){
		position = numerals.indexOf(sSuffix.charAt(dp + 1));
		
		if( position > 4 ){
		position = numerals.indexOf(sSuffix.charAt(dp));
		sSuffix = sSuffix.substring(0, dp - 1) + (position + 1);
		}
	}
	
	if( sSuffix != "" ){
		for( var index = 0; index < dp; index++ ){
			sTmp = sSuffix.substring(index, index + 1);
			position = numerals.indexOf(sTmp);
			num += position * fDecimal[index];
		}
	}
}

function testIt(form){
	sString = document.mortForm.amount.value;
	formatStr();
	bl = sString.length + 3;
	dp = 2;
	stn();
	amount = num;
	sString = document.mortForm.period.value;
	dp = 4;
	stn();
	period = num;
	sString = document.mortForm.months.value;
	dp = 0;
	stn();
	months = num;
	
	if( months < 1 || months > 999 || period < .0001 || period > 99 || amount < 1 || amount > nPower[9] )
		alrt();
	else
		computeForm();
}

function computeForm(){
	ls = "";
	vMortForm = 1;
	index = period / 12 / 100;
	fpv = 0;
	for( var j = 0; j < months; j++)
		vMortForm=vMortForm * (1 + index);
		
	tmp = (amount * vMortForm * index) / (vMortForm - 1);
	fpv += tmp;
	fCalc = ((months * fpv) - amount);

	prtSched();
}
 
function prtSched(){
	fpv += .01;
	sString = ""
	sString += fpv;
	formatStr();
	dp = 2;
	stn();
	fpv = num;
	pct = period / 12 / 100;
	if( bl < 14 )
		bl=14;

	ls = "";
		 
	ls2 = "Amortization Schedule: " + document.mortForm.months.value +
		 " months to repay " + dollar + document.mortForm.amount.value +" at " + document.mortForm.period.value + "%.";

	for( var j = 0; j < months; j++ ){
		ntr = (amount * pct);
		sString = "";
		sString += ntr;
		formatStr();
		ntr1 = dollar + sString;
		prp = fpv - ntr;
		
		if( prp > amount )
			prp = amount;

		sString = "";
		sString += prp;
		formatStr();
		prp1 = dollar + sString;
		amount -= prp;
		sString = "";
		sString += amount;
		formatStr();
		amount1 = dollar + sString;
		
		if( fpv > (ntr + prp) )
			fpv = ntr + prp;

		sString = "";
		sString += fpv;
		formatStr();
		fpv1 = dollar + sString;
		sString = "";
		sString += (j + 1) + ".";
		ls += 	"<TR>" + tblBeg + sString + br + tblEnd +
				tblBeg + fpv1 + br + tblEnd +
				tblBeg + ntr1 + br + tblEnd +
				tblBeg + prp1 + br + tblEnd +
				tblBeg + amount1 + br + tblEnd + "</TR>";
	}
	
	var winStr = ls;
	
	var header="<HTML><HEAD><TITLE>Bank of Maple Plain Ammortization Schedule</TITLE></HEAD>"+
	"<BODY BGCOLOR='ffffff' leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>"+

	"<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td width='339' background=/img/calcWin/bg.gif>"+
	"<img src='/img/calcWin/h1-1.jpg' width='339' height='36'></td>"+
	"<td align='right' background=/img/calcWin/bg.gif>"+
	"<a href='javascript:if(self.opener!=null) self.opener.focus(),window.close()'>"+
	"<img src='/img/calcWin/closeWin.gif' width='71' height='36' border=0></a></td></tr></table>"+

	"<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr valign='top'>"+
	"<td><img src='/img/calcWin/h2-1.jpg' width='87' height='36'>"+
	"<img src='/img/calcWin/subHead.gif' width='254' height='21' align='top'></td></tr></table>"+

	"<TABLE WIDTH=100% BORDER=0><TR><TD><CENTER>"+
	"<TR><TD ALIGN=CENTER COLSPAN=5>"+
	"<img src='/img/calcWin/ammortHead.gif' width='175' height='25'><BR>"+
	"</TD></TR>"+
	"<TR><TD COLSPAN=5 ALIGN=CENTER><FONT SIZE='1' FACE='arial, helvetica'>"+
	ls2+
	"</FONT></TD></TR>"+
	"<TR><TD COLSPAN=5 ALIGN=CENTER><img src='/img/redPixel.gif' width='400' height='1' border='0'></td></tr>"+
	"<TR><TD ALIGN=CENTER WIDTH=20%><FONT SIZE='1' FACE='arial, helvetica'>Payment No.</FONT></TD>"+
	"<TD ALIGN=CENTER WIDTH=20%><FONT SIZE='1' FACE='arial, helvetica'>Payment Amt.</FONT></TD>"+
	"<TD ALIGN=CENTER WIDTH=20%><FONT SIZE='1' FACE='arial, helvetica'>Interest</FONT></TD>"+
	"<TD ALIGN=CENTER WIDTH=20%><FONT SIZE='1' FACE='arial, helvetica'>Principal</FONT></TD>"+
	"<TD ALIGN=CENTER WIDTH=20%><FONT SIZE='1' FACE='arial, helvetica'>Balance</TD></TR>";
	
	var footer="</TABLE></BODY></HTML>"
	
	var newWindow=
	window.open("","","status,scrollbars,height=400,width=450");
	newWindow.document.write(header+winStr+footer);
	newWindow.document.close();
}

function formatStr(){
	position = sString.indexOf(".");
	if( position == 0 )
	{
		sString = "0" + sString;
		position++;
	}
	else if( position < 0){
		sString += ".00";
		position = sString.indexOf(".");
	}
	
	sString += "0000";
	sString = sString.substring(0, position + 4);
	sTmp = sString.charAt(sString.length - 1);
	position = numerals.indexOf(sTmp);
	sString = sString.substring(0, sString.length - 1);
	
	if( position > 5 )
		formatStr1();
}

function formatStr1(){
	for( var k = sString.length - 1; k > -1; k-- ){
		sTmp = sString.charAt(k);
		nPosition = numerals.indexOf(sTmp);
		if( nPosition < 0 )
			k--;
		else{
			sString = sString.substring(0, k) + numerals.substring(nPosition + 1,nPosition + 2) +
					  sString.substring(k + 1, sString.length);
			
			if( nPosition != 9 )
				k = -1;
		}
	}
}

function alrt(){
	alert("Months must be from 1 to 999, Loan amount from 1 to " + nPower[9] +
		  ", and Interest from .001 to 99%.");
}
