//The field associated with the calendar.
var gsFieldName = null

//The date that is currently in the field.
var gsFieldValue = null

//The days that are considered weekends.
var weekend = [0,6];

//Define the calendar month constants.
var Months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var DaysOfMonth     = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var DaysOfMonthLeap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];



//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function ShowCalendar( asFieldName, aoCalendarImage ) 
{

  //1.  Save the name of the field.  We'll  need this later when we wish to return the value.

  gsFieldName = asFieldName;


  //2.  Get the value of the date form the form and save it (global variable).  This function is in the JavaScript from the page that loaded the calendar because it will know what how to get the value.

  var lsFieldValue = GetFieldValue( asFieldName )

  if ( lsFieldValue != "" )
    gsFieldValue = lsFieldValue;
  else
    gsFieldValue = null;


  //3.  Hook the Calendar DIV.

  var elDivCalendar = document.getElementById( "divCalendar" );

  if ( ! elDivCalendar )
    return alert( "Error: could not locate the divCalendar DIV tag." );
 

  //4.  Move the Calendar DIV into place.

  var elImageCalendar = document.getElementById( "Calendar~" + asFieldName );

  if ( ! elImageCalendar )
    return alert( "Error: could not locate the Calendar Image tag." );

  var liLeft = 0;
  var liTop = 0;

  var element = elImageCalendar;

  while ( element != document.body )
  {
    liLeft += element.offsetLeft;
    liTop += element.offsetTop;

    element = element.offsetParent;
  }

  elDivCalendar.style.left = liLeft + 26;
  elDivCalendar.style.top  = liTop;



/*

  var loArrow = eval( 'document.arrow' + ciColNum );
  loArrow.src = "/sbs/images/downarrow.gif";

  var loSelectedDiv = eval( 'document.all.col' + ciColNum );

  var foundX = 0;
  var foundY = 0;
    
  var element = loSelectedDiv;
	  
  while (element != document.body) 
  {
    foundX += element.offsetLeft;
    foundY += element.offsetTop;

    element = element.offsetParent;
  }

  foundX += document.body.clientLeft -1;

  divDetail.style.top = foundY + 15;
  divDetail.style.left = foundX ;
*/





  //5.  Build the Calendar HTML.

  BuildCalendar( gsFieldValue );


  //6.  Show the Calendar DIV.

  elDivCalendar.style.visibility = "visible";

}



//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function SelectDate( asDateSelected )
{

  //1.  Hide the Calendar DIV.

  var elDivCalendar = document.getElementById( "divCalendar" );

  if ( ! elDivCalendar )
    return alert( "Error: could not locate the divCalendar DIV tag." );

  elDivCalendar.innerHTML = "";
  elDivCalendar.style.visibility = "hidden";


  //2.  Return the date selected.  This function is in the JavaScript from the page that loaded the calendar because it will know what to do with the value, 

  SetFieldValue( gsFieldName, asDateSelected );

}



//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function CloseCalendar()
{

  //1.  Hide the Calendar DIV.

  var elDivCalendar = document.getElementById( "divCalendar" );

  if ( ! elDivCalendar )
    return alert( "Error: could not locate the divCalendar DIV tag." );

  elDivCalendar.innerHTML = "";
  elDivCalendar.style.visibility = "hidden";

}



//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function BuildCalendar( asShowDate )
{

  //1.  Determine which month to display.

  var ldShowDate = new Date();

  if ( asShowDate != null && asShowDate != "" )
    ldShowDate = new Date( asShowDate );


  //2.  Find the values for LastMonth, LastYear, NextMonth and NextYear (used for the month/year navigation).

  var liShowMonth  = ldShowDate.getMonth() + 1;                      //getMonth is zero based.
  var liNextMonth  = liShowMonth + 1;
  var liLastMonth  = liShowMonth - 1;

  var liShowYear   = ldShowDate.getYear();
  var liNextYear   = liShowYear + 1;
  var liLastYear   = liShowYear - 1;

  var lsShowLastYear  = liShowMonth + "/1/" + liLastYear;
  var lsShowLastMonth = liLastMonth + "/1/" + liShowYear;

  var lsShowNextMonth = liNextMonth + "/1/" + liShowYear;
  var lsShowNextYear  = liShowMonth + "/1/" + liNextYear;


  //3.  Build the HTML for the Calender DIV.

  var lsCalendarHtml = "";

  lsCalendarHtml += "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"bgWhite\">\n" +
                   
                    //1a.  Build the Header.

                    "  <tr>\n" +
                    "    <td width=\"100%\">\n" +
                    "      <table width=\"100%\" class=\"bgDark\">\n" +
                    "        <tr>\n" +
                    "          <td align=\"left\"><font class=\"BodyBoldWhiteFont\">" + Months[liShowMonth - 1] + " " + liShowYear + "</font></td>\n" +
                    "          <td align=\"right\"><a class=\"LineNoAnchorWhite\" href=\"JavaScript:CloseCalendar();\">" + "Close" + "</a></td>\n" +
                    "        </tr>\n" +
                    "      </table>\n" +
                    "    </td>\n" +
                    "  </tr>\n" +

                    //1b.  Build the Month/Year Navigation.

                    "  <tr>\n" +
                    "    <td width=\"100%\">\n" +
                    "      <table cellpadding=\"0\" cellspacing=\"0\" border=\"1\" class=\"bgLight\">\n" +
                    "        <tr>\n" +
                    "          <td><font class=\"ViewBodyFont\"> &nbsp; <a class=\"NoLineAnchor\" href=\"JavaScript:BuildCalendar( '" + lsShowLastYear  + "' );\" ><< Year</a> &nbsp; </font></td>\n" +
                    "          <td><font class=\"ViewBodyFont\"> &nbsp; <a class=\"NoLineAnchor\" href=\"JavaScript:BuildCalendar( '" + lsShowLastMonth + "' );\" ><< Month</a> &nbsp; </font></td>\n" +
                    "          <td><font class=\"ViewBodyFont\"> &nbsp; <a class=\"NoLineAnchor\" href=\"JavaScript:BuildCalendar( '" + lsShowNextMonth + "' );\" > Month >></a> &nbsp; </font></td>\n" +
                    "          <td><font class=\"ViewBodyFont\"> &nbsp; <a class=\"NoLineAnchor\" href=\"JavaScript:BuildCalendar( '" + lsShowNextYear  + "' );\" > Year >></a> &nbsp; </font></td>\n" +
                    "        </tr>\n" +
                    "      </table>\n" +
                    "    </td>\n" +
                    "  </tr>\n" +

                    //1c.  Build the "Show" Month.

                    "  <tr>\n" +
                    "    <td width=\"100%\">\n" +
                           BuildMonth( ldShowDate ) +
                    "    </td>\n" +
                    "  </tr>\n" +

                    "</table>\n";


  //3.  Hook the Calendar DIV.

  var elDivCalendar = document.getElementById( "divCalendar" );

  if ( ! elDivCalendar )
    return alert( "Error: could not locate the divCalendar HTML tag." );


  //4.  Write the Calendar HTML to the InnerHTML of the Calendar DIV.

  elDivCalendar.innerHTML = lsCalendarHtml;

}



//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function BuildMonth( adShowDate )
{


  //1  Set the date object to the first day of the month.

  adShowDate.setDate(1);


  //2.  Find the day of the week for the first day of the month.

  var liFirstDay = adShowDate.getDay();


  //3.  Find the last date of the month (taking into consideration leap year).

  var liLastDate = 0;

  if ( ( adShowDate.getYear() % 4 ) == 0 ) 
  {
    if ( ( adShowDate.getYear() % 100 ) == 0 && ( adShowDate.getYear() % 400 ) != 0 )
      liLastDate = DaysOfMonth[ adShowDate.getMonth() ];
    else
      liLastDate = DaysOfMonthLeap[ adShowDate.getMonth() ];
  } 
  else
  {
    liLastDate = DaysOfMonth[ adShowDate.getMonth() ];
  }



  //4.  Build the Calendar heading for the days of the month.

  var lsMonthHTML = "";

  lsMonthHTML += "      <table cellpadding=\"0\" cellspacing=\"0\" border=\"1\" width=\"100%\">\n" +
                 "        <tr>\n" +
                 "          <td width=\"14%\" class=\"bgLight\"><font class=\"ViewBodyFont\">Sun</font></td>\n" +
                 "          <td width=\"14%\"><font class=\"ViewBodyFont\">Mon</font></td>\n" +
                 "          <td width=\"14%\"><font class=\"ViewBodyFont\">Tue</font></td>\n" +
                 "          <td width=\"14%\"><font class=\"ViewBodyFont\">Wed</font></td>\n" +
                 "          <td width=\"14%\"><font class=\"ViewBodyFont\">Thu</font></td>\n" +
                 "          <td width=\"14%\"><font class=\"ViewBodyFont\">Fri</font></td>\n" +
                 "          <td width=\"14%\" class=\"bgLight\"><font class=\"ViewBodyFont\">Sat</font></td>\n" +
                 "        </tr>\n";


  //5.  Write the first week of the month.

  lsMonthHTML += "        <tr>\n";

  //5a.  Write blank cells before the 1st day of the month.

  for ( var i=0; i<liFirstDay; i++ ) 
  {
    if ( ( i == 0 ) || ( i == 6 ) )  
      lsMonthHTML += "<td class=\"bgLight\"><font class=\"ViewBodyFont\">&nbsp;</font></td>";
    else
      lsMonthHTML += "<td><font class=\"ViewBodyFont\">&nbsp;</font></td>";
  }

  //5b.  Write the remaining days of the first week.

  var liCurrentDate = 1;

  for ( var i=liFirstDay; i<7; i++ ) 
  {
    if ( ( i == 0 ) || ( i == 6 ) )
      lsMonthHTML += "<td class=\"bgLight\"><font class=\"ViewBodyFont\"><a class=\"NoLineAnchor\" href=\"JavaScript:SelectDate('" + FormatDate( adShowDate.getMonth() + 1, liCurrentDate, adShowDate.getYear() ) + "');\">" + liCurrentDate + "</a></font></td>";
    else
      lsMonthHTML += "<td><font class=\"ViewBodyFont\"><a class=\"NoLineAnchor\" href=\"JavaScript:SelectDate('" + FormatDate( adShowDate.getMonth() + 1, liCurrentDate, adShowDate.getYear() ) + "');\">" + liCurrentDate + "</a></font></td>";

    liCurrentDate++;
  }

  lsMonthHTML += "        </tr>\n";



  //6.  Write the remaining weeks.

  var lbDone = false;			//Used to break out of the outer loop after we break out of the inner loop.
  var liLastDay = 0;			//When done displaying the month, this will contain the day of the week of the last day.

  for ( var i=2; i<7; i++ )
  {

    lsMonthHTML += "        <tr>\n";

    for ( var j=0; j<7; j++ ) 
    {

    if ( ( j == 0 ) || ( j == 6 ) )
      lsMonthHTML += "<td class=\"bgLight\"><font class=\"ViewBodyFont\"><a class=\"NoLineAnchor\" href=\"JavaScript:SelectDate('" + FormatDate( adShowDate.getMonth() + 1, liCurrentDate, adShowDate.getYear() ) + "');\">" + liCurrentDate + "</a></font></td>";
    else
      lsMonthHTML += "<td><font class=\"ViewBodyFont\"><a class=\"NoLineAnchor\" href=\"JavaScript:SelectDate('" + FormatDate( adShowDate.getMonth() + 1, liCurrentDate, adShowDate.getYear() ) + "');\">" + liCurrentDate + "</a></font></td>";

      liCurrentDate++;

      if ( liCurrentDate > liLastDate )
      {
        liLastDay = j;
        lbDone = true;
        break;
      }
    }

    if ( lbDone )
      break;

    lsMonthHTML += "        </tr>\n";

  }

  //4d.  Write blank cells after the last day of the month.

  for ( var i=1; i<(7-liLastDay); i++ )
  {
    if (  i == ( 7 - liLastDay - 1 ) )
      lsMonthHTML += "<td class=\"bgLight\"><font class=\"ViewBodyFont\">&nbsp;</font></td>";
    else
      lsMonthHTML += "<td><font class=\"ViewBodyFont\">&nbsp;</font></td>";
  }

  lsMonthHTML += "        </tr>\n";
  lsMonthHTML += "      </table>\n";


  return lsMonthHTML;

}



//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function FormatDate( aiMonth, aiDate, aiYear )
{

  var lsMonth = aiMonth;

  if ( aiMonth.toString().length < 2 )
    lsMonth = "0" + aiMonth 

  var lsDate = aiDate;

  if ( aiDate.toString().length < 2 )
    lsDate = "0" + aiDate

  return lsMonth + "/" +  lsDate + "/" + aiYear;

}












