var months = new Array(12);
months[0] = "January"; 
months[1] = "February"; 
months[2] = "March"; 
months[3] = "April"; 
months[4] = "May"; 
months[5] = "June"; 
months[6] = "July"; 
months[7] = "August"; 
months[8] = "September"; 
months[9] = "October"; 
months[10] = "November"; 
months[11] = "December"; 


function Calendar(calID) {

  this.table = calID;
  this.d = new Date();
  this.d2 = new Date();

  this.updateCalendar = function() {
  
    this.d2.setFullYear(this.d.getFullYear());
    this.d2.setMonth(this.d.getMonth());
    this.d2.setDate(1);
    
    var date = 1;        
    var t = document.getElementById(this.table);
    var tc = t.getElementsByTagName("td");
    tc[1].innerHTML = this.d.getFullYear();
    tc[4].innerHTML = months[this.d.getMonth()];

    var offset = 13;
    var firstDay = offset + parseInt(this.d2.getDay());
    var currentMonth = this.d2.getMonth();        

    for (var i = offset; i < offset + 42; i++) {
      if(i < firstDay || this.d2.getMonth() != currentMonth) {
        tc[i].getElementsByTagName("a")[0].style.visibility = "hidden";            
      }
      else {
        if(date == this.d.getDate()) {
          tc[i].getElementsByTagName("a")[0].className = "active_date";            
        }
        else {
          tc[i].getElementsByTagName("a")[0].className = "";                        
        }
        tc[i].getElementsByTagName("a")[0].innerHTML = date;
        tc[i].getElementsByTagName("a")[0].style.visibility = "visible";                        
        tc[i].getElementsByTagName("a")[0].href = "javascript: " + this.table + ".setDate(" + date + ")";            
        this.d2.setDate(++date);
      }
    }
    
    updateDateFields(calID);
  };


  this.browseYear = function(direction) {
    this.d.setFullYear(this.d.getFullYear() + direction);
    this.updateCalendar();
  };


  this.browseMonth = function(direction) {  
    this.d.setMonth(this.d.getMonth() + direction);         
    this.updateCalendar();
  };


  this.setDate = function(date) {        
    this.d.setDate(date);
    this.updateCalendar();
    document.getElementById(this.table).style.display = "none";
  };

  
  this.getDate = function() {
    return this.d;
  };
}


