//Student Class
function Student(firstName, lastName, birthDate)
{
  this.firstName = firstName;
  this.lastName = lastName;
  this.birthDate = birthDate;

  this.template =
  "<tr>" +
  "  <td class=fname>#FIRSTNAME#</td>" +
  "  <td class=lname>#LASTNAME#</td>" +
  "  <td class=bdate>#BIRTHDATE#</td>" +
  "  <td class=remove_item><a href='javascript:RemoveStudent(#ID#)'><img alt='Remove student from list' src='img/enrollment/remove.gif' border=0></a></td>" +
  "</tr>";

  this.template_info =
  "<tr>" +
  "  <td class=fname>#FIRSTNAME#</td>" +
  "  <td class=lname>#LASTNAME#</td>" +
  "  <td class=bdate>#BIRTHDATE#</td>" +
  "</tr>";

  this.toString = function()
  {
    return "First Name: " + this.firstName + "; " +
           "Last Name: " + this.lastName + "; " +
           "Birthdate: " + this.birthDate;
  }

  this.draw = function()
  {
    return this.template.replace(/#FIRSTNAME#/, this.firstName).replace(/#LASTNAME#/, this.lastName).replace(/#BIRTHDATE#/, this.birthDate);
  }

  this.info = function()
  {
    return this.template_info.replace(/#FIRSTNAME#/, this.firstName).replace(/#LASTNAME#/, this.lastName).replace(/#BIRTHDATE#/, this.birthDate);
  }

}

function Students()
{
  this.maxStudentsCount = 10;

  this.cost = 200;

  this.discounts = new Array(0, 50, 100);

  this.isSiblings = 1;

  this.list = new Array();

  this.item = function(i)
  {
    return this.list[i];
  }

  this.add = function(student)
  {
    this.list[this.list.length] = student;
  }

  this.remove = function(i)
  {
    for (j = i; j < this.list.length - 1; j++) this.list[j] = this.list[j + 1];
    this.list.pop();
  }

  this.studentsCount = function()
  {
    return this.list.length;
  }

  this.totalCost = function()
  {
    var total = 0
    for (i = 0; i < this.studentsCount(); i++)
    {
      if (this.isSiblings == 1)
      {
        if (i < this.discounts.length)
          total += this.cost - this.discounts[i];
        else
          total += this.cost - this.discounts[this.discounts.length - 1];
      }
      else
      {
        total += this.cost;
      }
    }
    return total;
  }

  this.draw = function()
  {
    var result = "<table class=students cellspacing=0 cellpadding=0>";
    result += "<tr>" +
              "  <th class=fname>Student First Name</th>" +
              "  <th class=lname>Student Last Name</th>" +
              "  <th class=bdate colspan=2>Date of Birth</th>" +
              "</tr>";
    for (i = 0; i < this.list.length; i++)
      result += this.item(i).draw().replace(/#ID#/, i);


    if (this.list.length < this.maxStudentsCount)
      result += "<tr class=inputs>" +
                "  <td><input name=fname type=text></td>" +
                "  <td><input name=lname type=text></td>" +
                "  <td colspan=2>" +
                "    <nobr><input style='text-align:right' name=bdate type=text id=date /><input style='height:22;width:22' type=button onclick=show_cal(this); value='...' /></nobr>" +
                "  </td>" +
                "</tr>" +
                "<tr>" +
                "  <td colspan = 4 class=add><input type=button onclick='AddStudent()' value='Add " + (this.studentsCount() > 0 ? "another " : "") + "student'></td>" +
                "</tr>";

    result += "</table>"

    if (this.studentsCount() > 1)
      result += "<table>" +
                "  <tr>" +
                "    <td>Are these students siblings?</td>" +
                "    <td>" +
                "      <input type=radio name=siblings value=1 onclick='ChangeIsSiblings(1)' " + (this.isSiblings == 1 ? "checked=checked" : "") + "> Yes" +
                "      <input type=radio name=siblings value=0 onclick='ChangeIsSiblings(0)' " + (this.isSiblings == 0 ? "checked=checked" : "") + "> No " +
                "    </td>" +
                "  </tr>" +
                "</table>";

    if (this.studentsCount() > 0)
      result += "Total students to enroll: <b>" + this.studentsCount() + "</b>" +
                "<br>" +
                "Total cost: <b>$" + this.totalCost() + "</b>";

    return result;
  }

  this.info = function()
  {
    var result = "<table class=students cellspacing=0 cellpadding=0>";
    result += "<tr>" +
              "  <th class=fname>Student First Name</th>" +
              "  <th class=lname>Student Last Name</th>" +
              "  <th class=bdate>Date of Birth</th>" +
              "</tr>";
    for (i = 0; i < this.list.length; i++)
      result += this.item(i).info();
    
    result += "</table>";

    result += "<p>These students are" + (this.isSiblings == 0 ? " not" : "") + " siblings.</p>";

    result += "Total students to enroll: <b>" + this.studentsCount() + "</b>" +
              "<br>" +
              "Total cost: <b>$" + this.totalCost() + "</b>";
    return result;
  }
}

var tableElement;
var students = new Students();

function DrawStudentsTable(elementId)
{
  tableElement = document.getElementById(elementId);
  RedrawStudentsTable();
}

function RedrawStudentsTable()
{
  tableElement.innerHTML = students.draw();
}

function RemoveStudent(id)
{
  students.remove(id);
  RedrawStudentsTable();
}

function AddStudent()
{
  students.add(new Student(document.getElementsByName('fname')[0].value,
          document.getElementsByName('lname')[0].value,
          document.getElementsByName('bdate')[0].value));
  RedrawStudentsTable();
}

function ChangeIsSiblings(isSiblings)
{
  students.isSiblings = isSiblings;
  RedrawStudentsTable();
}
