function WRMCalendar (calElement) {
    this.months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    
    this.curDate = new Date();

    this.drawCalendar = function (date) {
	date.setDate(1);
	
	this.copyDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
	
	//Calendar Navigators
	var HTMLOutput = '<a href="javascript:EventCalendar.drawCalendar(new Date(' + date.getFullYear() + ', ' + (date.getMonth() - 1) + ', ' + date.getDate() + '))" class="prev">Previous Month</a>\
	    <a href="javascript:EventCalendar.drawCalendar(new Date(' + date.getFullYear() + ', ' + (date.getMonth() + 1) + ', ' + date.getDate() + '))" class="next">Next Month</a>';
	    
	//Table open, caption and day headers
	HTMLOutput += '<table><caption>Events<br /> ' + this.months[date.getMonth()] + ' ' + date.getFullYear() + '</caption>\
	    <thead><tr><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr></thead><tbody><tr>';
	
	//Pad out the begining of the first week
	for (var i = 0; i < date.getDay(); i++) {
	    HTMLOutput += '<td>&nbsp;</td>';
	}
	
	//Store the current month so we can stop the loop when we leave it
	var ourMonth = date.getMonth();
	
	//Loop through the days of this month
	while (date.getMonth() == ourMonth) {
	    //Begin our cell
	    HTMLOutput += '<td';
	    
	    var today = new Date();
	    
	    //Add an ID if it's today
	    if (date.getFullYear() == today.getFullYear() && date.getMonth() == today.getMonth() && date.getDate() == today.getDate()) {
		HTMLOutput += ' id="today" ';
	    }
	    
	    //Rest of cell
	    HTMLOutput += '><a href="/upcoming-events.aspx?date=' + date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + '">' + date.getDate() + '</a></td>';
	    
	    date.setDate(date.getDate() + 1);
	    
	    //It's a new week, time to break to a new row
	    if (date.getDay() == 0 && date.getMonth() == ourMonth) {
		HTMLOutput += '</tr><tr>';
	    }
	}
	
	//Pad out the end of the last week
	//Make sure we're not on Monday and filling out an extra row
	if (date.getDay() != 0) {
	    for (var i = 0; i < 7 - date.getDay(); i++) {
		HTMLOutput += '<td>&nbsp;</td>';
	    }
	}
	
	//Finish off the table
	HTMLOutput += '</tr></tbody></table>';
	
	calElement.innerHTML = HTMLOutput;
	
	
    }
    
    this.previousMonth = function () {
	curDate.setMonth(curDate.getMonth() - 1);
	this.drawCalendar(curDate);
    }
    
    this.nextMonth = function () {
	curDate.setMonth(curDate.getMonth() + 1);
	this.drawCalendar(curDate);
    }

    this.gup = function (name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
	    return "";
	else
	    return results[1];
    }
}

var EventCalendar = new WRMCalendar(document.getElementById("calendar"));

if (EventCalendar.gup("date") == "") {
    EventCalendar.drawCalendar(new Date());
} else {
    var dateArray = EventCalendar.gup("date").split("-");
    EventCalendar.drawCalendar(new Date(dateArray[0], dateArray[1] - 1, dateArray[2]));
}

if (SI.ClearChildren) {
		SI.ClearChildren.clear();
	}
