// calendar.js

// ---------------------------------------------------------------------------
// History
// -------
// Original version by Badrinath Chebbi, 04/01/2002
// Downloaded from http://www.scriptsearch.com/cgi-bin/jump.cgi?ID=4049
// This version by David W. Brown, Vector Space IT, 22/12/2002
// Copyright (c) 2002, Vector Space IT
// Enquiries to dwb@vectorspace.co.uk
// ---------------------------------------------------------------------------

// Browser detection
var IE4 = (document.all && !document.getElementById) ? true : false;
var NS4 = (document.layers) ? true : false;
var IE5 = (document.all && document.getElementById) ? true : false;
var N6 = (document.getElementById && !document.all) ? true : false;

var aMonthNames = new Array(
  'January',
  'February',
  'March',
  'April',
  'May',
  'June',
  'July',
  'August',
  'September',
  'October',
  'November',
  'December'
);

var aMonthDisplay = new Array(
  '01',
  '02',
  '03',
  '04',
  '05',
  '06',
  '07',
  '08',
  '09',
  '10',
  '11',
  '12'
);

var aMonthDays = new Array(
  31,     // Jan
  28,     // Feb
  31,     // Mar
  30,     // Apr
  31,     // May
  30,     // Jun
  31,     // Jly
  31,     // Aug
  30,     // Sep
  31,     // Oct
  30,     // Nov
  31      // Dec
);

var days = new Array(42);

// ---------------------------------------------------------------------------
// Display the dates for month a of year b, where year is given as yyyy and
// month as a number in the range 0 to 11.
// ---------------------------------------------------------------------------
function daylayerdisplay(b, a)
{
  // Make a persistent copy of the given month
  mth = a;

  // Adjust the number of days in Feb depending on year
  if (b % 4 == 0 || b % 100 == 0) {
    aMonthDays[1] = 29;
  }
  else {
    aMonthDays[1] = 28;
  }

  // This next line is probably redundant - oDate doesn't seem to be used
  // anywhere
  var oDateNow = new Date();

  // Get the starting day for the month
  var oDate = new Date(aMonthNames[a] +  1 + "," + b);
  dayofweek = oDate.getDay();

  var count = 0;
  var count1;
  var end = aMonthDays[a] + (dayofweek);

  // Clear all entries
  for (s = 1; s <= 42; s++) {
    document.getElementById("day" + s).childNodes[0].innerHTML = "";
  }

  // Refill with correct values for this month
  for (s = (dayofweek + 1); s <= end; s++) {
    count = count + 1;
    document.getElementById("day" + s).childNodes[0].innerHTML = count;
    if (count <= 9) {
      count1 = 0 + "" + count;
    }
    else {
      count1 = count;
    }
    document.getElementById("day" + s).childNodes[0].id = count1;
  }
}

// ---------------------------------------------------------------------------
// Close the current calendar frame as indicated by the current entry in the
// hidden text box (calframeid). It then sets the specified new value into
// the hidden text box.
// ---------------------------------------------------------------------------
function setcalframeid(newframeid)
{
  // Close open calendar frame, if any
  calid = parent.document.callingform.calframeid.value;
  if (calid != '[none]') parent.document.getElementById(calid).style.display='none'
  // Set new frame id
  parent.document.callingform.calframeid.value = newframeid;
}

// ---------------------------------------------------------------------------
// Close the existing calendar frame and open a new one with the given id.
// ---------------------------------------------------------------------------
function opencalframe(newframeid)
{
  setcalframeid(newframeid);
  document.getElementById(newframeid).style.display='inline';
}

// ---------------------------------------------------------------------------
// Convert a date to standard format and return it the originating form
// component. If y == 1 today's date is returned; otherwise the date given
// by y, m and d is returned. Hide the calendar on completion.
// ---------------------------------------------------------------------------
function sendvalue(y, m, d) {

  if (m == 13 /* close */) {
    calid = parent.document.callingform.calframeid.value;
    parent.document.getElementById(calid).style.display='none'
  }
  else if (y == 1 /* today */) {

    // Get today's date
    todayobj = new Date();
    today = todayobj.getYear() + todayobj.getMonth() + todayobj.getDate();

    // Modify according to browser if necessary
    if (N6) {
      year = todayobj.getYear() + 1900;
    }
    else {
      year = todayobj.getYear();
    }

    // Put a zero in front of the day if only a single digit - for some reason
    // this doesn't appear to be necessary for the month
    if (todayobj.getDate() <= 9) {
      var todayday = 0 + "" + todayobj.getDate();
    }
    else {
      var todayday = todayobj.getDate();
    }

    // Format and return to component
    elid = parent.document.callingform.dateboxid.value;
    parent.document.callingform.elements[elid].value =
      todayday + "/" + aMonthDisplay[todayobj.getMonth()] + "/" + year;

    // Hide the calendar
    calid = parent.document.callingform.calframeid.value;
    parent.document.getElementById(calid).style.display = 'none'
  }
  else /* date */ {
    // Format the given date and return it to the form component
    elid = parent.document.callingform.dateboxid.value;
    parent.document.callingform.elements[elid].value = d + "/" + aMonthDisplay[m] + "/" + y;

    // Hide the calendar
    calid = parent.document.callingform.calframeid.value;
    parent.document.getElementById(calid).style.display='none'
  }
}

// ---------------------------------------------------------------------------
// Reduce the global month count by one; update the month drop-down; redraw
// the calendar for the new month.
// ---------------------------------------------------------------------------
function reducemonths() {

  // This bizarre form is for the sake of consistency with the increasemonths
  // function. It doesn't seem to be necessary here.
  mth = +mth - 1;

  // End of year
  if (mth == -1) {
    mth = 11;
    document.calendarform.year.value = parseFloat(document.calendarform.year.value) - 1;
  }

  // Update drop-down
  document.calendarform.month[mth].selected = "1";

  // Redraw calendar
  daylayerdisplay(document.calendarform.year.value, mth);
}


// ---------------------------------------------------------------------------
// Increase the global month count by one; update the month drop-down; redraw
// the calendar for the new month.
// ---------------------------------------------------------------------------
function increasemonths()
{
  // If the + sign is omitted from +mth the script doesn't work. No, I've no idea...
  mth = +mth + 1;

  // End of year
  if (mth == 12) {
    mth = 0;
    document.calendarform.year.value = parseFloat(document.calendarform.year.value) + 1;
  }

  // Update drop-down
  document.calendarform.month[mth].selected = "1";

  // Redraw calendar
  daylayerdisplay(document.calendarform.year.value, mth);
}


