
 
//*******************************************************************************************
//*******************************************************************************************
//*******************************************************************************************

var MonthlyRate, tempVal, NumMonths, Payment, EstPayment, Principal

function validate_num(){
var key = window.event.keyCode
if (key < 46 || key > 57)
  {
  window.event.returnValue = false
  }
}

//*******************************************************************************************
//*******************************************************************************************
//*******************************************************************************************
function FindInvPayment(){
  var PresentBalance;
  var Loop1;
  Payment = 0;
  PresentBalance = 0;

  while (PresentBalance < Principal) {
    PresentBalance = 0;
      for(Loop1=1;Loop1<=NumMonths;Loop1++){
        PresentBalance = PresentBalance + Payment;
        PresentBalance = PresentBalance * (1.0 + MonthlyRate);
      }
      Payment = Payment + 100;
  }
  Payment = Payment - 200;
  PresentBalance = 0;

  while (PresentBalance < Principal) {
    PresentBalance = 0;
      for(Loop1=1;Loop1<=NumMonths;Loop1++){
        PresentBalance = PresentBalance + Payment;
        PresentBalance = PresentBalance * (1.0 + MonthlyRate);
      }
      Payment = Payment + 10;
  }
  Payment = Payment - 20;
  PresentBalance = 0;

  while (PresentBalance < Principal) {
    PresentBalance = 0;
      for(Loop1=1;Loop1<=NumMonths;Loop1++){
        PresentBalance = PresentBalance + Payment;
        PresentBalance = PresentBalance * (1.0 + MonthlyRate);
      }
      Payment = Payment + 1;
  }
  Payment = Payment - 2;
  PresentBalance = 0;

  while (PresentBalance < Principal) {
    PresentBalance = 0;
      for(Loop1=1;Loop1<=NumMonths;Loop1++){
        PresentBalance = PresentBalance + Payment;
        PresentBalance = PresentBalance * (1.0 + MonthlyRate);
      }
      Payment = Payment + 0.10;
  }
  Payment = Payment - 0.20;
  PresentBalance = 0;

  while (PresentBalance < Principal) {
    PresentBalance = 0;
      for(Loop1=1;Loop1<=NumMonths;Loop1++){
        PresentBalance = PresentBalance + Payment;
        PresentBalance = PresentBalance * (1.0 + MonthlyRate);
      }
      Payment = Payment + 0.010;
  }
  Payment = Payment - 0.010;
  document.frmMain.Payment.value = Payment;

}
//******************************************************************************************
//******************************************************************************************
//******************************************************************************************

function FindInvPrincipal(){
  var Loop1;
  Principal = 0;

  for(Loop1=1;Loop1<=NumMonths;Loop1++){
      Principal = Principal + Payment;
      Principal = Principal * (1.0 + MonthlyRate);
  }
  document.frmMain.Principal.value = Principal;
}
//******************************************************************************************
//******************************************************************************************
//******************************************************************************************

function FindInvMonths(){
  var PresentBalance;
  NumMonths = 0;
  PresentBalance = 0;
  while (PresentBalance < Principal) {
    NumMonths = NumMonths + 1;
    PresentBalance = PresentBalance + Payment;
    PresentBalance = PresentBalance * (1.0 + MonthlyRate);
  }
  document.frmMain.NumMonths.value = NumMonths;
}

//*******************************************************************************************
//*******************************************************************************************
//*******************************************************************************************

function FindInvPercent(){
  var PresentBalance;
  var Loop1;
  MonthlyRate = 0;
  PresentBalance = 0;
  while (PresentBalance < Principal) {
   PresentBalance = 0;
      for(Loop1=1;Loop1<=NumMonths;Loop1++){
         PresentBalance = PresentBalance + Payment;
         PresentBalance = PresentBalance * (1.0 + MonthlyRate);
      }
      MonthlyRate = MonthlyRate + 0.001;
   }
   MonthlyRate = MonthlyRate - 0.002;

  PresentBalance = 0;
  while (PresentBalance < Principal) {
   PresentBalance = 0;
      for(Loop1=1;Loop1<=NumMonths;Loop1++){
         PresentBalance = PresentBalance + Payment;
         PresentBalance = PresentBalance * (1.0 + MonthlyRate);
      }
      MonthlyRate = MonthlyRate + 0.0001;
   }
   MonthlyRate = MonthlyRate - 0.0002;

  PresentBalance = 0;
  while (PresentBalance < Principal) {
   PresentBalance = 0;
      for(Loop1=1;Loop1<=NumMonths;Loop1++){
         PresentBalance = PresentBalance + Payment;
         PresentBalance = PresentBalance * (1.0 + MonthlyRate);
      }
      MonthlyRate = MonthlyRate + 0.00001;
   }
   MonthlyRate = MonthlyRate - 0.00001;

  document.frmMain.PercentRate.value = MonthlyRate * 1200.00;
}

//*****************************************************************************************
//*****************************************************************************************
//*****************************************************************************************

function FindPayment(){
  tempVal = Math.pow((1.+MonthlyRate),NumMonths);
  Payment = Principal*((MonthlyRate*tempVal)/(tempVal-1));
  document.frmMain.Payment.value = Payment;
}
//******************************************************************************************
//******************************************************************************************
//******************************************************************************************

function FindPrincipal(){
  tempVal = Math.pow((1.+MonthlyRate),NumMonths);
  Principal = Payment/((MonthlyRate*tempVal)/(tempVal-1));
  document.frmMain.Principal.value = Principal;
}
//******************************************************************************************
//******************************************************************************************
//******************************************************************************************

function FindMonths(){
  NumMonths = ((-Math.log(1-(MonthlyRate*Principal)/Payment))/(Math.log(1+MonthlyRate)))+.5;
  document.frmMain.NumMonths.value = NumMonths;
}
//******************************************************************************************
//******************************************************************************************
//******************************************************************************************

function FixLen(strInput, intLen){
  while (strInput.length < intLen) {strInput = strInput+ " ";}
  return strInput;
}
//******************************************************************************************
//******************************************************************************************
//******************************************************************************************

function MakeMoney(strInput){
  strInput = (Math.round(strInput * 100) / 100).toString();
  if (strInput.indexOf('.') == -1) {
    strInput = strInput + '.00';
    }
  if (strInput.indexOf('.') == (strInput.length - 2)) {
    strInput = strInput + '0';
    }
  if (strInput.indexOf('.') == (strInput.length - 1)) {
    strInput = strInput + '00';
    }
  return strInput;
}
//*****************************************************************************************
//*****************************************************************************************
//*****************************************************************************************




function FindPercent(){
 MonthlyRate = .0008;
while (true){
  tempVal = Math.pow((1.+MonthlyRate),NumMonths);
  EstPayment = Principal*((MonthlyRate*tempVal)/(tempVal-1));
  if (EstPayment >= Payment){MonthlyRate = MonthlyRate - .0016; break;}
  MonthlyRate = MonthlyRate + .0008;}
  MonthlyRate = MonthlyRate +.00008;
while (true){
  tempVal = Math.pow((1.+MonthlyRate),NumMonths);
  EstPayment = Principal*((MonthlyRate*tempVal)/(tempVal-1));
  if (EstPayment >= Payment){MonthlyRate = MonthlyRate -.00016; break;}
  MonthlyRate = MonthlyRate + .00008;}
  MonthlyRate = MonthlyRate +.000008;
while (true){
  tempVal = Math.pow((1.+MonthlyRate),NumMonths);
  EstPayment = Principal*((MonthlyRate*tempVal)/(tempVal-1));
  if (EstPayment >= Payment){MonthlyRate = MonthlyRate - .000016; break;}
  MonthlyRate = MonthlyRate + .000008;}
  MonthlyRate = MonthlyRate +.0000008;
while (true){
  tempVal = Math.pow((1.+MonthlyRate),NumMonths);
  EstPayment = Principal*((MonthlyRate*tempVal)/(tempVal-1));
  if (EstPayment >= Payment){MonthlyRate = MonthlyRate -.0000016; break;}
  MonthlyRate = MonthlyRate + .0000008;}

  document.frmMain.PercentRate.value = MonthlyRate * 1200.00;
}
//*****************************************************************************************
//*****************************************************************************************
//*****************************************************************************************

function DoCalc(){
var NumFilled;
var longString;
var TotalInterest;
var CurrentMonth;
var PayMonth;
var PayYear;
var MonthInterest;
var PayToPrincipal;
var strPayYear;
var strPayMonth;
var strMonthInterest;
var strPayToPrincipal;
var strPrincipal;
var strTotalInterest;
var strPresentBalance;



NumFilled = 0;
// make numbers

if (document.frmMain.Payment.disabled == true) {document.frmMain.Payment.value = "0"};
if (document.frmMain.Principal.disabled == true) {document.frmMain.Principal.value = "0"};
if (document.frmMain.PercentRate.disabled == true) {document.frmMain.PercentRate.value = "0"};
if (document.frmMain.NumMonths.disabled == true) {document.frmMain.NumMonths.value = "0"};

MonthlyRate = parseFloat(document.frmMain.PercentRate.value) / 1200.00;
if (MonthlyRate != MonthlyRate) { MonthlyRate = 0;}
NumMonths = parseFloat(document.frmMain.NumMonths.value);
if (NumMonths != NumMonths) { NumMonths = 0;}
Principal = parseFloat(document.frmMain.Principal.value); 
if (Principal != Principal) { Principal = 0;}
Payment = parseFloat(document.frmMain.Payment.value);
if (Payment != Payment) { Payment = 0;}
if (Payment  > 0){NumFilled++;}
if (Principal  > 0){NumFilled++;}
if (NumMonths  > 0){NumFilled++;}
if (MonthlyRate  > 0){NumFilled++;}
if (NumFilled != 3) {
   alert("Please enter all values!");
   }
   else{
     document.frmMain.Payment.value = Payment;
     document.frmMain.Principal.value = Principal;
     document.frmMain.NumMonths.value = NumMonths;
     document.frmMain.PercentRate.value = MonthlyRate * 1200.00;
     document.frmMain.outputbox.value = "";

   
       if (Payment == 0){FindPayment();}
       if (Principal == 0){FindPrincipal();}
       if (NumMonths == 0){FindMonths();}
       if (MonthlyRate == 0){FindPercent();}
   
   // round everything to hundredths
   document.frmMain.PercentRate.value = MakeMoney(MonthlyRate * 1200);
   document.frmMain.NumMonths.value = MakeMoney(NumMonths);
   document.frmMain.Principal.value = MakeMoney(Principal);
   document.frmMain.Payment.value = MakeMoney(Payment);

longString = ""

//if (1=2){
if (document.frmMain.calcAmort.checked){
     longString = "Year/Mo  Interest     Principal  Tot. Interest   Balance" + String.fromCharCode(10);
     longString = longString + "" + String.fromCharCode(10);
     TotalInterest = 0;
     CurrentMonth = 0;
     PayMonth = parseFloat(document.frmMain.StartMonth.value);
     if (PayMonth != PayMonth) { PayMonth = 1;}
     PayYear = parseFloat(document.frmMain.StartYear.value);
     if (PayYear != PayYear ) { PayYear = 1;}

       PresentBalance = Principal;
       while (CurrentMonth <= NumMonths-1){
         if (PayMonth == 13) { PayMonth = 1; PayYear = PayYear + 1;
           longString = longString + "" + String.fromCharCode(10);
           longString = longString + "Year/Mo  Interest     Principal  Tot. Interest   Balance" + String.fromCharCode(10);
           longString = longString + "" + String.fromCharCode(10);
         }
         CurrentMonth = CurrentMonth + 1;
         MonthInterest = PresentBalance * MonthlyRate;
         PayToPrincipal = Payment - MonthInterest;
         TotalInterest = TotalInterest + MonthInterest;
         PresentBalance = PresentBalance - PayToPrincipal;

         strPayYear = PayYear.toString();
         strPayYear = FixLen(strPayYear,3);

         strPayMonth = PayMonth.toString();
         strPayMonth = FixLen(strPayMonth,3);

         strMonthInterest = MakeMoney(MonthInterest);
         strMonthInterest = FixLen(strMonthInterest,12);

         strPayToPrincipal = MakeMoney(PayToPrincipal);
         strPayToPrincipal = FixLen(strPayToPrincipal,12);

         strTotalInterest = MakeMoney(TotalInterest);
         strTotalInterest = FixLen(strTotalInterest,12);

         strPresentBalance = MakeMoney(PresentBalance);
         strPresentBalance = FixLen(strPresentBalance,12);

         longString = longString + " " + strPayYear + "/" + strPayMonth + " " + strMonthInterest + " " + strPayToPrincipal  + " "  + strTotalInterest   + " "  + strPresentBalance + String.fromCharCode(10);
         PayMonth = PayMonth + 1;
}

     }


     document.frmMain.outputbox.value = longString;

   }
}

//*****************************************************************************************
//*****************************************************************************************
//*****************************************************************************************

function CalcType(sValue){
document.frmMain.Principal.disabled = false; //document.frmMain.Principal.value = "";
document.frmMain.PercentRate.disabled = false; //document.frmMain.PercentRate.value = "";
document.frmMain.NumMonths.disabled = false; //document.frmMain.NumMonths.value = "";
document.frmMain.Payment.disabled = false; //document.frmMain.Payment.value = "";

if (sValue == "1")
 {
// document.frmMain.Principal.disabled = false;
 //document.frmMain.Principal.value = "";
// document.frmMain.PercentRate.disabled = false;
 //document.frmMain.PercentRate.value = "";
// document.frmMain.NumMonths.disabled = false;
 //document.frmMain.NumMonths.value = "";
 document.frmMain.Payment.disabled = true;
 document.frmMain.Payment.value = "0";

 }

if (sValue == "2")
 {
 document.frmMain.Principal.disabled = true;
 document.frmMain.Principal.value = "0";
// document.frmMain.PercentRate.disabled = false;
 //document.frmMain.PercentRate.value = "";
// document.frmMain.NumMonths.disabled = false;
 //document.frmMain.NumMonths.value = "";
// document.frmMain.Payment.disabled = false;
 //document.frmMain.Payment.value = "";
 }

if (sValue == "3")
 {
// document.frmMain.Principal.disabled = false;
//document.frmMain.Principal.value = "";
 document.frmMain.PercentRate.disabled = true;
 document.frmMain.PercentRate.value = "0";
// document.frmMain.NumMonths.disabled = false;
 //document.frmMain.NumMonths.value = "";
// document.frmMain.Payment.disabled = false;
 //document.frmMain.Payment.value = "";
 }

if (sValue == "4")
 {
// document.frmMain.Principal.disabled = false;
 //document.frmMain.Principal.value = "";
// document.frmMain.PercentRate.disabled = false;
 //document.frmMain.PercentRate.value = "";
 document.frmMain.NumMonths.disabled = true;
 document.frmMain.NumMonths.value = "0";
// document.frmMain.Payment.disabled = false;
 //document.frmMain.Payment.value = "";
 }


}



