

// Loan Consolidation Calculator JavaScript
// externalized on 10/29/2001 by CJB
// stored in: \onramp\public\includes
// src string: "/includes/lnCnsldClc.js"
// aql- added new avg interest rate logic on 11/19/02 per CCC
var num=0;
var amt=0;
var per=0;
var months=0;
var nls="";
var rr="\r";
var b="                               ";
var d="-----------------------------"
+"----------------------------------";
var s="$";
var hold;
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=5;
 stn();
 per=num;
 str=document.isn.months.value;
 dp=0;
 stn();
 months=(str - 0);
 if (months < 1 || months > 999 ) {
  alert(months);
  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;//this was prp1 = s+(-str);  caused the numbers to reverse in report. fixed 10/16/2001
  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;
  
  //experiment for writing out the report
  hold = document.isn.sched.value;
  return(hold);
 }
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("There was an error with the calculation. Months must be from"
 +" 1 to 999, Loan amount from 1 to "+pwr[9]
 +" and Interest from .001 to 8.25%");
}
//-->

//********************loan consolidation calculator**
//********************mapping-your-future*10/11/2001**
//********************authored by c.j.bagley**
//********************javascript(clientside) application**

//globals for loan balances
var bClickCount = 0;
var loanBalances = new Array();
//globals for interest rates
var iClickCount = 0;
var interestRates = new Array();
//global to keep a running total of the balance amounts
var balTot = 0;
//global to keep a running total of the interest amounts
var intTot = 0;
//global array that holds the first step calc
var stepOne = new Array();
//global variable that holds the second step of the calculation
var stepTwo = 0;
//global variable to hold the weighted interest rate - NOT ROUNDED TO 4 DEC. PLACES
var wtdIntRate = 0;
//global variable to hold the rounded interest rate:
var outString = 0;
//click counter for calc button
var cClicked = 0;
//************************end global variables***************************************

  //*****************function updBalArray*************************
  //**************************************************************
  function updateBal(location, value)
  {
    //set these two local variables to location & value respectively  
    var bLoc = location;
    var bVal = value;
    
    loanBalances[bClickCount] = bVal;

    //forcing balTot to be numeric by subracting 0 from each value
    balTot = (balTot - 0) + (bVal - 0);
    
    //increment the balance array counter
    bClickCount++;
    
    //update these global values for the next time the function is called
    return(bClickCount,balTot);
  }
  //**************************************************************
  //*****************end function*********************************
  
  //*****************function updIntRateArray*********************
  //**************************************************************  
  function updateInt(location, value)
  {
    //set these two local variables to location & value respectively
    iLoc = location;
    iVal = value;
    
    interestRates[iClickCount] = iVal;
    //forcing intTot to be numeric by subtracting 0 from each value
    intTot = (intTot - 0) + (iVal - 0);
    
    //increment the interest rate counter
    iClickCount++;

    //update these global values for the next time function is called.
    return(iClickCount, intTot);
    
  }
  //**************************************************************
  //***********************end function***************************
  
  //*****************function calcValue***************************
  //**************************************************************
  function calcValue(theCaller)
  {
    if(cClicked == 0)
    {
      cClicked = (cClicked + 1);
      
      //step 1 in calc. wt. avg: multiply each loan's balance * interest rate
      for(j = 0; j < loanBalances.length; j++)
      {
        stepOne[j] = ((loanBalances[j] * interestRates[j])/ 100);
        //alert(stepOne[j]);
      }
      
      //step 2 in calc. wt. avg: add the elements of step one together
      for(i = 0; i < stepOne.length; i++)
      {
          stepTwo = (stepTwo - 0) + (stepOne[i] - 0);
          //alert(stepTwo);
      }
      
      //step 3 in calc. wt. avg: divide total from step two by the total
      //amount of the loan balances
      wtdIntRate = (stepTwo - 0) / (balTot - 0);
      
      //call function to round to the specified number of dec. places
      //in this case 5
      floatFix(wtdIntRate, 5);
      
      //call function to export the values to the form
      exportValues();
      return(cClicked);
    }
    //alert(cClicked);
    if(cClicked != 0)
      {
        alert("Oops! You cannot submit the data more than once! If you would like to compute another consolidation please reset fhe form and enter the new data.");
        //document.isn.per.value = " ";
        //document.isn.amt.value = " ";
        //document.isn.months.value = " ";
        //resetGlobals();
      }
  }    
  //**************************************************************
  //*****************end function*********************************
  
  //**************************************************************
  //*****************function floatFix*****************************
  function floatFix(val, places)
  {
    //this function is a custom function.  JS does not have a native
    //function to round a number to a given number of decimal places
    //so-this function rounds a value (val) to the specified number
    //of decimal places (places):
    var res = " " + Math.round(val * Math.pow(10,places));
    var dec = res.length - places;
    if(places != 0)
    {
      outString = res.substring(0,(dec+2)) + "." + res.substring(2,res.length);
    }
    else
    {
      outString = res;
    }
    return(outString);
  }
  //**************************************************************
  //*****************end function*********************************
  
  //**************************************************************
  //*****************function exportValues************************
  // this function exports the calculated interest rate
  // and total loan balances to the second form.
  function exportValues()
  {
      // find the decimal point values (everything after the '.'), and round it
      // up to the nearest 1/8th of 1 percent (use 3 decimal places)
      // this was added by aql on 11/19/02
      var decPoint = outString.indexOf(".");
      var decStart = decPoint + 1;
      var wholeNumStart = decPoint - 1;
      var wholeNum = outString.substring(0, 2);
      var decNum   = outString.substring(decStart, outString.length);
      // change the decimal numbers accordingly for interest rounding rules
      if ((decNum > 000) && (decNum <= 125)) {
          newDecNum = 125;
      } else if ((decNum > 125) && (decNum <= 250)) {
          newDecNum = 250;
      } else if ((decNum > 250) && (decNum <= 375)) {
          newDecNum = 375;
      } else if ((decNum > 375) && (decNum <= 500)) {
          newDecNum = 500;
      } else if ((decNum > 500) && (decNum <= 625)) {
          newDecNum = 625;
      } else if ((decNum > 625) && (decNum <= 750)) {
          newDecNum = 750;
      } else if ((decNum > 750) && (decNum <= 875)) {
          newDecNum = 875;
			} else if ((decNum > 875) && (decNum <= 999)) {
					newDecNum = 000;
					tmpWholeNum = eval(wholeNum.toString())+1;
					wholeNum = tmpWholeNum;
      } else {
          newDecNum = 000;
      }
      // create the new outString (avg interest rate variable) by combining the
      // whole number (the 7 in 7.125), the '.', and the new (rounded) decimal
      outString = wholeNum+"."+newDecNum;
      //new logic - this tests the avg'd interest rate if above 8.25
      //set it to 8.25%
		 if((outString-0) >8.25)
      {
        outString = 8.25;
      }
		 // stick the evaluated new avg interest rate into the per form element
      document.isn.per.value = eval(outString.toString());
        
    //new logic, set the number of months to the max allowed
    //according to the loan balance:
    if(balTot < 7500)
    {
      document.isn.months.value = 120;
    }
    else if(balTot >= 7500 && balTot < 10000)
    {
      document.isn.months.value = 144;
    }
    else if(balTot >= 10000 && balTot < 20000)
    {
      document.isn.months.value = 180; 
    }
    else if(balTot >= 20000 && balTot < 40000) 
    {
      document.isn.months.value = 240; 
    }
    else if(balTot >= 40000 && balTot < 60000)
    {
      document.isn.months.value = 300;
    }
    else if(balTot >= 60000)
    {
      document.isn.months.value = 360;
    }
    document.isn.amt.value = balTot;
  }
  //**************************************************************
  //*****************end function*********************************
  
  //**************************************************************
  //*****************function refresh()***************************
  function refresh()
  {
    //this function forces a page refresh to clear all of the arrays
    //in the application.  this value should equal the URL of this page.
    window.location = "/tools_loan_consolidator.html";
  }
  //**************************************************************
  //*****************end function*********************************  
  
  //**************************************************************
  //*****************function notEdit(notEditable)****************
  function notEdit(notEditable)
  {
    var nonEditable = notEditable;
    document.loans.reset.focus();
    alert("Oops, this field is not editable, this interest rate and loan amount is computed from the data above. If you wish to change this data please reset the form to continue.");
  }
  //**************************************************************
  //*****************end function*********************************
  
  
  //**************************************************************
  //*****************function writeReport*************************  
  function writeOut()
  {
    var win = window.open(" ","PrintSched","top=50,left=25,width=612,height=560,toolbar=yes,menubar=yes,scrollbars=yes");
    var d = win.document;
    d.write("<img src='/images/l2myfbar.gif' width=586 height=20 alt='Mapping Your Future' border=0><br>");
    d.write("<font size='+1' color='330099'><b>Printable Loan Amortization Schedule</b></font>");
    d.write("<div align='right'><INPUT TYPE='BUTTON' VALUE='Close Window' onClick='javascript:window.close(this);'></div></p>");
    d.write("<font family='verdana,arial,sans-serif' color=red><center>Use your browser's print command to print this report.</center></font>");
    d.write("<HR><PRE>"+hold+"</PRE><HR>");
    d.close();
  }
  //**************************************************************
  //*****************end function*********************************
  
  //**************************************************************
  //*****************function notEdit*****************************  
  function notEdit(notEditable)
  {
    var nonEditable = notEditable;
    document.loans.reset.focus();
    alert("Oops, this field is not editable, this interest rate and loan amount is computed from the data above. If you wish to change this data please reset the form to continue.");
  }
  //**************************************************************
  //*****************end function*********************************  
  
  //**************************************************************
  //*****************function inRange*****************************  
  function inRange(monthsGiven)
  {
    var months = monthsGiven;
    if(document.isn.amt.value < 7500 && months > 120)
    {
      alert("the maximum term for your loan balance is 120 months, you can choose a shorter term if you like, but not a longer one.");
      document.isn.months.value = 120;
    }
    else if(document.isn.amt.value >= 7500 && document.isn.amt.value < 10000 && months > 144)
    {
      alert("the maximum term for your loan balance is 144 months, you can choose a shorter term if you like, but not a longer one.");
      document.isn.months.value = 144;
    }
    else if(document.isn.amt.value >= 10000 && document.isn.amt.value < 20000 && months > 180)
    {
      alert("the maximum term for your loan balance is 180 months, you can choose a shorter term if you like, but not a longer one.");
      document.isn.months.value = 180; 
    }
    else if(document.isn.amt.value >= 20000 && document.isn.amt.value < 40000 && months > 240) 
    {
      alert("the maximum term for your loan balance is 240 months, you can choose a shorter term if you like, but not a longer one.");
      document.isn.months.value = 240; 
    }
    else if(document.isn.amt.value >= 40000 && document.isn.amt.value < 60000 && months > 300)
    {
      alert("the maximum term for your loan balance is 300 months, you can choose a shorter term if you like, but not a longer one.");
      document.isn.months.value = 300;
    }
    else if(document.isn.amt.value >= 60000 && months > 360)
    {
      alert("the maximum term for your loan balance is 360 months, you can choose a shorter term if you like, but not a longer one.");
      document.isn.months.value = 360;
    }
  }
  //**************************************************************
  //*****************end function*********************************  
//EOF


