function Calendar(table)
{
	this.calendar = table;
	this.calendar.handler = this;
	this.selectedDates = new Array();
	this.currentMonth;
	this.currentYear;
	this.modifiable = true;

	this.calendar.onmousedrag = function(){ return false; }
	this.calendar.onmousemove = function(){ return false; }
	this.calendar.onmousedown = function(){ return false; }
	this.calendar.onmouseup = function(){ return false; }

	this.changeMonth = function(month, year)
	{

		var prevMonth = month == 1 ? 12 : (month - 1);
		var prevYear = month == 1 ? (year - 1) : year;
		var nextMonth = month == 12 ? 1 : (month + 1);
		var nextYear = month == 12 ? (year + 1) : year;

		//this.rebuildTable(prevMonth, prevYear, nextMonth, nextYear, month, year);

		//alert(prevMonth + " / " + prevYear + " and " + nextMonth + " / " + nextYear + " ["+month+"/"+year+"]");
		this.buildTable(prevMonth, prevYear, nextMonth, nextYear, month, year);
	}

	this.setModifiable = function(tf){
		this.modifiable = tf;
	}

	this.handleCellClick = function(cell)
	{
		this.toggleCalendarCell(cell);
		
		/* DATE RANGE CHANGE HERE */
		//ref = "DA363gh36d7s";
		//importRooms(ref);
		return false;
	}

	this.buildTable = function(prevMonth, prevYear, nextMonth, nextYear, month, year){
		clearChildNodes(this.calendar);

		var tbody = document.createElement("tbody");
		var thead = document.createElement("thead");

		/* Nav / Title Label Row */
		nextTr = document.createElement("tr");


		if(this.modifiable)
		{
			nextTh = document.createElement("th");
			nextTh.handler = this;
			nextTh.prevMonth = prevMonth;
			nextTh.prevYear = prevYear;
			nextTh.onclick = function(){ this.handler.changeMonth(this.prevMonth, this.prevYear); };
			nextTh.setAttribute("class", "navigation");
			var text = document.createTextNode("<<");
			nextTh.appendChild(text);
			nextTr.appendChild(nextTh);
		}

		nextTh = document.createElement("th");

		if(this.modifiable)
		{
			nextTh.setAttribute("colspan", "5");
		}
		else
		{
			nextTh.setAttribute("colspan", "7");
		}
		text = document.createTextNode(monthsArray[month - 1] + " " + year);
		nextTh.appendChild(text);
		nextTr.appendChild(nextTh);


		if(this.modifiable)
		{
			nextTh = document.createElement("th");
			nextTh.handler = this;
			nextTh.nextMonth = nextMonth;
			nextTh.nextYear = nextYear;
			nextTh.onclick = function(){ this.handler.changeMonth(this.nextMonth, this.nextYear); };
			nextTh.setAttribute("class", "navigation");
			text = document.createTextNode(">>");
			nextTh.appendChild(text);
			nextTr.appendChild(nextTh);
		}

		thead.appendChild(nextTr);

		/* Day Label Row */
		nextTr = document.createElement("tr");
		for (var dayLoop in daysArray)
		{
			nextTh = document.createElement("th");
			text = document.createTextNode(daysArray[dayLoop].substr(0, 3));
			nextTh.appendChild(text);
			nextTr.appendChild(nextTh);
		} 
		thead.appendChild(nextTr);

		var fdtm = new Date(year, month - 1, 1);

		var currentDaysInMonth = daysInMonth(month-1, year);
		firstDay = fdtm.getDay();
		lastDay = firstDay + currentDaysInMonth;
		total = roundUp(lastDay, 7);

		tempContent = "";
		var nextTr = document.createElement("tr");
		for (cellLoop = 0; cellLoop < total; cellLoop++)
		{		
			displayDay = cellLoop - firstDay + 1;
			currentAttributes = new Object();
			currentAttributes.class = "calendarNumber";
			currentAttributes.id = generateCellId(year, month, displayDay);
			var nextTd = document.createElement("td");
			nextTd.handler = this;

			if(this.modifiable)
				nextTd.onmousedown = function(){ return this.handler.handleCellClick(this); };
			
			if (cellLoop % 7 == 0)
			{
				tbody.appendChild(nextTr);
				var nextTr = document.createElement("tr");
			}
			
			if (displayDay > currentDaysInMonth)
			{ // Next month overlap
				displayDay = displayDay - currentDaysInMonth;
				currentAttributes.class += " greyed";
				currentAttributes.id = generateCellId(nextYear, nextMonth, displayDay);
			}
			else if (displayDay <= 0)
			{ // Previous month overlap
				displayDay = daysInMonth(prevMonth - 1, prevYear) + displayDay;
				currentAttributes.class += " greyed";
				currentAttributes.id = generateCellId(prevYear, prevMonth, displayDay);
			}

			if (this.selectedDates[currentAttributes.id] != null)
			{
				randomHighlight = Math.floor(Math.random()*5);
				currentAttributes.class += " highlight" + randomHighlight;
			}
			
			for (attribute in currentAttributes)
			{
				nextTd.setAttribute(attribute, currentAttributes[attribute]);
			}
			
			text = document.createTextNode(displayDay);
			nextTd.appendChild(text);
			nextTr.appendChild(nextTd);
		}
		tbody.appendChild(nextTr);
		
		this.calendar.appendChild(thead);
		this.calendar.appendChild(tbody);
	}
	
	this.rebuildTable = function(prevMonth, prevYear, nextMonth, nextYear, month, year){

		var tempContent = "";
		var tableHead = "";
		var tableBody = "";

		tableHead += addTag("tr", 
						addTag("th", "&#60;&#60;", " class=\"navigation\" onclick=\"changeMonth(" + prevMonth + ", " + prevYear + ")\"") + 
						addTag("th", monthsArray[month - 1] + " " + year, " colspan=\"5\"") + 
						addTag("th", "&#62;&#62;", " class=\"navigation\" onclick=\"changeMonth(" + nextMonth + ", " + nextYear + ")\"")
					 );

		tempContent = "";
		for (var dayLoop in daysArray)
		{
			tempContent += addTag("th", daysArray[dayLoop].substr(0, 3));
		} 
		tableHead += addTag("tr", tempContent);
		tableHead = addTag("thead", tableHead);

		var fdtm = new Date(year, month - 1, 1);

		currentDaysInMonth = daysInMonth(month-1, year);
		firstDay = fdtm.getDay();
		lastDay = firstDay + currentDaysInMonth;
		total = roundUp(lastDay, 7);

		tempContent = "";
		for (cellLoop = 0; cellLoop < total; cellLoop++)
		{
			if (cellLoop % 7 == 0)
			{
				tableBody += addTag("tr", tempContent);
				tempContent = "";
			}

			displayDay = cellLoop - firstDay + 1;

			currentAttributes = new Object();
			currentAttributes.onclick = "toggleCalendarCell(this)";
			currentAttributes.class = "calendarNumber";
			currentAttributes.id = generateCellId(year, month, displayDay);

			if (displayDay > currentDaysInMonth)
			{ // Next month overlap
				displayDay = displayDay - currentDaysInMonth;
				currentAttributes.class += " greyed";
				currentAttributes.id = generateCellId(nextYear, nextMonth, displayDay);
			}
			else if (displayDay <= 0)
			{ // Previous month overlap
				displayDay = daysInMonth(prevMonth - 1, prevYear) + displayDay;
				currentAttributes.class += " greyed";
				currentAttributes.id = generateCellId(prevYear, prevMonth, displayDay);
			}

			if (this.selectedDates[currentAttributes.id] != null)
			{
				randomHighlight = Math.floor(Math.random()*5);
				currentAttributes.class += " highlight" + randomHighlight;
			}	
			
			attributeContent = "";
			for (attribute in currentAttributes)
			{
				attributeContent += " " + attribute + "='" + currentAttributes[attribute] + "'";
			}

			tempContent += addTag("td", displayDay, attributeContent);

		}
		
		tableBody += addTag("tr", tempContent);
		tableBody = addTag("tbody", tableBody);

		this.calendar.innerHTML = tableHead;
		this.calendar.innerHTML += tableBody;
	}

	this.toggleCalendarCell = function(cell)
	{
		var currentClass = cell.getAttribute("class");
		var classPrefix = "calendarNumber ";

		if (currentClass.indexOf("greyed") != -1)
		{
			classPrefix += "greyed ";
		}

		if (currentClass.indexOf("highlight") != -1)
		{
			cell.setAttribute("class", classPrefix);
			this.selectedDates[cell.getAttribute("id")] = null;
		}
		else
		{
			randomHighlight = Math.floor(Math.random()*5);
			cell.setAttribute("class", classPrefix + "highlight" + randomHighlight);
			this.selectedDates[cell.getAttribute("id")] = true;
		}

		var dateSpan = document.getElementById("selectedDates");
		dateSpan.innerHTML = generateDateString(this.selectedDates);

		/* DATE RANGE CHANGE HERE */
		//ref = "DA363gh36d7s";
		//importRooms(ref);
	}
}








