/*
	Note: Javascript Month index from 0 to 11.
*/
//Constructor
function calendar(id, d, width, target)
{
	this.id = id;
	this.dateObject = d;
	this.width = width;
	this.target = target;
	this.write = writeCalendar;
	this.length = getLength;
	this.month = d.getMonth();
	this.date = d.getDate();
	this.day = d.getDay();
	this.year = d.getFullYear();
	this.getFormattedDate = getFormattedDate;
	//create an array to hold all events in current month.
	this.eventdate = new Array();
	this.eventname = new Array();
	this.eventid = new Array();
	//get the first day of the month's day
	d.setDate(1);
	this.firstDay = d.getDay();
	//then reset the date object to the correct date
	d.setDate(this.date);
}

var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

function getFormattedDate()
{
	return days[this.day] + ', ' + months[this.month] + ' ' + this.date + ', ' + this.year;
	//return this.month + '/' + this.date + '/' + this.year;
}

function writeCalendar()
{
	var calString = "<table id=\"cal"+ this.id + "\" rules=\"none\" cellspacing=\"0\" border=0 width=\""+this.width+"\" style=\"calContainer\">\n";
	//write the month
	calString += "<tr height=23 valign=\"middle\"><td class=\"nav\" align=\"left\" style=\"cursor:pointer;\" onClick=\"changeMonth(-1,'" + this.id + "')\">&lt;</td><td align=\"center\" colspan=\"5\" class=\"month\">" + months[this.month] + ", " + this.year + "</td><td class=\"nav\" align=\"right\" style=\"cursor:pointer;\" onClick=\"changeMonth(1,'" + this.id + "')\">&gt;</td></tr>\n";
	//write a row containing days of the week
	calString += "<tr>";
	
	for(i = 0; i < days.length; i++){
		calString += "<td class=\"dayHeader\" width=\""+Math.round(this.width/7)+"\">" + days[i].substring(0,1) + "</td>";
	}
	
	calString += "</tr>\n";
	
	var lastrow = false;
	
	//write the body of the calendar
	calString += "<tr>";
	//create 6 rows so that the calendar doesn't resize
	for(j = 0; j < 42; j++)
	{
		var displayNum = (j - this.firstDay + 1);
		
		if(j < this.firstDay)
		{
			//write the leading empty cells
			calString += "<td class=\"empty\">&nbsp;</td>";
		}else if(displayNum == this.date)
		{
			var index = eventCheck(displayNum, this);
			if(index === false)
			{
				calString += "<td id=\"" + this.id +"selected\" class=\"date\">" + displayNum + "</td>";
			}
			else
			{
				calString += "<td id=\"" + this.id +"selected\" class=\"eventdays\"><a class=\"eventlnk\" href=\"event.php?y="+this.year+"&m="+(this.month+1)+"&d="+displayNum+"\" title=\"View Event\">" + displayNum + "</a></td>";
			}
		}else if(displayNum > this.length())
		{
			//Empty cells at bottom of calendar
			calString += "<td class=\"empty\">&nbsp;</td>";
		}else
		{
			//the rest of the numbered cells
			var index = eventCheck(displayNum, this);
			if(index === false)
			{
				calString += "<td id=\"\" class=\"days\">" + displayNum + "</td>";
			}
			else
			{
				calString += "<td id=\"\" class=\"eventdays\"><a class=\"eventlnk\" href=\"event.php?y="+this.year+"&m="+(this.month+1)+"&d="+displayNum+"\" title=\"View Event\">" + displayNum + "</a></td>";
			}
		}
		
		if(displayNum == this.length())
		{
			lastrow = true;
		}
		
		if(j%7 == 6)
		{
			calString += "</tr>\n";
			
			if(lastrow)
			{
				break;
			}else
			{
				calString += "<tr>";
			}
		}
	}
	
	calString += "</table>\n";
	return calString;
}

function getLength(){
	//thirty days has September...
	switch(this.month){
		case 1:
			if((this.dateObject.getFullYear()%4==0&&this.dateObject.getFullYear()%100!=0)||this.dateObject.getFullYear()%400==0)
				return 29; //leap year
			else
				return 28;
		case 3:
			return 30;
		case 5:
			return 30;
		case 8:
			return 30;
		case 10:
			return 30;
		default:
			return 31;
	}
}

function eventCheck(daynum, cal)
{
	cal = eval(cal);
	var range = new Array();
	for(var i = 0; i < cal.eventdate.length; i++)
	{
		range = cal.eventdate[i].split("-");

		if(daynum >= parseInt(range[0]) && daynum <= parseInt(range[1]))
		{
			return i;
		}
	}
	return false;
}

function changeMonth(mo,cal)
{
	var exeurl = "calendar.php";
	//more trickery!
	cal = eval(cal);
	
	cal.eventid = new Array();
	cal.eventname = new Array();
	cal.eventdate = new Array();
	
	//The Date object is smart enough to know that it should roll over in December
	//when going forward and in January when going back
	cal.dateObject.setMonth(cal.dateObject.getMonth() + mo);
	cal = new calendar(cal.id, cal.dateObject, cal.width, cal.target);
	cal.formattedDate = cal.getFormattedDate();
	
	var xmlReq = makeReq();
	
	if(xmlReq)
	{
		var paras="y="+cal.year+"&m="+(cal.month+1);
		xmlReq.open("POST", exeurl, true);
			
		xmlReq.onreadystatechange = function(){
			if(xmlReq.readyState == 4 && xmlReq.status == 200)
			{
				populateCalendar(xmlReq.responseXML, cal);
				document.getElementById(cal.target.toString()).innerHTML = cal.write();
			}
			else
			{
				return_msg = "<br /><br /><center>&nbsp;&nbsp;<img src='/images/progress_loading.gif' align='center'>&nbsp;<font face='Arial' size=2 color='#009933'>Loading...</font></center><br /><br />";
				document.getElementById(cal.target.toString()).innerHTML = return_msg;
			}
		};
		xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    	xmlReq.send(paras);
	}
}

function populateCalendar(xmlDoc, cal)
{
	cal = eval(cal);
	
	var currNode = null;
	counter = 0;
	for(var i = 0; i < xmlDoc.documentElement.childNodes.length; i++)
	{
		currNode = xmlDoc.documentElement.childNodes[i];
		
		if(currNode.nodeType != 3)
		{
			if(is_ie)
			{
				cal.eventid[counter] = currNode.childNodes[0].firstChild.nodeValue;
				cal.eventname[counter] = currNode.childNodes[1].firstChild.nodeValue;
				cal.eventdate[counter] = currNode.childNodes[2].firstChild.nodeValue + "-" + currNode.childNodes[3].firstChild.nodeValue;
			}
			else
			{
				cal.eventid[counter] = currNode.childNodes[1].firstChild.nodeValue;
				cal.eventname[counter] = currNode.childNodes[3].firstChild.nodeValue;
				cal.eventdate[counter] = currNode.childNodes[5].firstChild.nodeValue + "-" + currNode.childNodes[7].firstChild.nodeValue;
			}
			counter++;
		}
	}
}