/*
  Calendar and date related functions for the PMI Website.

  Author: Brendan O'Connor
  Date: December 20, 2003
*/

// create array of English month names
var theMonths = ["January","February","March","April","May","June","July","August",
"September","October","November","December"]

/************************
  DRAW CALENDAR CONTENTS
*************************/
// clear and re-populate table based on form's selections
function populateTable(form) 
{
  var yearControl = getObject("chooseYear")
  var strYearMonth = yearControl.options[yearControl.selectedIndex].value
  var strYearLength = strYearMonth.length
 	var strMonth = strYearMonth.substring(4, strYearLength)
  var theMonth = parseInt(strMonth)
	var theYear = parseInt(strYearMonth.substring(0,4))
	// initialize date-dependent variables
	var firstDay = getFirstDay(theYear, theMonth)
	var howMany = getMonthLen(theYear, theMonth)
	
	// fill in month/year in table header
	getObject("tableHeader").innerHTML = theMonths[theMonth] + 
	" " + theYear
	
	// initialize vars for table creation
	var dayCounter = 1
	var TBody = getObject("tableBody")
	// clear any existing rows
	while (TBody.rows.length > 0) 
  {
		TBody.deleteRow(0)
	}
	var newR, newC
	var done=false
  var calendarCellValue=""
  var delimiter = "&nbsp;&nbsp;"
  //var delimiter = "<br>"
  var calendarDate
	var cellHeight  //var cellHeight = (document.body.clientHeight - 300) / 5;
	if (firstDay + howMany > 28)
		{
			if (firstDay + howMany > 35)				{
					cellHeight = 50;				}			else				{					cellHeight = 60;				}
		}
	else
		{
					cellHeight = 68;		}
			while (!done) 
  {
		// create new row at end
		newR = TBody.insertRow(TBody.rows.length)
    newR.valign = "top"
		for (var i = 0; i < 7; i++) 
    {
			// create new cell at end of row
			newC = newR.insertCell(newR.cells.length)
			newC.className = "CalendarNonEventDay"
			newC.width = "100px"
      newC.height = cellHeight + "px"
      newC.align = "left"
			if (TBody.rows.length == 1 && i < firstDay) 
      {
				// no content for boxes before first day
				newC.innerHTML = ""	
        newC.className = "CalendarNonDay"
				continue
			}
			if (dayCounter == howMany) 
      {
				// no more rows after this one
				done = true
			}
			// plug in date (or empty for boxes after last day)
      if ( dayCounter <= howMany )
      {
        calendarDate = theYear+"/"+ monthDayFormat((theMonth+1))+"/"+monthDayFormat(dayCounter)
        var event = delimiter + getEventByDate(calendarDate)
        if (event != delimiter )
        {
          newC.className = "CalendarEventDay";
        }        calendarCellValue = "<p style='margin-left:5px; margin-right:5px;'><b>" + dayCounter++ + "</b>" + delimiter + event + "</p>";
      }
      else
      {
        calendarCellValue = "";
        newC.className = "CalendarNonDay";
      }      
      newC.innerHTML = calendarCellValue;
		}		
	}
}

/*******************
  INITIALIZATIONS
********************/
// create dynamic list of year choices
function fillYears(yearChooser,Delta)
{
	var today = new Date();
  var thisMonth = today.getMonth();
	var thisYear = today.getFullYear();
  var startYear = thisYear - 3;
  var option = null;  	for (i = startYear; i < startYear + 5; i++) 
  {
    for ( j = 0; j < 12; j++ )
    {
      optionValue = i + " " + theMonths[j];
      option = new Option(optionValue, i + " " + j);      
   		yearChooser.options[yearChooser.options.length] = option
      if ( i == thisYear )
      {
        if ( j == thisMonth ) //set it to the current month; may get reset to the delta if it exists
        {
          option.selected = true;        }
        if ( Delta > 0 && j == thisMonth + Delta )
        {
          option.selected = true;
        }
      }
    }
	}
}


