/* =============================================================================================== *
* Revolving 12 Month Calendar                  Copyright 2003 - Kevin Bilbee - All Rights Reserved *
* ================================================================================================ *
* ================================================================================================ *
* Version:           1.1                                                                           *
* Last Modified:     11/10/2003                                                                    *
* Author:            Kevin Bilbee                                                                  *
* Bug Reports:       kbilbee[at]pictureflipper.com                                                 *
* ================================================================================================ *
* ================================================================================================ *
* Terms of use: This calendar script can be used freely by Simi Valley Schools in the Simi         *
*               Valley School District, Simi Valley, California. All others must email             *
*               kbilbee[at]pictureflipper.com. This copyright and terms of use must stay intact.   *
*                                                                                                  *
* version 1.0 - Initial release                                                                    *
* version 1.1 - Fixed issue with calendar reporting the wrong month                                *
*               Fixed leading and trailing empty row                                               *
* ================================================================================================ */
var fiveDayCal = true;
var dayWidth = 124;
var eventList = new Array();
var index = 0;
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);
var visibleID = "";
var activeTab = "";
var now = new Date();
var curMonth = now.getMonth()+1;
var browserYear = now.getYear();
// the number of milliseconds for one day
var oneDay = 86400000;

function change_pic(objId, pic) {
		if (ie4) {
			document.all[objId].src = "../graphics/" + pic;
		} else {
			document.getElementById(objId).src = "../graphics/" + pic;;
		}
}
function makevisible(id, tab) {
	if (ie4) {
		if (visibleID != "") 
			document.all[visibleID].style.visibility='hidden';
		document.all[id].style.vidsiblilty='visible';
		document.all[tab].style.background='#638cda';
		document.all[tab].style.color='#ffffff';
		if (activeTab != "") {
			document.all[activeTab].style.background='#ffffff';
			document.all[activeTab].style.color='bbbbbb';
			document.all['postMessage'].style.visibility='hidden'; 
		}
	} else {
		if (visibleID != "") 
			document.getElementById(visibleID).style.visibility='hidden';
		document.getElementById(id).style.visibility='visible'; 
		document.getElementById(tab).style.background='#638cda';
		document.getElementById(tab).style.color='#ffffff';
		if (activeTab != "") {
			document.getElementById(activeTab).style.background='#ffffff';
			document.getElementById(activeTab).style.color='#777777';
			document.getElementById('postMessage').style.visibility='hidden'; 
		}
	}
	visibleID = id
	activeTab = tab;
}
function MouseTabOverStyle(tab, tabName) {
	if( tabName != activeTab) {
		tab.style.background='#ffffff';
		tab.style.color='#000000';
	}
}
function MouseTabOutStyle(tab, tabName) {
	if( tabName != activeTab) {
		tab.style.background='#ffffff';
		tab.style.color='#777777';
	}
} 
function selectMonth(month) {
	browserYear = now.getYear();
	if (month < curMonth) browserYear++;
	var year = browserYear;
	if (year < 200) year +=1900;
	switch (month) {
		case 1:
			makevisible('calJan','tabJan');
			break;
		case 2:
			makevisible('calFeb','tabFeb');
			break;
		case 3:
			makevisible('calMar','tabMar');
			break;
		case 4:
			makevisible('calApr','tabApr');
			break;
		case 5:
			makevisible('calMay','tabMay');
			break;
		case 6:
			makevisible('calJun','tabJun');
			break;
		case 7:
			makevisible('calJul','tabJul');
			break;
		case 8:
			makevisible('calAug','tabAug');
			break;
		case 9:
			makevisible('calSep','tabSep');
			break;
		case 10:
			makevisible('calOct','tabOct');
			break;
		case 11:
			makevisible('calNov','tabNov');
			break;
		case 12:
			makevisible('calDec','tabDec');
			break;
	}
	getObject('year').innerHTML = year; 
}
function buildCalendars() {
	for (var i = 1; i <= 12; i++) {
		buildCalendar(i);
	}
}
function buildCalendar(calMonth) {
	var calObj;
	switch (calMonth) {
		case 1:
			calObj = getObject('calJan');
			break;
		case 2:
			calObj = getObject('calFeb');
			break;
		case 3:
			calObj = getObject('calMar');
			break;
		case 4:
			calObj = getObject('calApr');
			break;
		case 5:
			calObj = getObject('calMay');
			break;
		case 6:
			calObj = getObject('calJun');
			break;
		case 7:
			calObj = getObject('calJul');
			break;
		case 8:
			calObj = getObject('calAug');
			break;
		case 9:
			calObj = getObject('calSep');
			break;
		case 10:
			calObj = getObject('calOct');
			break;
		case 11:
			calObj = getObject('calNov');
			break;
		case 12:
			calObj = getObject('calDec');
			break;
	}
	var year = browserYear;
	if (year < 200) year +=1900;
	if (calMonth < curMonth) year++;
	calObj.innerHTML = GenerateCalendarTable(calMonth,year);
}
function GenerateCalendarTable(month,year) {
	// first Day of the month
	var FDOM = new Date();
	FDOM.setDate(1);
	FDOM.setYear(year);
	FDOM.setMonth(month-1);

	// Day of the week for the first day of the month
	var DOW_FDOM = FDOM.getDay();
	// First Day of the calendar
	var FDOC = new Date();
	var LDOC = new Date();
	
	//	Set the first day for the calendar
	FDOC.setTime(FDOM.getTime()-(DOW_FDOM*oneDay));
	// Set the last day for the calendar
	LDOC.setTime(FDOC.getTime()+(35*oneDay));

	var calendar = '';
	
	// day of week titles
	var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

	// Loop 5 weeks
	// Loop 7 days
	calendar += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" width=\"100%\" class=\"cal\">";
	calendar += "<tr>";
	for (var d = 0; d < 7; d++) {
		// If a 5 day calendar skip saturday and sunday
		if (!(fiveDayCal && (d == 0 || d == 6)))
			calendar += "<td class=\"calTitle calCell\" align=\"center\">" + days[d] + "</td>";
	}
	calendar += "</tr>";
	
	var curMonthInRow = false;
	var curRow;
	
	for (var weeks = 1; weeks <= 6; weeks++) {
		curMonthInRow = false;
		curRow = "";
		curRow += "<tr>";
		for (var days = 1; days <= 7; days++) {
			if (!(fiveDayCal && (days == 1 || days == 7))) {
				if (FDOC.getMonth() == FDOM.getMonth()) {
					curRow += getEventCell(FDOC);
					curMonthInRow = true;
				} else {
					curRow += "<td class=\"ncm calCell\">&nbsp;</td>";
				}
			}
			FDOC.setTime(FDOC.getTime()+oneDay);
		}
		curRow += "</tr>";
		
		if (curMonthInRow) {
			calendar += curRow;
		}
	}
	calendar += "</table>";

	return calendar;
}
function GetEventList(start, daysToDisplay) {
	var events = "";
	var emonth;
	var eday;
	var eyear;
	events += "<ul>";
	// search the eventList
	for (var d = 0; d < daysToDisplay; d++) {
		var emonth = start.getMonth()+1;
		var eday = start.getDate();
		var eyear = start.getYear();
		if (eyear < 200) eyear = eyear + 1900;
		for (e = 0; e < eventList.length; e++) {
			if (eventList[e].year == eyear && eventList[e].month == emonth && eventList[e].day == eday) {
				events += "<li>"+eventList[e].month+"/"+eventList[e].day+" - "+eventList[e].description+"</li>";
			}
		}
		start.setTime(start.getTime()+oneDay);
	}
	events += "</ul>";
	return events;
}
function GetEventList2(start, daysToDisplay) {
	var events = "";
	var emonth = -1;
	var eday = -1;
	var eyear = -1;
	var pmonth = -2;
	var monthStr = "";
	// search the eventList
	for (var d = 0; d < daysToDisplay; d++) {
		var emonth = start.getMonth()+1;
		var eday = start.getDate();
		var eyear = start.getYear();
		if (eyear < 200) eyear = eyear + 1900;
		
		if (pmonth != emonth) {
			monthStr = strMonth(emonth)
			events += "<p class=\"calMonthTitle\">"+monthStr+" "+eyear+"</p>";
			pmonth = emonth;
		}
		for (e = 0; e < eventList.length; e++) {
			if (eventList[e].year == eyear && eventList[e].month == emonth && eventList[e].day == eday) {
				events += "<p>"+monthStr+" "+eventList[e].day+" - "+eventList[e].description+"</p>";
			}
		}
		start.setTime(start.getTime()+oneDay);
	}
	return events;
}
function strMonth(emonth) {
	if ( emonth == 1 )
		return "January";
	else if ( emonth == 2 )
			return "Feburary";
	else if ( emonth == 3 )
			return "March";
	else if ( emonth == 4 )
			return "April";
	else if ( emonth == 5 )
			return "May";
	else if ( emonth == 6 )
			return "June";
	else if ( emonth == 7 )
			return "July";
	else if ( emonth == 8 )
			return "August";
	else if ( emonth == 9 )
			return "September";
	else if ( emonth == 10 )
			return "October";
	else if ( emonth == 11 )
			return "November";
	else if ( emonth == 12 )
			return "December";
}
function getEventCell(eventDate) {
	//bgcolor = "SkyBlue" or "White" or "Yellow";
	var bgcolor = "";
	var dayevents = "";
	var emonth = eventDate.getMonth()+1;
	var eday = eventDate.getDate();
	var eyear = eventDate.getYear();
	if (eyear < 200) eyear = eyear + 1900;
	
	// search the eventList
	for (e = 0; e < eventList.length; e++) {
		if (eventList[e].year == eyear && eventList[e].month == emonth && eventList[e].day == eday) {
			dayevents += "<li>"+eventList[e].description+"</li><br>";
			if (bgcolor == "")
				bgcolor = eventList[e].bgcolor;
		}
	}
	var cell = "<td width=\""+dayWidth+"\" style=\"background-color:"+bgcolor+";\" class=\"calCell\" valign=\"top\"><table cellspacing=\"0\" cellpadding=\"2\" width=\"100%\"><tr><td width=\"1\" class=\"calDate\" valign=\"top\">"+eday+"</td><td>";
	cell += dayevents;
	cell += "</td></tr></table></td>";
	return cell;
}
function event (year, month, day, bgcolor, description) {
	index+=1;
	this.month = month;
	this.day = day;
	this.year = year;
	this.bgcolor = bgcolor;
	this.description = description;
}
function getObject(objId) {
	if (ie4) {
		return document.all[objId]; 
	} else {
		return document.getElementById(objId);
	}
}

