// ----------------------------------------------------------------------------
// NAME         : cal.js
// VERSION      : 1.0
// DESCRIPTION  : library for popup Calendar functions
// DEPENDENCIES : date.js   1.0
//                gen.js    1.0
// ----------------------------------------------------------------------------

// ----------------------------- Function List --------------------------------
//
// void cal_openCalendar (object o, string monthlist, string daylist, 
//                        string title, int startMonth, int endMonth)
// void cal_writeCalendar (
// void cal_selectDate (int dateNo)
//
// ----------------------------------------------------------------------------

// ----------------------------- GLOBALS --------------------------------------
var cal_Window   = null;  // Object to store calendar window
var cal_mList    = null;  // Object to store month list
var cal_dList    = null;  // Object to store day list
var cal_yList    = null;  // Object to containing year list
var cal_MaxYear  = "";    // Maximum year value from drop down on search page

// Array of month names
cal_monthNames = new Array("January",
                           "February",
                           "March",
                           "April",
                           "May",
                           "June",
                           "July",
                           "August",
                           "September",
                           "October",
                           "November",
                           "December");

//-----------------------------------------------------------------------------
// NAME       : cal_openCalendar
// PARAMETERS : object o
//              string monthlist
//              string daylist
//              string title
//              int    startMonth 
//              int    endMonth   
// RETURNS    : undefined
// BEHAVIOUR  : Creates a new window and calls write calendar to populate it
//              If a window has already been created then it simply gives it 
//              focus. If you do not wish to restrict the Calendars months
//              then leave the last two parameters null.
//-----------------------------------------------------------------------------
function cal_openCalendar(o, monthlist, daylist, title, startMonth, endMonth, yearlist)
{
  cal_mList = eval ("o.form." + monthlist);
  cal_dList = eval ("o.form." + daylist);
  if (yearlist == null)
    cal_yList = null;
  else
    cal_yList = eval ("o.form." + yearlist);

  //Setup the maximum year value, the calendar cannot go beyond this
  if (cal_yList != null) {
    var numberItems = cal_yList.options.length - 1;
    cal_MaxYear = cal_yList.options[numberItems-0].value;
  } else {
    cal_maxYear = date_getCurrentYear()+1;
  }

 if (cal_Window == null || cal_Window.closed) // We must create the new window
  {
    cal_Window = 
          window.open("", "calWindow", "titlebar=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no,WIDTH=270,HEIGHT=310");

    // Populate the calendar window with a default month
    cal_writeCalendar(title, cal_mList.selectedIndex, startMonth, endMonth, getDropDownValue(cal_yList));
  }
  else //if (cal_Window.document.calForm.mycaller.value != o.value)
  {
    // Populate the calendar window with a default month
    cal_writeCalendar(title, cal_mList.selectedIndex, startMonth, endMonth, getDropDownValue(cal_yList));
  } 

  // Give focus to the window
  cal_Window.focus();
}

//-----------------------------------------------------------------------------
// NAME       : cal_writeCalendar
// PARAMETERS : string title
//              int    month
//              int    startMonth
//              int    endMonth
// RETURNS    : undefined
// BEHAVIOUR  : Populate a calendar window using document.write. This is 
//              called whenever a new calendar window is created or the
//              month is changed
//-----------------------------------------------------------------------------
function cal_writeCalendar(title, month, startMonth, endMonth, year) 
{
  var rangeErrMax = "N"; //Set past max date range error to no
  var rangeErrMin = "N"; //Set before min date range error to no
  var today = new Date;  //Todays date

  //Set max range limit to drop down year value on search page
  if (month == 11) {
    if (parseInt(year) == parseInt(cal_MaxYear)) {
      rangeErrMax = "Y";
    }
  }  

  //Set min range limit for month
  if (parseInt(year) == parseInt(today.getFullYear())) {
    if (parseFloat(month + 1) == parseFloat(today.getMonth() + 1)) {
      rangeErrMin = "Y";
    }
  }

  // Deal with 'wraparound' months
  if (month == 12) 
  {
    month = 0;
    year = parseInt(year) + 1; 
  }
  else if (month == -1)
  { 
    month = 11;
    year = parseInt(year) - 1;
  }
  
  // If start and end month are restricted...
  if (startMonth != '') {
    if (month == startMonth) {
      month = endMonth - 1;
    }
  } else if (endMonth != '') {
    if (month == endMonth) {
      month = startMonth + 1;
    }
  }
  // Deal with 'wraparound' months
  if (month == 12) 
  {
    if (parseInt(year) < parseInt(cal_MaxYear)) {
      month = 0;
      year = parseInt(year) + 1; 
    }
  }
  else if (month == -1)
  { 
    month = 11;
    year = parseInt(year) - 1;
  }

  // Variables to hold the date
  var calYear = date_getCurrentYear();
  if (year == "") year = null;
  if (year != null) {
    calYear = year;
  } else if (cal_yList != null) {
    calYear = cal_yList.options[cal_yList.selectedIndex].value;
  } else {
    calYear = date_getCurrentYear();
    if (date_isItNextYear(month)) {
      calYear++;
    }
  }
  // Write out a large string to be written to the page
  var page = ""

  // Write the head of the document
  page += "<HTML>\n<HEAD>\n";
  page += "<TITLE>Calendar</TITLE>\n";
  page += "</HEAD>\n";

  // Begin the body of the document
  page += "<BODY BGCOLOR=\"#FFFFFF\"><CENTER>\n";

  // Write hidden fields
  page += "<FORM NAME=\"calForm\">\n";
  page += "<INPUT TYPE=\"HIDDEN\" NAME=\"year\"       VALUE=\"" + calYear    + "\">\n";
  page += "<INPUT TYPE=\"HIDDEN\" NAME=\"month\"      VALUE=\"" + month      + "\">\n";
  page += "<INPUT TYPE=\"HIDDEN\" NAME=\"startmonth\" VALUE=\"" + startMonth + "\">\n";
  page += "<INPUT TYPE=\"HIDDEN\" NAME=\"endmonth\"   VALUE=\"" + endMonth   + "\">\n";
  //page += "<INPUT TYPE=\"HIDDEN\" NAME=\"mycaller\"   VALUE=\"" + mycaller   + "\">\n";
  page += "<INPUT TYPE=\"HIDDEN\" NAME=\"title\"      VALUE=\"" + title      + "\">\n";
  page += "</FORM>\n";

  // Create a heading for the page
  page += "<FONT FACE=\"Verdana\" SIZE=\"3\"><B>";
  page += title + "<BR>";
  page += "<FONT SIZE=\"2\">" + cal_monthNames[month];
  page += "&nbsp;&nbsp;" + calYear + "</B></FONT>\n";

  // Write a table to hold the calendar headings
  page += "<TABLE BORDER=\"0\" CELLSPACING=\"2\" CELLPADDING=\"1\" HEIGHT=\"20\">\n";

  // Write the days of the week
  page += "\t<TR VALIGN=\"TOP\" HEIGHT=\"16\">\n";
  page += "\t\t<TD BGCOLOR=\"#000000\" ALIGN=\"CENTER\" WIDTH=\"31\"><FONT FACE=\"Verdana\" SIZE=\"1\" COLOR=\"#FFFFE0\"><B>Sun</B></FONT></TD>\n";
  page += "\t\t<TD BGCOLOR=\"#000000\" ALIGN=\"CENTER\" WIDTH=\"31\"><FONT FACE=\"Verdana\" SIZE=\"1\" COLOR=\"#FFFFFF\"><B>Mon</B></FONT></TD>\n";
  page += "\t\t<TD BGCOLOR=\"#000000\" ALIGN=\"CENTER\" WIDTH=\"31\"><FONT FACE=\"Verdana\" SIZE=\"1\" COLOR=\"#FFFFFF\"><B>Tue</B></FONT></TD>\n";
  page += "\t\t<TD BGCOLOR=\"#000000\" ALIGN=\"CENTER\" WIDTH=\"31\"><FONT FACE=\"Verdana\" SIZE=\"1\" COLOR=\"#FFFFFF\"><B>Wed</B></FONT></TD>\n";
  page += "\t\t<TD BGCOLOR=\"#000000\" ALIGN=\"CENTER\" WIDTH=\"31\"><FONT FACE=\"Verdana\" SIZE=\"1\" COLOR=\"#FFFFFF\"><B>Thu</B></FONT></TD>\n";
  page += "\t\t<TD BGCOLOR=\"#000000\" ALIGN=\"CENTER\" WIDTH=\"31\"><FONT FACE=\"Verdana\" SIZE=\"1\" COLOR=\"#FFFFFF\"><B>Fri</B></FONT></TD>\n";
  page += "\t\t<TD BGCOLOR=\"#000000\" ALIGN=\"CENTER\" WIDTH=\"31\"><FONT FACE=\"Verdana\" SIZE=\"1\" COLOR=\"#FFFFE0\"><B>Sat</B></FONT></TD>\n";
  page += "\t</TR>\n";

  page += "</TABLE>\n";

  // Write a table to hold the calendar body
  page += "<TABLE BORDER=\"0\" CELLSPACING=\"2\" CELLPADDING=\"1\" HEIGHT=\"165\">\n";

  // Dynamically create the days of the month
  var tempDate = 0; // temporary variable to hold the date

  // Retrieve the first day of the month
  var firstDay = new Date(calYear, month, 1).getDay();
  var maxDate = date_howManyDays(month, calYear);
  var cal_NowDay = today.getDate(); //Todays date
  var content = "";

  for (var rows = 0; rows<6; rows++)
  {
    page += "\t<TR>\n";

    for (var cols = 0; cols<7; cols++)
    {
      page += "\t\t<TD";
      if (cols == 0 || cols == 6)
      {
        page += " BGCOLOR=\"lightgrey\"";
      }
      else
      {
        page += " BGCOLOR=\"linen\"";
      }
      
      page += " ALIGN=\"CENTER\" WIDTH=\"31\" HEIGHT=\"23\">";
      
      if (tempDate > maxDate)
      {
        content = "__";
      }
      else
      {
        if (tempDate == 0 ) 
        {
          if (cols == firstDay) 
          {
            tempDate = 1;
          }
          else
          {
            content = "__";
          }
        }

        if (tempDate != 0)
        {
          //If on this month and year, and date is before tomorrow
	  //Blank field out  
          if ((parseInt(tempDate) < parseInt(cal_NowDay + 1)) &&
	    (parseFloat(month) == parseFloat(today.getMonth())) &&
            (parseInt(year) == parseInt(today.getFullYear()))) {
              content = "__";
	  }
	  else {
            content = "<FONT FACE=\"Tahoma, Arial\" SIZE=\"1\"";
            content += "><B>" 
            content += "<A HREF=\"javascript:opener.cal_selectDate(" + tempDate + ");\">";
            content += tempDate + "</A></B></FONT>";
          }
          tempDate++;
        }
      }
      
      page += content;
      page += "</TD>\n";
    }

    page += "\t</TR>\n";
    if (tempDate > maxDate)
    {
      break;
    }
  }

  // Close table
  page += "</TABLE><BR>\n";

  //Is the calender on minimum allowed date?
  if (rangeErrMin != "Y") {
  // Add buttons for previous and next
  page += "<A HREF=\"javascript:opener.cal_writeCalendar(document.calForm.title.value, parseInt(document.calForm.month.value)-1, parseInt(document.calForm.startmonth.value), parseInt(document.calForm.endmonth.value), document.calForm.year.value)\"><FONT FACE=\"Tahoma, Arial\" SIZE=\"2\"><< Previous</FONT></A>&nbsp;&nbsp;\n";
  }

  //Is calender on maximum allowed date?
  if (rangeErrMax != "Y") {
  page += "<A HREF=\"javascript:opener.cal_writeCalendar(document.calForm.title.value, parseInt(document.calForm.month.value)+1, parseInt(document.calForm.startmonth.value), parseInt(document.calForm.endmonth.value), document.calForm.year.value)\"><FONT FACE=\"Tahoma, Arial\" SIZE=\"2\">Next >></FONT></A><BR>\n";
  }

  // Write the tail of the document
  page += "</CENTER>\n</BODY>\n</HTML>\n";

  // Open the document to be written
  cal_Window.document.open();

  // Write the HTML page using the 'page' string
  cal_Window.document.write(page);

  // Close the document to prevent further writing to it
  cal_Window.document.close();
}

//------------------------------- cal_selectDate ------------------------------
// NAME      : cal_selectDate
// PARAMETER : int dateNo
// RETURNS   : undefined
// BEHAVIOUR : Close calender and send date to text field (and hidden value)
//             in main window
//-----------------------------------------------------------------------------
function cal_selectDate(dateNo)
{
  var tempDate = new Date(cal_Window.document.calForm.year.value, 
                          cal_Window.document.calForm.month.value, dateNo);
  var monthString = tempDate.getMonth() + 1;
  if (monthString < 10)
  {
    monthString = "0" + monthString;
  }

  gen_makeItemSelected(cal_mList, monthString);

  // After month has been changed, update days available
  gen_updateDays (cal_mList, cal_dList.name, null, null);

  var dateString = tempDate.getDate();
  if (dateString < 10) 
  {
    dateString = "0" + dateString;
  }
  gen_makeItemSelected(cal_dList, dateString);
  if (cal_yList != null) {
    gen_makeItemSelected(cal_yList, cal_Window.document.calForm.year.value);
    cal_mList.onchange();
  }
  cal_Window.close();
}


