// Global variables
//
// Modded by TinK 02/10/2005
//
// Now Works
// Cal window comes to front each time
//

var calWindow  = null;  // Object to store calender window
var myDate     = null;  // Object to store full date 

// Array of month names
var monthNames = new Array("January",
                       "February",
                       "March",
                       "April",
                       "May",
                       "June",
                       "July",
                       "August",
                       "September",
                       "October",
                       "November",
                       "December");
                       
//---------------------------------------------------------------------------
// name: isItNextYear
// parameters: int themonth
// Check to see if a month is this year or next...
//---------------------------------------------------------------------------
function isItNextYear(themonth)
{
  var returnvalue  = false;
  var currentDate  = new Date();
  var currentMonth = currentDate.getMonth();
  if (themonth < currentMonth)
  {
    returnvalue = true;
  }

  return returnvalue;
}

//---------------------------------------------------------------------------
// name: getCurrentYear
// parameters: none
// Returns the current year in four digit format
//---------------------------------------------------------------------------
function getCurrentYear()
{
  var currentDate = new Date();
  return currentDate.getFullYear();
}

//---------------------------------------------------------------------------
// name: howManyDays
// parameters: int thisMonth 
//             int thisYear  
// Returns the number of days in the month
//---------------------------------------------------------------------------
function howManyDays(thisMonth, thisYear)
{
  var monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  if (thisMonth != 1)
  {
    return monthDays[thisMonth];
  }
  else
  {
    // month is february
    if (   (   (thisYear%400)  == 0 ) 
        || (   ((thisYear%100) != 0 ) 
            && ((thisYear%4)   == 0 )
           ) 
       )
    {
      return 29;
    }
    else
    {
      return 28;
    }
  }
}

//---------------------------------------------------------------------------
// name: openCalendar
// parameters: string mycaller
//             string title
//             int    startMonth 
//             int    endMonth   
// Creates a new window and calls write calendar to populate it
// If a window has already been created then it simply gives it focus
//---------------------------------------------------------------------------
function openCalendar(mycaller, title, startMonth, endMonth)
{
  var month = 0;
  var outmonth = 0;
  var retmonth = 0;

  if( eval ("document.FSSform." + mycaller + "Mon.selectedIndex") != null)
  {
    month = eval ("document.FSSform." + mycaller + "Mon.selectedIndex");

    outmonth = eval ("document.FSSform.OutMon.selectedIndex");
    retmonth = eval ("document.FSSform.RetMon.selectedIndex");

    if (retmonth < outmonth) { 
	month = outmonth
    }

  }

  if (calWindow == null || calWindow.closed) // We must create the new window
  {
    calWindow = 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
    writeCalendar(mycaller, title, month, startMonth, endMonth);
  }
  else if (calWindow.document.calForm.mycaller.value != mycaller)
  {
    // Populate the calendar window with a default month
    writeCalendar(mycaller, title, month, startMonth, endMonth);
  } 

  // Give focus to the window
  if (window.focus) {calWindow.focus()};


}

//---------------------------------------------------------------------------
// name: writeCalendar
// parameters: string mycaller
//             string title
//             int    month
//             int    startMonth
//             int    endMonth
// Populate a calendar window using document.write. This is called whenever
// a new calendar window is created or the month is changed
//---------------------------------------------------------------------------
function writeCalendar(mycaller, title, month, startMonth, endMonth) 
{
  // Deal with 'wraparound' months
  if (month == 12) 
  {
    month = 0;
  }
  else if (month == -1)
  { 
    month = 11;
  }
  // Extra processing for BAOffers
  // Loop around April-December
  if (month == startMonth)
  {
    month = endMonth - 1;
  }
  else if (month == endMonth)
  {
    month = startMonth + 1;
  }
  // Deal with 'wraparound' months
  if (month == 12) 
  {
    month = 0;
  }
  else if (month == -1)
  { 
    month = 11;
  }

  // Variables to hold the date
  var calYear = getCurrentYear();
  if (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\">" + 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 = howManyDays(month, calYear);
  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)
        {
          content = "<FONT FACE=\"Tahoma, Arial\" SIZE=\"1\"";
          content += "><B>" 
          content += "<A HREF=\"javascript:opener.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";

  // Add buttons for previous and next
  page += "<A HREF=\"javascript:opener.writeCalendar(document.calForm.mycaller.value, document.calForm.title.value, parseInt(document.calForm.month.value)-1, parseInt(document.calForm.startmonth.value), parseInt(document.calForm.endmonth.value))\"><FONT FACE=\"Tahoma, Arial\" SIZE=\"2\"><< Previous</FONT></A>&nbsp;&nbsp;\n";
  page += "<A HREF=\"javascript:opener.writeCalendar(document.calForm.mycaller.value, document.calForm.title.value, parseInt(document.calForm.month.value)+1, parseInt(document.calForm.startmonth.value), parseInt(document.calForm.endmonth.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
  calWindow.document.open();

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

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

//---------------------------------------------------------------------------
// name: selectDate
// parameter: int dateNo
// Close calender and send date to text field (and hidden value) in main
// window
//---------------------------------------------------------------------------
function selectDate(dateNo)
{
  var tempDate = new Date(calWindow.document.calForm.year.value, calWindow.document.calForm.month.value, dateNo);
  //var datetextobject = eval("document.FSSform." + calWindow.document.calForm.mycaller.value + "DateText");
  //datetextobject.value = tempDate.toString();
  var dateMonth = eval("document.FSSform." + calWindow.document.calForm.mycaller.value + "Mon");
  var dateDay   = eval("document.FSSform." + calWindow.document.calForm.mycaller.value + "Day");
  var monthString = tempDate.getMonth() + 1;
  if (monthString < 10)
  {
    monthString = "0" + monthString;
  }

  makeItemSelected(dateMonth, monthString);

  // After month has been changed, update days available
  if (calWindow.document.calForm.mycaller.value == "Out")
  {
    setDays('Out');
  }
  else if (calWindow.document.calForm.mycaller.value == "Ret")
  {
    setDays('Ret');
  }

  var dateString = tempDate.getDate();
  if (dateString < 10) 
  {
    dateString = "0" + dateString;
  }
  makeItemSelected(dateDay, dateString);

// Hacked in by TinK
var weekday=new Array(7)
weekday[0]="Sun"
weekday[1]="Mon"
weekday[2]="Tue"
weekday[3]="Wed"
weekday[4]="Thu"
weekday[5]="Fri"
weekday[6]="Sat"

var theYear = date_getCurrentYear();
if (date_isItNextYear(monthString-1)) theYear++;

var d=new Date(theYear, monthString-1, dateString);

  var thedate=dateString + "" + monthString;
  if (calWindow.document.calForm.mycaller.value == "Out")
  {
	document.FSSform.shortOutDate.value=thedate;
	document.getElementById("shortOutDay").innerHTML = weekday[d.getDay()];
  }
  else if (calWindow.document.calForm.mycaller.value == "Ret")
  {
	document.FSSform.shortRetDate.value=thedate;
	document.getElementById("shortRetDay").innerHTML = weekday[d.getDay()];
  }

  calWindow.close();
}

//---------------------------------------------------------------------------
// name: makeItemSelected
// parameters: object list
//             string item
// Selects the option in the list with value equal to item
//---------------------------------------------------------------------------
function makeItemSelected(list, item) 
{
  for (var i=0; i<list.length; i++)
  {
    if (list.options[i].value == item)
    {
      list.selectedIndex = i;
      return;
    }
  }
}

//----------------------------------------------------------------------------
// name: cleanUp
// parameters: none
// Closes the popup calendar window
//----------------------------------------------------------------------------
function cleanUp ()
{
  if (calWindow != null && !(calWindow.closed))
  {
    calWindow.close();
  }
}
