/*
		Calendar Program
		author: Alex Lessard <alex@axondm.com>
		date: 12/30/08
		
		This calendar takes events and displays them in a month view. 
		Event types include Single Day events, All Day events, and span events.
		Each event shows a mouseover with the event title and brief summary
		Clicking on events generate a container that pulls in the event data with an ajax call.
		You can navigate months by the previous and next arrows as well as a dropdown by clicking
		on the month name.

*/

function stripslashes( str ) {

    return (str+'').replace('/\0/g', '0').replace('/\(.)/g', '$1');

}

function date ( format, timestamp ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
    // +      parts by: Peter-Paul Koch (http://www.quirksmode.org/js/beat.html)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: MeEtc (http://yass.meetcweb.com)
    // +   improved by: Brad Touesnard
    // +   improved by: Tim Wiel
    // *     example 1: date('H:m:s \\m \\i\\s \\m\\o\\n\\t\\h', 1062402400);
    // *     returns 1: '09:09:40 m is month'
    // *     example 2: date('F j, Y, g:i a', 1062462400);
    // *     returns 2: 'September 2, 2003, 2:26 am'
 	
	//3-09-2010 - Scott Harwell - Added 25200000 miliseconds to the equation since the dates parsed are in GMT but the dates captured are in EST.
    var a, jsdate=((timestamp) ? new Date(timestamp*1000+25200000) : new Date());
    var pad = function(n, c){
        if( (n = n + "").length < c ) {
            return new Array(++c - n.length).join("0") + n;
        } else {
            return n;
        }
    };
    var txt_weekdays = ["Sunday","Monday","Tuesday","Wednesday",
        "Thursday","Friday","Saturday"];
    var txt_ordin = {1:"st",2:"nd",3:"rd",21:"st",22:"nd",23:"rd",31:"st"};
    var txt_months =  ["", "January", "February", "March", "April",
        "May", "June", "July", "August", "September", "October", "November",
        "December"];
 
    var f = {
        // Day
            d: function(){
                return pad(f.j(), 2);
            },
            D: function(){
                t = f.l(); return t.substr(0,3);
            },
            j: function(){
                return jsdate.getDate();
            },
            l: function(){
                return txt_weekdays[f.w()];
            },
            N: function(){
                return f.w() + 1;
            },
            S: function(){
                return txt_ordin[f.j()] ? txt_ordin[f.j()] : 'th';
            },
            w: function(){
                return jsdate.getDay();
            },
            z: function(){
                return (jsdate - new Date(jsdate.getFullYear() + "/1/1")) / 864e5 >> 0;
            },
 
        // Week
            W: function(){
                var a = f.z(), b = 364 + f.L() - a;
                var nd2, nd = (new Date(jsdate.getFullYear() + "/1/1").getDay() || 7) - 1;
 
                if(b <= 2 && ((jsdate.getDay() || 7) - 1) <= 2 - b){
                    return 1;
                } else{
 
                    if(a <= 2 && nd >= 4 && a >= (6 - nd)){
                        nd2 = new Date(jsdate.getFullYear() - 1 + "/12/31");
                        return date("W", Math.round(nd2.getTime()/1000));
                    } else{
                        return (1 + (nd <= 3 ? ((a + nd) / 7) : (a - (7 - nd)) / 7) >> 0);
                    }
                }
            },
 
        // Month
            F: function(){
                return txt_months[f.n()];
            },
            m: function(){
                return pad(f.n(), 2);
            },
            M: function(){
                t = f.F(); return t.substr(0,3);
            },
            n: function(){
                return jsdate.getMonth() + 1;
            },
            t: function(){
                var n;
                if( (n = jsdate.getMonth() + 1) == 2 ){
                    return 28 + f.L();
                } else{
                    if( n & 1 && n < 8 || !(n & 1) && n > 7 ){
                        return 31;
                    } else{
                        return 30;
                    }
                }
            },
 
        // Year
            L: function(){
                var y = f.Y();
                return (!(y & 3) && (y % 1e2 || !(y % 4e2))) ? 1 : 0;
            },
            //o not supported yet
            Y: function(){
                return jsdate.getFullYear();
            },
            y: function(){
                return (jsdate.getFullYear() + "").slice(2);
            },
 
        // Time
            a: function(){
                return jsdate.getHours() > 11 ? "pm" : "am";
            },
            A: function(){
                return f.a().toUpperCase();
            },
            B: function(){
                // peter paul koch:
                var off = (jsdate.getTimezoneOffset() + 60)*60;
                var theSeconds = (jsdate.getHours() * 3600) +
                                 (jsdate.getMinutes() * 60) +
                                  jsdate.getSeconds() + off;
                var beat = Math.floor(theSeconds/86.4);
                if (beat > 1000) beat -= 1000;
                if (beat < 0) beat += 1000;
                if ((String(beat)).length == 1) beat = "00"+beat;
                if ((String(beat)).length == 2) beat = "0"+beat;
                return beat;
            },
            g: function(){
                return jsdate.getHours() % 12 || 12;
            },
            G: function(){
                return jsdate.getHours();
            },
            h: function(){
                return pad(f.g(), 2);
            },
            H: function(){
                return pad(jsdate.getHours(), 2);
            },
            i: function(){
                return pad(jsdate.getMinutes(), 2);
            },
            s: function(){
                return pad(jsdate.getSeconds(), 2);
            },
            //u not supported yet
 
        // Timezone
            //e not supported yet
            //I not supported yet
            O: function(){
               var t = pad(Math.abs(jsdate.getTimezoneOffset()/60*100), 4);
               if (jsdate.getTimezoneOffset() > 0) t = "-" + t; else t = "+" + t;
               return t;
            },
            P: function(){
                var O = f.O();
                return (O.substr(0, 3) + ":" + O.substr(3, 2));
            },
            //T not supported yet
            //Z not supported yet
 
        // Full Date/Time
            c: function(){
                return f.Y() + "-" + f.m() + "-" + f.d() + "T" + f.h() + ":" + f.i() + ":" + f.s() + f.P();
            },
            //r not supported yet
            U: function(){
                return Math.round(jsdate.getTime()/1000);
            }
    };
 
    return format.replace(/[\\]?([a-zA-Z])/g, function(t, s){
        if( t!=s ){
            // escaped
            ret = s;
        } else if( f[s] ){
            // a date function exists
            ret = f[s]();
        } else{
            // nothing special
            ret = s;
        }
 
        return ret;
    });
}

	function setImgSize(obj)
	//obj: this is the img element that you want to resize
	//POST: Will create a sized thumbnail to fix in a 200x200 box and then adjust padding so it's centered vertically and horizontally
	{
		if(obj.width > 200)
		{
			obj.width=200;
		}
		else if(obj.width<200)
		{
			var test = ((190-obj.height)/2)-10;
			obj.style.marginTop=test+'px';
		}
		if(obj.height>175)
		{
			var width=(obj.width * 175)/obj.height;
			obj.height=175;
			obj.width=width;
		}
		else
		{
			var test = (200-obj.height)/2;
			obj.style.paddingTop=(190-obj.height)/2;
		}
	}
	

	function removeElement(idNum) {
	//POST: Simple destory element function based on element ID
		var element = document.getElementById(idNum);
		element.parentNode.removeChild(element);
	}

function TimezoneDetect(){
    var dtDate = new Date('1/1/' + (new Date()).getUTCFullYear());
    var intOffset = 100; //set initial offset high so it is adjusted on the first attempt
    var intMonth;
    var intHoursUtc;
    var intHours;
    var intDaysMultiplyBy;
   
    //go through each month to find the lowest offset to account for DST
    for (intMonth=0;intMonth < 12;intMonth++){
        //go to the next month
        dtDate.setUTCMonth(dtDate.getUTCMonth() + 1);
       
        //To ignore daylight saving time look for the lowest offset.
        //Since, during DST, the clock moves forward, it'll be a bigger number.
        if (intOffset > (dtDate.getTimezoneOffset() * (-1))){
            intOffset = (dtDate.getTimezoneOffset() * (-1));
        }
    }
 
    return intOffset;
}

	function mktime() {
		// http://kevin.vanzonneveld.net
		// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   improved by: baris ozdil
		// +      input by: gabriel paderni 
		// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   improved by: FGFEmperor
		// +      input by: Yannoo
		// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +      input by: jakes
		// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// *     example 1: mktime(14, 10, 2, 2, 1, 2008);
		// *     returns 1: 1201871402
		// *     example 2: mktime(0, 0, 0, 0, 1, 2008);
		// *     returns 2: 1196463600
		
		var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
		d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
	 
		var dateManip = {
			0: function(tt){ return d.setHours(tt); },
			1: function(tt){ return d.setMinutes(tt); },
			2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
			3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
			4: function(tt){ return d.setDate(tt+mb); },
			5: function(tt){ return d.setYear(tt+ma); }
		};
		
		for( i = 0; i < argc; i++ ){
			no = parseInt(argv[i]*1);
			if (isNaN(no)) {
				return false;
			} else {
				// arg is number, let's manipulate date object
				if(!dateManip[i](no)){
					// failed
					return false;
				}
			}
		}
	 
		var offset = d.getTimezoneOffset()*-60;

		return Math.floor((d.getTime()/1000)+offset);
	}

	function in_array(needle, haystack, strict) {
		// http://kevin.vanzonneveld.net
		// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
		// *     returns 1: true
	 
		var found = false, key, strict = !!strict;
	 
		for (key in haystack) {
			if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
				found = true;
				break;
			}
		}
	 
		return found;
	}	

	function get_event_info(id)
	//PRE: submit a valid event_id stored in the database.
	//POST: returns event HTML as a string which is applied to a created event container.
	{
		var xmlHttp;
		var type;
		try
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp = new XMLHttpRequest();
			xmlHttp.overrideMimeType('text/xml');					
		}
		catch (e)
		{ // Internet Explorer
			try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
			}
			catch (e)
			{
				try
				{

					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

				}
				catch (e)
				{
					alert("Your browser does not support AJAX!");
				}
			}
		}
		
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4 && xmlHttp.status == 200)
			{
				var event_info = xmlHttp.responseText;
				document.getElementById('event_menu').innerHTML += '<span style="text-decoration:underline;cursor:pointer;position:absolute;right:10px;top:5px;" onclick="removeElement(\'body_cover\');">Close</span><br/>';
				document.getElementById('event_viewer').innerHTML = event_info;
				$("a.fancybox").fancybox({
					'hideOnContentClick': false,
					'frameWidth': 800,
					'frameHeight': 600
				});
			}
		}
		var mybody = document.createElement('div');
		mybody.setAttribute('id', 'body_cover');
		var event_div = document.createElement('div');
		event_div.setAttribute('id', 'event_menu');
		
		var mydiv = document.createElement('div');
		mydiv.setAttribute('id', 'event_viewer');
		mydiv.innerHTML = 'Loading Event<br/><br/><img src="../_img/loading.gif" style="margin-left:auto;margin-right:auto" border="0"/>';		

		event_div.appendChild(mydiv);		
		mybody.appendChild(event_div);

		document.body.appendChild(mybody);		
		xmlHttp.open("GET",http_root+"/event/calendar.php?event_id=" + id,true);
		xmlHttp.send(null);	
	}

	function filter_events(showhide,category_index)
	//PRE: showhide is a boolean. True: show, False: hide. category_index is the array of event IDs to be processed
	//POST: Parses through the array provided and performs the specified action (show or hide)
	{
		eval(all_dates);		//all_dates is a global array generated by display_calendar that holds all the unique IDs of days in the calendar
		if(showhide.checked)
		{
			for (x in cat_events[category_index]) 
			{ 
				if (cat_events[category_index].hasOwnProperty(x)) 
				{ 
					if(cat_events[category_index][x][3] == 0 || cat_events[category_index][x][3] == cat_events[category_index][x][2])
					{
						for (y in dates)
						{
							if (dates.hasOwnProperty(y)) 
							{
								if(dates[y] == cat_events[category_index][x][2])
								{					
									document.getElementById(cat_events[category_index][x][2]+'_'+cat_events[category_index][x][0]+'_'+cat_events[category_index][x][4]).style.visibility='visible';
									document.getElementById('p_'+cat_events[category_index][x][2]+'_'+cat_events[category_index][x][0]+'_'+cat_events[category_index][x][4]).style.display='';
								}
							}
						}
					}
					else
					{
						for (y in dates)
						{
							if (dates.hasOwnProperty(y)) 
							{
								if(dates[y] >= cat_events[category_index][x][2] && dates[y] <= cat_events[category_index][x][3])
								{
									document.getElementById(dates[y]+'_'+cat_events[category_index][x][0]+'_'+cat_events[category_index][x][4]).style.visibility='visible';
									document.getElementById('p_'+dates[y]+'_'+cat_events[category_index][x][0]+'_'+cat_events[category_index][x][4]).style.display='';
								}
							}
						}					
					}
				} 
			}	
		}
		else
		{
			for (x in cat_events[category_index]) 
			{ 
				if (cat_events[category_index].hasOwnProperty(x)) 
				{ 
					if(cat_events[category_index][x][3] == 0 || cat_events[category_index][x][3] == cat_events[category_index][x][2])
					{
						for (y in dates)
						{
							if (dates.hasOwnProperty(y)) 
							{
								if(dates[y] == cat_events[category_index][x][2])
								{					
									document.getElementById(cat_events[category_index][x][2]+'_'+cat_events[category_index][x][0]+'_'+cat_events[category_index][x][4]).style.visibility='hidden';
									document.getElementById('p_'+cat_events[category_index][x][2]+'_'+cat_events[category_index][x][0]+'_'+cat_events[category_index][x][4]).style.display='none';
								}
							}
						}
					}
					else
					{
						for (y in dates)
						{
							if (dates.hasOwnProperty(y)) 
							{

								if(dates[y] >= cat_events[category_index][x][2] && dates[y] <= cat_events[category_index][x][3])
								{
									document.getElementById(dates[y]+'_'+cat_events[category_index][x][0]+'_'+cat_events[category_index][x][4]).style.visibility='hidden';
									document.getElementById('p_'+dates[y]+'_'+cat_events[category_index][x][0]+'_'+cat_events[category_index][x][4]).style.display='none';
								}
							}
						}					
					}
				} 
			}	
		}
	}

	function display_filter_list(dates,events)
	//PRE: dates: all the days in the displayed month. events: string of js to be evals that houses all the events queried.
	//POST: Generates HTML for the div #filter_list already on the page. The filter list contains the categories that events exist in as well as a checkbox that triggers the filter_events function
	{	
		eval(events);
		var xmlHttp;
		var type;
		try
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp = new XMLHttpRequest();
			xmlHttp.overrideMimeType('text/xml');					
		}
		catch (e)
		{ // Internet Explorer
			try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
			}
			catch (e)
			{
				try
				{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

				}
				catch (e)
				{
					alert("Your browser does not support AJAX!");
				}
			}
		}
		
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4 && xmlHttp.status == 200)
			{
				var test = xmlHttp.responseText;
				eval(events);
				eval(test);
				var temp = '';	
				cat_events = new Array();
				for(x = 0 ; x < category.length; x++)
				{	
					cat_events[x] = new Array();
					for(y = 0 ; y < single_events.length; y++)
					{
						if(single_events[y][4] == category[x][1])
						{
							cat_events[x][cat_events[x].length] = single_events[y];
						}
					}
					for(y = 0 ; y < all_events.length; y++)
					{
						if(all_events[y][4] == category[x][1])
						{
							cat_events[x][cat_events[x].length] = all_events[y];
						}
					}
					for(y = 0 ; y < span_events.length; y++)
					{
						if(span_events[y][4] == category[x][1])
						{
							cat_events[x][cat_events[x].length] = span_events[y];
						}
					}	
				}
		
		
				if(category.length > 0)
				{
					document.getElementById('filter_list').innerHTML = '<h3>Categories</h3>';				
					var tempHTML = '';
					for (x in category) 
					{ 
						if (category.hasOwnProperty(x)) 
						{ 
							tempHTML += '<div class="category_item" style="background-color:'+category[x][4]+';color:'+category[x][5]+'"><input type="checkbox" checked="checked" onclick="filter_events(this,\''+x+'\');"/>	'+category[x][2]+'</div>';
						} 
					}
					document.getElementById('filter_list').innerHTML += '<div id="category_items">'+tempHTML+'</div>';
				}
			}
		}
		
		xmlHttp.open("GET",http_root+"/event/event_categories.php",true);
		xmlHttp.send(null);	
	}	
	
	function get_events(month,year)
	//PRE: month: number 0 - 11 that represents the 12 months in the year, year: the year of the events being requested
	//POST: Applies events captured from an ajax call and sends them to the display_month function to be handled in the calendar creation.
	{
		var xmlHttp;
		var type;
		try
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp = new XMLHttpRequest();
			xmlHttp.overrideMimeType('text/xml');					
		}
		catch (e)
		{ // Internet Explorer
			try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
			}
			catch (e)
			{
				try
				{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

				}
				catch (e)
				{
					alert("Your browser does not support AJAX!");
				}
			}
		}
		
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4 && xmlHttp.status == 200)
			{
				var events = xmlHttp.responseText;	//JS is returned from the event_request.php site as a string.
				removeElement('calendar_box');		//Before sending events to be displayed this destorys the temporary container with the loading screen.
				display_month(month,year,events);
			}
		}
		
		var mydiv = document.createElement('div');
		mydiv.setAttribute('id', 'calendar_box');	// This is the master container of the calendar itself.
		mydiv.innerHTML = 'Loading Calendar<br/><br/><img src="../_img/loading.gif" style="position:absolute;left:50%;margin-left:-50px" border="0"/>';		
		document.getElementById('calendar_content').appendChild(mydiv);		
		xmlHttp.open("GET",http_root+"/event/event_request.php?request=calendar&month="+month+"&year="+year,true);
		xmlHttp.send(null);	
	}	
	
	function display_month(month,year,events)
	//PRE: Requires the Month (0-11), the year, and the unevaluated string of javascript that contains the events
	//POST: Generates the Month/Year calendar, a global array of date IDs contained in that month, and then passes this data to three functions
	//			add_events: applies events to the displayed calendar, add_events_list; applies events to the print view event listing, filter_list: creates the category based event filter
	{
		var days = new Array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
		var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');	
		var long_months = new Array(0,2,4,6,7,9,11);
		var currentTime = new Date();
		
		
		var last_month = (month-1);
		if(last_month < 0)
		{
			last_month = 11;
			last_year = (year-1);
		}else {
			last_year = year;
		}
		
		var next_month = (month+1);
		if(next_month == 12)
		{
			next_month = 0;
			next_year = (year+1);
		}
		else
		{
			next_year = year;
		}
		
	
		var firstday = new Date(year,month,1)
		var startday = firstday.getDay();

		if(startday == 0)
		{
			startday = 7;
		}
		
		var count = 1;
		
		var monthLength = 0;
		
		if (month == 1) 
		{ // February only!
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
			{
				monthLength = 29;
			}
			else
			{
				monthLength = 28;
			}
		}else if(in_array(month,long_months))
		{
			monthLength = 31;
		}else
		{
			monthLength = 30;	
		}
	
		var mydiv = document.createElement('div');
		mydiv.setAttribute('id', 'calendar_box');		

		var titlediv = document.createElement('div');
		titlediv.className="calendar_title_header";
		if(month != currentTime.getMonth() || year != currentTime.getFullYear())
		{
			titlediv.innerHTML = '<span id="this_month_link" onclick="removeElement(\'calendar_box\'); get_events(currentTime.getMonth(),currentTime.getFullYear());">Return to this month</span>';
		}
		titlediv.innerHTML = titlediv.innerHTML  +'<div id="calendar_nav"><div id="calendar_left_arrow" style="z-index:5" onclick="removeElement(\'calendar_box\'); get_events('+last_month+','+last_year+');"><</div><div id="calendar_month_title" style="z-index:5" onclick="toggletab(\'calendar_month_menu\')" onmouseover="this.style.backgroundColor=\'lightgrey\'" onmouseout="this.style.backgroundColor=\'transparent\'"> '+months[month]+' '+ year +'</div><div id="calendar_right_arrow" style="z-index:5" onclick="removeElement(\'calendar_box\'); get_events('+next_month+','+next_year+');">></div></div>';

//Seasonal Headers

		if(month > 1 && month < 5)
		//Spring Graphics
		{
			titlediv.style.backgroundImage='url('+http_root+'/event/_img/spring.jpg)';
			titlediv.style.color='#344F8D';
			if(document.getElementById('this_month_link'))
			{
				document.getElementById('this_month_link').style.color='#344F8D';
			}
		}

		if(month > 4 && month < 8)
		//Summers Graphics
		{
			titlediv.style.backgroundImage='url('+http_root+'/event/_img/summer.jpg)';
			titlediv.style.color='#344F8D';
			if(document.getElementById('this_month_link'))
			{
				document.getElementById('this_month_link').style.color='#344F8D';
			}
		}

		if(month > 7 && month < 11)
		//Fall Graphics
		{
			titlediv.style.backgroundImage='url('+http_root+'/event/_img/fall.jpg)';
		}

//Seasonal Headers	

		mydiv.appendChild(titlediv);	

		var menudiv = document.createElement('div');
		menudiv.setAttribute('id', 'calendar_month_menu');
		for(z=0;z<12;z++)
		{
			menudiv.innerHTML = menudiv.innerHTML + '<div id="month_menu_item" onmouseover="this.style.backgroundColor=\'lightgrey\'" onmouseout="this.style.backgroundColor=\'transparent\'" onclick="removeElement(\'calendar_box\'); get_events('+z+','+year+');">' + months[z]+' '+year+'</div>';
		}

		titlediv.appendChild(menudiv);
		
		var filter_div = document.createElement('div');
		filter_div.setAttribute('id','filter_list');
		mydiv.appendChild(filter_div);
		

		for(x = 0;x < 7;x++)
		{
			var daydiv = document.createElement('div');
			daydiv.className="calendar_day_header";
			daydiv.setAttribute('onclick', 'closetab(\'calendar_month_menu\')');			
			daydiv.innerHTML = days[x%7];
			mydiv.appendChild(daydiv);
		}
				
		var colors = new Array('white','#cccccc');
		all_dates = 'var dates = new Array(';
		
		for(x = 0;x < 35;x++)
		{
			var color = colors[x % 2];
			var daydiv = document.createElement('div');
			daydiv.setAttribute('onclick', 'closetab(\'calendar_month_menu\')');						
			daydiv.className="calendar_day_box";
//			daydiv.style.width='14%';			
			if((x+1) >= startday)
			{
				var display = 1;
			} 
			
			if(count > monthLength)
			{
				display = 0;
			}
			
			if(display)
			{
				if(count > 1)
				{
					all_dates = all_dates + ',';
				}
				all_dates = all_dates + '\''+mktime(0,0,0,month+1,count,year)+'\'';
				daydiv.setAttribute('id', mktime(0,0,0,month+1,count,year));
				if(daydiv.addEventListener)
				{
					daydiv.setAttribute('onmouseover', 'this.style.color=\'white\';this.style.backgroundColor=\'lightgrey\'');
					daydiv.setAttribute('onmouseout', 'this.style.color=\'black\';this.style.backgroundColor=\'white\'');						
				}
				else
				{
						daydiv.onmouseover = function() { this.style.color='white';this.style.backgroundColor='lightgrey'; };
						daydiv.onmouseout = function() { this.style.color='black';this.style.backgroundColor='white'; };						
				}					
				daydiv.innerHTML = '<span style="margin-left:2px"><b>'+count+'</b></span><br/>';
				count++;
				if(x == 7 || x == 14 || x == 21 || x == 28)
				{
					daydiv.setAttribute('clear','both');
				}
			}	
			mydiv.appendChild(daydiv);
			
		}
		if(count <= monthLength)
		{
			for(x = 0;x < 7;x++)
			{
				var daydiv = document.createElement('div');
				daydiv.className="calendar_day_box";
//				daydiv.style.width='14%';							
				if((x+1) >= startday)
				{
					var display = 1;
				} 
				
				if(count > monthLength)
				{
					display = 0;
				}
				
				if(display)
				{
					if(count > 1)
					{
						all_dates = all_dates + ',';
					}				
					all_dates = all_dates + '\''+mktime(0,0,0,month+1,count,year)+'\'';
					daydiv.setAttribute('id', mktime(0,0,0,month+1,count,year));

					if(daydiv.addEventListener)
					{
						daydiv.setAttribute('onmouseover', 'this.style.color=\'white\';this.style.backgroundColor=\'lightgrey\'');
						daydiv.setAttribute('onmouseout', 'this.style.color=\'#5d7f96\';this.style.backgroundColor=\'white\'');						
					}
					else
					{
						daydiv.onmouseover = function() { this.style.color='white';this.style.backgroundColor='lightgrey'; };
						daydiv.onmouseout = function() { this.style.color='#5d7f96';this.style.backgroundColor='white'; };						
					}					
					
					daydiv.innerHTML = '<b>'+count+'</b> ';
					count++;
				}
				
				mydiv.appendChild(daydiv);
			}
		}		
		all_dates = all_dates + ');';
		eval(all_dates);
		document.getElementById('calendar_content').appendChild(mydiv);
		document.getElementById('calendar_month_menu').style.display='none';		
		add_events(dates,events);
		add_events_list(dates,events);
		display_filter_list(dates,events);
	}

function add_events_list(dates,events)
//PRE: dates: All the unique day IDs in the month displaed, events: unevaled string of events pulled from database.
//POST: Applies events to a printer friendly display that is hidden until the calendar is printed.
{
	eval(events);
	var current_days = new Array();
	var newTable = document.createElement('table');
	newTable.cellSpacing='0';
	newTable.cellPadding='0';
	newTable.setAttribute('id','calendar_list_table');
	var newTableBody = document.createElement('tbody');
	for(x in dates)
	{
		if (dates.hasOwnProperty(x)) 
		{
			document.getElementById('calendar_content_list').innerHTML = '<h1>'+date('F Y',dates[x])+'</h1>';
			for(y in single_events)
			{
				if (single_events.hasOwnProperty(y)) 
				{
					if(single_events[y][2] == dates[x])
					{
						var newtr = document.createElement('tr');
						newtr.setAttribute('id','p_'+dates[x]+'_'+single_events[y][0]+'_'+single_events[y][4]);
						var newtd1 = document.createElement('td');
						newtd1.style.fontSize='14pt';
						newtd1.style.fontWeight='bold';
						newtd1.style.width='50px';
						var newtd2 = document.createElement('td');						

						if(!in_array(date('d',dates[x]), current_days))
						{
							newtd1.innerHTML = date('d',dates[x]);
							newtd1.style.borderTop='1px';
							newtd1.style.borderTopStyle='solid';							
							newtd1.style.borderTopColor='black';
							newtd2.style.borderTop='1px';
							newtd2.style.borderTopStyle='solid';							
							newtd2.style.borderTopColor='black';														
							current_days[current_days.length] = date('d',dates[x]);
						}

						newtd1.valign='top';
						newtd2.style.paddingtop='5px';
						newtd2.style.paddingBottom='10px';
						newtd2.style.paddingRight='10px';
						
						newtd2.innerHTML = '<b style="margin-right:10px">'+single_events[y][1]+'</b>'+date('g:i',single_events[y][7])+' - '+date('g:i',single_events[y][8]);

						var newp = document.createElement('span');
						newp.innerHTML = single_events[y][5];
						newtd2.appendChild(newp);


						newtr.appendChild(newtd1);						
						newtr.appendChild(newtd2);			
						newTableBody.appendChild(newtr);
					}
				}
			}

			for(y in all_events)
			{
				if (all_events.hasOwnProperty(y)) 
				{
					if(all_events[y][2] == dates[x])
					{
						var newtr = document.createElement('tr');
						newtr.setAttribute('id','p_'+dates[x]+'_'+all_events[y][0]+'_'+all_events[y][4]);						
						var newtd1 = document.createElement('td');
						newtd1.style.fontSize='14pt';
						newtd1.style.fontWeight='bold';
						newtd1.style.width='50px';						
						var newtd2 = document.createElement('td');						

						if(!in_array(date('d',dates[x]), current_days))
						{
							newtd1.innerHTML = date('d',dates[x]);
							newtd1.style.borderTop='1px';
							newtd1.style.borderTopStyle='solid';							
							newtd1.style.borderTopColor='black';
							newtd2.style.borderTop='1px';
							newtd2.style.borderTopStyle='solid';							
							newtd2.style.borderTopColor='black';														
							current_days[current_days.length] = date('d',dates[x]);
						}
						newtd1.valign='top';
						newtd2.style.paddingtop='5px';
						newtd2.style.paddingBottom='10px';
						newtd2.style.paddingRight='10px';

						newtd2.innerHTML = '<b>'+all_events[y][1]+'</b>';

						var newp = document.createElement('span');
						newp.innerHTML = all_events[y][5];
						newtd2.appendChild(newp);

						newtr.appendChild(newtd1);						
						newtr.appendChild(newtd2);			
						newTableBody.appendChild(newtr);
					}
				}
			}		

			for(y in span_events)
			{
				if (span_events.hasOwnProperty(y)) 
				{
					if(span_events[y][2] <= dates[x] && span_events[y][3] >= dates[x] )
					{
						var newtr = document.createElement('tr');
						newtr.setAttribute('id','p_'+dates[x]+'_'+span_events[y][0]+'_'+span_events[y][4]);												
						var newtd1 = document.createElement('td');
						newtd1.style.fontSize='14pt';
						newtd1.style.fontWeight='bold';
						newtd1.style.width='50px';
						var newtd2 = document.createElement('td');						

						if(!in_array(date('d',dates[x]), current_days))
						{
							newtd1.innerHTML = date('d',dates[x]);
							newtd1.style.borderTop='1px';
							newtd1.style.borderTopStyle='solid';							
							newtd1.style.borderTopColor='black';
							newtd2.style.borderTop='1px';
							newtd2.style.borderTopStyle='solid';							
							newtd2.style.borderTopColor='black';							
							current_days[current_days.length] = date('d',dates[x]);
						}

						newtd1.valign='top';
						newtd2.style.paddingtop='5px';
						newtd2.style.paddingBottom='10px';
						newtd2.style.paddingRight='10px';
						newtd2.innerHTML = '<b>'+span_events[y][1]+'</b><br/>';

						var newp = document.createElement('span');
						newp.innerHTML = span_events[y][5];
						newtd2.appendChild(newp);


						newtr.appendChild(newtd1);						
						newtr.appendChild(newtd2);			
						newTableBody.appendChild(newtr);					
					}
				}
			}				
		}
	}
	
	newTable.appendChild(newTableBody);
	document.getElementById('calendar_content_list').appendChild(newTable);
}
	
	function first_empty(check_array)
	{
		var value = -1;
		for(x = 0; x < check_array.length; x++)
		{
			if(check_array[x][0] == 'empty')
			{
				return x;
			}
		}
		return value;
	}
	
	function add_events(dates,events)
	{	
		eval(events);
		var tracks = new Array(dates.length);

	<!-- start of track processing for span events -->	
		for(x = 0;x< dates.length;x++)
		{	
			tracks[x] = new Array();
			var count = 0;
			for(y=0; y< span_events.length;y++)
			{
				if(span_events[y][2] == dates[x])		//Start date
				{					
					var temp = document.createElement('div');
					temp.className='span_event_start';
					temp.setAttribute('id',dates[x]+'_'+span_events[y][0]+'_'+span_events[y][4]);

					if(temp.addEventListener)
					{
						temp.setAttribute('onclick','get_event_info('+span_events[y][0]+')');									
						temp.setAttribute('onmouseover','return overlib(\''+stripslashes(span_events[y][5])+'\',CAPTION,\''+stripslashes(span_events[y][1])+'\',TEXTCOLOR, \'#000\',CELLPAD, 10, 5,5,15,WIDTH,150)');
						temp.setAttribute('onmouseout','return nd();');
						temp.setAttribute('onfocus','this.blur()');
						temp.setAttribute('title','');
					}
					else
					{
						var clicker = 'temp.onclick = function() { get_event_info('+span_events[y][0]+') };';
						eval(clicker);
						var mouseover = 'temp.onmouseover = function () { return overlib(\''+stripslashes(span_events[y][5])+'\',CAPTION,\''+stripslashes(span_events[y][1])+'\',TEXTCOLOR, \'#000\',CELLPAD, 10, 5,5,15,WIDTH,150); };'
						eval(mouseover);
						temp.onmouseout = function() { return nd(); };
						temp.onfocus = function() { this.blur() };
						temp.title='';
						
					}

					if(span_events[y][1].length > 8)
					{
						var trunc = '';
						for(g = 0; g < 8; g++)
						{
							trunc += span_events[y][1].charAt(g);
						}
						trunc += '...';
					}else{
						trunc = span_events[y][1];
					}
					temp.innerHTML='<img src="'+http_root+'/event/_img/magnify_glass.png" align="left"/> '+trunc;
					temp.style.fontSize='8pt';
					temp.style.paddingLeft='2px';
					temp.style.lineHeight='10px';
					temp.style.cursor='pointer';					
					temp.style.overflow='hidden';				
					var check = category[span_events[y][4]];
					if(typeof check != "undefined" && typeof color[check] != "undefined")
					{
 						temp.style.backgroundColor=color[check][0];
						temp.style.color=color[check][1];;
					}
					else
					{
						temp.style.color='black';
					}

					while(tracks[x][count])
					{
						count++;
					}
				
					tracks[x][count] = new Array(dates[x],span_events[y][0],temp,"start");

				}else if(span_events[y][3] == dates[x])	// End Date
				{
					var temp = document.createElement('div');			
					temp.className='span_event_end';
					temp.setAttribute('id',dates[x]+'_'+span_events[y][0]+'_'+span_events[y][4]);
					
					if(temp.addEventListener)
					{
						temp.setAttribute('onclick','get_event_info('+span_events[y][0]+')');									
						temp.setAttribute('onmouseover','return overlib(\''+stripslashes(span_events[y][5])+'\',CAPTION,\''+stripslashes(span_events[y][1])+'\',TEXTCOLOR, \'#000\',CELLPAD, 10, 5,5,15,WIDTH,150)');
						temp.setAttribute('onmouseout','return nd();');
						temp.setAttribute('onfocus','this.blur()');
						temp.setAttribute('title','');
					}
					else
					{
						var clicker = 'temp.onclick = function() { get_event_info('+span_events[y][0]+') };';
						eval(clicker);
						var mouseover = 'temp.onmouseover = function () { return overlib(\''+stripslashes(span_events[y][5])+'\',CAPTION,\''+stripslashes(span_events[y][1])+'\',TEXTCOLOR, \'#000\',CELLPAD, 10, 5,5,15,WIDTH,150); };'
						eval(mouseover);
						temp.onmouseout = function() { return nd(); };
						temp.onfocus = function() { this.blur() };
						temp.title='';
					}

					if(x == 0)
					{
						if(span_events[y][1].length > 8)
						{
							var trunc = '';
							for(g = 0; g < 8; g++)
							{
								trunc += span_events[y][1].charAt(g);
							}
							trunc += '...';
						}else{
							trunc = span_events[y][1];
						}
						temp.innerHTML='<img src="'+http_root+'/event/_img/magnify_glass.png" align="left"/> '+trunc;
						temp.style.fontSize='8pt';
						temp.style.color='black';
						temp.style.paddingLeft='2px';
						temp.style.lineHeight='10px';
						temp.style.fontWeight='bold';
						temp.style.cursor='pointer';					
						temp.style.overflow='hidden';
		
					}

					var check = category[span_events[y][4]];
					if(typeof check != "undefined" && typeof color[check] != "undefined")
					{
 						temp.style.backgroundColor=color[check][0];
						temp.style.color=color[check][1];;
					}
					else
					{
						temp.style.color='black';
					}

					var recorded = 0;
					if(x > 0)
					{
						for(z = 0; z < tracks[x-1].length;z++)
						{
							if(tracks[x-1][z][1] == span_events[y][0])
							{
								tracks[x][z] = new Array(dates[x],span_events[y][0],temp,"end");
								recorded = 1;
								break;
							}						
						}
					}
					if(recorded == 0)
					{
						while(tracks[x][count])
						{
							count++;
						}
						tracks[x][count] = new Array(dates[x],span_events[y][0],temp,"end");						
					}
			
					
				}else if(dates[x] < span_events[y][3] && dates[x] > span_events[y][2]) // Mid point
				{			
					var temp = document.createElement('div');				
					temp.className='span_event_note';					
					temp.setAttribute('id',dates[x]+'_'+span_events[y][0]+'_'+span_events[y][4]);

					if(temp.addEventListener)
					{
						temp.setAttribute('onclick','get_event_info('+span_events[y][0]+')');									
						temp.setAttribute('onmouseover','return overlib(\''+span_events[y][5]+'\',CAPTION,\''+span_events[y][1]+'\',TEXTCOLOR, \'#000\',CELLPAD, 10, 5,5,15,WIDTH,150)');
						temp.setAttribute('onmouseout','return nd();');
						temp.setAttribute('onfocus','this.blur()');
						temp.setAttribute('title','');
					}
					else
					{
						var clicker = 'temp.onclick = function() { get_event_info('+span_events[y][0]+') };';
						eval(clicker);
						var mouseover = 'temp.onmouseover = function () { return overlib(\''+span_events[y][5]+'\',CAPTION,\''+span_events[y][1]+'\',TEXTCOLOR, \'#000\',CELLPAD, 10, 5,5,15,WIDTH,150); };'
						eval(mouseover);
						temp.onmouseout = function() { return nd(); };
						temp.onfocus = function() { this.blur() };
						temp.title='';
					}

					if(x == 0)
					{
						if(span_events[y][1].length > 8)
						{
							var trunc = '';
							for(g = 0; g < 8; g++)
							{
								trunc += span_events[y][1].charAt(g);
							}
							trunc += '...';
						}else{
							trunc = span_events[y][1];
						}
						temp.innerHTML='<img src="'+http_root+'/event/_img/magnify_glass.png" align="left"/> '+trunc;
						temp.style.fontSize='8pt';
						temp.style.color='black';
						temp.style.paddingLeft='2px';
						temp.style.lineHeight='10px';
						temp.style.fontWeight='bold';
						temp.style.cursor='pointer';					
						temp.style.overflow='hidden';
						
					}

					var check = category[span_events[y][4]];
					if(typeof check != "undefined" && typeof color[check] != "undefined")
					{
 						temp.style.backgroundColor=color[check][0];
						temp.style.color=color[check][1];;
					}
					else
					{
						temp.style.color='black';
					}

					var recorded = 0;
					if(x > 0)
					{
						for(z = 0; z < tracks[x-1].length;z++)
						{
							if(tracks[x-1][z][1] == span_events[y][0])
							{
								tracks[x][z] = new Array(dates[x],span_events[y][0],temp,"mid");
								recorded = 1;
								break;
							}						
						}
					}
					if(recorded == 0)
					{
						while(tracks[x][count])
						{
							count++;
						}
						tracks[x][count] = new Array(dates[x],span_events[y][0],temp,"mid");						
					}
				}	
				
			}		//end of span events loop			

			for(z = 0; z < tracks[x].length; z++)
			{
				if(!tracks[x][z])
				{
					var empty = document.createElement('div');
					empty.className="span_event_empty";
					tracks[x][z] = new Array(dates[x],'-1',empty,"empty");
				}
			}

		}		// end of dates loop

		for(x = 0; x < tracks.length; x++)
		{
			for(y = 0; y < tracks[x].length; y++)
			{

				if(document.getElementById(tracks[x][y][0]))
				{
					document.getElementById(tracks[x][y][0]).appendChild(tracks[x][y][2]);
				}
				else
				{
//					alert(tracks[x][y][0]);
				}
			}

		}
		
	<!-- end of track processing for span events -->
	
		for(y=0; y< all_events.length;y++)
		{
			if(T = document.getElementById(all_events[y][2]))
			{
			
				var temp = document.createElement('div');							

				if(all_events[y][1].length > 8)
				{
					var trunc = '';
					for(g = 0; g < 8; g++)
					{
						trunc += all_events[y][1].charAt(g);
					}
					trunc += '...';
				}else{
					trunc = all_events[y][1];
				}
				temp.innerHTML='<img src="'+http_root+'/event/_img/magnify_glass.png" align="left"/> '+trunc;

				temp.className='all_event_note';
				temp.setAttribute('id',all_events[y][2]+'_'+all_events[y][0]+'_'+all_events[y][4]);				

				if(temp.addEventListener)
				{
					temp.setAttribute('onclick','get_event_info('+all_events[y][0]+')');									
					temp.setAttribute('onmouseover','return overlib(\''+all_events[y][5]+'\',CAPTION,\''+all_events[y][1]+'\',TEXTCOLOR, \'#000\',CELLPAD, 10, 5,5,15,WIDTH,150)');
					temp.setAttribute('onmouseout','return nd();');
					temp.setAttribute('onfocus','this.blur()');
					temp.setAttribute('title','');
				}
				else
				{
					var clicker = 'temp.onclick = function() { get_event_info('+all_events[y][0]+') };';
					eval(clicker);
					var mouseover = 'temp.onmouseover = function () { return overlib(\''+all_events[y][5]+'\',CAPTION,\''+all_events[y][1]+'\',TEXTCOLOR, \'#000\',CELLPAD, 10, 5,5,15,WIDTH,150); };'
					eval(mouseover);
					temp.onmouseout = function() { return nd(); };
					temp.onfocus = function() { this.blur() };
					temp.title='';
				}

				var check = category[all_events[y][4]];
				if(typeof check != "undefined" && typeof color[check] != "undefined")
				{
					temp.style.backgroundColor=color[check][0];
					temp.style.color=color[check][1];;
				}
				else
				{
					temp.style.color='black';
				}

				document.getElementById(all_events[y][2]).appendChild(temp);



			}
		}
		for(y=0; y< single_events.length;y++)
		{
			if(T = document.getElementById(single_events[y][2]))
			{
				var temp = document.createElement('div');
				if(single_events[y][1].length > 8)
				{
					var trunc = '';
					for(g = 0; g < 8; g++)
					{
						trunc += single_events[y][1].charAt(g);
					}
					trunc += '...';
				}else{
					trunc = single_events[y][1];
				}
				temp.innerHTML='<img src="'+http_root+'/event/_img/magnify_glass.png" align="left"/> '+trunc;				
				temp.className='single_event_note';
				temp.setAttribute('id',single_events[y][2]+'_'+single_events[y][0]+'_'+single_events[y][4]);

				if(temp.addEventListener)
				{
					temp.setAttribute('onclick','get_event_info('+single_events[y][0]+')');									
					temp.setAttribute('onmouseover','return overlib(\''+single_events[y][5]+'\',CAPTION,\''+single_events[y][1]+'\',TEXTCOLOR, \'#000\',CELLPAD, 10, 5,5,15,WIDTH,150)');
					temp.setAttribute('onmouseout','return nd();');
					temp.setAttribute('onfocus','this.blur()');
					temp.setAttribute('title','');
				}
				else
				{
					var clicker = 'temp.onclick = function() { get_event_info('+single_events[y][0]+') };';
					eval(clicker);
					var mouseover = 'temp.onmouseover = function () { return overlib(\''+single_events[y][5]+'\',CAPTION,\''+single_events[y][1]+'\',TEXTCOLOR, \'#000\',CELLPAD, 10, 5,5,15,WIDTH,150); };'
					eval(mouseover);
					temp.onmouseout = function() { return nd(); };
					temp.onfocus = function() { this.blur() };
					temp.title='';
				}

				var check = category[single_events[y][4]];
				if(typeof check != "undefined" && typeof color[check] != "undefined")
				{
					temp.style.backgroundColor=color[check][0];
					temp.style.color=color[check][1];;
				}
				else
				{
					temp.style.color='black';
				}
				document.getElementById(single_events[y][2]).appendChild(temp);
			}
		}		
	}

	function closetab(tabid){
	//PRE: valid document ID
	//POST: sets the ID display style to none (ie. it hides it)
		document.getElementById(tabid).style.display="none";   
	}

	
	function toggletab(tabid)
	//PRE: valid document ID
	//POST: sets the ID display style to none (ie. it hides it)	if it's visible, or visible if it is already hidden.
	{
	
	   if(document.getElementById(tabid).style.display=="none")
	   {	
			document.getElementById(tabid).style.display="block";
	   }
	   else
	   {
			document.getElementById(tabid).style.display="none";   
	   }
	}
