function CShowCalendar(Url, ItemsPerPage, LangKey)
{  	
	this.DateNow 	= new Date();
	this.Day      = this.DateNow.getDate();
	this.Month    = this.DateNow.getMonth();
	this.Year 	  = this.DateNow.getFullYear();
  
	this.bFirstCallback = true;
  
  this.bGetDay = false;
  this.bGetMonth = false;

  var _this = "";
                      
  this.aActiveDays        = new Array();
	this.aActiveDaysInformation = new Array();
  this.InformationDay     = 0;
  this.CurrentDaysInMonth = 0;
  this.Offset             = 0;
  this.bCalendarReady     = true;                 
  this.bEventsReady       = false;
  this.bUpdate            = false;
  this.EventUpdateUrl     = "";
  this.InformationUpdateUrl = "";
  this.Url                = Url;
  this.ItemsPerPage       = ItemsPerPage; 
	this.LangKey						= LangKey;
	this.CalendarTitle			= "";
	
	this.Category           = "";

	switch(this.LangKey) {
		
		case "en" :
		{
			
			//Not implemented
			this.DayLabels   = ['s', 'm', 't', 'w', 't', 'f', 's'];
			this.MonthLabels = ['January', 'February', 'March', 'April',
	      		          		'May', 'June', 'July', 'August', 'September',
	          			    		'October', 'November', 'December'];
	   	
	   	this.NextTxt = "next";
	    this.PrevTxt = "previous";
	    this.CalendarTitle = "Calendar";
		}
		break;
			
		default : 
		{
			this.DayLabels   = ['z', 'm', 'd', 'w', 'd', 'v', 'z'];
			this.MonthLabels = ['januari', 'februari', 'maart', 'april',
	      		          		'mei', 'juni', 'juli', 'augustus', 'september',
	          			    		'oktober', 'november', 'december'];
	          			    		
	    this.NextTxt = "volgende";
	    this.PrevTxt = "vorige";
	    this.CalendarTitle = "Kalender";
		}
		break;
	}

  this.currentPage = 0 ;
  this.Pages = 0;

  if (window.XMLHttpRequest)  {
    this.CallBackObjEvents = new XMLHttpRequest();
    this.CallBackObjCalendar = new XMLHttpRequest();
    this.bCalendarReady = true;
    this.bEventsReady = true;
  } else {
    if (window.ActiveXObject) {
      try {
        this.CallBackObjEvents = new ActiveXObject("Msxml2.XMLHTTP");
        this.CallBackObjCalendar = new ActiveXObject("Msxml2.XMLHTTP");   
        this.bCalendarReady = true;
        this.bEventsReady = true;
      }
      catch (e) {
        try {
          this.CallBackObjEvents = new ActiveXObject("Microsoft.XMLHTTP");
          this.CallBackObjCalendar = new ActiveXObject("Microsoft.XMLHTTP");   
          this.bCalendarReady = true;
          this.bEventsReady = true;
        }
        catch (e) {
          return;
        }
      }
    }
  }
}

CShowCalendar.prototype.GetCallBackEvents = function(Day)
{                          
  this.InformationDay = Day;

	var UrlSuffix = "year=" + this.Year + "&month=" + this.Month + "&day=" + this.Day + "&langkey=" + this.LangKey;
  if(this.bGetDay) {
    UrlSuffix += "&bDay=1";
  } else {
    UrlSuffix += "&bDay=0"; 
  } 
  if(this.bGetMonth) { 
    UrlSuffix += "&bMonth=1"; 
  } else {
    UrlSuffix += "&bMonth=0";
  }
  this.bGetMonth = false;
  this.bGetDay = false;
  
  UrlSuffix = UrlSuffix + "&bEvent=1";

  var _this = this; // set the var so we can scope the callback
         
  this.CallBackObjEvents.abort();
  this.CallBackObjEvents.open('POST', this.Url, true);
  this.CallBackObjEvents.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  this.CallBackObjEvents.setRequestHeader("Content-length", UrlSuffix.length);
  this.CallBackObjEvents.setRequestHeader("Connection", "close");
                                                     
  this.CallBackObjEvents.onreadystatechange = function() {_this.setInformationEvents()};
  
  this.CallBackObjEvents.send(UrlSuffix);
}

CShowCalendar.prototype.GetCallBackInformation = function(Day)
{                            
  this.InformationDay = Day;
   
  var UrlSuffix = "year=" + this.Year + "&month=" + this.Month + "&day=" + this.Day + "&langkey=" + this.LangKey;
  UrlSuffix += "&bDay=0"; 
  UrlSuffix += "&bMonth=1";
  
  UrlSuffix = UrlSuffix + "&bInfo=1";

  var _this = this; // set the var so we can scope the callback

  this.CallBackObjCalendar.abort();  
  this.CallBackObjCalendar.open('POST', this.Url, true);
  this.CallBackObjCalendar.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  this.CallBackObjCalendar.setRequestHeader("Content-length", UrlSuffix.length);
  this.CallBackObjCalendar.setRequestHeader("Connection", "close");
                                                     
  this.CallBackObjCalendar.onreadystatechange = function() {_this.setInformationCalendar()};
  
  this.CallBackObjCalendar.send(UrlSuffix);
}

CShowCalendar.prototype.Start = function()
{ 
  if((getCookie("year") > 0) && (getCookie("month") >= 0)) {
    this.bGetMonth = true;   
    this.bGetDay = false;  
    
    this.Year  = getCookie("year");
    this.Month = getCookie("month");
    this.Day   = getCookie("day");  

    if(this.Day < 0) {
      this.bGetMonth = true;
      this.bGetDay = false;      
    } else {
      this.bGetDay = true;   
      this.bGetMonth = false;
    }
  } else {
    this.Day = -1;
  }      
  this.Display();
}

CShowCalendar.prototype.Display = function()
{      
	this.getInformation(this.InformationDay);  
	   	  
  this.CurrentDaysInMonth = this.getDaysInMonth(this.Month, this.Year);  
  var StartingDay         = this.getFirstDayOfMonth(this.Month, this.Year);
  var MonthName           = this.MonthLabels[this.Month];
  
	var MonthsGoBack = this.DateNow.getMonth() - 1;
  var YearGoBack = 0;
  
  if(MonthsGoBack == -1) {
    MonthsGoBack = 11; 
    YearGoBack   = 1;  
  }

  if(String(MonthsGoBack).length == 1) {
    MonthsGoBack = "0" + String(MonthsGoBack);
  }
  
  CurrentMonth = String(this.Month);
  if(String(this.Month).length == 1) {
    CurrentMonth = "0" + String(this.Month);
  }

  if((this.Year != this.DateNow.getFullYear()) && (this.Year > this.DateNow.getFullYear())){
    YearGoBack = 0; 
  }  
  
  var ShowPrevMonth = "<div id='month_left' class='left' style='visibility: hidden'>&nbsp;</div>";
  if((parseInt((String(this.Year) + CurrentMonth))) >= parseInt(((String(this.DateNow.getFullYear() - YearGoBack) + String(MonthsGoBack))))) {		
    	ShowPrevMonth = "<div id='month_left' class='left'>&nbsp;</div>";
  } 
 //alert(this.Category.indexOf("all"));
 	if(this.Category.indexOf("all") != -1) {
		var CalendarTitle = this.CalendarTitle;
	} else {
		var CalendarTitle = this.Category;
	}
  
  var Html 	= "<div id='calendar'>"
					  + "<div id='cal_title' class='title'>" + CalendarTitle + "</div>"
					  + "<div class='header'>" 
						+ ShowPrevMonth 
						+ "<div id='month_head' class='head'>" + MonthName + "&nbsp;" + this.Year + "</div>" 
						+ "<div id='month_right' class='right'>&nbsp;</div>" 
						+ "</div>";
    			
  Html += "<div class='labels'>";
	for (var i = 0; i <= 6; i++ ){
	  Html += "<div class='label'>" + this.DayLabels[i] + "</div>";
	}
	Html += "</div>";

	Html += "<div class='days'>";
	for(var i = 1; i <= StartingDay; i++) {
		Html += "<div class='empty'>&nbsp;</div>";
	} 
  
	for(var j = 1; j < (this.CurrentDaysInMonth + 1); j++) {
    var style = "";
    if((j == this.DateNow.getDate()) && (this.Month == this.DateNow.getMonth()) && (this.Year == this.DateNow.getFullYear())) {
      style = "style=\"text-decoration:underline;\"";    
    }
		if(this.Day == j) {
			Html += "<div "+style+" id='day_" + j + "' class='click_day'>" + j + "</div>";	
		} else {
			Html += "<div "+style+" id='day_" + j + "' class='day'>" + j + "</div>";	
		}          
	}
	Html += "</div>";

	Html += "</div>";

	document.getElementById("calendarholder").innerHTML = Html;    
  
  var _this = this; 
  var MonthLeft  = parseFloat(this.Month) - 1;
  var MonthRight = parseFloat(this.Month) + 1;                  
  
  document.getElementById("month_left").onclick = function() {_this.setNewMonth(MonthLeft)};  
  document.getElementById("month_right").onclick = function() {_this.setNewMonth(MonthRight)};  
  document.getElementById("month_head").onclick = function() {_this.resetChoosenDay()};  
  for(var k = 1; k < (this.CurrentDaysInMonth + 1); k++) {  
    (function(k) {
      document.getElementById("day_" + k).onclick = function() {_this.setNewDay(k)};  
    })(k);  
  }   
}  

CShowCalendar.prototype.resetChoosenDay = function()
{
  this.Day = -1;
  this.InformationDay = 0;
  this.bGetMonth = true;                                 
  this.Display();
} 

/*
CShowCalendar.prototype.getDaysInMonth = function(Month, Year)
{
	var DaysInMonth = new Date(Year, Month, 0);
	return DaysInMonth.getDate();
}

*/

CShowCalendar.prototype.getDaysInMonth = function(Month, Year)
{
	var DaysInMonth = new Date(Year, (Month + 1), 0);
	return DaysInMonth.getDate();
}



CShowCalendar.prototype.getFirstDayOfMonth = function(Month, Year)
{
	var FirstDayOfMonth = new Date(Year, Month, 1);
	return FirstDayOfMonth.getDay();
}

CShowCalendar.prototype.setNewDay = function(Day)
{                      
  if(this.Day >= 0) {   
	  document.getElementById("day_" + this.Day).className = "day";
  }
  this.Day = Day;    
  setCookie("year", this.Year, getExpDate(0, 0, 3));
  setCookie("month", this.Month, getExpDate(0, 0, 3)); 
  if(document.getElementById("day_" + Day).className == "active_day") {
    setCookie("day", this.Day, getExpDate(0, 0, 3)); 
  } else {                                                             
    setCookie("day", -1, getExpDate(0, 0, 3));
  }
	document.getElementById("day_" + Day).className = "current_day";
    
  this.bGetDay = true;
  this.bGetMonth = false;   
  
  this.Display();                      
}

CShowCalendar.prototype.setNewMonth = function(Month)
{        
  this.Day = -1;
	if((Month >= 0) && (Month <= 11)) {
		this.Month = Month;
	} else {
		if(Month > 11) {
			this.Month = 0;
			this.Year++;
		} else {
			this.Month = 11;
			this.Year--;
		}
	}
  
  this.bGetMonth = true;

  setCookie("year", this.Year, getExpDate(0, 0, 3));
  setCookie("month", this.Month, getExpDate(0, 0, 3));
  setCookie("day", -1, getExpDate(0, 0, 3));  
	document.getElementById("informationholder").innerHTML = "<img src=\"pics/calendar/loader.gif\" />";
	this.Display();
}
    
CShowCalendar.prototype.getInformation = function(Day)
{        
  this.GetCallBackEvents(Day);
  this.GetCallBackInformation(Day);          
}

CShowCalendar.prototype.setInformationEvents = function()
{    
  if (this.CallBackObjEvents.readyState != 4 || this.CallBackObjEvents.status != 200) {
    return;
  }  
     
  var XmlDoc = this.CallBackObjEvents.responseXML;      
  var Root = XmlDoc.getElementsByTagName("com.ivengi.ccallback").item(0);          
  var aActiveDays = new Array();  
  var aInformation = new Array();
  var Html = "";               
  
  this.Category = Root.childNodes[0].childNodes[0].nodeValue;
  
  if(this.Category.indexOf("all") != -1) {
		document.getElementById("cal_title").innerHTML = this.CalendarTitle;
	} else {
		document.getElementById("cal_title").innerHTML = this.Category;
	}
	
  for (var iRoot = 0; iRoot < Root.childNodes.length; iRoot++) {
    var Node = Root.childNodes[iRoot];

    if (Node.nodeName != "#text") {     
      var Date     = "";
      var Header   = "";
      var Location = "";
      var Link     = "";
      var Day      = "";
      var Month    = "";
      var Year     = "";
      for (var iNode = 0; iNode < Node.childNodes.length; iNode++) {
      
        var NodeElement = Node.childNodes[iNode];

        if (NodeElement.nodeName != "#text") {
          if(this.array_key_exists(0, NodeElement.childNodes)) { 
            if (NodeElement.nodeName == "date") {
            	if(NodeElement.childNodes[0] != null) {
              	Date = NodeElement.childNodes[0].nodeValue;
              }
            }
            if (NodeElement.nodeName == "header") {
            	if(NodeElement.childNodes[0] != null) {
              	Header = NodeElement.childNodes[0].nodeValue;
              }
            }
            if (NodeElement.nodeName == "location") {
            	if(NodeElement.childNodes[0] != null) {
              	Location = NodeElement.childNodes[0].nodeValue;
              }
            }
            if (NodeElement.nodeName == "link") {
            	if(NodeElement.childNodes[0] != null) {
              	Link = NodeElement.childNodes[0].nodeValue;
              }
            }
            if (NodeElement.nodeName == "day") {
            	if(NodeElement.childNodes[0] != null) {
              	Day = NodeElement.childNodes[0].nodeValue;
              	aActiveDays.push(Day);
              }
            }
            if (NodeElement.nodeName == "month") {
            	if(NodeElement.childNodes[0] != null) {
              	Month = NodeElement.childNodes[0].nodeValue;
              }
            }
            if (NodeElement.nodeName == "year") {
            	if(NodeElement.childNodes[0] != null) {
              	Year = NodeElement.childNodes[0].nodeValue;
              }
            }
          }
        }
      } 
      
      var aInfoDay          = new Array();
      aInfoDay["date"]      = Date;
      aInfoDay["header"]    = Header;
      aInfoDay["location"]  = Location;
      aInfoDay["link"]      = Link;
      
      aInfoDay["day"]       = Day;
      aInfoDay["month"]     = Month;
      aInfoDay["year"]      = Year;
			//alert(this.Day);
     
     
 /*
      if(this.Month < Month) {
        aInformation.push(aInfoDay);
      } else {
        if(this.Month == Month && this.Day <= Day) {  
            aInformation.push(aInfoDay);    
        }
      }
     
      */
     
      
      if((this.Year == Year && this.Month == Month && this.Day == -1)) {
        aInformation.push(aInfoDay);
      } else {
        if(this.Year == Year && this.Month == Month && this.Day == Day) {  
        	aInformation.push(aInfoDay);    
        } else {
        	// Show all information if no cookies are set 
        	if((!getCookie("month") && !getCookie("day") && aInfoDay["date"].length > 0)) {
        		aInformation.push(aInfoDay);
        	} 
        }
      }
     

      
    }
  }
  
  var PrevNext = "<div class='all'>";
  PrevNext += "<div id='prev' class='prev'>&laquo; " + this.PrevTxt +  "</div>"; 
  PrevNext += "<div id='next' class='next'>" + this.NextTxt +  " &raquo;</div>"; 
  PrevNext += "<br class=\"clear\" />"
  PrevNext += "</div>";

  Html += "<span id=\"page_container\">";
  var counter = 0;
  this.Pages = 0;    
  var bOpened = false;
  
  for(var l = 0; l < aInformation.length; l++) {
    AddtoClass = "_even";  
    if(((l % this.ItemsPerPage) % 2) == 0) {
      AddtoClass = "_uneven"; 
    }
    if(l % this.ItemsPerPage == 0) { 
      bOpened = true;   
      this.Pages++;                                              
      if(counter < 1) {
        Html += "<span class=\"event_page\" id=\"page_"+counter+"\">"; 
      } else {
        Html += "<span class=\"event_page\" id=\"page_"+counter+"\" style=\"display:none\">";
      }
      counter++;   
    } 
    if(aInformation[l]["header"] != "" && aInformation[l]["location"] != "" && aInformation[l]["date"] != "") {
      Html += "<div class='item" + AddtoClass + "' onclick='window.location=\"" + aInformation[l]["link"] + "\"'>"
         +  "<div class='header'><a href='" + aInformation[l]["link"] + "'>" + aInformation[l]["header"] + "</a></div>"  
         +  "<div class='intro'>" + aInformation[l]["date"] + "&nbsp;-&nbsp;" + aInformation[l]["location"] + "</div>"  
         +  "</div>";    
    }
    if(l % this.ItemsPerPage == (this.ItemsPerPage-1)) {
       Html += "</span>"; 
       bOpened = false; 
    } 

  }
  if(bOpened) {
    Html += "</span>"; 
  }
  Html += "</span>";  
  this.CurrentPage = 0;
  document.getElementById("informationholder").innerHTML = "<img src=\"pics/calendar/loader.gif\" />";
  document.getElementById("informationholder").innerHTML = Html + PrevNext + "<br class=\"clear\" />";  
    
  var _this = this;
  document.getElementById("prev").style.visibility = 'hidden'; 
  if(this.Pages <= 1) {
    document.getElementById("next").style.visibility = 'hidden';
  } 
  document.getElementById("prev").onclick = function() {_this.previousPage()};  
  document.getElementById("next").onclick = function() {_this.nextPage()};  
}

CShowCalendar.prototype.previousPage = function()
{
  if(this.currentPage < 1) {
    return;
  }
  if(this.currentPage == 1) {
    document.getElementById("prev").style.visibility = 'hidden';
  } 
  if(this.currentPage < (this.Pages)) {
    document.getElementById("next").style.visibility = 'visible';
  } 
  this.currentPage = (this.currentPage - 1);
  this.switchPage();
}

CShowCalendar.prototype.nextPage = function()
{
  if(this.currentPage >= 0) {
    document.getElementById("prev").style.visibility = 'visible';
  }   
  if(this.currentPage >= (this.Pages-2)) {
    document.getElementById("next").style.visibility = 'hidden';
  } 
  this.currentPage = (this.currentPage + 1);
  this.switchPage();
}

CShowCalendar.prototype.switchPage = function()
{
  var BaseDiv = document.getElementById('page_container');
  var aPages = BaseDiv.getElementsByTagName('span'); 
  for(var i = 0; i < aPages.length; i++){
    if(aPages[i].className == "event_page") {
      aPages[i].style.display = "none";  
    }
  }
  var PageDivId = 'page_'+this.currentPage;
  document.getElementById(PageDivId).style.display = 'inline';
}

CShowCalendar.prototype.array_key_exists = function(key, search) 
{
/*
    if(!search || (search.constructor !== Array && search.constructor !== Object)) {
      return false;
    }
  */     
    return key in search;
}

CShowCalendar.prototype.setInformationCalendar = function()
{    
  if (this.CallBackObjCalendar.readyState != 4 || this.CallBackObjCalendar.status != 200) {
    return;
  } 
  
  var XmlDoc = this.CallBackObjCalendar.responseXML;      
  var Root = XmlDoc.getElementsByTagName("com.ivengi.ccallback").item(0);       
  
  var aInformation = new Array();
  var Html = "";  
  
  this.Category = Root.childNodes[0].childNodes[0].nodeValue;
  if(this.Category.indexOf("all") != -1) {
		document.getElementById("cal_title").innerHTML = this.CalendarTitle;
	} else {
		document.getElementById("cal_title").innerHTML = this.Category;
	}
                        
  for (var iRoot = 0; iRoot < Root.childNodes.length; iRoot++) {
    var Node = Root.childNodes[iRoot];
    if (Node.nodeName != "#text") {     
      var Date     = "";
      var Header   = "";
      var Location = "";
      var Link     = "";
      var Day      = "";
      var Month    = "";
      var Year     = "";
      for (var iNode = 0; iNode < Node.childNodes.length; iNode++) {
        var NodeElement = Node.childNodes[iNode];
        if (NodeElement.nodeName != "#text") {    
          if(this.array_key_exists(0, NodeElement.childNodes)) { 
            if (NodeElement.nodeName == "date") {
              if(NodeElement.childNodes[0] != null) {
              	Date = NodeElement.childNodes[0].nodeValue;
              }
            }
            if (NodeElement.nodeName == "header") {
            	if(NodeElement.childNodes[0] != null) {
              	Header = NodeElement.childNodes[0].nodeValue;
              }
            }
            if (NodeElement.nodeName == "location") {
            	if(NodeElement.childNodes[0] != null) {
              	Location = NodeElement.childNodes[0].nodeValue;
              }
            }
            if (NodeElement.nodeName == "link") {
            	if(NodeElement.childNodes[0] != null) { 
              	Link = NodeElement.childNodes[0].nodeValue;
              }
            }
            if (NodeElement.nodeName == "day") {
            	if(NodeElement.childNodes[0] != null) {
              	Day = NodeElement.childNodes[0].nodeValue;
              }
            }
            if (NodeElement.nodeName == "month") {
            	if(NodeElement.childNodes[0] != null) {
              	Month = NodeElement.childNodes[0].nodeValue;
              }
            }
            if (NodeElement.nodeName == "year") {
            	if(NodeElement.childNodes[0] != null) {
              	Year = NodeElement.childNodes[0].nodeValue;
              }
            }
          }
        }
      } 
      
      var aInfoDay          = new Array();
      aInfoDay["date"]      = Date;
      aInfoDay["header"]    = Header;
      aInfoDay["location"]  = Location;
      aInfoDay["link"]      = Link;
			
      aInfoDay["day"]       = Day;
	    aInfoDay["month"]     = Month;
	    aInfoDay["year"]      = Year;
      
	  if(aInfoDay["date"] != "") {
        	aInformation.push(aInfoDay);
	  }
    }
  }  
  if(aInformation.length > 0) {
    for(var DayItem = 0; DayItem < aInformation.length; DayItem++) {
				
        if(aInformation[DayItem]["year"] == this.Year && aInformation[DayItem]["month"] == this.Month) {
            this.aActiveDaysInformation.push(aInformation[DayItem]["day"]);      
        }
    }
  }
	
	var TotalCount  = aInformation.length
  var TotalPages  = Math.ceil(TotalCount / this.ItemsPerPage); 
  
  for(var j = 1; j < (this.CurrentDaysInMonth + 1); j++) {
    if(this.Day == j) {
      document.getElementById("day_" + j).className = "click_day";  
    } else {
      document.getElementById("day_" + j).className = "day";   
    }          
  }
             
  for(var k = 0; k < this.aActiveDaysInformation.length; k++) {
    if(document.getElementById("day_" + this.aActiveDaysInformation[k]).className != "click_day") {
      document.getElementById("day_" + this.aActiveDaysInformation[k]).className = "active_day";  
    } 
  }
  this.aActiveDaysInformation = new Array();
}
