/**********************************************************************
HTML CALENDAR CONTROL
copyright (c) 2001 The Ashley Group, Ltd.
ALL RIGHTS RESERVED
**********************************************************************

Created By:     Patrick Milliken
Creation Date:  5/15/2001

Purpose:        The Calendar Control is a pop-up calendar that writes
        a user-selected date back to a specified text box on
        the calling form. The calling form opens a pop-up
        window, then this code dynamically writes the calendar
        HTML to the window. No separate Calendar.HTM exists
        for this control.

Modified By:    Patrick Milliken
Last Mod Date:  6/11/2001

**********************************************************************/

/*--------------------------------------------------------------
 Declare and initialize global variables
----------------------------------------------------------------*/
var ActiveDate
var theMonth
var theDate
var theYear
var theFirst
var firstDay

/*--------------------------------------------------------------
 Build the HTML for the calendar based on the date argument then
 write the HTML to the pop-up window.
----------------------------------------------------------------*/
function doCalendar(ActiveDate)
  {
  Today = new Date()
  theDate = ActiveDate.getDate()
  theMonth = ActiveDate.getMonth() + 1
  theYear = ActiveDate.getFullYear()
  FOM = new Date(theMonth + '/1/' + theYear)
  firstDay = FOM.getDay()
  lastDay = getLastDay(theMonth, theYear)
  ColWidth = 35

  // Build the Header.
  HTML = '<html><head><title>Calendar</title>'
    + '<style type="text/css">a {text-decoration:none;} a:hover {color:#CC0000; text-decoration:underline;}'
    + 'BODY {cursor: default;}'
    + '.BodyCell { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-style: normal; font-weight: normal; }'
    + '.HeaderCell { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-style: normal; font-weight: bold; color: #FFFFFF;}'
    + '.Today {font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;font-weight: bold;color: #000066;}'
    + '.Address{font-family: Arial, Helvetica, sans-serif;font-size: 10px;font-style: normal;font-weight: normal;color: #FFFFFF;}'
    + '.UserDate{font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;font-weight: bold;color: #FF0000;}'
    + '</style></head>'
    + '<body bgcolor="#000000" text="#000000" link="#000066" vlink="#000066" '
    + 'alink="#CC0000" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">'
    + '<form action="calendar.htm" method="post" name="frmForm">'
    + '<table border="0" cellspacing="1" cellpadding="1" align="center">'
    + '<tr>'
    + '<td colspan="7" bgcolor="#003399">'
    + '<table width="100%" border="0" cellspacing="0" cellpadding="0" height="16">'
    + '<tr>'
    + '<td align="left" valign="middle" width="25%" nowrap>'
    + '<a href="javascript:opener.changeYear(\'' + ActiveDate + '\', -1)" title="Go To Last Year"><span class="HeaderCell">&lt;&lt;</span></a><span class="HeaderCell">&nbsp;&nbsp;</span>'
    + '<a href="javascript:opener.changeMonth(\'' + ActiveDate + '\', -1)" title="Go To Last Month"><span class="HeaderCell">&lt;</span></a></td>'
    + '<td align="center" valign="middle">'
    + '<span class="HeaderCell">' + returnMonth(theMonth) + ' ' + theYear + '</span></td>'
    + '<td align="right" valign="middle" width="25%" nowrap>'
    + '<a href="javascript:opener.doToday()" title="Go To Today"><span class="HeaderCell">^</span></a><span class="HeaderCell">&nbsp;&nbsp;</span>'
    + '<a href="javascript:opener.changeMonth(\'' + ActiveDate + '\', 1)" title="Go To Next Month"><span class="HeaderCell">&gt;</span></a><span class="HeaderCell">&nbsp;&nbsp;</span>'
    + '<a href="javascript:opener.changeYear(\'' + ActiveDate + '\', 1)" title="Go To Next Year"><span class="HeaderCell">&gt;&gt;</span></a></td>'
        + '</tr>'
    + '</table>'
    + '</td>'
    + '</tr>'
    + '<tr>'
    + '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>Di</b></span></th>'
    + '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>Lu</b></font></th>'
    + '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>Ma</b></span></th>'
    + '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>Me</b></span></th>'
    + '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>Je</b></span></th>'
    + '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>Ve</b></span></th>'
    + '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>Sa</b></span></th>'
    + '</tr>'

  // Build the Body.

  HTML += '<tr align="center" valign="middle">'

  for (i = 1; i <= 6; i++)
    {
    for (j = 1; j <= 7; j++)
      {
      theDay = (j + ((i - 1) * 7) - firstDay)
      if (theDay < 1 && firstDay + lastDay > 42)
        {
        theDay = (42 - firstDay) + j
        }
      if (theDay < 1 || theDay > lastDay)
        HTML += '<td width="' + ColWidth + '" height="22" bgcolor="#CCCCCC">&nbsp;</td>'
      else
        {
        if (theMonth + "/" + theDay + "/" + theYear == getDate(UserDate) && getDate(UserDate) != getDate(Today))
          HTML += '<td width="' + ColWidth + '" height="22" bgcolor="#FFFFFF"><span class="UserDate">' + theDay + '</span></td>'
        else
          {
          HTML += '<td width="' + ColWidth + '" height="22" width="27" bgcolor="#FFFFFF"><a href="javascript:opener.ReturnValue(\'' + theDay + "/" + theMonth + "/" + theYear + '\')">'
          if (theMonth == Today.getMonth() + 1 && theDay == Today.getDate() && theYear == Today.getFullYear())
            HTML += '<span class="Today">' + theDay + '</span></a></td>'
          else
            HTML += '<span class="BodyCell">' + theDay + '</span></a></td>'
          }
        }
      }
    if (i != 6)
      HTML += '</tr><tr align="center" valign="middle">'
    }

  // Build the Footer.

  HTML += '</tr></table>'
    + '<input type="hidden" name="ActiveDate" value="' + ActiveDate + '">'
    + '</form></body></html>'

  // Write the complete HTML to the pop-up window.

  nWin.document.open("text/html")
  nWin.document.write(HTML)
  nWin.document.close()
  }

/*--------------------------------------------------------------
 Return a date formatted as MM/DD/YYYY.
----------------------------------------------------------------*/
function getDate(DateVal)
  {
  return((DateVal.getMonth() + 1) + "/" + DateVal.getDate() + "/" + DateVal.getFullYear())
  }

/*--------------------------------------------------------------
 Handle a click on the last or next month links.
----------------------------------------------------------------*/
function changeMonth(ActiveDate, offset)
  {
  ActiveDate = new Date(ActiveDate)
  theMonth = ActiveDate.getMonth() + (1 + offset)
  theYear = ActiveDate.getFullYear()
  if (theMonth < 1)
    {
    theMonth = 12
    theYear--
    }
  else if (theMonth > 12)
    {
    theMonth = 1
    theYear++
    }
  ActiveDate = new Date(theMonth + '/' + ActiveDate.getDate() + '/' + theYear)
  doCalendar(ActiveDate)
  }

/*--------------------------------------------------------------
 Handle a click on the last or next year links.
----------------------------------------------------------------*/
function changeYear(ActiveDate, offset)
  {
  ActiveDate = new Date(ActiveDate)
  theMonth = ActiveDate.getMonth() + 1
  theYear = ActiveDate.getFullYear() + (1 * offset)
  ActiveDate = new Date(theMonth + '/' + ActiveDate.getDate() + '/' + theYear)
  doCalendar(ActiveDate)
  }

/*--------------------------------------------------------------
 Handle a click on the Go To Today link.
----------------------------------------------------------------*/
function doToday()
  {
  ActiveDate = new Date()
  doCalendar(ActiveDate)
  }

/*--------------------------------------------------------------
 Return the last day of the month for the specified month and
 year (note the leap year calculation).
----------------------------------------------------------------*/
function getLastDay(theMonth, theYear)
  {
  if (theMonth != 2)
    {
    if (theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11)
      return(30)
    else
      return(31)
    }
  else
    {
    if (theYear % 4 > 0)
      return(28)
    else if (theYear % 100 > 0)
      return(29)
    else if (theYear % 4 == 0)
      return(29)
    else
      return(28)
    }
  }

/*--------------------------------------------------------------
 Return the name of the month based on the month's numeric
 value.
----------------------------------------------------------------*/
function returnMonth(theMonth)
  {
  if (theMonth == 1)
    return('Janvier')
  else if (theMonth == 2)
    return('Fevrier')
  else if (theMonth == 3)
    return('Mars')
  else if (theMonth == 4)
    return('Avril')
  else if (theMonth == 5)
    return('Mai')
  else if (theMonth == 6)
    return('Juin')
  else if (theMonth == 7)
    return('Juillet')
  else if (theMonth == 8)
    return('Aout')
  else if (theMonth == 9)
    return('Septembre')
  else if (theMonth == 10)
    return('Octobre')
  else if (theMonth == 11)
    return('Novembre')
  else if (theMonth == 12)
    return('Decembre')
  }

/*--------------------------------------------------------------
 When a user clicks on a day of the month, write the date value
 back to the specified text box on the calling form.
----------------------------------------------------------------*/
function ReturnValue(theDate)
   {
  document.forms[ReturnFn].elements[ReturnEl].value = theDate
  CloseNWin()
   }


var nWin = ""

function showCalendar(elName, formName)
        {
        Height = 190
        Width = 245
        Top = 100
        Left = 100
        if (nWin == "")
                {
                nWin = window.open("", "Calendar", "height=" + Height + ",width=" + Width
                        + ",top=" + Top + ",left=" + Left + ",status=no,toolbar=no,menubar=no,"
                        + "location=no,resizable=no,scrollbars=no")
                nWin.opener = window
                }
        else
                {
                CloseNWin()
                showCalendar(elName, formName)
                }

        ReturnEl = elName
        ReturnFn = formName
        el = document.forms[formName].elements[elName]
        if (el.value.length==10)
          UserDate = new Date(el.value.substr(6,4),el.value.substr(3,2)-1,el.value.substr(0,2))
        else
          UserDate = new Date()
        datNow = new Date(UserDate)
        doCalendar(datNow)
        }

function CloseNWin()
        {
        if (nWin != "")
                {
                nWin.close()
                nWin = ""
                }
        }


