//Copyright 2000. Compendium Research Corporation. All Rights Reserved.

// <SCRIPT LANGUAGE = "JavaScript">
<!--Hide

var bDebug = 0 ;        // change to 1 for extra output
var sNl = "\r";
var sOut = "" ;         // html output string
var nErrorNumber = -1.2345e-12 ;

var sSub = "%s" ;

var shStart = "<html>" + sNl
      + "<head><title>%s</title>" + sNl
      + "<style type='text/css'>" + sNl
      + "<!--" + sNl
      + "/* make all text the same font */" + sNl
      + "TD {FONT-FAMILY: Rockwell} " + sNl
      + "TH {FONT-FAMILY: Rockwell} " + sNl
      + "-->" + sNl
      + "</style>" + sNl
      + "<scr" + "ipt LANG" + "UAGE='JavaS" + "cript' src='monpay.js'></scr" + "ipt>" + sNl
      + "</head>" + sNl
      + "<body>" + sNl
      + "<BASEFONT FACE='Rockwell'>";

var shHeading1 =  "<TABLE border=0 cellpadding=0 "
                + "cellspacing=0 width=500>" + sNl
      + "<br><TR><TD>"
      + "<font size=+2><b>%s</b></font></TD></TR></TABLE><br>" ;

var shHeadJpg = "<TABLE width=500 cellpadding=0 cellspacing=0>" + sNl
      + "<TR><TD><img src= eagle.jpg " + sNl
      + "width=150 height=200></TD></TR></TABLE>";
	  
var strVariable = "Return to Our Homepage";
      strVariable = strVariable.link("index.html");

var shHeading2 =  "<TABLE border=0 cellpadding=0 "
                + "cellspacing=0 width=500>" + sNl
      + "<br><TR><TD align=left>"
      + "<font size=+2><b>%s</b></font></TD></TR></TABLE>";;

var shBeginTable1 =
      "<TABLE width=300 border=1 cellpadding=3 cellspacing=0><BR>";
var shEndTable = "</table>";

var shBeginRow = "<tr>";
var shEndRow = "</tr>";


var shCell1 = "<td width=150><font size=-1><B>%s</B></font></td>";
var shCell2 = "<td width=150><font size=-1>%s</font></td>";

function EmitRow1(s1,s2)
{
   Emit(shBeginRow);
   Emit1(shCell1,s1);
   Emit1(shCell2,s2);
   Emit(shEndRow);
}


var shSelf =  
     "<TABLE width=300 cellpadding=0 cellspacing=0>" + sNl
   + "<tr><td><font size=-1><br>To recalculate the" + sNl
   + "scenario, enter new numbers and click on the" + sNl
   + "'Re-Calculate' button below:<br></font></td></tr>" + sNl
   + "</TABLE>" + sNl
   + "<form name='apppform'>" + sNl
   + "<TABLE width=300 border=1 cellpadding=3 cellspacing=0>" + sNl
   + "  <TR>" + sNl
   + "    <TD colSpan=2><font size=-1><B>(Note: Please enter" + sNl
   + "		only numbers)</B></font></TD></TR>" + sNl
   + "  <TR>" + sNl
   + "    <TD width=150><font size=-1><B>Loan Amount</B>" + sNl
   + "				</font></TD>" + sNl
   + "    <TD width=150><INPUT name=appp0001" + sNl
   + "			size=10></TD></TR>" + sNl
   + "  <TR>" + sNl
   + "    <TD width=150><font size=-1><B>Term (in years)</B>" + sNl
   + "					</font></TD>" + sNl
   + "    <TD width=150><INPUT name=appp0002 size=10>" + sNl
   + "				</TD></TR>" + sNl
   + "  <TR>" + sNl
   + "    <TD width=150><font size=-1><B>Interest Rate</B>" + sNl
   + "					</font></TD>" + sNl
   + "    <TD width=150><INPUT name=appp0003 size=10>" + sNl
   + "			</TD></TR></TABLE><BR>" + sNl

   + "<TABLE width=300>" + sNl
   + "<TR align=center><TD><font size=-1>" + sNl
   + "<input type=button value='Re-Calculate'" + sNl
   + "	onClick = 'Execute(document.apppform)'>" + sNl
   + "	</font></TD></TR></TABLE>" + sNl
   + "</FORM></BODY></HTML>";




function Execute(oForm)
{
   var iError = 0;
   var nPrincipal, nInputInterest, iYears;

   if ( (nPrincipal = Validate(oForm.appp0001.value, 0)) == nErrorNumber)
      iError = 100;
   else if ( (iYears = Validate(oForm.appp0002.value, 1)) == nErrorNumber)
      iError = 110;
   else if ( (nInputInterest = Validate(oForm.appp0003.value, 0)) == nErrorNumber)
      iError = 120;

   var iPpy = 12;
   var iMonths = 12 * iYears;
   var iPeriods = iPpy * iYears;
   var nPercent = nInputInterest / iPpy / 100;

   var nPayment = 0;


   if (!iError)
   {
      var sMessage = "";
      if (nPrincipal == 0)
	 sMessage = "Please enter a principal loan balance";
      else if (nPrincipal < 0)
	 sMessage = "Principal loan balance must be positive";
      else if (nPercent == 0)
	 sMessage = "Please enter an interest rate";
      else if (nPercent < 0)
	 sMessage = "Interest rate must be positive";
      else if (iYears == 0)
	 sMessage = "Please enter number of years";
      else if (iYears < 0)
	 sMessage = "Number of years must be positive";
//    else if (iYears > 30)
//	 sMessage = "Amortization length must be 30 years or less";
      if (sMessage != "")
      {
	 iError = 170;
	 alert(sMessage);
      }
   }

   if (!iError)
   {
      nPayment = nPrincipal / PresentValue(1, nPercent, iPeriods);
      nPayment = Round(nPayment + .005, 2);

      Emit1(shStart, "Mortgage Payment Results");
	  Emit1(shHeadJpg, "eagle.jpg");
      Emit1(strVariable);
      
	  Emit1(shHeading1, "Mortgage Payment Calculator Results");

	  
//    Emit1(shHeading2, "MORTGAGE INFORMATION");

      Emit(shBeginTable1) ;
      EmitRow1("Loan Amount", DollarFormat(nPrincipal, 0, 2)) ;
      EmitRow1("Interest Rate", nInputInterest + " %") ;
      EmitRow1("Loan Term", iYears + " Years") ;
      EmitRow1("Monthly Payment", DollarFormat(nPayment, 0, 2)) ;

      Emit(shEndTable);
	if (navigator.appName!="Netscape")
		Emit(shSelf) ;
   }

   if (!iError)
   {
//    document.open() ;
      document.write(sOut) ;
      document.close() ;

   }

}

function Emit(sStr) { sOut += sStr + sNl ; }

function Emit1(sStr, s1)
{
   var iK ;
   if ( (iK = sStr.indexOf(sSub)) >= 0)
      sStr = sStr.substring(0, iK) + s1
                + sStr.substring(iK + sSub.length) ;
   Emit(sStr) ;
}

function Emitx(sStr, sSubs)
{
   var iK, iL, iPos1 = 0, iPos2 = 0 ;
   while ( (iK = sStr.indexOf(iPos1, sSub)) >= 0)
   {
      if ( (iL = eSubs.indexOf(iPos2, sSep)) < 0)
         iL = eSubs.length() ;
      sStr = sStr.substring(0, iK)
                        + sSubs.substring(iPos1, iL)
                        + sStr.substring(iK + sSub.length()) ;
      iPos1 = iK + sSub.length() ;
      if ( (iPos2 = iL + eSep.length()) > sSubs.length())
         iPos2 = sSubs.length() ;
   }
   Emit(sStr) ;
}


var sBlanks = "                                             "
                + "                                          " ;
var sDashes = "---------------------------------------------"
                + "-----------------------------------------" ;

var nPowers = [
        0.0000000000000001,
        0.000000000000001,
        0.00000000000001,
        0.0000000000001,
        0.000000000001,
        0.00000000001,
        0.0000000001,
        0.000000001,
        0.00000001,
        0.0000001,
        0.000001,
        0.00001,
        0.0001,
        0.001,
        0.01,
        0.1,
        1.,
        10.,
        100.,
        1000.,
        10000.,
        100000.,
        1000000.,
        10000000.,
        100000000.,
        1000000000.,
        10000000000.] ;

function Power(n)
{
    return nPowers[n+16] ;
}

var sMonths = ["January", "February", "March", "April", "May",
        "June", "July", "August", "September", "October",
        "November", "December"] ;

function Mmm(iK) { if (!(iK >= 1 && iK <= 12)) return "MMM" + iK ;
            return sMonths[iK-1].substring(0, 3) ; }

function ExpN(nX, iN)   // compute x ** n, where n is integral
{
   var nResult = 1 ;
   var bSign = 0 ;
   if (iN < 0)
   {
      bSign = 1 ;
      iN = -iN ;
   }
   while (iN > 0)
   {
      if (iN & 1)
         nResult *= nX ;
      nX *= nX ;
      iN >>= 1 ;
   }
   if (bSign)
      nResult = 1 / nResult ;
   return nResult ;
}

function PresentValue(nPayment, nPercent, iNumPeriods)
{
   var nAmount = (Math.abs(nPercent) > 1e-20)
         ? nPayment * (1 - ExpN(1 + nPercent, -iNumPeriods))
                                        / nPercent 
         : nPayment * iNumPeriods ;
   return nAmount ;
}

function SparsePresentValue(nPayment, nPercent, iInterval, iNumPeriods)
{
   var nAmount = (Math.abs(nPercent) > 1e-20)
         ? nPayment * (1 - ExpN(1 + nPercent, -iNumPeriods))
                        / (ExpN(1 + nPercent, iInterval) - 1)
         : nPayment * iNumPeriods ;
   return nAmount ;
}

function Round(nVal, iD)
{
   var iSign = 1 ;
   if (nVal < 0)
   {
      nVal = - nVal ;
      iSign = -1 ;
   }
   var iInt = Math.round(nVal) ;
   if (iD > 0)
      iInt = Math.floor(nVal) ;
   var nFp = nVal - iInt ;
// alert ('iInt, nFp = ' + iInt + ", " + nFp) ;
   if (iD > 0)
      nFp = Math.round(nFp * Power(iD)) / Power(iD) ;
   nVal = iSign * (iInt + nFp) ;
   return nVal ;
}

function Validate(sVal, bInt)
{
   var sMessage = "" ;
   var bDot = 0, bE = 0, iState = 0 ;
   var sCh, iK ;
   var bInvalid = 0 ;
   var nValue = bInt ? parseInt(sVal) : parseFloat(sVal) ;

   for (iK = 0 ; sMessage == "" && iK < sVal.length ; ++iK)
   {
      sCh = sVal.charAt(iK) ;
      if (sCh == " ")
      {
         if (iState > 0)
            iState = 9 ;
      }
      else
      {
         if (iState == 9)
            sMessage = "Number '" + sVal + "' has an embedded blank" ;
         else if (sCh == '.' && !bInt)
         {
            if (bDot || bE)
	       bInvalid = 1 ;
            else
               bDot = 1 ;
         }
         else if ((sCh == 'e' || sCh == 'E') && !bInt)
         {
            if (bE)
	       bInvalid = 1 ;
            else
            {
               bE = 1 ;
               iState = 6 ;
            }
         }
         else if (sCh == '+' || sCh == '-')
         {
            if (iState == 0 || iState == 6)
               ++iState ;
            else
               sMessage = "Number '" + sVal + "' contains a sign "
                                + "in an illegal position"
         }
         else if (sCh >= '0' && sCh <= '9')
         {
            if (iState == 1 || iState == 7)
               ++iState ;
            else if (iState == 0 || iState == 6)
               iState += 2 ;
         }
         else
	    bInvalid = 1 ;
      }
      if (bInvalid)
	 sMessage = "Number '" + sVal + "' contains"
                        + " invalid non-numeric character(s)" ;
   }

   if (sMessage == "")
      if (iState == 1 || iState == 6 || iState == 7)
         sMessage = "Illegal number: " + sVal ;
      else if (iState == 0)
	 nValue = 0 ;
   if (sMessage != "")
   {
      alert(sMessage) ;
      nValue = nErrorNumber ;
   }
   return nValue
}


// DollarFormat -- could be jazzed up to produce "CR" or "DB"
function DollarFormat(nVal, iW, iD)
{
   return GenFmt(nVal, iW, iD, 1) ;
}

function Format(nVal, iW, iD)
{
   return GenFmt(nVal, iW, iD, 0) ;
}

function GenFmt(nVal, iW, iD, bDollar) // format val into w chars,
                        // d digs after decimal point
{
   var sOut = "" ;
   var iSign = 0 ;
   nVal = Round(nVal, iD) ;
   if (nVal < 0)
   {
      nVal = - nVal ;
      iSign = 1 ;
   }
   var iInt = Math.round(nVal) ;
   if (iD > 0)
      iInt = Math.floor(nVal) ;
   var nFp = nVal - iInt ;
   var iDigs = 1 ;
   if (iInt > 9)
      iDigs = Math.floor(Math.log(iInt+.1)/Math.log(10)) + 1 ;
   var iLeft = iW - iSign - (bDollar ? 1 : 0) ;
   if (iD > 0)
      iLeft -= iD + 1 ;
   if (iLeft > iDigs)
      sOut += sBlanks.substring(0, iLeft - iDigs) ;
   if (iSign)
      sOut += '-' ;
   if (bDollar)
      sOut += '$' ;
   sOut += iInt ;
   if (iD > 0)
   {
      nFp = Math.round((1 + nFp) * Power(iD)) ;
      sOut += '.' + String(nFp).substring(1) ;
   }
   return sOut ;
}

function PrepadString(sStr, iW)
{
   if (sStr.length < iW)
      sStr = sBlanks.substring(0, iW - sStr.length) + sStr ;
   return sStr ;
}

function CenterString(sStr, iW)
{
   var iBlanks = iW - sStr.length ;
   if (iBlanks > 0)
      sStr = sBlanks.substring(0, Math.floor(iBlanks/2)) + sStr
                + sBlanks.substring(0, iBlanks - Math.floor(iBlanks/2)) ;
   return sStr ;
}


//-->

// </SCRIPT>
