var MonthOfYear = ['','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var DaysInMonth = ['',31,28,31,30,31,30,31,31,30,31,30,31];

// text = id for the text input field
// calendar = id for the calendar div
function calendar(text, calendar)
{
  this.Txt = text;
  this.Ids = calendar;
  this.Cdate = new Date();
  this.monthM = this.Cdate.getMonth()+1;
  this.dayM = this.Cdate.getDate();
  this.yearM = this.Cdate.getFullYear();
  this.monthC = this.monthM;
  this.dayC = this.dayM;
  this.yearC = this.yearM;
    
  this.CalendarRedisplay = function(M,Y)
  {
    if ((M==0) && (Y==0))
    {
       M = this.monthM;
       Y  = this.yearM;
    }
    else
    {
      M = this.monthC+M;
      Y = this.yearC+Y;
      if (M < 1) { M = 12; Y--; }
      if (M > 12) { M = 1; Y++; }
    }

    this.monthC = M;
    this.yearC = Y;
    document.getElementById(this.Ids).innerHTML =  this.displayCalendar(M,Y);
  }

  this.displayCalendar = function(month, year)
  {
    month = parseInt(month);
    year = parseInt(year);
    var i = 0;
    var days = getDaysInMonth(month,year);
    var firstOfMonth = new Date (year, (month-1), 1);
    var startingPos = firstOfMonth.getDay();
    days += startingPos;
    
    var page = '<table width="100%" cellspacing=1 cellpadding=1><tr><td colspan="7">';
    page += '<button class="btn" onClick="CalendarRedisplay(0,-1)">|</button> ';
    page += '<button class="btn" onClick="CalendarRedisplay(-1,0)">&lt;</button> ';
    page += '<button class="btn" onClick="CalendarRedisplay(0,0)">';
    page += MonthOfYear[month]+' '+year+'</button>';
    page += ' <button class="btn" onClick="CalendarRedisplay(1,0)">&gt;</button>';
    page += ' <button class="btn" onClick="CalendarRedisplay(0,1)">|</button>';
    page += '</td></tr>';
    page += '<tr><td scope="col">Su</td><td scope="col">Mo</td><td scope="col">Tu</td><td scope="col">';
    page += 'We</td><td scope="col">Th</td><td scope="col">Fr</td><td scope="col">Sa</td></tr>';
    
    page += '<tr>';    
    for (i = 0; i < startingPos; i++)
    {
      if ( i%7 == 0 ) page += "</tr><tr>";
      page += '<td>&nbsp;</td>';
    }  
    
    for (i = startingPos; i < days; i++)
    {
      if ( i%7 == 0 ) page += "</tr><tr>";
      page += '<td';
      page += ' onclick="update('+(i-startingPos+1)+')">';
      page += (i-startingPos+1) + '</td>';
    }
    
    for (i=days; i<42; i++)
    {
      if ( i%7 == 0 ) page += "</tr><tr>";
      page += "<td>&nbsp;</td>";
    }
    
    page += '</tr></table>';
    return page;
  }

  this.display = function()
  {
	this.toggle();
  	document.getElementById(this.Ids).innerHTML = this.displayCalendar(this.monthC,this.yearC);
  }
  
  this.toggle = function()
  {
    var sel = document.getElementById(this.Ids);
    if (sel.style.display != 'block')
    {
      document.getElementById(this.Ids).style.display = 'block';
    }
    else
    {
      document.getElementById(this.Ids).style.display = 'none';
    }
  }
  
  this.update = function(info)
  {
    this.dayC = info;
    this.toggle();
    document.getElementById(this.Txt).value = this.yearC+'-'+pad(this.monthC)+'-'+pad(this.dayC);
  }
  
  this.display();
} 

function getDaysInMonth(month,year)
{
  var days = DaysInMonth[month];
  if ((month == 2) && isLeapYear(year))
  {
    days=29;
  } 
  
  return days;
}

function isLeapYear (Year)
{
  if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) { return (true); }
  else { return (false); }
}

function pad(value)
{
  return value=(value < 10)?'0'+value:value;
}

