var xmlHttp = false;
var sDate;

// ---------------------------------------------------------------------
function createCalendar()
{
   var sHTML;
   var i;

   // Tabellen-Code zusammenstellen
   sHTML = '<div id="calendar">'
   sHTML += ' <div id="calgoprev">&nbsp;</div>'
   sHTML += ' <div id="caltitle">&nbsp;</div>'
   sHTML += ' <div id="calgonext">&nbsp;</div>'
   sHTML += ' <div>'
   sHTML += '  <div class="wd">Mo</div>'
   sHTML += '  <div class="wd">Di</div>'
   sHTML += '  <div class="wd">Mi</div>'
   sHTML += '  <div class="wd">Do</div>'
   sHTML += '  <div class="wd">Fr</div>'
   sHTML += '  <div class="wd">Sa</div>'
   sHTML += '  <div class="wd">So</div>'

   // Tabelle mit 6 Zeilen und 7 Spalten erstellen
   for(i = 1; i <= 42; i++) {    
      sHTML += '<div class="d" id="calCell' + i + '">&nbsp;</div>'
   }
		
   sHTML += ' </div>'
   sHTML += '</div>'

    // Kalender ausgeben
    document.write(sHTML);
}

// ---------------------------------------------------------------------
function getCalendar(iMonth, iYear)
{ 
   // aktuelles Datum in Variable speichern
   sDate = new Date(); 

   // wenn Parameter leer, dann aktuellen Monat verwenden
   if (iMonth == null) {
   iMonth = sDate.getMonth();
   iYear = sDate.getFullYear(); }
   
   // Mozilla und Co.
   if (window.XMLHttpRequest) {
      xmlHttp = new XMLHttpRequest(); } 
   // IE
   else if (window.ActiveXObject) {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } 
   else {
      // Falls nicht unterstützt
      xmlHttp = false; }

   // URL zusammenstellen und Inhalt über GET asynchron holen
   var sXMLUrl = 'http://meinserver/contentcount.asp?m=' + (iMonth + 1) + '&y=' + iYear;
   xmlHttp.open("GET", sXMLUrl, true);
   xmlHttp.onreadystatechange = function() {

      // Objekt meldet "loaded"
      if (xmlHttp.readyState == 4) {
         // Objekt meldet "OK"
         if (xmlHttp.status == 200) {
            // Funktion zum Füllen der Tabelle ausführen
            fillCalendar(iMonth, iYear); }
      }
   }

   xmlHttp.send(null); 
}

// ---------------------------------------------------------------------
function fillCalendar(iMonth, iYear)
{
   // Monats-Array bilden
   var aMonths = new Array(
      'Januar', 'Februar', 'März', 'April', 
      'Mai', 'Juni', 'Juli', 'August', 
      'September', 'Oktober', 'November', 'Dezember');

   // Monate ermitteln
   var iThisMonth = new Date(iYear, iMonth, 1);
   var iPrevMonth = new Date(iYear, iMonth - 1, 1);
   var iNextMonth = new Date(iYear, iMonth + 1, 1);

   // Erster Wochentag und Anzahl Tage/Monat ermitteln
   var iFirstWeekday = iThisMonth.getDay();
   if (iFirstWeekday == 0) iFirstWeekday = 7;
   var iDaysInMonth = Math.floor((iNextMonth.getTime() 
      - iThisMonth.getTime()) / (1000 * 60 * 60 * 24));
   
   // Link zu vorherigem Monat
   var sPrev = '<a href="javascript: getCalendar(' 
      + iPrevMonth.getMonth() + ',' 
      + iPrevMonth.getFullYear() + ')">&lt;</a>';
   hItem = document.getElementById("calprev")
   hItem.innerHTML = sPrev;

   // Überschrift aus Monats-Array
   hItem = document.getElementById("caltitle")
   hItem.innerHTML = aMonths[iMonth] + ' ' + iYear;

   // Link zu nächstem Monat
   var sNext = '<a href="javascript: getCalendar(' 
      + iNextMonth.getMonth() + ',' 
      + iNextMonth.getFullYear() + ')">&gt;</a>';
   hItem = document.getElementById("calnext")
   hItem.innerHTML = sNext;
   
   // Leere Tage am Anfang auffüllen
   for(iCellId=1; iCellId < iFirstWeekday; iCellId++) { 
      hItem = document.getElementById("calCell" + iCellId);
      hItem.innerHTML = '&nbsp;'; }
   
   // XML-Objekt initialisieren
   var xmlDok = xmlHttp.responseXML;

   // Variable initialieren
   var iCellDay=1;

  // Schleife über alle Tage des Monats
  for(iCellId = iFirstWeekday; iCellDay <= iDaysInMonth; iCellId++) {

      // Anzahl Beiträge aus XML ermitteln
      iItemCount = xmlDok.getElementsByTagName("day")
         [iCellDay - 1].firstChild.nodeValue;

      // Zellinhalt bestimmen (Link auf Beiträge oder nicht)
      if (iItemCount == 0) {
         sDayHTML = iCellDay; }
      else {
         var sDayString = iCellDay + "." + (iMonth + 1) + "." + iYear;
         sDayHTML = '<a href="/showdatum.asp?day=' 
            + sDayString + '">' + iCellDay + '</a>';
      }

      // Zellinhalt zuweisen
      hItem = document.getElementById("calCell" + iCellId)
      hItem.innerHTML = sDayHTML; 

      // Zelle mit CSS-Klasse versehen (anders, wenn Tag=heute)
      var sDateString = iCellDay + '.' + iMonth + '.' + iYear
      var sDateToday = 
         sDate.getDate() + '.' + 
         sDate.getMonth() + '.' +
         sDate.getFullYear();
			
      if (sDateString == sDateToday) { 
         hItem.className = "d td"; }
      else {
         hItem.className = "d"; }

      iCellDay++;
   }

   // Leere Tage am Ende auffüllen
   for(iCellId; iCellId <= 42; iCellId++) {
   hItem = document.getElementById("calCell" + iCellId)
   hItem.innerHTML = '&nbsp;'; }
}
   