function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function HoverOff(theObject,theColor)
{
	var thePage = document.getElementsByName("PageName");
	if (theObject.innerText!=thePage[0].innerText)
		{
		theObject.style.color=theColor;
		}
}

function round(number,X) 
{
	// rounds number to X decimal places, defaults to 2
	X = (!X ? 2 : X);

	var sValue, sTemp, i;

	sValue = Math.round(number*Math.pow(10,X))/Math.pow(10,X);
	sValue = sValue.toString();

	i = sValue.indexOf(".");
	sTemp = sValue.substr(i + 1);

	if(sTemp.length < X) 
	{
		while(sTemp.length < X) 
		{
			sTemp += "0";
			sValue += "0";
		}
	}
	
	return sValue;
}

function CalculateAnnualInvestment() 
{
	var iYearsOfAttendance = 4;
	var oResult = document.getElementById("result")
	var fTuition = parseFloat(document.getElementById("tuition").value);
	var iYears = parseInt(document.getElementById("years").value);
	var fInflation = getPercentage(document.getElementById("inflation").value);
	var fInterest = getPercentage(document.getElementById("interest").value);
	var i;
	var f;
	var fNeeded = 0.0;
	var fRunningTotal = fTuition;

	// inflate the price
	for(i = 0; i < iYears;i++) 
	{
		f = fInflation * fRunningTotal;
		fRunningTotal += f;
	}

	// get the amount we need, inflating the price each year
	for(i = 0; i < iYearsOfAttendance;i++) 
	{
		fNeeded += fRunningTotal;
		f = fInflation * fRunningTotal;
		fRunningTotal += f;
	}

	// pmt = (f * i) / ((1 + i)^n - 1);
	f = (fNeeded * fInterest) / (Math.pow((1 + fInterest), iYears) - 1);
	oResult.value = round(f);
}

function CalculateNeededAtRetirement() 
{
	var oIncome = document.getElementById("reqincome")
	var oYears = document.getElementById("years")
	var oYearsAfter = document.getElementById("yearsafter")
	var oInflation = document.getElementById("inflation")
	var oInterest = document.getElementById("interest")
	var oResult = document.getElementById("result")
	var fIncome = parseFloat(oIncome.value);
	var iYears = parseInt(oYears.value);
	var iYearsAfter = parseInt(oYearsAfter.value);
	var fInflation = getPercentage(oInflation.value);
	var fInterest = getPercentage(oInterest.value);
	var f;
	var fRunningTotal = 0;

	//F = P(1 + r)^n
	fIncome = fIncome * (Math.pow((1 + fInflation), iYears));

	var fy, fi;

	fi = 1 + fInflation;
	fy = 1 + fInterest;

	fRunningTotal = fIncome / ( (1 - (fi / fy)) / (1 - Math.pow((fi / fy), iYearsAfter)) )

	f = fRunningTotal;

	oResult.value = round(f);
}

function CalculateMonthlyPayment()
{
	// payment = principle * monthly interest/(1 - (1/(1+MonthlyInterest)*Months))
	if ((document.calc.loan.value == null || document.calc.loan.value.length == 0) || (document.calc.months.value == null || document.calc.months.value.length == 0) || (document.calc.rate.value == null || document.calc.rate.value.length == 0))
	{ 
		document.calc.result.value = "Incomplete Data";
	}
	else
	{
 		var princ = document.calc.loan.value;
	 	var term  = document.calc.months.value;
 		var intr   = document.calc.rate.value / 1200;
		var fValue = princ * intr / (1 - (Math.pow(1/(1 + intr), term)));
 		document.calc.result.value = round(fValue);
	}
}

function Clear()
{
	var oResult = document.getElementById("result")
	oResult.value = "";
}

function require_entries() 
{
	alert("At least three fields must be supplied.");
}

function ClearAll()
{
	// the objects
	var oPrincipal = document.getElementById("principal");
	var oFuture = document.getElementById("futurevalue");
	var oRate = document.getElementById("calcrate");
	var oTime = document.getElementById("calctime");

	oPrincipal.value = ""
	oFuture.value = ""
	oRate.value = ""
	oTime.value = ""
}

function getPercentage(sValue) 
{
	var i, sTemp;

	i = sValue.indexOf("%");
	if(i == -1) 
	{
		return parseFloat(sValue);
	}

	sTemp = sValue.substr(0, i);
	return parseFloat(parseFloat(sTemp) / 100);
}

function round(number,X) 
{
	// rounds number to X decimal places, defaults to 2
	X = (!X ? 2 : X);

	var sValue, sTemp, i;

	sValue = Math.round(number*Math.pow(10,X))/Math.pow(10,X);
	sValue = sValue.toString();

	i = sValue.indexOf(".");
	sTemp = sValue.substr(i + 1);

	if(sTemp.length < X) 
	{
  		while(sTemp.length < X) 
		{
    		sTemp += "0";
    		sValue += "0";
		}
	}
  
	return sValue;
}

function solve_equation() 
{
	// the objects
	var oPrincipal = document.getElementById("principal");
	var oFuture = document.getElementById("futurevalue");
	var oRate = document.getElementById("calcrate");
	var oTime = document.getElementById("calctime");

	// final value
	var fVal;

	// temp vars;
	var fPrincipal, fFuture, fRate, fTime;

	if(oPrincipal.value.length == 0) 
	{
		// solving for principal
		if(oFuture.value.length == 0 || oRate.value.length == 0 || oTime.value.length == 0) 
		{
			require_entries();
			return;
		}

		fFuture = parseFloat(oFuture.value);
		fRate = parseFloat(oRate.value);
		fTime = parseFloat(oTime.value);
		fVal = fFuture / (1 + (fRate * fTime));
		oPrincipal.value = round(fVal, 2);
	}

	if(oFuture.value.length == 0) 
	{
		// solving for future value
		if(oPrincipal.value.length == 0 || oRate.value.length == 0 || oTime.value.length == 0) 
		{
			require_entries();
			return;
		}

		fPrincipal = parseFloat(oPrincipal.value);
		fRate = parseFloat(oRate.value);
		fTime = parseFloat(oTime.value);
		fVal = fPrincipal * (1 + (fRate * fTime));
		oFuture.value = round(fVal, 2);
	}

	if(oRate.value.length == 0) 
	{
		// solving for rate
		if(oPrincipal.value.length == 0 || oFuture.value.length == 0 || oTime.value.length == 0) 
		{
			require_entries();
			return;
		}

		fPrincipal = parseFloat(oPrincipal.value);
		fFuture = parseFloat(oFuture.value);
		fTime = parseFloat(oTime.value);
		fVal = (fFuture - fPrincipal) / (fPrincipal * fTime);
		oRate.value = round(fVal, 2);
	}

	if(oTime.value.length == 0) 
	{
		// solving for time
		if(oPrincipal.value.length == 0 || oFuture.value.length == 0 || oRate.value.length == 0) 
		{
			require_entries();
			return;
		}

		fPrincipal = parseFloat(oPrincipal.value);
		fFuture = parseFloat(oFuture.value);
		fRate = parseFloat(oRate.value);
		fVal = (fFuture - fPrincipal) / (fPrincipal * fRate);
		oTime.value = round(fVal, 2);
	}
}

var num=0;
var amt=0;
var per=0;
var months=0;
var nls="";
var rr="\r";
var b="                               ";
var d="-----------------------------"
+"----------------------------------";
var s="$";
function iA(){
 this.length=iA.arguments.length;
 for (var i=0;i<this.length;i++){
  this[i]=iA.arguments[i];
  }
 }
var pwr=new iA(10);
var dec=new iA(16);
pwr[0]=1;
for (var i=0;i<9;i++){
 pwr[i+1]=pwr[i]*10;
}
dec[0]=.1;
dec[1]=.01;
dec[2]=.001;
dec[3]=.0001;
dec[4]=.00001;
dec[5]=.000001;
dec[6]=.0000001;
dec[7]=.00000001;
dec[8]=.000000001;
dec[9]=.0000000001;
dec[10]=.00000000001;
dec[11]=.000000000001;
dec[12]=.0000000000001;
dec[13]=.00000000000001;
dec[14]=.000000000000001;
dec[15]=.0000000000000001;

var ns="01234567890";
var cr="";
var str="";

function stn(){
 num=0;
 pos=str.indexOf(".");
 sfx="";
 if (pos>-1){
  sfx=str.substring(pos+1,str.length);
  str=str.substring(0,pos);
  }
 strl=str.length;
 for (var i=strl-1;i>-1;i--){
  cr=str.substring(i,i+1);
  pos=ns.indexOf(cr);
  num+=pos*pwr[strl-i-1];
  }
  if (sfx!=""&&sfx.length>dp){
   pos=ns.indexOf(sfx.charAt(dp+1));
   if (pos>4){
    pos=ns.indexOf(sfx.charAt(dp));
    sfx=sfx.substring(0,dp-1)+(pos+1);
    }
   }
  if (sfx!=""){
   for (var i=0;i<dp;i++){
    cr=sfx.substring(i,i+1);
    pos=ns.indexOf(cr);
    num+=pos*dec[i];
    }
/*    sfx="";
    sfx+=num;
    pos=sfx.indexOf(".");
    sfx=sfx.substring(pos+1,sfx.length);
    if (sfx.charAt(dp+1)=="9"){
     num+=dec[sfx.length-2];
     } */
  }
 }

function testIt(form){
 str=document.isn.amt.value;
 fmtIt();
 bl=str.length+3;
 dp=2;
 stn();
 amt=num;
 str=document.isn.per.value;
 dp=4;
 stn();
 per=num;
 str=document.isn.months.value;
 dp=0;
 stn();
 months=num;
 if (months<1||months>999||per<.0001||per>99||amt<1||amt>pwr[9]){
  alrt();
  }
 else{
 computeForm();
 }
}

function computeForm(){
 ls="";
 isnnum=1;
 i=per/12/100;
 fpv=0;
 for (var j=0;j<months;j++)
  isnnum=isnnum*(1+i);
  tmp=(amt*isnnum*i)/(isnnum-1);
  fpv+=tmp;
  fcalc=((months*fpv)-amt);

 prtSched();
 }

function prtSched(){
 fpv+=.01;
 str=""
 str+=fpv;
 fmtIt();
 dp=2;
 stn();
 fpv=num;
 pct=per/12/100;
 if (bl<14){
  bl=14;
  }
 ls="Amortization Schedule: "+document.isn.months.value
 +" months to repay "+s+document.isn.amt.value
 +" at "+document.isn.per.value+"%."+rr+d+rr
 +"Payment       Payment       Interest      Principal     Balance"
 +rr
 +"Number        Amount        Amount        Reduction     Due"
 +rr+d+rr;
 for (var j=0;j<months;j++){
  ntr=(amt*pct);
  str="";
  str+=ntr;
  fmtIt();
  ntr1=s+str;
  prp=fpv-ntr;
  if (prp>amt){
   prp=amt;
   }
  str="";
  str+=prp;
  fmtIt();
  prp1=s+str;
  amt-=prp;
  str="";
  str+=amt;
  fmtIt();
  amt1=s+str;
  if (fpv>(ntr+prp)){
   fpv=ntr+prp;
   }
  str="";
  str+=fpv;
  fmtIt();
  fpv1=s+str;
  str="";
  str+=(j+1)+".";
  ls+=b.substring(0,2)+str+b.substring(0,12-str.length)
  +fpv1+b.substring(0,14-fpv1.length)+ntr1
  +b.substring(0,14-ntr1.length)+prp1
  +b.substring(0,14-prp1.length)+amt1
  +rr;
  }
  document.isn.sched.value=ls+d+rr
  +"  * Interest calculated at 1/12th of annual interest rate on"
  +rr+"    the remaining principal amount. (Rounding errors "
  +"possible)."+rr+d+rr;
 }
function fmtIt(){
 pos=str.indexOf(".");
 if (pos==0){
  str="0"+str;
  pos++;
  }
 if (pos<0){
  str+=".00";
  pos=str.indexOf(".");
  }
 str+="0000";
 str=str.substring(0,pos+4);
 cr=str.charAt(str.length-1);
 pos=ns.indexOf(cr);
 str=str.substring(0,str.length-1);
 if (pos>5){
  fmtIt1();
  }
 }
 function fmtIt1(){
  for (var k=str.length-1;k>-1;k--){
   cr=str.charAt(k);
   posn=ns.indexOf(cr);
   if (posn<0){
    k--;
   }
   else{
    str=str.substring(0,k)+ns.substring(posn+1,posn+2)
    +str.substring(k+1,str.length);
    if (posn!=9){
     k=-1;
    }
   }
  }
 }
function alrt(){
 alert("You couldn't know. Months must be from"
 +" 1 to 999, Loan amount from 1 to "+pwr[9]
 +" and Interest from .001 to 99%");
}
