function draw_calendar(id, year, month, monthlist)
{	
	if(year == start_year && year == end_year)
	{
		if(month < start_month)
		{
			month = start_month;
			monthlist.selectedIndex = month-1;
		}
		if(month > end_month)
		{
			month = end_month;
			monthlist.selectedIndex = month-1;
		}
		for(i=1; i <= 12; i++)
		{
			monthlist.options[i-1].style.color = (i < start_month || i > end_month) ? '#a0a0a0' : '#000000';
		}
	}
	else if(year == start_year)
	{
		if(month < start_month)
		{
			month = start_month;
			monthlist.selectedIndex = month-1;
		}
		for(i=1; i <= 12; i++)
		{
			monthlist.options[i-1].style.color = i < start_month ? '#a0a0a0' : '#000000';
		}
	}
	else if(year == end_year)
	{
		if(month > end_month)
		{
			month = end_month;
			monthlist.selectedIndex = month-1;
		}
		for(i=1; i <= 12; i++)
		{
			monthlist.options[i-1].style.color = i > end_month ? '#a0a0a0' : '#000000';
		}
	}
	else
	{
		for(i=1; i <= 12; i++)
		{
			monthlist.options[i-1].style.color = '#000000';
		}
	}
	
	cal_month = month;
	cal_year = year;
	
	if(ajaxurl.length > 0)
	{
		if(cal_month.length == 1) var url2 = ajaxurl + cal_year + '-0' + cal_month; 
		else var url2 = ajaxurl + cal_year + '-' + cal_month;
		
		ccal_http.open("GET", url2, true);
		ccal_http.onreadystatechange = function() { if(ccal_http.readyState == 4) draw_calendar2(id, ccal_http.responseText); };
		ccal_http.send(null);
	}
	else
	{
		draw_calendar2(id, '');
	}
}

function draw_calendar2(id, text)
{
	var titles = new Array(31);
	var titleinfo = new Array(31);
	if(text.length > 0)
	{
		var lines=text.split("\n");
		for(i = 0;i<lines.length; i++)
		{
			if(lines[i].substr(0,1) == '*')
			{
				var da = parseInt(lines[i].substr(1, lines[i].indexOf("|")));
				var me = lines[i].substr(lines[i].indexOf("|")+1);
				titles[da] = me;
				titleinfo[da] = 2;
			}
    		else
			{
				var da = parseInt(lines[i].substr(0, lines[i].indexOf("|")));
				var me = lines[i].substr(lines[i].indexOf("|")+1);
				titles[da] = me;
				titleinfo[da] = 1;								
			}
		}
	}
	
	var myDate = new Date()
	myDate.setFullYear(cal_year, cal_month-1, 1)

	var d = myDate.getDay();
	
	// monday first!
	d = (d+6)%7;

	// get num of days
	var days=30;
	if(cal_month == 1 || cal_month == 3 || cal_month == 5 || cal_month == 7 || cal_month == 8 || cal_month == 10 || cal_month == 12) days=31;
	if(cal_month == 2)
	{
		if(cal_year % 400 == 0) days = 29;
		else if(cal_year % 100 == 0) days = 28;
		else if(cal_year % 4 == 0) days = 29;
		else days = 28;
	}
	
	var i=0, j=d+days+((7-(d+days)%7)%7), output='<table><tr id="calheader">';

	for(i=0;i<7;i++)
	{
		output += '<th>'+sday[i]+'</th>';
	}
	
	output += '</tr>';
	var string;
	var click;
	
	for(i=0;i<j;i++)
	{
		if(i % 7 == 0) output += '<tr>';
		
		if(i<d || (i-d+1)>days) output += '<td class="empty">&nbsp;</td>';
		else
		{
			string = (i-d+1);
			click = 0;
			if(titleinfo[i-d+1] == 1 || titleinfo[i-d+1] == 2)
			{
				string = "<a href='"+linkurl+cal_year+"-"+(cal_month<10?'0':'')+""+cal_month+"-"+(string < 10?'0':'')+""+string+"' onmouseover=\"return overlib('"+titles[i-d+1]+"');\" onmouseout=\"return nd();\">"+string+"</a>";
				
				if(titleinfo[i-d+1] == 1) click = 1;
				else click = 2;
			}
			
			if(cal_year == sel_year && cal_month == sel_month && i-d+1 == sel_day)
			{
				if(click == 2) output += '<td class="selday2">';
				else output += '<td class="selday">';
			}
			else if(cal_year == this_year && cal_month==this_month && i-d+1 == this_day)
			{
				if(click == 2) output += '<td class="today2">';
				else output += '<td class="today">';
			}
			else if(click == 0)
			{
				output += '<td class="notclickable">';
			}
			else
			{
				if(click == 2) output += '<td class="regular2">';
				else output += '<td>';
			}
			
			output += string+'</td>';
		}
		
		if(i % 7 == 6) output += '</tr>';
	}
	output += '</table>';
	
	document.getElementById('calendar_'+id).innerHTML = output;
}

function ccal_getHTTPObject() 
{
	var xmlhttp;
	/*@cc_on
		@if (@_jscript_version >= 5)
			try 
			{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (e) 
			{
				try 
				{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (E)
				{
					xmlhttp = false;
				}
			}
		@else
			xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
		try 
		{
			xmlhttp = new XMLHttpRequest();
		} 
		catch (e) 
		{
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

var ccal_http = ccal_getHTTPObject(); // We create the HTTP Object


