/*******************************************************
CALENDAR 1.2
Last Modified September 16, 2003

All code by Ryan Parman, unless otherwise noted.
(c) 1997-2003, Ryan Parman
http://www.skyzyx.com
Distributed according to SkyGPL 2.1, http://www.skyzyx.com/license/
*******************************************************/


/*******************************************************
DRAW AND POPULATE THE CALENDAR
Use calendar() to draw the current month.  Use calendar(1) to draw 
January of the current year.  Use calendar(1, 2003) to draw January 
of 2003.  For the next month, use calendar(dateObj.getMonth()+2).
*******************************************************/
var calQTY=0;

function calendar(whatMonth, whatYear)
{
	// INITIALIZATION!  DO NOT EDIT!
	calQTY++;
	var allDay = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
	var allMonth = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	var allMonthDays= new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var cal=new Date();
	var thisDay=cal.getDay();
	var thisDate=cal.getDate();

	// HANDLE MONTH FOR ROLLOVER (FOR MULTIPLE MONTHS)
	if (!whatMonth) thisMonth=cal.getMonth();
	else
	{
		if (whatMonth > 12)
		{
			thisMonth=whatMonth-13;
			whatYear=cal.getFullYear()+1;
		}
		else thisMonth=whatMonth-1;
	}

	var thisYear=(!whatYear) ? cal.getFullYear():whatYear;
	var theFirst=new Date(thisYear, thisMonth, 1);
	if (((thisYear%4 == 0) && !(thisYear%100 == 0)) || (thisYear%400 == 0)) allMonthDays[1] = 29;

	ct1=0;

	// DRAW THE CALENDAR (XHTML-COMPLIANT)
	document.write('<table class="calendar">');
	document.write('<tr>');
	document.write('<td colspan="7" class="month"><div align="center" id="MMYY'+calQTY+'">&nbsp;</div></td>');
	document.write('</tr>');

	// FILL DAYS
	document.write('<tr>');
	for (w=0; w<=6; w++)
	{
		document.write('<td class="daysofweek" id="'+allDay[w]+calQTY+'"><div align="center">'+allDay[w]+'</div></td>');
	}
	document.write('</tr>');

	// HOW MANY ROWS?
	if (theFirst.getDay() == 6 && allMonthDays[thisMonth] >29) weeks=6;
	else if (theFirst.getDay() >= 5 && allMonthDays[thisMonth] >30) weeks=6;
	else if (theFirst.getDay() == 0 && allMonthDays[thisMonth] < 29) weeks=4;
	else weeks=5;

	// DRAW THE "DAY" CELLS
	for (x=1; x<=weeks; x++)
	{
		document.write('<tr>');

		for (y=1; y<=7; y++)
		{
			ct2=(ct1*7)+y;
			document.write('<td class="empty" id="'+ct2+'_'+calQTY+'" valign="top" align="right">&nbsp;</td>');
		}
		document.write('</tr>');
		ct1++;
	}
	document.write('</tr>');
	document.write('</table>');

	// MONTH AND YEAR
	document.getElementById('MMYY'+calQTY).innerHTML=allMonth[thisMonth]+' '+thisYear;

	// FILL NUMBERS IN CELLS AND CHANGE CLASSNAME TO "DATE"
	thisBegin=theFirst.getDay();
	for (z=1; z<=allMonthDays[thisMonth]; z++)
	{
		// Date Numbers
		document.getElementById(thisBegin+z+'_'+calQTY).innerHTML=z;
		document.getElementById(thisBegin+z+'_'+calQTY).className='date';
	}

	// HIGHLIGHT CURRENT DAY AND DATE
    if ((!whatMonth) || whatMonth -1 == cal.getMonth())
	{
		document.getElementById((thisDate+thisBegin)+'_'+calQTY).className='highlight';
		document.getElementById(allDay[thisDay]+calQTY).className='highlight';
	}
  return thisMonth;
}


/*******************************************************
READ AND ASSOCIATE CALENDAR DATA WITH JAVASCRIPT (OPTIONAL)
This script uses JavaScript objects to load the information.  
*******************************************************/
// CREATES EVENT OBJECT
function event(evYear, evMonth, evDate, evLabel, evURL)
{
	// SET UP VARIABLES
	this.year=(evYear) ? evYear:null;
	this.month=(evMonth) ? evMonth:null;
	this.date=(evDate) ? evDate:null;
	this.label=(evLabel) ? evLabel:'Event';
	this.url=(evURL) ? evURL:'#';

	this.add=function()
	{
		var xCal=new Date();
		xCalMonth=parseInt(xCal.getMonth());
		xCalYear=parseInt(xCal.getFullYear());

		xmlEvtYY=(this.year) ? parseInt(this.year):xCalYear;
		xmlEvtMM=(this.month) ? parseInt(this.month):'All';
		xmlEvtDD=(this.date) ? parseInt(this.date):null;
		xmlEvtURL=this.url;
		xmlEvtVal=this.label;
		tempMM='';

        if (this.month < 11 && evYear != xCalYear)  //Note this is to allow for year roll over 
		   xCalYear = xCalYear + 1;
			// Month and Year must match.
			if (xmlEvtMM == this_Mnth+1 && xmlEvtYY == xCalYear)
			{
				// DIG FOR DAY
				var xFirst=new Date(xCalYear, (this_Mnth), 1);
				xFirstDay=xFirst.getDay();
			
				/*******************************************************
				This is the only place where you can edit.  This is what is written into 
				the calendar cell.  Use xmlEvtURL for the event's URL, and xmlEvtVal 
				for the event's name/label.  Change the CSS settings in "a.event" to 
				modify text settings.  Edit "a.event:hover" to edit hover settings.  
				Hover-property CSS settings are inherited from "a.event".
				*******************************************************/
                if (evURL == "X") //If you don't want a link place an X in the URL of the event data
 				  evtString='<span class="event">'+xmlEvtVal+' </span>';
                else
				  evtString='<a href="'+xmlEvtURL+'" class="event">'+xmlEvtVal+'</a>';

				/*******************************************************
				 Do not edit past this point.
				*******************************************************/
				n = 1;
				document.getElementById((xmlEvtDD+xFirst.getDay())+'_'+n).innerHTML+='<br />'+evtString;
			}

			if (tempMM == 'All') xmlEvtMM='All';
	}
}
