var cal_ID = 0;

function CALENDAR(year, month) {

//--------------------------------------------------------------------------------------------------------
// You should change these variables only if you want to translate them into your language:
//--------------------------------------------------------------------------------------------------------
  // weekdays: must start with Saturday because January 1st of year 1 was a Saturday
  this.weekdays = new Array('Za', 'Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr');

  // months: must start with January
  this.months = new Array('Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni',
                          'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December');


  this.error = new Array('Year must be 1 - 3999!', 'Month must be 1 - 12!');
  this.offset = 1;   
  this.weekNumbers = true;   

//--------------------------------------------------------------------------------------------------------
// Don't change from here:
//--------------------------------------------------------------------------------------------------------
  this.size = 0;
  this.mDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  if(year == null && month == null) {
    var obj = new Date();
    year = obj.getYear();
    if(year < 1900) year += 1900;
    month = obj.getMonth() + 1;
  }
  else if(year != null && month == null) month = 1;
  this.year = year;
  this.month = month;

//========================================================================================================
// Functions
//========================================================================================================
  this.set_styles = function() {

    cal_ID++;

    html = '<style>';
    html += ' div.kalender table.table tr td {';
    html += ' 	padding: 3px; text-align:center;  ';
    html += ' }';

    html += ' 	div.kalender table.table tr td.week {';
    html += ' 	background-color:#e1e9ed; border-left-width: 1px; border-left-style:solid; border-left-color:#999999';
    html += ' 	}';
		
    html += ' 	div.kalender table.table tr td.today {';
    html += ' 	font-weight:bold;background-color:#e1e9ed;  ';
    html += ' 	}';

    html += ' 	div.kalender table.table tr td.sunday {';
    html += ' 	color: #740808;  ';
    html += ' 	}';
    html += ' 	</style>';

    return html;

  }

  this.leap_year = function(year) {
    return (!(year % 4) && (year < 1582 || year % 100 || !(year % 400))) ? true : false;
  }

  this.get_weekday = function(year, days) {
    var a = days;
    if(year) a += (year - 1) * 365;
    for(var i = 1; i < year; i++) if(this.leap_year(i)) a++;
    if(year > 1582 || (year == 1582 && days >= 277)) a -= 10;
    if(a) a = (a - this.offset) % 7;
    else if(this.offset) a += 7 - this.offset;

    return a;
  }

  this.get_week = function(year, days) {
    var firstWDay = this.get_weekday(year, 0);
    return Math.floor((days + firstWDay) / 7) + (firstWDay <= 3);
  }

  this.table_head = function() {
    var html, ind, wDay, i;

    var cols = this.weekNumbers ? 8 : 7;
    html = '<tr class="legend">';

    for(i = 0; i < this.weekdays.length; i++) {
      ind = (i + this.offset) % 7;

      wDay = this.weekdays[ind];


      html += '<td>'+wDay+'</td>';
    }
    if(this.weekNumbers) html += '<td>&nbsp;</td>';
    html += '</tr>';

    return html;
  }

  this.create = function() {
    var obj, html, curYear, curMonth, curDay, start, stop, title, daycount,
        inThisMonth, weekNr, wdays, days, ind, cls, content, date, i;

    this.size = (this.hFontSize > this.dFontSize) ? this.hFontSize : this.dFontSize;
    if(this.wFontSize > this.size) this.size = this.wFontSize;

    obj = new Date();
    curYear = obj.getYear();
    if(curYear < 1900) curYear += 1900;
    curMonth = obj.getMonth() + 1;
    curDay = obj.getDate();

    if(this.year < 1 || this.year > 3999) html = '<b>' + this.error[0] + '</b>';
    else if(this.month < 1 || this.month > 12) html = '<b>' + this.error[1] + '</b>';
    else {
      if(this.leap_year(this.year)) this.mDays[1] = 29;
      for(i = days = 0; i < this.month - 1; i++) days += this.mDays[i];

      start = this.get_weekday(this.year, days);
      stop = this.mDays[this.month-1];


      html = this.set_styles();
      title = this.months[this.month-1] + ' ' + this.year;
      html += '<h4>'+title+'</h4>';

      html += '<div class="kalender"><table width="100%" border=0 cellspacing="0" cellpadding="0" class="table">';
      
      html += this.table_head();
      daycount = 1;

      if((this.year == curYear) && (this.month == curMonth)) inThisMonth = true;
      else inThisMonth = false;

      if(this.weekNumbers) weekNr = this.get_week(this.year, days);

      while(daycount <= stop) {
        html += '<tr>';

        for(i = wdays = 0; i <= 6; i++) {
          ind = (i + this.offset) % 7;
        
          if(ind == 1) cls = 'sunday';
          else cls = '';

          date = this.year + '-' + this.month + '-' + daycount;

          if((daycount == 1 && i < start) || daycount > stop) content = '&nbsp;';
          else {
            content = daycount;
            if(inThisMonth && daycount == curDay) cls = 'today';
            else if(this.year == 1582 && this.month == 10 && daycount == 4) daycount = 14;
            daycount++;
            wdays++;
          }
          html += '<td ';
		if (cls!='') html+=' class="'+cls+'"';
	  html +='>'+content+'</td>';
        }

        if(this.weekNumbers) {
          if(!weekNr) {
            if(this.year == 1) content = '&nbsp;';
            else if(this.year == 1583) content = 52;
            else content = this.get_week(this.year - 1, 365);
          }
          else if(this.month == 12 && weekNr >= 52 && wdays < 4) content = 1;
          else content = weekNr;

          html += '<td class="week">'+content+'</td>';
          weekNr++;
        }
        html += '</tr>';
      }
      html += '</table></div>';
    }
    return html;
  }
}

myCal = new CALENDAR();
document.write(myCal.create());
