//----------------------------------------------------------------------

function Month( iYYYY, iMM, sCanvasID, sFunction, CallerParams )
{
  var dToday = new Date();
  
  // iMM is treated as an offset from current month if iYYYY == 0
  if ( !iYYYY ) {
    if ( iMM < 0 ) iMM = 0;
    iYYYY = dToday.getFullYear() + parseInt( ( dToday.getMonth() + iMM ) / 12 );
    iMM = ( ( dToday.getMonth() + iMM ) % 12 ) + 1;
  }
  else if ( iMM < 1 )
    iMM = dToday.getMonth() + 1;
  else if ( iMM > 12 )
    iMM = 12;

  if ( iYYYY < dToday.getFullYear() )
    iYYYY = dToday.getFullYear();

  if ( iYYYY == dToday.getFullYear() && iMM < dToday.getMonth() + 1 )
    iMM = dToday.getMonth() + 1;

  // Define some properties...
  this.YYYY = iYYYY;
  this.MM = iMM - 1;  // 0-11 only
  this.cellFunction = ( sFunction && sFunction != "" )? sFunction : "void";
  this.auxCallerParams = ( CallerParams )? CallerParams : "";
  this.canvasID = "_" + sCanvasID;
  this.canvas = document.getElementById( sCanvasID );
  
  if ( this.canvas ) {
    this.canvas.innerHTML = "<div id=\'" + this.canvasID + "_MonthHeader\' class=\'MonthHeader\'></div>" +
                            "<div class=\'WeekDay Cell\'>S</div>" +
                            "<div class=\'WeekDay Cell\'>M</div>" +
                            "<div class=\'WeekDay Cell\'>T</div>" +
                            "<div class=\'WeekDay Cell\'>W</div>" +
                            "<div class=\'WeekDay Cell\'>T</div>" +
                            "<div class=\'WeekDay Cell\'>F</div>" +
                            "<div class=\'WeekDay Cell\'>S</div>" +
                            "<br/>";

    for ( var j=1; j<=6; j++) {
      for ( var i=1; i<=7; i++ ) {
        this.canvas.innerHTML += "<div id=\'" + this.canvasID + j + i + "\' class=\'Cell NA\'></div>";

        if ( i == 7 )
          this.canvas.innerHTML += "<br/>";
      }  // for ( var i=1; i<=7; i++ )
    }  // for ( var j=1; j<=6; j++)
  }  // if ( this.canvas )
}

//----------------------------------------------------------------------

Month.prototype.render = function()
{
  if ( this.canvas && this.MM >= 0 && this.MM <= 11 ) {
    var dToday = new Date();
    var dTemp = new Date( this.YYYY, this.MM, 1 );
    var bCurrMonth = ( this.YYYY == dToday.getFullYear() && this.MM == dToday.getMonth() )? true : false;
  
    var cell = document.getElementById( this.canvasID + "_MonthHeader" );
	
	var fullMonth=new Array(12);
	fullMonth[0]="January";
	fullMonth[1]="February";
	fullMonth[2]="March";
	fullMonth[3]="April";
	fullMonth[4]="May";
	fullMonth[5]="June";
	fullMonth[6]="July";
	fullMonth[7]="August";
	fullMonth[8]="September";
	fullMonth[9]="October";
	fullMonth[10]="November";
	fullMonth[11]="December";

    cell.innerHTML = fullMonth[this.MM] + " " + this.YYYY;

    // Generate day entries...
    var iDayCount = 1;
    var bFlag = false;
    
    for ( var j=1; j<=6; j++ ) {
      for ( var i=1; i<=7; i++ ) {
        cell = document.getElementById( this.canvasID + j + i );
        dTemp.setDate(iDayCount);

        if ( dTemp.getDate() == 1 ) {
          if ( iDayCount == 1 ) bFlag = true;
          else if ( bFlag ) bFlag = false;
        }

        if ( bFlag && dTemp.getDay() == i - 1 ) {
          if ( bCurrMonth && iDayCount < dToday.getDate() ) {
            cell.innerHTML = iDayCount;
            cell.className = "Cell NA";
          }
          else {
            cell.innerHTML = "<a href=\"javascript:" + this.cellFunction +
                             "(" + this.YYYY + "," + (this.MM+1) + "," + iDayCount + "," + "\'" + this.auxCallerParams + "\'" + ");\">" +
                             iDayCount + "</a>";
            cell.className = "Cell NoShade";
          }
          
          iDayCount++;
        }
        else {
          cell.innerHTML = "&nbsp;";
          cell.className = "Cell NA";
        }
      }  // for ( var i=1; i<=7; i++ )
    }  // for ( var j=1; j<=6; j++ )
  }  // if ( this.canvas )
}

//----------------------------------------------------------------------

Month.prototype.prior = function()
{
  this.MM--;

  if ( this.MM < 0 ) {
    this.YYYY--;
    this.MM = 11;
  }
  
  this.render(); 
}

//----------------------------------------------------------------------

Month.prototype.next = function()
{
  this.MM++;

  if ( this.MM > 11 ) {
    this.YYYY++;
    this.MM = 0;
  }
  
  this.render(); 
}

//----------------------------------------------------------------------
