function calendar()
{
	
	var self = this
	var now = new Date()
	var selectedDate = now
	var events = []
	
	var container = document.createElement("DIV")
	container.className = "calendar"	

	var next = document.createElement("A")
	next.innerHTML = "Next"
	next.href="javascript:;"
	next.className="next-month"
	next.onclick = function()
	{
		selectedDate.setMonth(selectedDate.getMonth() + 1);
		refresh()
	}

	var prev = document.createElement("A")
	prev.innerHTML = "Previous"
	prev.className="prev-month"
	prev.href="javascript:;"
	prev.onclick = function()
	{
		selectedDate.setMonth(selectedDate.getMonth() - 1);
		refresh()
	}
	
	var monthCaption = document.createElement("SPAN")
	monthCaption.className="current-month"

	var table = document.createElement("TABLE")
	table.border="1"
	table.cellpadding="0"
	table.cellspacing="0"

	var Days = "MTWTFSS"
	var headRow = table.insertRow(-1)
	for (var r=0; r<7; r++)
	{
		var td = headRow.insertCell(-1)
		td.innerHTML = Days.charAt(r)
	}

	container.appendChild(prev)
	container.appendChild(next)
	container.appendChild(monthCaption)
	container.appendChild(table)
	
	
	function refresh()
	{
		var selectedYear = selectedDate.getFullYear() ;
		var selectedMonth = selectedDate.getMonth() ;
		// var currentDay = selectedDate.getDay()
		
		var d = 0
		var firstOfMonth = new Date(selectedYear, selectedMonth,1)
		var firstDayOfMonth = (6 + firstOfMonth.getDay()) % 7
		var lastOfMonth = new Date(new Date(selectedYear, selectedMonth+1,1)-1)
		
		var day = new Date(selectedYear, selectedMonth,1 - firstDayOfMonth)
		
		monthCaption.innerHTML = ["January","February","March","April","May","June","July","August","September","October","November","December"][selectedDate.getMonth()] + ' '+ selectedDate.getFullYear()
		
		for (var r = 0; r < 6 ; r++)
		{
			var row = table.rows[r+1] || table.insertRow(-1)

			for (var c = 0 ; c < 7 ; c++)
			{
				var cell = row.cells[c] || row.insertCell(-1)
				var className = ""
				var currentDay = day.getDate()
				cell.day = currentDay
				
				// alert("currentDay: " + currentDay) ; // debug
				
				// Find how many events on this day and put the class accordingly
				var event_count = self.getEventCount(selectedYear, selectedMonth+1, currentDay)
				
				if (day.getMonth() != selectedMonth)
				{
				  cell.innerHTML = currentDay ;
				  cell.onclick = null
				  cell.className = "empty"
				}
				else if (event_count <= 1)
				{
				  // If there is only one event the whole td is one cell
				  cell.innerHTML = currentDay ;
				  
				  cell.onclick = function() 
				  {
					if (self.onclick) 
					  self.onclick( selectedYear, selectedMonth+1, this.day ) 
				  }
				  var o = self.getEvent(selectedYear, selectedMonth+1, currentDay)
					
				  if (o)
				  {
                    if(o.isFull == 'False')					
					  className = o.className ;
					else	
					  className = o.className + "_full" ;
					//cell.title = o.tooltipMsg ;
				  }
				  cell.className = className
				} // end of max one event in cell
				else if (event_count == 2) 
				{	
				  var cell1_id = "cell_1_" + currentDay ;
				  var cell2_id = "cell_2_" + currentDay ;
				
				  var inner_html ; 
				  inner_html = "<div class=\"double_cell\" id=\"" + cell1_id + "\"></div>";
				  inner_html += "<div class=\"middle_cell\">" + currentDay + "</div>";
				  inner_html += "<div class=\"double_cell\" id=\"" + cell2_id + "\"></div>";
				  
				  cell.innerHTML = inner_html ;
				  cell.onclick = null ; // Make the main td unclickable
				  cell.className = null ; // Remove any class in case
				  
				  var cell1 = document.getElementById(cell1_id) ;
				  var cell2 = document.getElementById(cell2_id) ;
					
				  cell1.day = currentDay ;
				  cell2.day = currentDay ;
					
				  cell1.onclick = function() 
				  {
					if (self.onclick) 
					  self.onclick( selectedYear, selectedMonth+1, this.day , 1) ;
				  }
				  cell2.onclick = function() 
				  {
					if (self.onclick) 
					  self.onclick( selectedYear, selectedMonth+1, this.day , 2) ;
				  } 
					
				  var o1 = self.getEvent(selectedYear, selectedMonth + 1, currentDay , 1)
				  var o2 = self.getEvent(selectedYear, selectedMonth + 1, currentDay , 2)
					
				  if (o1)
				  {
                    if(o1.isFull == 'False')					
					  className1 = o1.className ;
					else	
					  className1 = "split_" + o1.className + "_full" ;
					  //cell.title = o.tooltipMsg ;
				  }
				  if (o2)
				  {
                    if(o2.isFull == 'False')					
					  className2 = o2.className ;
					else	
					  className2 = "split_" + o2.className + "_full" ;
					//cell.title = o.tooltipMsg ;
				  }
				  cell1.className += " " + className1 ;
				  cell2.className += " " + className2 ;
				} // two events in a day 
				else if (event_count == 3) 
				{	
				  var cell1_id = "cell_1_" + currentDay ;
				  var cell2_id = "cell_2_" + currentDay ;
				  var cell3_id = "cell_3_" + currentDay ;
				
				  var inner_html ; 
				  inner_html = "<div class=\"double_cell\" id=\"" + cell1_id + "\"></div>";
				  inner_html += "<div class=\"middle_cell\" id=\"" + cell2_id + "\">" + currentDay + "</div>";
				  inner_html += "<div class=\"double_cell\" id=\"" + cell3_id + "\"></div>";
				  
				  cell.innerHTML = inner_html ;
				  cell.onclick = null ; // Make the main td unclickable
				  cell.className = null ; // Remove any class in case
				  
				  var cell1 = document.getElementById(cell1_id) ;
				  var cell2 = document.getElementById(cell2_id) ;
				  var cell3 = document.getElementById(cell3_id) ;
					
				  cell1.day = cell2.day = cell3.day = currentDay ;
					
				  cell1.onclick = function(currentDay) 
				  {
					if (self.onclick) 
					  self.onclick( selectedYear, selectedMonth+1, this.day , 1) ;
					return false ;	
				  }
				  cell2.onclick = function() 
				  {
					if (self.onclick) 
					  self.onclick( selectedYear, selectedMonth+1, this.day , 2) ;
					return false ;	
				  } 
				  cell3.onclick = function(currentDay) 
				  {
					if (self.onclick) 
					  self.onclick( selectedYear, selectedMonth+1, this.day , 3) ;
					return false ;	
				  }
					
				  var o1 = self.getEvent(selectedYear, selectedMonth + 1, currentDay , 1)
				  var o2 = self.getEvent(selectedYear, selectedMonth + 1, currentDay , 2)
				  var o3 = self.getEvent(selectedYear, selectedMonth + 1, currentDay , 3)
					
				  if (o1)
				  {
                    if(o1.isFull == 'False')					
					  className1 = o1.className ;
					else	
					  className1 = "split_" + o1.className + "_full" ;
				  	//cell.title = o.tooltipMsg ;
				  }
				  if (o2)
				  {
                    if(o2.isFull == 'False')					
					  className2 = o2.className ;
					else	
					  className2 = "split_" + o2.className + "_full" ;
					//cell.title = o.tooltipMsg ;
				  }
				  if (o3)
				  {
                    if(o3.isFull == 'False')					
					  className3 = o3.className ;
					else	
					  className3 = "split_" + o3.className + "_full" ;
					//cell.title = o.tooltipMsg ;
				  }
				  cell1.className += " " + className1 ;
				  cell2.className += " " + className2 ;
				  cell3.className += " " + className3 ;
				} // three events in a day 
				
				// go to next day
				day.setDate(currentDay + 1);
			} // end of one row
			
			if (day >= lastOfMonth && table.rows[r+1])
			{
				while (table.rows[r+2])
				{
					table.deleteRow(r+2)
				}
				break;
			}
		}
	}
	
	this.getEvent = function( year, month, day , event_no)
	{
	  result = null ;
	  
	  if (events[year] && events[year][month] && events[year][month][day])
	  {
	    if (! event_no) // if no event no then by default return the first event
		  result = events[year][month][day][0] ;
		else  
		  result = events[year][month][day][event_no - 1] ;
	  }
	  
	  return result ;
	}
	
	// Counts how many event for this day
	this.getEventCount = function( year, month, day )
	{
	  var result = 0 ;
	  
	  if (events[year] && events[year][month] && events[year][month][day])
		result = events[year][month][day].length ;
		  
	  return result ;
	}
	
	this.addEvent = function( year, month, day, objData )
	{
		
		  if (!events[year]) 
		    events[year]=[] ;
		  if (!events[year][month]) 
		    events[year][month]=[] ;
		  if (!events[year][month][day]) 
		    events[year][month][day]=[] ;
		  
		  // Add max 3 events to the same cell	
		  if (! events[year][month][day][0])	
		    events[year][month][day][0] = objData ;
		  else if (! events[year][month][day][1])
		  {	
		    // alert("day: " + day) ; // debug
		    events[year][month][day][1] = objData ;
		  }	
		  else if (! events[year][month][day][2])	
		    events[year][month][day][2] = objData ;
	}


	this.appendTo = function(elm)
	{
		elm.appendChild( container )
		refresh()
	}

}





