/*
                            c a l e n d a r . j s

        This defines the Calendar object, its properties and methods. 
        The Event object, defined in event.js, interacts with the calendar object:
        each day in a calendar becomes a container for any number of event objects.
       
        The calconfig.js member configures certain installation values for the calendar. 
*/
var calObjDate = new Date();
var SYSDATE    = new Date();
var t_events = new Array;			// array to hold events for each day
function Calendar ()
{ 
//***  calendar properties     ***
	this.month = 0;                  	//month number
	this.monthName ="";
	this.today = 1;                        	//today (manipulated)  
	this.year = 2003;               	//full year 
	this.startday = 0;              	//day of week month starts 
	this.days = 0;                	 	//number of days in month
	this.columns;				//number of columns in this type of month (2 day, 5 day or 7 day)	  
	this.html = '';				

//***  calendar methods***
	this.build = buildCalendar;
	this.lastMonth = setLastMonth;
	this.nextMonth = setNextMonth;
	this.getMonthName = getMonthName;
	this.getDaysInMonth = getDaysInMonth;
	this.displayIn = showCalendar;
	this.setCalendarType = setCalendarType;
	this.isCalendarDay = isCalendarDay;
}


function buildCalendar()
{
	var t_array = new Array();


	this.today = calObjDate.getDate(); 	// init to today - this property is manipulated.
	this.month = calObjDate.getMonth();   	// returns a value 0=January,1=February,..11=December
	this.year =  calObjDate.getFullYear();

	calObjDate.setDate(1);                  	// set day to 1
	this.startday = calObjDate.getDay();    	// what day does the 1st of the month start on? 0=sun 1=mon.. 6=sat   

	this.getDaysInMonth();
	this.getMonthName();



	var calDay = 1;			// days printed on calendar
	var cellNo = 0;			// cell numbers in the 6 x 7 matrix

	this.html =  '<table class=calendar>';
	this.setCalendarType();




/* ************************************************************************************************************  
   This is the nested loop - outter loop of 6 for total number of possible rows. Inner loop of 7 for days in week.
    cellNo holds 0 - 42.  calDay goes from 1 to daysInMonth  Each cellNo will load an event into eventarray  
   ************************************************************************************************************ */ 


 	for (WeekNo=0; WeekNo<6; WeekNo++)
	{
		this.html += '<tr>';
		for (DayofWeek=0; DayofWeek<7; DayofWeek++)
		{
			if (cellNo >= this.startday && calDay <= this.days)
			{
				if (isCalendarDay(DayofWeek)==1)
				{	
					var v_repeat = setRepeat(WeekNo, this.startday, DayofWeek);     // set variable to find repeatable events
					t_events = getDayEvents(this.month,calDay,this.year,v_repeat);  // find all programmed events
	                        	var v_cstyle = setStyle(calDay);  				// get the font, background etc for this cell 
					this.html+= buildCell(calDay, v_cstyle);
				}

				calDay++;
			}
			else    
				if (isCalendarDay(DayofWeek)==1)
					this.html += '<td> &nbsp;</td>';

			cellNo++;
		}
		this.html += '</tr>';
	}
	this.html += '</table>';
	calObjDate.setDate(1);  //set the date back to 1 
};


// ################ showCalendar works at least for MIE5 and NS6 #######################

function showCalendar(DivID) 
{
	if (document.layers)     // old Netscape 4.xx
	{
		document.layers[DivID].document.write(this.html);
		document.layers[DivID].document.close();
	} else	{
		var refNode = document.getElementById(DivID);
		if (document.all)   //MSIE
		{
			refNode.innerHTML = this.html;
		}
		else  // NS6+ 
		{	
			var range = document.createRange();
			range.selectNodeContents(refNode);
			range.deleteContents();
			var cfrag = range.createContextualFragment(this.html);
			refNode.appendChild(cfrag);
		};
	};
};



// **** this method gets the number of days in the month. ****
function getDaysInMonth() 
{
        var MonthDaysArray = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); 
	if ((this.month==1) && (this.year%4==0)) 
	{
		MonthDaysArray[1] = 29;
		if ((this.year%100==0) && (this.year%400!=0)) 		
        		MonthDaysArray[1] = 28;
	};
	this.days = MonthDaysArray[this.month];
};

// **** set calendar to last month ****
function setLastMonth() 
{
	calObjDate.setMonth(this.month-1);
	this.build();	
}

// **** set calendar to next month. ****
function setNextMonth() 
{
	calObjDate.setMonth(this.month+1);
	this.build();	
}


// **** number of columns in calendar based on type of calendar **** 
function setCalendarType()
{
	if (CALENDARTYPE == 'full')
	{
		this.html += '<tr class=headrow><td colspan=7 class=head>'+this.monthName+' '+this.year+'</td></tr>';
		this.html += '<tr class=headrow><td class=head>Sun</td><td class=head>Mon</td>'+
				'<td class=head>Tues</td><td class=head>Wed</td><td class=head>Thur</td>'+
				'<td class=head>Fri</td><td class=head>Sat</td></tr>';
	}

	if (CALENDARTYPE == 'weekday')
	{
		this.html += '<tr class=headrow><td colspan=5 class=head>'+this.monthName+' '+this.year+'</td></tr>';
		this.html += '<tr class=headrow><td class=head>Mon</td><td class=head>Tues</td><td class=head>Wed</td><td class=head>Thur</td><td class=head>Fri</td></tr>';
	}

	if (CALENDARTYPE == 'weekend')
	{
		this.html += '<tr class=headrow><td colspan=2 class=head>'+this.monthName+' '+this.year+'</td></tr>';
		this.html += '<tr class=headrow><td class=head>Sun</td><td class=head>Sat</td></tr>';
	}
}

// **** determines whether this day should be on the full, weekday or weekend calendar **** 
function isCalendarDay(dayOfWeek)
{
	var v_test = 0;

	if (CALENDARTYPE == 'full')
	{
		v_test = 1;
	}

	if (CALENDARTYPE == 'weekday')
	{
		if (dayOfWeek != 0 && dayOfWeek != 6)
		   v_test = 1;
	}

	if (CALENDARTYPE == 'weekend')
	{
		if (dayOfWeek == 0 || dayOfWeek == 6)
		   v_test = 1;

	}

	return v_test;
}



// **** this function gets the number of days in the month. ****
function getMonthName() 
{
        var MonthArray = new Array( 'January', 'February', 'March', 'April', 'May', 'June', 
		'July', 'August', 'September', 'October', 'November', 'December'); 
	this.monthName = MonthArray[this.month];
};




/* **** this function sets the Event.repeat property to something like '1st Monday' ****
        when reading events into the calendar, if there is an event for 'Monday' or '1st Monday', 
        the event descriptions will be copied into this date                           */

function setRepeat(WeekNo, startDay, DayofWeek)
{
	var weekday = getDay(DayofWeek);
	var weekno  = getWeekNo(WeekNo, startDay, DayofWeek);
	var t_repeat  = weekno+' '+weekday;
	return t_repeat;
};


function getDay(DayofWeek)
{
	var tArray = new Array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	return tArray[DayofWeek];

};

/* ******
	Get the week number for the eventday we are filling in.
	 e.g. if the month startDay is Wednesday(3) and we are filling in Monday(1) subtract a date
   ***** */
function getWeekNo(WeekNo, startDay, DayofWeek)
{
	var tArray = new Array( '1st', '2nd', '3rd', '4th', '5th', '6th');
	var t_no = (DayofWeek < startDay ) ? WeekNo-1 : WeekNo;
	return tArray[t_no];
};



/***   -------here we set the style for each cell ---------------     ***/
function setStyle(calDay)
{
var t_style = "";
if (t_events.length !=0)
   t_style = t_events[0].cstyle;

calObjDate.setDate(calDay);  			//set the date to the day being filled in 

if (SHOWPASTSTYLESPAST != 'yes' && calObjDate<SYSDATE)
   		t_style = 'oldday'; 	

if (calObjDate.valueOf()==SYSDATE.valueOf())    // note that straight compare does not work with dates
   t_style = 'tday';     			

if (calObjDate>SYSDATE && t_style == "")
   t_style = 'futday';
return t_style;
}

/***   ------- build each cell ---------------     ***/
function buildCell(calDay, v_cstyle)
{
	var t_html = '<td valign=top class='+v_cstyle+'><h5>'+calDay+'</h5>';
	for (e=0; e < t_events.length && e <= MAXEVENTS; e++)
	{
		if (t_events[e].ldesc !="")
		{
			t_html+='<a href="javascript:void(0);" '
			t_html+='onmouseover="return overlib(&#34;'+t_events[e].ldesc+'&#34;,HAUTO, VAUTO, STICKY, MOUSEOFF);" ';
			t_html+=' onmouseout="return nd();">';
                	t_html+= t_events[e].sdesc+'</a><br><br>';
		}
		else
                	t_html+= t_events[e].sdesc+'<br><br>';
	}
	t_html+='</td>';
	return t_html;
}

