// Description: Dynamic HTML Calendar
// Author: Thomas Ballard
// Highly Modified By: Jan (John) Butrym
// Copyright 2001  E.S.Q. Software  All Rights Reserved
// Questions, Comments, Work? thomas@esqsoft.com

// document.getElementById('maintablecalwidth').className='c2';
// document.getElementById('calendar').className='c2';

monthname=new Array("Month","January","February","March","April","May","June","July","August","September","October","November","December");
var currentdate=new Date;
var startday=new Date;
var realdate=new Date;
var lastclickeddate=new Date;
var monthnumber;
var yearnumber;
var daynumber;
var endday;
var tempclassname;
var theday;
var daydrawn;
var filename;

function initcalendar()
{
	setupcalendar();
	setiframe();
}

function setupcalendar()
{
	monthnumber=(currentdate.getMonth()+1);
	yearnumber=(currentdate.getFullYear());
	daynumber=(currentdate.getDate());
	startday.setFullYear(currentdate.getFullYear());
	startday.setMonth(currentdate.getMonth());
	startday.setDate(1);
	endday=31;

	switch(monthnumber)
	{
		case 4:
		case 6:
		case 9:
		case 11:
		--endday;
		break;

		case 2:
		endday=29;
		if ((yearnumber%4)!=0)
		{
			--endday;
		}
	}

	var str='\
	<table border=1 cellpadding=1 cellspacing=0>\
	<tr><td colspan=7><table cellpadding=0 cellspacing=0 border=0 width=100%><tr>\
	<td width=5%><input type="button" id="left" class="buttons" value="&lt;&lt;" onClick="prevyear(this);" onMouseover="mover(this);" onMouseout="mout(this);" onFocus="blur();"></td>\
	<td width=5%><input type="button" id="left" class="buttons" value="&lt;" onClick="prevmonth(this);" onMouseover="mover(this);" onMouseout="mout(this);" onFocus="blur();"></td>\
	<td width=80% id="month" class="month">'+monthname[monthnumber]+' '+yearnumber+'</td>\
	<td width=5%><input type="button" id="right" class="buttons" value="&gt;" onClick="nextmonth(this);" onMouseover="mover(this);" onMouseout="mout(this);" onFocus="blur();"></td>\
	<td width=5%><input type="button" id="right" class="buttons" value="&gt;&gt;" onClick="nextyear(this);" onMouseover="mover(this);" onMouseout="mout(this);" onFocus="blur();"></td>\
	</tr></table></td></tr><tr><td width=25 class="weekday">Sun</td>\
	<td width=25 class="weekday">Mon</td><td width=25 class="weekday">Tue</td>\
	<td width=25 class="weekday">Wed</td><td width=25 class="weekday">Thu</td>\
	<td width=25 class="weekday">Fri</td><td width=25 class="weekday">Sat</td></tr>';

	theday=0;
	daydrawn=0;

	for (var indexy=0; indexy<6; indexy++)
	{
		str+='<tr>';

		for (var indexx=0; indexx<7; indexx++)
		{
			if ((theday<1)&&(indexx==startday.getDay()))
			{
				theday=1;
			}
			if ((theday==lastclickeddate.getDate())&&(currentdate.getMonth()==lastclickeddate.getMonth())&&(currentdate.getFullYear()==lastclickeddate.getFullYear())&&(theday<=endday)&&(!daydrawn))
			{
				daydrawn=1;
				str+='<td class="today" ';
			}
			else
			{
				str+='<td class="day" ';
			}

			str+='onClick="mchange(this)" onMouseover="mover(this);" onMouseout="mout(this);">&nbsp;';

			if ((theday>0)&&(theday<=endday))
			{
				str+=theday++;
			}

			str+='</td>';
		}

		str+='</tr>';

		if (theday>endday)
		{
			indexy=6;
		}
	}

	str+='<tr><td colspan=7 class="weekday"><input type="button" id="center" class="buttons" value="Set To Today" onClick="settocurrent(this);" onMouseover="mover(this);" onMouseout="mout(this);" onFocus="blur();"></td></tr>'
	str+='<tr><td colspan=7 class="weekday"><input type="button" id="center" class="buttons" value="Printable Version" onClick="printannouncements(this);" onMouseover="mover(this);" onMouseout="mout(this);" onFocus="blur();"></td></tr>'
	str+='</table>';
	//calendar.innerHTML=str;
	document.getElementById('calendar').innerHTML=str;

	return;
}

function mover(a)
{
	if (a.innerHTML!='&nbsp;')
	{
		//tempb=a.style.backgroundColor;
		//tempf=a.style.color;
		tempclassname=a.className;
		a.className='mouseovercolor '+tempclassname;
		//a.style.backgroundColor="775555";
		//a.style.color="eeeeee";
	}
}

function mout(a)
{
	if (a.innerHTML!='&nbsp;')
	{
		a.className=tempclassname;
		//a.style.backgroundColor=tempb;
		//a.style.color=tempf;
	}
}

function mchange(a)
{
	var tempday;

	if (a.innerHTML!='&nbsp;')
	{
		tempday=a.innerHTML.substr(6);
		currentdate.setDate(tempday);
		lastclickeddate.setFullYear(currentdate.getFullYear()); 
		lastclickeddate.setMonth(currentdate.getMonth());
		lastclickeddate.setDate(currentdate.getDate());
		setupcalendar();
		setiframe();
	}
}

function prevmonth()
{
	if (monthnumber>1)
	{
		--monthnumber;
	}
	else
	{
		monthnumber=12;
		--yearnumber;
	}

	currentdate.setMonth(monthnumber-1);
	currentdate.setFullYear(yearnumber);
	setupcalendar();
}

function nextmonth()
{
	if (monthnumber<12)
	{
		++monthnumber;
	}
	else
	{
		monthnumber=1;
		++yearnumber;
	}

	currentdate.setFullYear(yearnumber); 
	currentdate.setMonth(monthnumber-1);
	setupcalendar();
}

function prevyear()
{
	--yearnumber;
	currentdate.setFullYear(yearnumber); 
	setupcalendar();
}

function nextyear()
{
	++yearnumber;
	currentdate.setFullYear(yearnumber);
	setupcalendar();
}

function settocurrent()
{
	currentdate.setFullYear(realdate.getFullYear()); 
	currentdate.setMonth(realdate.getMonth());
	currentdate.setDate(realdate.getDate());
	lastclickeddate.setFullYear(realdate.getFullYear()); 
	lastclickeddate.setMonth(realdate.getMonth());
	lastclickeddate.setDate(realdate.getDate());
	setupcalendar();
	setiframe();
}

function setiframe()
{
	filename=lastclickeddate.getFullYear()+"-";

	if((lastclickeddate.getMonth()+1)<10)
	{
		filename+="0"+(lastclickeddate.getMonth()+1);
	}
	else
	{
		filename+=(lastclickeddate.getMonth()+1);
	}

	filename+="-";

	if(lastclickeddate.getDate()<10)
	{
		filename+="0"+lastclickeddate.getDate();
	}
	else
	{
		filename+=lastclickeddate.getDate();
	}

	filename+=".html";

	filename="announcements/"+filename;

	document.getElementById('announcementsframe').src=filename;
	//frames['announcementsframe'].location.href=filename;
}

function printannouncements()
{
	window.open(filename,'printwindow');
}