<!--
/******************************************************************************
************************* CALENDAR CLASS IN JAVASCRIPT ************************
*******************************************************************************
***********************    Created by Janne Cederberg,  ***********************
***********************     mr_cederberg@hotmail.com    ***********************
***********************       for GGCA, www.ggca.hu     ***********************
***********************           June 9, 2003          ***********************
*******************************************************************************

SYNTAX:
	objName = new JC_calendar([int month],[int yearInFourDigits]);

METHODS:
	addEvents();
		adding events to the calendar
		example: objName.addEvents('7|event1','11|event2');
	
		string eventdata = "dateNumber|eventDescription"
		Notice the pipe (|) as the separator between the date and text info
		addEvents(string eventData, [string event_2_data], ..., [string event_n_data]);

	assignPDF();
		assigning a PDF-file relating to a calendar
		example: objName.assignPDF('calendar.pdf');
	
	printCalendar();
		prints the calendar stored in the object
		example: objName.printCalendar();
	
	printCalendarAndEvents();
		prints the calendar and specified events

	printEvents([int eventIndex], [int event2Index], [int event_n_index]);
		prints the specified events
		if used without parametres, prints all events
		if you can only print desired events by referring to their array index
		(0->) in the objName.events array

	printPDFlink();
		prints a link to the assigned PDF-file

EXAMPLES ON USAGE:
	//THIS WOULD CREATE A NEW CALENDAR OBJECT CALLED may
	//Because no month and year was specified, this calendar
	//     is always going to show the current month
	//Notice that the date showed is based on the date on
	//	the webvisitors computer, which might be messed up
	may = new JC_calendar();
	may.addEvents("1|testi","7|K-6 Olympic Day at GGCA");
	may.printCalendarAndEvents();

	cal2 = new JC_calendar(7,2003);
	cal2.printCalendar();

	now = new Date();
	cal3 = new JC_calendar(prompt('Give month', now.getMonth()), prompt('Give year', now.getFullYear()));
	cal3.printCalendar();

	august = new JC_calendar(8,2003);
	august.addEvents("2|abc");
	august.printCalendarAndEvents();


***********************************************************
***********************************************************/

//this func is a calendar class constructor function
function JC_calendar(parCalMonth,parCalYear){
	now = new Date();
	if((parCalMonth) && (parCalMonth>12 || parCalMonth<1)){
		alert('The month parametre must be 1<= month <=12. Current month will be used.');
		parCalMonth = now.getMonth()+1;
	}
	if((parCalYear) && (parCalYear>9999 || parCalYear<1970)){
		alert('The year parametre must be 1970<= year <=9999. Current year will be used.');
		parCalYear = now.getFullYear();
	}
	//number of days in each month
	this.arrDaysPerMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	//define arrays for string names of months, weekdays
	this.arrMonthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	this.arrDayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	
	//use parametre values if set, otherwise use current month & year
	this.intMonth = (parCalMonth) ? Number(parCalMonth)-1 : now.getMonth();
	this.intFullYear = (parCalYear) ? Number(parCalYear) : now.getFullYear();
	this.blnCal4CurrMonth = ((this.intMonth.toString().indexOf(now.getMonth()) != -1) && (this.intFullYear.toString().indexOf(now.getFullYear()) != -1)) ? true : false;
	this.intDayOfMonth = (this.blnCal4CurrMonth) ? now.getDate() : false;
	//index of the first day of the month: 0=Sunday, 1=Monday...
	//if the month the calendar is made for is not current, will return false
	this.intDayOfWeek = (this.blnCal4CurrMonth) ? now.getDay() : false;
	
	this.blnIsLeapYear = (this.intFullYear%4 == 0) ? true : false;
	//make the leap year correction
	if(this.blnIsLeapYear) this.arrDaysPerMonth[1] = 29;
	this.intNumOfDaysInMonth = this.arrDaysPerMonth[this.intMonth];

	tmp = new Date(this.intFullYear,this.intMonth,1);
	this.intWeekdayOfFirstOfMonth = tmp.getDay();
	delete tmp;
	
	this.strMonthName = this.arrMonthNames[this.intMonth];
	this.strMonthShortName = (this.intMonth != 8) ? (this.arrMonthNames[this.intMonth].length<=4) ? this.arrMonthNames[this.intMonth] : this.arrMonthNames[this.intMonth].substring(0,3)+"." : this.arrMonthNames[this.intMonth].substring(0,4)+".";
	this.strDayName = (this.intDayOfWeek) ? this.arrDayNames[this.intDayOfWeek] : false;
	this.strShortDayName = (this.intDayOfWeek) ? this.strDayName.substring(0,3) : false;
	this.strDayNameOfFirstOfMonth = this.arrDayNames[this.intWeekdayOfFirstOfMonth];
	this.strShortDayNameOfFirstOfMonth = this.strDayNameOfFirstOfMonth.substring(0,3);
	
	this.arrEvents = new Array(0); //this will be set to an array when events are added. I won't set it to an array here to save memory, because this variable might never be used
	this.pdfFile = '';
	this.pdfLinkText = '';
	
////////////////////////////////////////////////////////////////////////
/////////////////////////// METHODS ////////////////////////////////////
////////////////////////////////////////////////////////////////////////

	this.addEvents = JC_addEvents;
	this.assignPDF = JC_assignPDF;
	this.printCalendar = JC_printCalendar;
	this.printCalendarAndEvents = JC_printCalendarAndEvents;
	this.printEvents = JC_printEvents;
	this.printPDFlink = JC_printPDFlink;
}

function JC_addEvents(){
	//if(this.arrEvents.length) this.arrEvents = new Array();
	if(JC_addEvents.arguments.length > 0){
		for(index = 0; index < JC_addEvents.arguments.length; index++){
			this.arrEvents[this.arrEvents.length] = JC_addEvents.arguments[index].split("|");
		}
	}else
		alert('addEvents()-method: you specified no events to be added.');
}

function JC_assignPDF(filename){
	this.pdfFile = filename;
}

function JC_printEvents(){
	if(this.arrEvents){
		if(!JC_printEvents.arguments.length){
			for(index in this.arrEvents){
				document.writeln("<b>" +this.arrEvents[index][0]+ "</b> - " +this.arrEvents[index][1]+ "<br>");
			}
		}else{
			for(index = 0; index < JC_printEvents.arguments.length; index++){
				//check that the array index requested exists
				if(JC_printEvents.arguments[index] < this.arrEvents.length)
					document.writeln("<b>" +this.arrEvents[JC_printEvents.arguments[index]][0]+ "</b> - " +this.arrEvents[JC_printEvents.arguments[index]][1]+ "<br>");
				else
					alert("printEvents()-method:\n\nArray index 2 that was request for printing does not exist in the events-array\nPlease notice that array indexing starts from 0 (zero)");
			}
		}
	}
}

function JC_printCalendar(){
	d = document;
	d.writeln('<table border=0 cellspacing=0 cellpadding=3 style="font-size: 12px; border: 1px solid black;">');
	d.writeln('<tr><th colspan=' +this.arrDayNames.length+ ' bgcolor="#777788" style="color: white; border-bottom: 1px solid black;">'+this.strMonthName+" "+this.intFullYear+'</th></tr>');
	for(index = 0; index < this.arrDayNames.length; index++){
		d.writeln('<th>'+this.arrDayNames[index].substring(0,3)+'</th>');
	}
	d.writeln('</tr><tr>');
	
	cellsWritten = 0;
	//WRITE THE FIRST DAY OF THE MONTH
	for(index = 0; index < this.intWeekdayOfFirstOfMonth; index++){
		d.writeln('<td>&nbsp;</td>');
		cellsWritten++;
	}

	//WRITE IN THE REST OF THE DAYS FOR THE MONTH
	for(index = 1; index <= this.intNumOfDaysInMonth; index++){
		if(this.blnCal4CurrMonth && (this.intDayOfMonth==index))
			//IF THE DATE IS CURRENT
			d.writeln('<td align=center bgcolor="#ccccdd" style="border: 1px solid black;"><b>'+index+'</b></td>');
		else{
			//ALL OTHER DAYS
			blnEventForCurrentDate = false;
			//alert(this.arrEvents);
			for(j in this.arrEvents){
				//check if there's an event for the day being currently drawn into the calendar
				if(this.arrEvents[j][0].indexOf("-") != -1){
					//this.arrEvents[j][0] = this.arrEvents[j][0].replace(" ",""); //chop out any extra empty spaces
					arrTemp = this.arrEvents[j][0].split("-");
					if((arrTemp[0] <= index) && (arrTemp[1] >= index))
						blnEventForCurrentDate = true;
					//delete arrTemp;
				}else
					if(this.arrEvents[j][0] == index) blnEventForCurrentDate = true;
			}
			d.writeln('<td align="center"');
			if(blnEventForCurrentDate)
				d.writeln(' bgcolor="#dddddd"');
			d.writeln('>'+index+'</td>');
			blnEventForCurrentDate = false;
		}
		if((cellsWritten%7 == 6) && (index+1<=this.intNumOfDaysInMonth)) d.writeln('</tr><tr>');
		cellsWritten++;
	}
	
	if(cellsWritten%7 != 0){
		for(index = cellsWritten%7; index < 7; index++){
			d.writeln('<td></td>');
		}
	}
	d.writeln('</tr></table>');
}

function JC_printCalendarAndEvents(){
	d = document;
	d.writeln("<table border=0 width=536 cellspacing=10 cellpadding=0>");
	d.writeln("<tr><td valign=top width=300 style='font-size: 12px;'>");
	this.printEvents();
	d.writeln("</td><td width=10>&nbsp;</td><td width=240 valign=top>");
	this.printCalendar();
	d.writeln("</td></tr></table>");
}

function JC_printPDFlink(){
	if(this.pdfFile.length > 0){
		d = document;
		d.write('<a href="' +this.pdfFile+ '"><small>View printable PDF</small> <img src="images/pdf_icon_small.gif" border="0" alt="view as PDF"></a>');
	}
}
//-->