/*
calendar.js - is a calendar script for creating a calendar of events
for a particular month.  
Requires the events.js and the sermondata.js to be loaded on the page
prior to calling the showCalendar function.

The loadCalendar() method in the events.js should be used to edit your event lists.
and the loadSermons method in the sermondata.js should be used to edit the
sermon list.

*/

/*
	Browser Detection 
*/
if (document.all) {n=0;ie=1;ns6=0;}//ie
else if (document.getElementById){n=0;ie=0;ns6=1;}//ns6
else if (document.layers) {n=1;ie=0;ns6=0;}//ns4

/*
	 configuration variables
*/
var TABLE_COLOR = "#d3d3d3";
var MONTHYEAR_TEXT_COLOR = "#000000";
var WEEKDAYS_BGCOLOR = "#d3d3d3";
var WEEKDAYS_TEXT_COLOR = "#dc143c";
var CURRENT_DATE_BGCOLOR = "#C0C0C0";
var DATE_BGCOLOR = "#ffffff";
var DATE_TEXT_COLOR = "#000000";



 
function getDayStr(day_num){
        switch(day_num){
                case 0: return "Sunday";
                case 1: return "Monday";
                case 2: return "Tuesday";
                case 3: return "Wednesday";
                case 4: return "Thursday";
                case 5: return "Friday";
                case 6: return "Saturday";
                default: return false;
        }
}

function getMonthStr(month_num){
        switch(month_num){
                case 0: return "January";
                case 1: return "February";
                case 2: return "March";
                case 3: return "April";
                case 4: return "May";
                case 5: return "June";
                case 6: return "July";
                case 7: return "August";
                case 8: return "September";
                case 9: return "October";
                case 10: return "November";
                case 11: return "December";
                default: return false;
        }
}

function getDaysInMonth(month_num, year_num){
        switch(month_num){
                case 0: return 31;
                case 1: if((year_num % 4) == 0){return 29;}else{return 28;}
                case 2: return 31;
                case 3: return 30;
                case 4: return 31;
                case 5: return 30;
                case 6: return 31;
                case 7: return 31;
                case 8: return 30;
                case 9: return 31;
                case 10: return 30;
                case 11: return 31;
                default: return false;
        }
}

function defaultCalendar(){
	loadCalendar();
	loadSermons();	
	
	cookieStr=getCookie('curcal');
	if (cookieStr!=null){
		dsplit = cookieStr.split('/');
		mNum = parseInt(dsplit[0]);
		yNum = parseInt(dsplit[1]);
		removeCookie('curcal');
	}else
	{	today = new Date();
		mNum = today.getMonth() + 1 ;
		yNum  = today.getFullYear();
	}
	writeCalendar(mNum,yNum );
}

function writeCalendar(calMonthNum,yearNum){
	htmlStr = '';
	htmlStr = showCalendar(calMonthNum,yearNum);
	// stylesheet
	if (ie){
	   document.writeln("<style> .caldate {font-family:sans-serif; font-size: 10pt; color: "+DATE_TEXT_COLOR+";} .calwkdy {font-family:sans-serif; font-size: 10pt; 	font-weight : bold; color: "+WEEKDAYS_TEXT_COLOR+";} .caltitle {font-family:sans-serif; font-size: 14pt; color: "+MONTHYEAR_TEXT_COLOR+"; font-weight: bold;}.calevents {font-family:sans-serif; font-size: 7pt; color: "+DATE_TEXT_COLOR+";}<\/style>");
	}else{
	   document.writeln("<style> .caldate {font-family:sans-serif; font-size: 8pt; color: "+DATE_TEXT_COLOR+";} .calwkdy {font-family:sans-serif; font-size: 8pt; 	font-weight : bold; color: "+WEEKDAYS_TEXT_COLOR+";} .caltitle {font-family:sans-serif; font-size: 12pt; color: "+MONTHYEAR_TEXT_COLOR+"; font-weight: bold;}.calevents {font-family:sans-serif; font-size: 7pt; color: "+DATE_TEXT_COLOR+";}<\/style>");
	}
	document.writeln('<DIV ID="wccCalendar">');
	document.writeln(htmlStr);
	document.writeln("</DIV>");
	
	document.close();
	return true;
	
}
function replaceCalendar(calMonthNum,yearNum){
	setCookie('curcal',calMonthNum+'/'+yearNum,null);
	htmlStr = '';
	htmlStr = showCalendar(calMonthNum,yearNum);
	document.getElementById("wccCalendar").innerHTML = htmlStr;
	return true;
}
	
	
function showCalendar(calMonthNum, yearNum){
	loadCalendar();
	loadSermons();
		
	// date functions work with a zero based array for 
	// processing so monthNum = 0 = January.
	// same is true for new Date(yy,mm,dd) constructor.
	
	monthNum = calMonthNum -1;
	if (yearNum < 1000) { yearNum += 2000; }
	var startDate = new Date(yearNum, monthNum, 1);
	var monthStr = getMonthStr(monthNum);
	
	var startNum = startDate.getDay();
	var daysInMonth = getDaysInMonth(monthNum, yearNum);
// Setup for previous month scrolling.	
	pMonthNum = calMonthNum - 1;
	if(calMonthNum == 1){
	   	pYearNum = yearNum - 1;
		pMonthNum = 12;
	} else
		pYearNum = yearNum;
// setup for next month scrolling.	
	nMonthNum = calMonthNum + 1;
	if (calMonthNum == 12){
		nYearNum = yearNum + 1;
		nMonthNum = 1;
	} else
		nYearNum = yearNum;
		
	
	var htmlStr = '';
	htmlStr += '<table width = "100%" border="1" cellspacing="0" cellpadding="4" bordercolorlight="'+TABLE_COLOR+'" bordercolordark="'+TABLE_COLOR+'">';
	htmlStr += '<tr><td align="center" bgcolor="'+TABLE_COLOR+'">';
	htmlStr += '<img src="images/previous.jpg" width="60" height="11" border="1" alt="Previous" usemap="#previous">';
	htmlStr += '<map name="previous">';
	htmlStr += '<area alt="Previous" shape="default" coords="0,0,60,11" href="" LANGUAGE="Javascript" onclick="replaceCalendar(pMonthNum,pYearNum);return false;">';
	htmlStr += '</map>';
	htmlStr += '</td>';
	htmlStr += '<td colspan="5" align="center" bgcolor="'+TABLE_COLOR+'">';
	htmlStr += "<font class=caltitle>"+monthStr+" "+yearNum+"</font></td>";
	htmlStr += '<td align="center" bgcolor="'+TABLE_COLOR+'">';
	htmlStr += '<img src="images/next.jpg" width="36" height="10" border="1" alt="Next" usemap="#next">';
	htmlStr += '<map name="next"><area alt="Next" shape="default" coords="0,0,36,10" href="" LANGUAGE="Javascript" onClick="replaceCalendar(nMonthNum,nYearNum);return false;" >';
	htmlStr += '</map>';
	htmlStr += '</td></tr>';
	htmlStr += '<tr><td align="center" bgcolor="'+TABLE_COLOR+'">';
	htmlStr += '<tr><td width="20" bgcolor="'+WEEKDAYS_BGCOLOR+'"><font class=calwkdy>Sun</font></td><td width="20" bgcolor="'+WEEKDAYS_BGCOLOR+'"><font class=calwkdy>Mon</font></td><td width="20" bgcolor="'+WEEKDAYS_BGCOLOR+'"><font class=calwkdy>Tue</font></td><td width="20" bgcolor="'+WEEKDAYS_BGCOLOR+'"><font class=calwkdy>Wed</font></td><td width="20" bgcolor="'+WEEKDAYS_BGCOLOR+'"><font class=calwkdy>Thu</font></td><td width="20" bgcolor="'+WEEKDAYS_BGCOLOR+'"><font class=calwkdy>Fri</font></td><td width="20" bgcolor="'+WEEKDAYS_BGCOLOR+'"><font class=calwkdy>Sat</font></td></tr>';
	htmlStr +="<tr>";
	
	//insert the blank boxes before day one on row 1.
	for (x=0; x < startNum; x++){
	        htmlStr +='<td width="14%" bgcolor="'+DATE_BGCOLOR+'"><font class=caldate>&nbsp;</font></td>';
	}
	//insert the days
	for (x=1; x <= daysInMonth; x++){

	        eventStr = '<td width="14%" bgcolor="'+DATE_BGCOLOR+'" valign="top" align="left" >';
			eventStr += '<font class=caldate>'+x +'<br></font>';
			eventStr += '<font class="calevents">'+ getEvents(calMonthNum+'-'+x+'-'+yearNum) +'</font>';
			eventStr += '</td>';
			htmlStr +=eventStr;		
			
	        if (((x+startNum) % 7 == 0) && (x != daysInMonth)){
	         htmlStr +="</tr><tr>";
	     }
	}
	//insert the blank boxes after the last day of the month.
	if ((daysInMonth+startNum) % 7 != 0){
	     if (daysInMonth+startNum+1 > 35){
	         var boxes = 42;
	     }else{
	      var boxes = 35;
	     }
	     for (x=daysInMonth+startNum; x < boxes; x++){
	         htmlStr +='<td width="14%" bgcolor="'+DATE_BGCOLOR+'"><font class=caldate>&nbsp;</font></td>';
	     }
	}
	return htmlStr;
}
