/*******************************************************
CALENDAR 1.2
Last Modified September 16, 2003
Use either the XML or JavaScript versions to load event data.

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" cellspacing=0>');
        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 && !whatYear)
        {
                document.getElementById((thisDate+thisBegin)+'_'+calQTY).className='highlight';
                document.getElementById(allDay[thisDay]+calQTY).className='highlight';
        }
}


/*******************************************************
READ AND ASSOCIATE CALENDAR DATA WITH JAVASCRIPT (OPTIONAL)
This version of the script uses JavaScript objects to load the information.  
Because it's based on almost the same code as the XML version. I didn't 
change any variable names when I created this JavaScript version.  
Although the XML version is cooler, this version is more compatible with 
older and non-standard browsers.
*******************************************************/
// 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='';

                // MATCH MONTH+YEAR
                for (n=1; n<=parseInt(calQTY); n++)
                {
                        // Handle year-rollover when displaying multiple months.
                        if (xCalMonth+n > 12)
                        {
                                xCalMonth=xCalMonth-12;
                                xCalYear=xCalYear+1;
                        }

                        // For monthly events
                        if (xmlEvtMM == 'All') { xmlEvtMM=xCalMonth+n; tempMM='All'; }

                        // Month and Year must match.
                        if (xmlEvtMM == xCalMonth+n && xmlEvtYY == xCalYear)
                        {
                                // DIG FOR DAY
                                var xFirst=new Date(xCalYear, (xCalMonth+(n-1)), 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".
                                *******************************************************/

                                evtString='<a href="'+xmlEvtURL+'" class="event">'+xmlEvtVal+'</a>';

                                /*******************************************************
                                 Do not edit past this point.
                                *******************************************************/
                                
                                document.getElementById((xmlEvtDD+xFirst.getDay())+'_'+n).innerHTML+='<br />'+evtString;
                        }

                        if (tempMM == 'All') xmlEvtMM='All';
                }
        }
}


/*******************************************************
READ AND ASSOCIATE CALENDAR DATA WITH XML (OPTIONAL)
Use calFill() to load the information that contains the events.  The events 
will automatically populate the calendar.  Unfortunately, due to lack of 
widespread XML support, browsers supporting this method are limited 
to Internet Explorer 6.0+ and Gecko 1.0+
*******************************************************/
// CALLED BY CALFILL()
function xmlRead()
{
        // YEARS
        xmlEvtArr = xml_doc.getElementsByTagName('EVENT');
        xmlEvtArrLen=xmlEvtArr.length-1;
        for (i=0; i<=xmlEvtArrLen; i++)
        {
                var xCal=new Date();
                xCalMonth=parseInt(xCal.getMonth());
                xCalYear=parseInt(xCal.getFullYear());

                xmlEvtYY=(xmlEvtArr[i].getAttribute('year')) ? parseInt(xmlEvtArr[i].getAttribute('year')):xCalYear;
                xmlEvtMM=(xmlEvtArr[i].getAttribute('month')) ? parseInt(xmlEvtArr[i].getAttribute('month')):'All';
                xmlEvtDD=(xmlEvtArr[i].getAttribute('date')) ? parseInt(xmlEvtArr[i].getAttribute('date')):null;
                xmlEvtURL=xmlEvtArr[i].getAttribute('url');
                xmlEvtVal=xmlEvtArr[i].firstChild.data;
                tempMM='';

                // MATCH MONTH+YEAR
                for (n=1; n<=parseInt(calQTY); n++)
                {
                        // Handle year-rollover when displaying multiple months.
                        if (xCalMonth+n > 12)
                        {
                                xCalMonth=xCalMonth-12;
                                xCalYear=xCalYear+1;
                        }

                        // For monthly events
                        if (xmlEvtMM == 'All') { xmlEvtMM=xCalMonth+n; tempMM='All'; }

                        // Month and Year must match.
                        if (xmlEvtMM == xCalMonth+n && xmlEvtYY == xCalYear)
                        {
                                // DIG FOR DAY
                                var xFirst=new Date(xCalYear, (xCalMonth+(n-1)), 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".
                                *******************************************************/

                                evtString='<a href="'+xmlEvtURL+'" class="event">'+xmlEvtVal+'</a>';

                                /*******************************************************
                                 Do not edit past this point.
                                *******************************************************/
                                
                                document.getElementById((xmlEvtDD+xFirst.getDay())+'_'+n).innerHTML+='<br />'+evtString;
                        }

                        if (tempMM == 'All') xmlEvtMM='All';
                }
        }
}

// FUNCTION TO CALL ON WEBPAGE
function calFill(zFileName)
{
        var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
        var ie = (typeof window.ActiveXObject != 'undefined');

        if (moz)
        {
                xml_doc = document.implementation.createDocument("", "", null);
                xml_doc.onload = xmlRead;
        }

        else if (ie)
        {
                xml_doc = new ActiveXObject("Microsoft.XMLDOM");
                xml_doc.onreadystatechange = function()
                {
                        if (xml_doc.readyState == 4) setTimeout(xmlRead,0);
                }
        }

        xml_doc.load(zFileName);
}
