var calTarget;
var calDate = new Date();

function calCancel() {
  document.getElementById("calSet").style.visibility = "hidden";
  return false;
  };

function calClose(dYear,dMonth,dDay) {
  var theString = dYear + "-";
  if (dMonth < 9) {
    theString += "0";
    };
  theString += (dMonth + 1) + "-";
  if (dDay < 10) {
    theString += "0";
    };
  theString += dDay + " 00:01:00";
  calTarget.value = theString;
  document.getElementById("calSet").style.visibility = "hidden";
  return false;
  };

function calShiftMonth(howFar) {
  // takes an integer as a parameter, and uses that to move the date
  // up or down by a monthly increment.
  howFar = parseInt(howFar); // make sure it's an integer
  if (isNaN(howFar)) { return false; };
  var monthNow = calDate.getMonth() + howFar;
  var yearNow = calDate.getFullYear();
  if (monthNow < 0) {
    // ran off the end...
    monthNow = 11;
    yearNow--;
    };
  if (monthNow > 11) {
    // ran off the end...
    monthNow = 0;
    yearNow++;
    };
  calDate.setMonth(monthNow);
  calDate.setFullYear(yearNow);
  calSetup(calTarget);
  return false;
  };

function calSetup(target) {
  // This function uses the global date object calDate,
  // and sets the calendar to show the month of that, with that date
  // "highlighted".  Each individual day of the month is clickable,
  // and results in the function "calClose()" being called with
  // the given day as it's parameter.  The "target" parameter is
  // a pointer to a form input tag, which will have it's value changed
  // by the calClose() function - we must save it in the global
  // variable calTarget.
  calTarget = target;

  // Clear out the old contents:
  for (qwe = 0; qwe < 6; qwe ++) {
    for (rty = 0; rty < 7 ; rty ++) {
      document.getElementById("calDay"+qwe+rty).innerHTML = "&nbsp;";
      };
    };

  var theMonth, daysInMonth;
  switch (calDate.getMonth()) {
    case  0: theMonth = "January"; daysInMonth = 31; break;
    case  1: // February - the complex one.
      theMonth = "February";
      daysInMonth = 28;
      if ((calDate.getFullYear() % 4) == 0) {
        daysInMonth = 29; // Anyone still using this function
                         // in their code in the year 2100 without
                        // fixing it first needs to be slapped.
        };
      break;
    case  2: theMonth = "March"; daysInMonth = 31; break;
    case  3: theMonth = "April"; daysInMonth = 30; break;
    case  4: theMonth = "May"; daysInMonth = 31; break;
    case  5: theMonth = "June"; daysInMonth = 30; break;
    case  6: theMonth = "July"; daysInMonth = 31; break;
    case  7: theMonth = "August"; daysInMonth = 31; break;
    case  8: theMonth = "September"; daysInMonth = 30; break;
    case  9: theMonth = "October"; daysInMonth = 31; break;
    case 10: theMonth = "November"; daysInMonth = 30; break;
    case 11: theMonth = "December"; daysInMonth = 31; break;
    default: theMonth = "ERROR!"; daysInMonth = 0;
    };
  document.getElementById("calCurMonth").innerHTML = theMonth + " " + calDate.getFullYear();
  var theDay = calDate.getDate(); // The only one that starts at 1!
  // Now find out what day of the week we're starting on:
  calDate.setDate(1);
  var theDOW = calDate.getDay(); // 0=Sunday, 1=Monday, ..., 6=Saturday
  calDate.setDate(theDay); // Put things back where we had them...
  var theWeek = 0; // This is always the week we start on
  var theGuts; // temp variable...
  for (var i = 1;i <= daysInMonth ; i++) {
    theGuts = "<a href=\"#\" onclick=\"return calClose(" + calDate.getFullYear();
    theGuts += ", " + calDate.getMonth() + ", " + i + ")\">";
    if (i < 10) { theGuts += "0"; };
    theGuts += i + "</a>";
    document.getElementById("calDay"+theWeek+theDOW).innerHTML = theGuts;
    theDOW++;
    if (theDOW > 6) {  // ran off the end of the week
      theDOW = 0;
      theWeek++;
      };
    };
  document.getElementById("calSet").style.visibility = "visible";
  };

