// Calendar functions - by Jim Scarletta
// based on code from http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=35&MsgID=170217&Setting=A9999F0001

//set variables for the day, date, month and year 

	var cellSize = '35px';
	var dayName = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat"); 
	var monthName = new Array("January","February","March","April","May","June","July","August","September","October","November","December"); 
	var monthDays = new Array("31","28","31","30","31","30","31","31","30","31","30","31"); 
	var today = new Date(); 
	var thisDate = today.getDate(); 
	var thisMonth = today.getMonth(); 

	//Get the year (I.E. 5+) 
	var thisYear = today.getFullYear(); 
	var events = []; 

function drawEventDetail(id) {
//	alert(str);
	var thisID = id -1 ;
	var strDate = events[thisID].day;
	var strEventInfo = "";
	var strSuffix = "";
	var altColor = "";
	switch (strDate) {
		case 1 :
			strSuffix ='st';
		case 2 :
			strSuffix ='nd';
		case 3 :
			strSuffix ='rd';
		default :
			strSuffix = 'th';
	} 
	strEventInfo = "<TABLE width=100%><TR bgcolor='#f5deb3'><TD><B>Events for the " + strDate + strSuffix + ":</B><BR><BR></TD></TR>";
	
	for(var counter in events ){ 
		var evt = events[ counter ];
		if ((evt.day == strDate) && (evt.month == events[thisID].month) ){
			if (altColor == '#f5f5dc') {
				altColor = '#ffffe0';
			}
			else {
				altColor = '#f5f5dc';
			}
			strEventInfo += "<TR bgcolor=" + altColor + "><TD>"
			var title = events[counter].title;
			var strURL = events[counter].URL;
			strEventInfo += title;
			if (strURL) {
				strURL = '<BR> &nbsp; (Click <a href="' + strURL + '">here</a> for more information.)<BR><BR>';
				strEventInfo = strEventInfo + strURL;				
			}
			strEventInfo += "</TD></TR>";
		}
	}
	strEventInfo += "</TABLE>";
	document.all.calEventDetail.innerHTML = strEventInfo;
	return;		
}

function drawEvent( evt ){ 
	var theTd = document.getElementById( evt.day-1 ); 
	theTd.bgColor = '#ccccff';
	theTd.innerHTML = '<b><table style="cursor:hand;" width=30px border=0 cellspacing=0 cellpadding=0  align=center title="Click to view events for this day." onclick="drawEventDetail(' + evt.id + ');"><tr><td align=center>' +
						theTd.innerHTML + '</TR></TD></TABLE></b>'; 
} 

function CalEvent(id, nthDayAdjust, nthDay, nthWeek, day, month, year, title, URL) { 
	this.id = id; 
	this.title = title; 
	this.day = day; 
	this.month = month; 
	this.year = year; 
	this.everyYear = year == -1 ? true : false; 
	this.everyMonth = month == -1 ? true : false; 
	this.nthDayAdjust = nthDayAdjust;
	this.nthDay = nthDay;
	this.nthWeek = nthWeek;
	this.URL=URL;
} 

var intLastID = 0;
function addEvent(nthDayAdjust, nthDay, nthWeek, day, month, year, title, URL) { 
	// Whatever your parameters for this are up to you 
	intLastID++;
	var temp = new CalEvent(intLastID, nthDayAdjust, nthDay, nthWeek, day, month, year, title, URL); 
	events.push(temp); 
} 


//set function to go forward one month and refreash calendar 

function nextMonth(m) { 
	document.all.calEventDetail.innerHTML="(Click a day to see the events for that day)"
	if (m == 11) { 
		thisMonth = 0; 
		thisYear++; 
	} else { 
		thisMonth++; 
	} 
} 

//set function to go back one month and refreash calendar 

function prevMonth(m) { 
	document.all.calEventDetail.innerHTML="(Click a day to see the events for that day)"
	if (m == 0) { 
		thisMonth = 11; 
		thisYear--; 
	} else { 
		thisMonth--; 
	} 
} 

function prevYear(){ 
	document.all.calEventDetail.innerHTML="(Click a day to see the events for that day)"
	thisYear--; 
	writeCal(); 
} 

function nextYear(){ 
	document.all.calEventDetail.innerHTML="(Click a day to see the events for that day)"
	thisYear++; 
	writeCal(); 
} 
//set function to write the calendar 

function writeCal() { 
//set allotment for leap year 
	if ( ( ((thisYear%4) == 0) && ((thisYear%100) != 0) ) || ((thisYear%400) == 0) ) { 
		monthDays[1] = 29; 
	} else { 
		monthDays[1] = 28; 
	} 
	var tempDate = new Date(thisYear,thisMonth,1); 
	var firstDay = tempDate.getDay(); 

	var dayCount = 1; 
	// set the cariable that will hold the table "t" for the calendar 
	var t = "<table align='center' border='5' bordercolor='#000000' cols='7'>"; 
	t = t + "<tr>"; 
	t = t + "<td NOWRAP align='center' bgcolor='#A2A2A2' colspan='7' height='10px'><font size='5'><b>"+ ( monthName[thisMonth].toUpperCase() ) +" " +thisYear+ "</b></font></td>"; 
	t = t + "</tr>"; 
	
	t = t + "<tr>"; 
	//prev year 
	t = t + "<td align='center' bgcolor='#BBBBBB' width='" + cellSize + "' height='10px' title='Previous Year' style='cursor:hand' onclick='prevYear();'><<</td>"; 
	//prev month 
	t = t + "<td align='center' bgcolor='#BBBBBB' width='" + cellSize + "' height='10px' style='cursor:hand' title='Previous Month' onclick='prevMonth(thisMonth);writeCal();'><</td>"; 
	t = t + "<td align='center' bgcolor='#BBBBBB' colspan='3' height='10px'>&nbsp;</td>"; 
	//next month 
	t = t + "<td align='center' bgcolor='#BBBBBB' width='" + cellSize + "' height='10px' style='cursor:hand' title='Next Month' onclick='nextMonth(thisMonth);writeCal();'>></td>"; 
	//next year 
	t = t + "<td align='center' bgcolor='#BBBBBB' width='" + cellSize + "' height='10px' style='cursor:hand' title='Next Year' onclick='nextYear();'>>></td>"; 
	t = t + "</tr>"; 
	t = t + "<tr bgcolor='#CCCCCC'>"; 
	for (var i = 0; i < dayName.length; i++) { 
	t = t + "<td align='center' bgcolor='#CCCCCC' width='" + cellSize + "'>" +( dayName[i].substring(0,9) )+"</td>"; 
	} 
	t = t + "</tr>"; 
	t = t + "<tr>"; 
	for (var i = 0; i < firstDay; i++) { 
	t = t + "<td bgcolor='#DDDDDD' bordercolor='#FFFFFF' width='" + cellSize + "' height='10px'></td>"; 
	} 
	for (var i = 0; i < monthDays[thisMonth]; i++) { 
	t = t + "<td id ='"+i+"' align='center' valign='top' bgcolor='#DDDDDD' width='" + cellSize + "' height='10px'>" + (dayCount++) + "</td>"; 
	if ( ( ((i + firstDay + 1) % 7) == 0 ) && ( dayCount < (monthDays[thisMonth]+1) ) ) { 
	t = t + "</tr><tr>"; 
	} 
	} 
	var totCells = firstDay + monthDays[thisMonth]; 
	for (var i = 0; i < ( ((totCells > 28) ? ((totCells > 35) ? 42:35):28) - totCells ); i++) { 
	t = t + "<td bgcolor='#DDDDDD' bordercolor='#FFFFFF' width='" + cellSize + "' height='10px'></td>"; 
	} 
	t = t + "</tr>"; 
	t = t + "</table>"; 
	// this tells where to write the calendar when rendering in the DIV set aside for it 
	document.all.cal.innerHTML = t; 
	addEvents(); 
} 

function addEvents(){ 
	for(var counter in events ){ 
		var evt = events[ counter ]; 
		if (evt.nthDay && evt.nthWeek) {
			var tempDate = new Date(thisYear,thisMonth,1); 
			var firstDay = tempDate.getDay(); 
			var month = tempDate.getMonth();
			var year = tempDate.getYear();

			var nthDayResult = 0; // return value
			var day = 1;

			if (firstDay <= evt.nthDay) {
				nthDayResult = evt.nthDay - firstDay + 1 + (evt.nthWeek - 1)*7;
			}
			else {
				nthDayResult = evt.nthDay + (7-firstDay) + 1 + (evt.nthWeek - 1)*7;
			}
			evt.day = nthDayResult + evt.nthDayAdjust;
			drawEvent( evt ); 
			continue;
		}
		if( (evt.everyYear || (thisYear+1) == evt.Year)&& (evt.month == (thisMonth+1) || evt.everyMonth )){ 
			drawEvent( evt) 
			continue; 
		} 
		if( evt.everyMonth ){ 
			drawEvent( evt ); 
			continue; 
		} 
		if( evt.month == (thisMonth+1) ){ 
			drawEvent( evt ); 
			continue; 
		} 
	} 
} 

function getEventForDay(day, month, year) { 
// Probably the most used one 
	var listOfEvents = []; 
	for (var i = 0; i < events.length; i++) { 
		if (events[i].day == day && events[i].month == month && events[i].year == year) { 
		push(listOfEvents, events[i].title); 
		} 
	} 
	return listOfEvents; 
} 


