/**********************************************************
    Created By: Edward Narkiewicz  1999               
		    ednark@wm.edu                         
	                                                  
 	Created for: The Gay Student Union                
                   @ The College of William and Mary    
                                                       
 This code enables a user to create separate calendar   
 objects each with separate lists of events. These can  
 be displayed separately in the space of the user's     
 choosing. Each calendar object either links internally
 to the events of that day ro opens a popup window
 Events are created in an EventList object specific to
 that month. The Events are displayed in a standard table
 in a place of the user's choosing, or displayed in a 
 popup window in standard tabke form. User may choose colors                              
                                                        
 a lot of code but made to be very flexible to the user 
 ************************************************************/

/*******************************
	COLORS
	#RRGGBB format please
 ******************************/
	var clrmain       = "#762D2D"  // Background color for Main Date
	var clrdays       = "#0A5689"  // Background color for Days of the Week
	var clrpopup      = "#CCCCCC"  // Background color for Allevents popup
	var clrdayspassed = "#0A5689"  // Background color for days already passed
	var clrdaysahead  = "#0A5689"  // Background color for days ahead and current
	var clrempty      = "#0A5689"  // Background color for days not in the month
	var tdcolor		= "#444444"
/**********************************************************
 Makes arrays of the days of the week                   
 and the months of the year (including # days in month) 
 **********************************************************/
var days = new Array('Sun','Mon','Tue','Wed','Thr','Fri','Sat')
	
/* months stores from 0-11: jan=0 dec=11 */
var months = new Array(['January',31],['Febuary',28],['March',31],['April',30],['May',31],['June',31],['July',31],['August',31],['September',30],['October',31],['November',30],['December',31])


/**********************************************************
 Collects the current date from the user's computer     
 **********************************************************/
var now = new Date()

/**********************************************************
  CalenderMonth()                                       
                                                        
  Makes new Calendar Object with optional title argument 
  contains the title, day of the week, month,           
  day of the month, year, and monthly events           
 **********************************************************/
function CalenderMonth(){
	this.dayofweek = now.getDay()
	this.month = now.getMonth()
	this.dayofmonth = now.getDate()
	this.year = now.getYear()

	/* makes leap year and y2k correction.. */
	if( this.year%4==0 )
		months[1][1]=29
	/* if the host comp gives 2 digits, make correction */
	/* leaves bug for now<1990 and now>2090 */
	if( this.year < 1000 ) 
	if( this.year > 90 )
		this.year += 1900
	else 
		this.year += 2000

	/* extrapolates from the current dayofweek, the dayofweek */
        /* of the beginning of the month                          */
	this.firstofmonth = this.dayofweek-this.dayofmonth%7+1
		if( this.firstofmonth<0 )
			this.firstofmonth = this.firstofmonth + 7

	/* loads this.events with EventList object... defined later */
	this.events = new EventList(this.month,this.year)
}// CALENDER CLASS


/**********************************************************
 calender.lastmonth()                                   
                                                        
  takes calender object and moves info back one month   
 **********************************************************/
CalenderMonth.prototype.lastmonth = function(){
	this.month--
		if(this.month<0){
			this.month += 12
			this.year--
		}
	this.firstofmonth = this.firstofmonth - months[this.month][1]%7
		if(this.firstofmonth<0)
			this.firstofmonth += 7
	this.events = new EventList(this.month,this.year)
}


/**********************************************************
 calender.nextmonth()                                   
                                                      
  takes calender object and moves info foreward a month 
 **********************************************************/
CalenderMonth.prototype.nextmonth = function(){
	this.firstofmonth = this.firstofmonth + months[this.month][1]%7
		if(this.firstofmonth>6)
			this.firstofmonth -= 7
	this.month++
		if(this.month>11){
			this.month -= 12
			this.year++;
		}
	this.events = new EventList(this.month,this.year)
}


/**********************************************************
 calender.changemonth(x)                                
                                                        
  takes calender object and moves info back or          
  foreward by x months, back if x is negative, foreward 
  if x is not                                           
 **********************************************************/
CalenderMonth.prototype.changemonth = function(x){
	if(x){
		for(var i=0;i<Math.abs(x);i++){
			if(x<0)
				this.lastmonth()
			else
				this.nextmonth()
		}//for
	}//if
}


/**********************************************************
 displayMonth(m,type)                                   
                                                        
 this function displays a passed calendermonth object.  
 by default it will have every day link to a popup      
 window with that days event. User may specify a 'type' 
 if type is set to '1' then each day with at least one  
 event will link internally to an anchor set up by a    
 seperate user call to the showEvents() function.       
 'varname' is the string of the variable name of the    
 calender object is question:                           
 	var a = new CalenderMonth();                      
      displayMonth(a,"a");                              
 this makes and displays a new popup style calender a   
 **********************************************************/
function displayMonth(m,varname,style)
{
	var apos = "'"
	var daycounter = 1
	var when
	var disp

	if(style)
		type = style
	else
		type = 0

	var curr_month = now.getMonth()
	var curr_year = now.getYear()
		/* if the host comp gives <4 digits, make correction */
		if( curr_year < 1000 )
		if(curr_year > 90)
			curr_year += 1900
		else
			curr_year += 2000

	if( (m.year<curr_year) || ((m.year==curr_year)&&(m.month<curr_month)) )
		when = -1;
	if( (m.year==curr_year) && (m.month==curr_month) )
		when = 0;
	if( (m.year>curr_year) || ((m.year==curr_year)&&(m.month>curr_month)) )
		when = 1;

	document.writeln('<TABLE BORDER=0 WIDTH=180>')

	document.writeln('<TR align=center bgcolor='+clrmain+'>')

	document.writeln('<TD colspan=7><font face="arial" size="3"><B>' + months[m.month][0] + ', ' + m.year + '</B></font></TD>')
	document.writeln('</TR>')
	
	/* show the days of the week */

	document.writeln('<TR align=center bgcolor='+clrdays+'>')

	{
		document.writeln('<TD width=10% align=center><B><font color="8E0000" face="arial" size="1"><i>&nbsp;Sun</i></font></B></SMALL></TD>')
		document.writeln('<TD width=10% align=center><B><font color="000000" face="arial" size="1">&nbsp;Mon</font></B></SMALL></TD>')
		document.writeln('<TD width=10% align=center><B><font color="000000" face="arial" size="1">&nbsp;Tue</font></B></SMALL></TD>')
		document.writeln('<TD width=10% align=center><B><font color="000000" face="arial" size="1">&nbsp;Wed</font></B></SMALL></TD>')
		document.writeln('<TD width=10% align=center><B><font color="000000" face="arial" size="1">&nbsp;Thu</font></B></SMALL></TD>')
		document.writeln('<TD width=10% align=center><B><font color="000000" face="arial" size="1">&nbsp;Fri</font></B></SMALL></TD>')
		document.writeln('<TD width=10% align=center><b><font color="000000" face="arial" size="1">&nbsp;Sat</font></B></SMALL></TD>')
	}//for

        /* run 6 iterations.. one for each possible week the month might span */
	for(var wk=1; wk<=6; wk++)
	{
         /* if we have not exceed the number of days in the month continue */
         //if(daycounter<=months[m.month][1])
	 //{
		document.writeln('<TR align=center>')
                /* for each of the seven days of the week... */
		for(dw=0;dw<7;dw++)
		{
                 
		/* if its not yet the first of the month, or if its after the end */
		 if( (wk==1 && dw<m.firstofmonth) || daycounter>months[m.month][1] )
			document.writeln('<TD width=10% bgcolor='+clrempty+'><SMALL>&nbsp;</SMALL></TD>')
		 else
		 {
                        /* if the date being displayed has passed */
			if( daycounter==m.dayofmonth) {bgclr=tdcolor}
			/* if the date being displayed has not passed */
			else {bgclr=clrdaysahead}
                        
			if(m.events.event[daycounter][0])
				if(type==1)
					document.writeln('<TD width=10% bgcolor="' + bgclr + '"><font face="arial" size="1"><A HREF="#' + daycounter + '_' + m.month + '_' + m.year + '">' + daycounter + '</A></SMALL></TD>')
				else
					document.writeln('<TD width=10% bgcolor="' + bgclr + '"><font face="arial" size="1"><A HREF="javascript:popupevent(' + varname + ', ' + daycounter + ')" class=cipan>' + daycounter + '</A></SMALL></TD>')
			      
				else
				if( dw==0 )
				document.writeln('<TD width=10% bgcolor="' + bgclr + '"><font color="8e0000" face="arial" size="1"><i>' + daycounter + '</SMALL></i></font></TD>')
				else		
				document.writeln('<TD width=10% bgcolor="' + bgclr + '"><font face="arial" size="1" color="white">' + daycounter + '</SMALL></font></TD>')

			daycounter++
		 }//ifelse
		}//for
	document.writeln('</TR>')
	 //}//if
	}//for
	document.writeln('</TABLE>')
}//END DISPLAYMONTH



/**********************************************************
 EventList(month,year)                                  
                                                        
  Object to hold a list of events for a specific month  
 and year                                               
 **********************************************************/
function EventList(month,year){

	this.month = month;
	if(year)
		this.year = year
	else { 
		this.year = now.getYear()
		/* if the host comp gives 2 digits, make correction */
		if( this.year < 1000 )
		if( this.year > 90 )
			this.year += 1900
		else 
			this.year += 2000
	}//else

	this.event = new Array(months[this.month][1]+1);

	for(var k=0; k<this.event.length; k++)
		this.event[k] = new Array()
}//EVENTLIST


/*********************************************************
 calender.addEvent(day,time,name,location,description)  
                                                        
  adds a single event to the eventslist of the attached 
  valender object                                       
  ********************************************************/
CalenderMonth.prototype.addEvent = function(day,time,name,location,description){
	this.events.addEvent(day,time,name,location,description)
}


/*********************************************************
 eventlist.addEvent(day,time,name,location,description) 
                                                        
  adds a single event to the list of the attached       
  event object                                          
 *********************************************************/
EventList.prototype.addEvent = function(day,time,name,location,description){
	this.event[day][this.event[day].length] = [name,time,location,description]
}//ADDEVENT


/********************************************************
  showEvents()                                           
                                                        
  writes the events in table format the same width as   
  each months calander is and gives each day an anchor  
  tag of day_month_year                                 
 *********************************************************/
function showEvents(m) {
	document.write('<TABLE BORDER=0 CELLPADDING=2 WIDTH=250>')
	for(var i=0;i<m.events.event.length;i++)
	{
		if(m.events.event[i][0])
		{
			document.write('<TR><TD BGCOLOR="#0A5689"><A NAME="' + i + '_' + m.month + '_' + m.year + '"><B>' + months[m.month][0] + ' ' + i + ', ' + m.year + '</B></A></TD><TD BGCOLOR="'+clrmain+'" ALIGN=CENTER><A HREF="#top"><FONT SIZE=-2>top</FONT></A></TD></TR>')

			for(var j=0;j<m.events.event[i].length;j++) {
				document.write('<TR><TD BGCOLOR="0A5689" COLSPAN=2><B><SMALL>' + m.events.event[i][j][0] + '</SMALL></B></TD></TR>')
				document.write('<TR><TD BGCOLOR="0A5689" COLSPAN=2><SMALL>' + m.events.event[i][j][1] + ', ' +  m.events.event[i][j][2] + '</SMALL></TD></TR>')
				document.write('<TR><TD BGCOLOR="0A5689" COLSPAN=2><SMALL><DD>' + m.events.event[i][j][3] + '</SMALL></TD></TR>')
			}//for

			document.write('<TR><TD BGCOLOR="'+clrempty+'"> &nbsp; </SMALL></TD></TR>')
		}//if
	}//for
	document.write('</TABLE>')
}//SHOWEVENTS

/********************************************************
 popupevent()                                           
                                                        
 	this function displays a single days events in    
  one newly created window. takes a calender object and  
  a day of the month as its parameters                   
 *********************************************************/
var eventopen = 0;

function popupevent(m,i)
{
	if(eventopen==1){eventopen=0;eventWindow.close();popupevent(m,i);return;}
	
	
	eventWindow = window.open("","CalenderPopup","width=300,height=330,scrollbars")
	if(eventWindow)
		eventopen=1


	if(i==0)
		limit=m.events.event.length;
	else
		limit=i+1;

	for(i; i<limit; i++)
	{
	if(m.events.event[i][0])
	{
		eventWindow.document.write('<title>Event(s) on ' + i + ' ' + months[m.month][0] + ' ' + m.year + '</title>')
		eventWindow.document.write('<body link="yellow" vlink="yellow" alink="yellow">')
		eventWindow.document.write('<TABLE ALIGN=CENTER BORDER=0 CELLPADDING=2 CELLSPACING=0 WIDTH=250>')
		eventWindow.document.write('<TR><TD BGCOLOR="'+clrmain+'"><font color="#C2C2C2" face="arial"><B>' + months[m.month][0] + ' ' + i + ', ' + m.year+ '</B></font></TD></TR>')

		for(var j=0;j<m.events.event[i].length;j++)
		{
			eventWindow.document.write('<TR><TD BGCOLOR="#000000"><B><SMALL><font color="yellow" face="arial">' + m.events.event[i][j][0] + '</SMALL></FONT></B></TD></TR>')
			eventWindow.document.write('<TR><TD BGCOLOR="#0A5689"><b><SMALL><font color="white" face="arial">' + m.events.event[i][j][1] + ' ' +  m.events.event[i][j][2] + '</SMALL><bb></TD></TR>')
			eventWindow.document.write('<TR><TD BGCOLOR="#0A5689"><b><SMALL><font face="arial" color="black">' + m.events.event[i][j][3] + '</SMALL><b></TD></TR>')
		}//for

		eventWindow.document.writeln('</TABLE>')
	}//if
	}//for
	
	eventWindow.document.writeln('<center><form><input type="button" value="Close" onClick="self.opener.eventopen=0;self.close();"></form></center>')

	eventWindow.focus();
	return;
}


/*********************************************************
 this blurb of code sets up the CalMonth array
	which contains all the calender objects
 ********************************************************/
var CalMonth = new Array()

var this_month = now.getMonth()
var this_month_year = now.getYear()
	/* if the host comp gives < 4 digits, make correction */
	if(this_month_year < 1000 )
		if(this_month_year > 90 )
			this_month_year += 1900
		else 
			this_month_year += 2000

/* Create Calender years for l999 through two years from now */
for(var yr=1999; yr<=this_month_year+1; yr++)
{
	CalMonth[yr] = new Array()
		for(var i=0; i<12; i++)
		{
			CalMonth[yr][i] = new CalenderMonth()
			CalMonth[yr][i].changemonth( i-this_month+(12*(yr-this_month_year)) )
		}//for
}

/*********************************************************
	incrementgrabmonth()
 takes a two item array (arr[1] = year) (arr[0] = month)
	increments the month by one and if it spills into
	the next year it incrments the year and sets the
	month back to 0(january)
 ********************************************************/
function incrementgrabmonth(arr,offset)
{
	if(offset>0)
		for(var i=0; i<offset; i++)
		{
			arr[0] += 1
			if( arr[0]>11 )
			{
				arr[0] -= 12
				arr[1] += 1
			}//if
		}//FOR
	if(offset<0)
		for(var i=0; i>offset; i--)
		{
			arr[0] -= 1
			if(arr[0]<0)
			{
				arr[0] += 12
				arr[1] -=1
			}//if
		}//for
}//INCREMENTGRABMONTH

/*********************************************************
	grabmonth()
 takes the year and the month and grabs that calender month 
	from the calmonth[] array. If only one item is given
	it assumes it is an offset from the current month.
	It returns the object CalMonth[][]

	grabmonth(2000,0) = CalMonth[2000][0] = January, 2000
	grabmonth(0) = CalMonth[current year][current month] = this month
	grabmonth(5) = 5 months ahead
	grabmonth(-5) = 5 month ago
 ********************************************************/
function grabmonth(yr, mnth)
{
	if(mnth>=0)
		return CalMonth[yr][mnth]
	var temp_month	
	var temp_arr = new Array()
		temp_arr[0] = this_month
		temp_arr[1] = this_month_year
	incrementgrabmonth(temp_arr,yr)
		var temp_month = temp_arr[0]
		var temp_year = temp_arr[1]
	return CalMonth[temp_year][temp_month]
}//GRABMONTH