var begTags = "<center><H2><font size='2' face='Verdana'>";



var thisMonth = ""; 

var thisYear = "";



var todayDay =   new Date().getDate();

var todayMonth = new Date().getMonth()+1;

var todayYear =  new Date().getYear();



var today = todayYear +"-"+ (todayMonth<10?"0"+todayMonth:todayMonth) + "-" + (todayDay<10?"0"+todayDay:todayDay);



var weekHead1 = "</font></H2></center><TABLE width='100%' border=1 bordercolordark='#5eacec' bordercolorlight='#FFFFFF' cellpadding='0'>" + "\n" +

"<TBODY><TR><TH vAlign=center align=middle width='14%' bgcolor='#5eacec'><font size='1' color='#FFFFFF' face='Verdana'>"+"\n" +

"Sunday</font></TH><TH vAlign=center align=middle width='14%' bgcolor='#5eacec'><font size='1' color='#FFFFFF' face='Verdana'>"+"\n" +

"Monday</font></TH><TH vAlign=center align=middle width='14%' bgcolor=#5eacec><font size='1' color='#FFFFFF' face='Verdana'>"+"\n" +

"Tuesday</font></TH><TH vAlign=center align=middle width='14%' bgcolor='#5eacec'><font size='1' color='#FFFFFF' face='Verdana'>"+"\n" +

"Wednesday</font></TH><TH vAlign=center align=middle width='14%' bgcolor=#5eacec><font size='1' color='#FFFFFF' face='Verdana'>"+"\n" +

"Thursday</font></TH><TH vAlign=center align=middle width='14%' bgcolor='#5eacec'><font size='1' color='#FFFFFF' face='Verdana'>"+"\n" +

"Friday</font></TH><TH vAlign=center align=middle width='14%' bgcolor='#5eacec'><font size='1' color='#FFFFFF' face='Verdana'>"+"\n" +

"Saturday</font></TH></TR><TR>";



// html code for end of the month

var endTags = "</TR></TBODY></TABLE><br><p>";



	// Constant array of month names

	var months=new Array;

	months["01"] = "January";

	months["02"] = "February";

	months["03"] = "March";

	months["04"] = "April";

	months["05"] = "May";

	months["06"] = "June";

	months["07"] = "July";

	months["08"] = "August";

	months["09"] = "September";

	months["10"] = "October";

	months["11"] = "November";

	months["12"] = "December";





// produce the HTML code for a month calendar	

function getMonth()

{



        // determine which font size to use for dates. 

        var fontsize=5;



		//integer to keep track of how many days were added to the month in the html string

		cellCount = 0;

		

		//prints out blank cells needed to start off month calendar

		for(var blanks = 0; blanks < numBlankDays; blanks++)

		{

				cellsHTML = cellsHTML + "<TD vAlign=top align=left width='14%' >&nbsp;<br><br><br><br></TD>";

                cellsHTML += "\n";

				cellCount++;

		}



		//prints out cells for each day of the month

		for(var days = 1; days <= numberOfDays; days++)

		{

		  sEventId = "";

		  thisDate = (thisYear+"-"+thisMonth+"-"+(days<10?"0"+days:days));

		  oEventList = getEvent(thisDate);

	    cellsHTML = cellsHTML + "<TD vAlign=top align=left width='14%' > " 

		  	+ " <table border=0 cellpadding=0 cellspaning='0' width='100%'> "

		  	+ " <tr><td bgcolor='"+(thisDate==today?"red":(oEventList.length==0?"#F3F7FC":"#F3F7FC"))+"' align='right' id='td_" + thisDate + "'> "

		  	+ " <font size='"+fontsize+"' color='"+(thisDate==today?"#5B5B5B":(oEventList.length==0?"#5B5B5B":"#0099CC"))+"' face='Verdana' id='font_" + thisDate + "'>" 

		  	//+ (oEventList.length==0?"":"<a href='javascript:openLink(\"/events/GetDetails.php?EVENT_DATE="+thisDate+"\");'>")

		  	      +days 

		  	//+ (oEventList.length==0?"":"</a>");

		  	+ " </font> "

		  	+ " </TD></tr> " ;

		if (oEventList.length == 0 ) {

	    cellsHTML += " <tr><td height='60' bgcolor="+ (oEventList.length==0?"#ffffff":"#F3F7FC")+"> "

		  	+ (oEventList.length==0?"":"<font size='1' color='#404040' face='Verdana'>")

		  	+ (oEventList.length==0?"":"<a href='javascript:openLink(\"../event_details.php?id="+oEventList[i][0]+"\");'>")

		  	+ (oEventList.length==0?"":oEventList[i][1])

		  	+ (oEventList.length==0?"":"</a>");

		  	+ (oEventList.length==0?"":"</font>");

		}

		if (oEventList.length > 0 ) {

		  for (i=0; i < oEventList.length; i++) {

	    cellsHTML += " <tr><td height='60' bgcolor="+ (oEventList.length==0?"#ffffff":"#F3F7FC")+" > "

		  	+ (oEventList.length==0?"":"<font size='1' color='#404040' face='Verdana' class='event'>")

		  	+ (oEventList.length==0?"":"<a href='javascript:openLink(\"../event_details.php?id="+oEventList[i][0]+"\");'>")

		  	+ (oEventList.length==0?"":oEventList[i][1])

		  	+ (oEventList.length==0?"":"</a>");

		  	+ (oEventList.length==0?"":"</font>");

		  	}

		}

	    cellsHTML += " </TD></tr> " 

		  	+ " </table></TD>";

            cellsHTML += "\n";

    		cellCount++;

			if(cellCount % 7 == 0)

			{

				cellsHTML = cellsHTML + "</tr><tr>";

                cellsHTML += "\n";

			}

		}

		

		//prints out blank cells needed to end off month calendar

		while (cellCount % 7 != 0)

		{

			cellsHTML = cellsHTML + "<TD vAlign=top align=left width='14%' >&nbsp;<br><br></TD>";

            cellsHTML += "\n";

    		cellCount++;

		}

		



	navHead = "<TABLE width='100%' border=0 bordercolordark='#5eacec' bordercolorlight='#FFFFFF' cellpadding='0'>" + "\n" 

		+ " <tr> "

		+ "   <td align='left'><a href='javascript:prevMonth();' target='_self'><img src='../images/button_previous.jpg' border='0'> Previous Month</a></td>\n"

		+ "   <td align='center' class='event_bold'>"+monthName + " " + yearName + "</td>\n"

		+ "   <td align='right'><a href='javascript:nextMonth();' target='_self'>Next Month <img src='../images/button_next.jpg' border='0'></a></td>\n"

		+ " </tr> "

		+ "</table>";

		//returns full html code for the month

        var weekHead = weekHead1;

		return begTags + navHead + weekHead + "\n" + cellsHTML + endTags;

}





// Determine the number of days in the month and the starting/ending dates

  function createMonth(inputMonth, inputYear, inputDay) {



        var leapYear = false;

	monthName = months[inputMonth];

        yearName = inputYear.toString();

		cellsHTML = "";

		numBlankDays = inputDay - 1;



		//figure out if the year is a leap year

		if(inputYear%4 == 0)

		{

			leapYear = true;

		}

		else

		{

			leapYear = false;

		}



		//figure out the number of days in the month

		//if month is NOT february, april, june, september, nor november then it has 31 days

		if(inputMonth != 2 && inputMonth  != 4 && inputMonth != 6 && inputMonth != 9 && inputMonth != 11)

		{

			numberOfDays = 31;

		}

		else

		{

			//if month is NOT february, then it has 30 days

			if(inputMonth != 2)

			{

				numberOfDays = 30;

			}

			else

			{

				//if month is a leap year, then it has 29 days. if not, it has 28

				if(leapYear)

				{

					numberOfDays = 29;

				}

				else

				{

					numberOfDays = 28;

				}

			}

		}



		endDay = (numberOfDays + numBlankDays) % 7;

  }



  function showCalendar(currentMonth, currentYear) {

    thisMonth = currentMonth; 

    thisYear = currentYear;

      // Use Date function to return starting day of week for the month

      // Note that Date uses base zero numbering for month and day.

      var startDate = new Date(currentYear, currentMonth-1, 1);

      var startDay = startDate.getDay() + 1;

      var monthLimit = 1; // document.CalendarSettings.numMonths.value;

	//Iterate for as many months as user desires.

	for(var index = 0; index < monthLimit; index++)

	{

            createMonth( currentMonth, currentYear, startDay);

            document.write(getMonth());

            document.write("\n");



	}

  }



  function prevMonth() {

    thisMonth--;    

    if(thisMonth == 0) {

    	thisYear--;

    	thisMonth = 12;

    }

    

    // showCalendar(thisMonth,thisYear);

    document.location.href = "../events/index.php?MONTH="+thisMonth+ "&YEAR="+thisYear;

  }

  function nextMonth() {

    thisMonth++;    

    if(thisMonth == 13) {

    	thisYear++;

    	thisMonth = 1;

    }

    //showCalendar(thisMonth,thisYear);

    document.location.href = "../events/index.php?MONTH="+thisMonth+ "&YEAR="+thisYear;

  }

  

  function getEvent(thisDate) {

	oEventList = new Array();

  	if (aEvent[thisDate]!= null) {

 		for (i=0,j=0;i <= aEvent[thisDate].length; i++) {

			if (aEvent[thisDate][i] != null) {

			oEventList[j] = new Array();

			oEventList[j][0] = aEvent[thisDate][i][0]; // event id

			oEventList[j][1] = aEvent[thisDate][i][1] +" - " + aEvent[thisDate][i][2]; // event time + desc

			// alert(j + " " + oEventList[j][0]);

			j++;

		//	return aEvent[thisDate][i][0] +" - " + aEvent[thisDate][i][1];

			}

		}

  	}

  	return oEventList;

  	

  }

  
