// Global variables
var objDate = new Date();
var curMonth = objDate.getMonth();
var curYear = objDate.getFullYear();
var curDate = objDate.getDate();
var month = curMonth;
var year = curYear;
var dateLimits = [31,28,31,30,31,30,31,31,30,31,30,31];
var dates = [];
var notes = [];
var hFlags = [];
// Verifies data
function verify(f)
{
  var v = parseInt(f.year.value);
  if (isNaN(v) || v < 1645 || v > 7000)
  {
    alert(sYearInv);
    return false;
  }
  f.year.value = v;
  var m = f.month.options[f.month.selectedIndex].value;
  f.action = "/cgi-bin/annotate.cgi/" + f.year.value + "-" + m + ".xml";
  f.ndays.value = dates.length;
  return true;
}
// End of function verify
// Is leap year
function isLeapYear(y)
{
  if (Math.ceil(y / 100) * 100 == y)
  {
    if (Math.ceil(y / 400) * 400 == y)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  else
  {
    if (Math.ceil(y / 4) * 4 == y)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
}
// Initialize page values
function initPage()
{
  if (curDate > 20)
  {
    curMonth++;
    month++;
    if (curMonth == 12)
    {
      curMonth = 0;
      month = 0;
      curYear++;
      year++;
    }
  }
  if (isLeapYear(year))
  {
    dateLimits[1] = 29;
  }
  document.forms[0].year.value = curYear;
  document.forms[0].month.options[curMonth].selected = true;
  addRow(0);
  displayTable();
}
// End of function loadYear
// Count number of annotated days
// Reset Button Text
function resetText(f)
{
  f.getcal.value = sGetCal;
  f.year.value = curYear;
  f.month.options[curMonth].selected = true;
  days = [0];
  notes = [""];
  hFlags = [false];
  var t = document.getElementById('dateMessages');
  var n = t.rows.length;
  var i;
  for (i = 0; i < n - 2; i++)
  {
    t.deleteRow(2);
  }
  displayTable();
  return false;
}
// End of function resetText
// display a row
function displayRow(i)
{
  var t = document.getElementById('dateMessages');
  var r = t.rows[i + 1];
  r.vAlign = "center";
  r.align = "center";
  var d = r.cells[0];
  var cellHTML = '<select name="date' + i + '" ' + 
                'id="date' + i + '" onBlur="copyDate(this)">';
  var j = 1;
  for (j = 1; j <= dateLimits[month]; j++) {
    cellHTML += '<option value="' + j + '">' + j;
  }
  d.innerHTML = cellHTML + '</select>';
  if (dates[i] > dateLimits[month]) {
    dates[i] = dateLimits[month] - 1;
  }
  document.getElementById('date' + i).options.selectedIndex = dates[i];
  var n = r.cells[1];
  n.innerHTML = '&nbsp;&nbsp;<input type="text" name="note' + i + '" ' +
                'id="note' + i + '" size="50" ' +
                'maxlength="50" value="' + notes[i] + '" ' +
                'onBlur="copyNote(this)" onFocus="this.select()">';
  var h = r.cells[2];
  h.innerHTML = '<input type="checkbox" name="isHoliday' + i +
                '" id="isHoliday' + i + '" onClick="copyHFlags(this)">&nbsp;&nbsp;&nbsp;';
  document.getElementById('isHoliday' + i).checked = hFlags[i];
  var a = r.cells[3];
  a.innerHTML = '<img src="add.gif" ' +
                'onClick="addRow(this.parentNode.parentNode.rowIndex)">';
  if (i > 0) {
    var d = r.cells[4];
    d.innerHTML = '<img src="delete.gif" ' +
                  'onClick="delRow(this.parentNode.parentNode.rowIndex)">';
  }
}
// End of displayRow
// Function displayTable
function displayTable()
{
  var n = dates.length;
  var i;
  for (i = 0; i < n; i++)
  {
    displayRow(i);
  }
}
// End of displayTable
// Add a new row
function addRow(i)
{
  var t = document.getElementById('dateMessages');
  if (t.rows.length >= dateLimits[month]) {
    alert(sRowMax);
    return false;
  }
  dates.splice(i, 0, 0);
  notes.splice(i, 0, "");
  hFlags.splice(i, 0, false);
  var r = t.insertRow(i + 1);
  r.insertCell(0);
  r.insertCell(1);
  r.insertCell(2);
  r.insertCell(3);
  r.insertCell(4);
  displayTable();
}
// End of addRow
// Delete a row
function delRow(i)
{
  dates.splice(i - 1, 1);
  notes.splice(i - 1, 1);
  hFlags.splice(i - 1, 1);
  document.getElementById('dateMessages').deleteRow(i);
  displayTable();
}
// End of delRow
// Function copyDate
function copyDate(s)
{
  var i = s.parentNode.parentNode.rowIndex - 1;
  dates[i] = s.value - 1;
}
// End of copyDate
// Function copyNote
function copyNote(s)
{
  var i = s.parentNode.parentNode.rowIndex - 1;
  notes[i] = s.value;
}
// End of copyNote
// Function copyHFlags
function copyHFlags(s)
{
  var i = s.parentNode.parentNode.rowIndex - 1;
  hFlags[i] = s.checked;
}
// End of copyHFlags
// Function copyMonth
function copyMonth(s)
{
  month = s.value - 1;
  displayTable();
}
// End of copyMonth
// Function copyYear
function copyYear(s)
{
  var v = s.value;
  if (isNaN(v) || v < 1645 || v > 7000)
  {
    alert(sYearInv);
    s.focus();
    return false;
  }
  year = s.value;
  if (isLeapYear(year))
  {
    dateLimits[1] = 29;
  }
  else
  {
    dateLimits[1] = 28;
  }
  displayTable();
}
// End of copyYear
