function select5(myAmortizer) { //mostly superceded by items in amortizer.js...
  this.valid = false;
  this.principalPaid = 0;

  this.Amortizer = myAmortizer;
  if (this.Amortizer.isValid()) {
    this.Amortizer.setACH(false);
    this.Amortizer.setOnePercentOff(false);
    this.principal = myAmortizer.getPrincipal();
    this.interest = myAmortizer.getInterest();
    this.decimalInterest = myAmortizer.getDecimalInterest();
    this.years = myAmortizer.getTermYears();
  } else {
    alert("Could not create a Select/5 repayment schedule based on your Level Repayment Plan!");
    return undefined;
  }
  this.yearsThreeToFivePrincipalPayment = 0;
  this.yearsThreeToFivePrincipalPaid = 0;
  this.yearsThreeToFiveInterestPaid = 0;

 

  this.getPrincipalPaidThisPayment = select5_getPrincipalPaidThisPayment;
  this.getSchedule = select5_getSchedule;
  this.getYearsThreeToFivePrincipalPayment = select5_getYearsThreeToFivePrincipalPayment;
  this.getYearsThreeToFivePrincipalPaid = select5_getYearsThreeToFivePrincipalPaid;
  this.getYearsThreeToFiveInterestPaid = select5_getYearsThreeToFiveInterestPaid;
  this.getYearsThreeToFiveSchedule =  select5_getYearsThreeToFiveSchedule;
  
  this.getRemainingAmortizer = select5_getRemainingAmortizer;
 
  this.setSchedule = select5_setSchedule;
 
  this.schedule = new paymentSchedule();
  this.yearsThreeToFiveSchedule = new paymentSchedule();
}




function select5_getPrincipalPaidThisPayment(payment) {
  var k= payment;
  if (this.schedule.getLength() == 0 ) {
    this.setSchedule();
  }

  return this.schedule.getElementAt(k-1).getPrincipal();
}

function select5_getInterestPaidThisPayment(payment) {
  var k= payment;
  if (this.schedule.getLength() == 0 ) {
    this.setSchedule();
  }
  return this.schedule.getElementAt(k-1).getInterest();


}

function select5_setSchedule(payment) {
  
  var currentPrincipalPayment = 0;
  var currentInterestPayment = this.Amortizer.getInterestOnlyMonthlyPayment();
  var paid = undefined;
  var P = this.Amortizer.getPrincipal();
  var M = this.Amortizer.getMonthlyPayment(true);
  var q = this.Amortizer.getInterval();
  var k = ((payment == undefined) ? (q * this.Amortizer.getTermYears()) : payment);
  var i = this.Amortizer.getDecimalInterest();


  for (var j = 1; j <= k ; j++) { 
    currentPrincipalPayment = M - currentInterestPayment;
    paid = new Payment(currentPrincipalPayment,currentInterestPayment);
    this.schedule.add(paid);
    currentInterestPayment =  (( P -= currentPrincipalPayment) * i / q);  
  }
 
}

function select5_getSchedule() {
  return this.schedule;

}
function select5_getYearsThreeToFiveSchedule() {
  if (this.yearsThreeToFiveSchedule.getLength() == 0) {
    this.yearsThreeToFiveSchedule = this.Amortizer.getSchedule(36,(this.Amortizer.getInterestOnlyMonthlyPayment() + this.yearsThreeToFivePrincipalPayment) );
  }
  return this.yearsThreeToFiveSchedule;
}

function select5_getYearsThreeToFivePrincipalPayment(mySchedule) {
  var DEBUG = false;
  if (document.debug) DEBUG = true; 

  var PrincipalPayment = 0;
 
 this.Amortizer.setSchedule();
 var mySchedule = this.Amortizer.getSchedule();
    
    for ( var i = 0 ; i < 36; i++) {
   
      PrincipalPayment += mySchedule.getElementAt(i).getPrincipal();
      if (DEBUG) {STDERR += i + " princ=" + setScale(mySchedule.getElementAt(i).getPrincipal(),2) + "\n";}
    }
     if (DEBUG) {STDERR += "---" + setScale(PrincipalPayment,2) + " -------\n";} 
    PrincipalPayment = (PrincipalPayment / 36) / 2; //half of the 3-year average
  
 
  this.principalPaid = (PrincipalPayment * 36);  //getting un-creative.
  this.yearsThreeToFivePrincipalPayment = PrincipalPayment;
  return PrincipalPayment;
}

function select5_getYearsThreeToFivePrincipalPaid() {
  var mySchedule = this.getYearsThreeToFiveSchedule();
  var myPrincipal = 0;
  for (var i = 0; i < mySchedule.getLength() ; i++) {
    myPrincipal += mySchedule.getElementAt(i).getPrincipal();
  }
  this.yearsThreeToFivePrincipalPaid = myPrincipal;
  return this.yearsThreeToFivePrincipalPaid;
}
function select5_getYearsThreeToFiveInterestPaid() {
 var mySchedule = this.getYearsThreeToFiveSchedule();
  var myInterest = 0;
  for (var i = 0; i < mySchedule.getLength() ; i++) {
    myInterest += mySchedule.getElementAt(i).getInterest();
  }
  this.yearsThreeToFiveInterestPaid = myInterest;
  return this.yearsThreeToFiveInterestPaid;
}

function select5_getRemainingAmortizer(discount) {
  var myDiscount = 0;
  var newP = this.Amortizer.getRemainingPrincipal(36,(this.Amortizer.getInterestOnlyMonthlyPayment() + this.yearsThreeToFivePrincipalPayment) );
  // alert(newP);
  var remainingAmortizer = new amortizer(newP, this.Amortizer.getInterest(), (this.Amortizer.getTermYears() - 3));
 
  return remainingAmortizer;
}
