var ns6=document.getElementById&&!document.all
var ie4=document.all

var Selected_Month;
var Selected_Year;
var Current_Date = new Date();
var Current_Month = Current_Date.getMonth();
var ClassSuffix = "";
var onClickEvent = "";
var onClickIds = "";
var additionalFunction = "";

var Days_in_Month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var Month_Label = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

var Current_Year = Current_Date.getYear();
if (Current_Year < 1000)
Current_Year+=1900

var Today = Current_Date.getDate();

function Header(Year, Month) {
   if (Month == 1) {
   	Days_in_Month[1] = ((Year % 400 == 0) || ((Year % 4 == 0) && (Year % 100 !=0))) ? 29 : 28;
   }
   var Header_String = Month_Label[Month] + ' ' + Year;
   return Header_String;
}

function Defaults() {
   if (!ie4&&!ns6)
   return
   Selected_Month = Current_Month;
   Selected_Year = Current_Year;
   Make_Calendar(Current_Year, Current_Month);
}

function Make_Calendar(Year, Month) {
   var First_Date = new Date(Year, Month, 1);
   var Heading = Header(Year, Month);
   var First_Day = First_Date.getDay() + 1;
   if (((Days_in_Month[Month] == 31) && (First_Day >= 6)) ||
       ((Days_in_Month[Month] == 30) && (First_Day == 7))) {
      var Rows = 6;
   }
   else if ((Days_in_Month[Month] == 28) && (First_Day == 1)) {
      var Rows = 4;
   }
   else {
      var Rows = 5;
   }
   
   if(Month == 0)
   	Prev_Month_Days = Days_in_Month[11]
   else
   	Prev_Month_Days = Days_in_Month[Month - 1] - (First_Day - 2);
   Next_Month_Days = 1;

   	var HTML_String = '<table class="mainTable'+ClassSuffix+'" cellspacing="0" cellpadding="0">';
	HTML_String += '		<tr>';
	HTML_String += '			<td colspan="7">';
	HTML_String += '				<table border="0" cellspacing="0" cellpadding="0">';
	HTML_String += '					<tr>';
	if(ClassSuffix != "")
	{
		HTML_String += '						<td WIDTH="26" ALIGN="RIGHT" VALIGN="MIDDLE" class="monthYearRow'+ClassSuffix+'"><a HREF="javascript:SkipBack();' + additionalFunction + '"><img src="../images/calendar/previous'+ClassSuffix+'.jpg" alt="" border="0"></a></td>';
		HTML_String += '						<td WIDTH="725" ALIGN="CENTER" VALIGN="MIDDLE" class="monthYearText'+ClassSuffix+' monthYearRow'+ClassSuffix+'">'+Heading+'</td>';
		HTML_String += '						<td WIDTH="26" ALIGN="LEFT" VALIGN="MIDDLE" class="monthYearRow'+ClassSuffix+'"><a HREF="javascript:SkipForward();' + additionalFunction + '"><img src="../images/calendar/next'+ClassSuffix+'.jpg" alt="" border="0"></a></td>';
	} else {
		HTML_String += '						<td WIDTH="20" ALIGN="RIGHT" VALIGN="MIDDLE" class="monthYearRow"><a HREF="javascript:SkipBack();' + additionalFunction + '"><img src="../images/calendar/previous.jpg" width="7" height="11" alt="" border="0"></a></td>';
		HTML_String += '						<td WIDTH="183" ALIGN="CENTER" VALIGN="MIDDLE" class="monthYearText monthYearRow">'+Heading+'</td>';
		HTML_String += '						<td WIDTH="20" ALIGN="LEFT" VALIGN="MIDDLE" class="monthYearRow"><a HREF="javascript:SkipForward();' + additionalFunction + '"><img src="../images/calendar/next.jpg" width="7" height="11" alt="" border="0"></a></td>';
	}
	HTML_String += '					</TR>';
	HTML_String += '				</TABLE>';
	HTML_String += '			</TD>';
	HTML_String += '		</tr>';
	HTML_String += '	  <tr class="dayNamesRow'+ClassSuffix+'">';
	if(ClassSuffix != "")
	{
		HTML_String += '			<td class="dayNamesText'+ClassSuffix+'">Sunday</td>';
		HTML_String += '			<td class="dayNamesText'+ClassSuffix+'">Monday</td>';
		HTML_String += '			<td class="dayNamesText'+ClassSuffix+'">Tuesday</td>';
		HTML_String += '			<td class="dayNamesText'+ClassSuffix+'">Wednesday</td>';
		HTML_String += '			<td class="dayNamesText'+ClassSuffix+'">Thursday</td>';
		HTML_String += '			<td class="dayNamesText'+ClassSuffix+'">Friday</td>';
		HTML_String += '			<td class="dayNamesText'+ClassSuffix+'">Saturday</td>';
	} else {
		HTML_String += '			<td class="dayNamesText">Su</td>';
		HTML_String += '			<td class="dayNamesText">Mo</td>';
		HTML_String += '			<td class="dayNamesText">Tu</td>';
		HTML_String += '			<td class="dayNamesText">We</td>';
		HTML_String += '			<td class="dayNamesText">Th</td>';
		HTML_String += '			<td class="dayNamesText">Fr</td>';
		HTML_String += '			<td class="dayNamesText">Sa</td>';
	}
	HTML_String += '	  </tr>';
	
   	var Day_Counter = 1;
   	var Loop_Counter = 1;
   	for (var j = 1; j <= Rows; j++) {
      HTML_String += '<TR class="rows'+ClassSuffix+'">';
      for (var i = 1; i < 8; i++) {
         if ((Loop_Counter >= First_Day) && (Day_Counter <= Days_in_Month[Month])) {
			if(checkEvent(Day_Counter,Month,Year))
			{
				formattedDate = (Month+1)+'/'+Day_Counter+'/'+Year;
				displayEvents = displayDayEvents();
				//check the date
				if ((Day_Counter == Today) && (Year == Current_Year) && (Month == Current_Month))
	               cssClass = "today";
				else
					cssClass = "s20";
				HTML_String += '<td CLASS="'+cssClass+ClassSuffix;
				if(ClassSuffix == "")
					HTML_String += '" onmouseover="return overlib(\'<div class=&quot;popupDate&quot;>' + formattedDate + '</div>'+displayEvents+'\');" onmouseout="return nd();" '+ getOnClickEvent() +' style="cursor: pointer;">' + Day_Counter + '</td>';
				else
					HTML_String += '">'+ Day_Counter + '<br>' + displayEvents + '</td>';
			}
            else {
				//check the date
				if ((Day_Counter == Today) && (Year == Current_Year) && (Month == Current_Month))
	               cssClass = "today";
				else
					cssClass = "s2";
               HTML_String += '<td CLASS="'+cssClass+ClassSuffix+'">' + Day_Counter + '</td>';
            }
            Day_Counter++;    
         }
         else {
		 	HTML_String += '<td CLASS="sOther'+ClassSuffix+'">';
		 	if(Day_Counter <= Days_in_Month[Month])
            {
				HTML_String += Prev_Month_Days;
				Prev_Month_Days++;
			}
			else if(Loop_Counter >= First_Day)
			{
				HTML_String += Next_Month_Days;
				Next_Month_Days++;
			}
			HTML_String += '</td>'
         }
         Loop_Counter++;
      }
      HTML_String += '</tr>';
   }
   HTML_String += '</table>';
   cross_el=ns6? document.getElementById("Calendar") : document.all.Calendar
   cross_el.innerHTML = HTML_String;
}

function setClassSuffix(s) 
{ 
	ClassSuffix = s; 
}

function setOnClickEvent(s)
{
	onClickEvent = "javascript:"+s+"(";
}

function getOnClickEvent(){
	if(onClickEvent != "")
		return 'onclick="'+onClickEvent + '\'' + onClickIds + '\');"';
	else
		return "";
}

function getLinkEvent(event, id){
	if(onClickEvent != "")
		return event + '\'' + id + '\');"';
	else
		return "";
}

function isToday(){
	if ((Day_Counter == Today) && (Year == Current_Year) && (Month == Current_Month)) {
    	return true;
    } else {
		return false;
	}
}

function Skip(Direction) {
   if (Direction == '+') {
      if (Selected_Month == 11) {
         Selected_Month = 0;
         Selected_Year++;
      }
      else {
         Selected_Month++;
      }
   }
   else {
      if (Selected_Month == 0) {
         Selected_Month = 11;
         Selected_Year--;
      }
      else {
         Selected_Month--;
      }
   }
   Make_Calendar(Selected_Year, Selected_Month);
}

function SkipForward() { Skip('+'); }

function SkipBack() { Skip('-'); }

//used when changing month by drop down
function changeCalendar(newMonth, newYear){
	Selected_Month = parseInt(newMonth);
	Selected_Year = parseInt(newYear);
	Make_Calendar(Selected_Year, Selected_Month);
}

//array to hold all events
var events = new Array();
var eventIndex = 0;
//array to hold events for a particular day
var dayEvents = new Array();
var dayEventIndex = 0;
//var singleEvent;
/*
*	event object	
*/
function EventObj(eId,eD,eM,eY,eN,eL,eWD)
{
	this.id = eId
	this.eDate = eD;
	this.eMonth = eM;
	this.eYear = eY;
	this.eName = eN;
	this.eLoc = eL;
	this.eWrittenDate = eWD;
	this.clickEvent = onClickEvent; 
}

/*
*	function to create a Event object and add it
*	to the rules array	
*/
function createEvent(id,d,m,y,n,l,wd)
{
	events[eventIndex++] = new EventObj(id,d,m,y,n,l,wd);
}

function checkEvent(d,m,y)
{
	//alert('checkEvent - day:' + d + ' month:' + m + ' year:'+ y);
	//reset day variables
	dayEvents = new Array();
	dayEventIndex = 0;
	found = false;
	for(i=0;i<events.length;i++)
	{
		e = events[i];
		if(e.eYear == y && e.eMonth == (m+1) && e.eDate == d)
		{
			//singleEvent = e;
			dayEvents[dayEventIndex++] = e;
			found = true; 
		}
	}
	return found;
}

function displayDayEvents()
{
	htmlEvents = "";
	onClickIds = "";
	for(i=0;i<dayEvents.length;i++)
	{
		e = dayEvents[i];
		onClickIds += e.id; 
		//<div class=&quot;popupEventDate&quot;>12-03 :: 12-05</div>
		if(ClassSuffix == "")
			htmlEvents += '<div class=&quot;popupEventTitle&quot;>'+e.eName+'</div><div class=&quot;popupEventDescription&quot;>'+ e.eLoc+'<br>'+e.eWrittenDate+'</div>';
		else
		{
			
			htmlEvents += '<A HREF="'+getLinkEvent(e.clickEvent, e.id)+'" class="eventLink">'+e.eName+'</A><div class="popupEventDescription'+ClassSuffix+'">'+ e.eLoc+'<br>'+e.eWrittenDate+'</div>';
		}
		if(i+1 < dayEvents.length)
		{
			onClickIds += ",";
			htmlEvents += "<br>";
		}
	}
	return htmlEvents;
}
