gYear = '';
gMonth = '';
gDay = '';

function calendarJsMod( domID, day, month, year, day_names_len  ) {
	
	var day_names, month_names, first_day_month, calendar, eachweek, eachweek_cnt, fullweek, days, row, idStyle, classStyle;
	//var highlightDays = document.getElementById("highlightDays").value.split(","); alert(highlightDays);
	
	
	gYear = year;
	gMonth = month;
	gDay = day;

	day_names = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	month_names = new Array("", "January","February","March","April","May","June","July","August","September","October","November","December");
	
	first_day_month = new Date(year,(month-1),1).getDay();

	calendar = '<table id="calendar"> <tr id="nav">';
	
	calendar += '<th colspan="1" id="prevMonth" onClick="calendar_prev(\'' + domID + '\', '+ day +', '+ month +', '+ year +', '+ day_names_len +' );"> &laquo; </th>';
	calendar += '<th colspan="6" id="month"> ' + month_names[month] + ' ' + year + ' <img id="loading" src="img/loader_.gif"> </th>';
	calendar += '<th colspan="1" id="nextMonth" onClick="calendar_next(\'' + domID + '\', '+ day +', '+ month +', '+ year +', '+ day_names_len +' );"> &raquo; </th>';

	calendar += '</tr> <tr id="weekdays">';
	
	for(w = 0; w < 7; w++) 
		calendar += '<th class="weekday"> ' + day_names[w].substr(0,day_names_len) + '</th>';
		
		
	calendar += '</tr> <tr class="days">';
		
		
	for(w = 0; w < first_day_month; w++ ) { calendar += '<td class="noday"> &nbsp; </td>'; }
		
	eachweek = 0;
	eachweek_cnt = 0;
	
	fullweek = [];
	
	if(month == 1) var mon_len = 31;
	else if(month == 2) var mon_len = 28;
	else if(month == 3) var mon_len = 31;
	else if(month == 4) var mon_len = 30;
	else if(month == 5) var mon_len = 31;
	else if(month == 6) var mon_len = 30;
	else if(month == 7) var mon_len = 31;
	else if(month == 8) var mon_len = 31;
	else if(month == 9) var mon_len = 30;
	else if(month == 10) var mon_len = 31;
	else if(month == 11) var mon_len = 30;
	else if(month == 12) var mon_len = 31;
	
	for(days = 1; days <= mon_len; days++, first_day_month++) {
	
		if(first_day_month == 7) {
			
			++eachweek_cnt;
			
			if( eachweek_cnt == 1 )
				row = "st";
			else if( eachweek_cnt == 2 ) 
				row = "nd";
			else if( eachweek_cnt == 3 )
				row = "rd";
			else if( eachweek_cnt == 4 || eachweek_cnt == 5 || eachweek_cnt == 6 )
				row = "th";
				
			eachweek = 0;
			first_day_month = 0;
			calendar += '<td class="fullweek" onClick="calendar_week(\' ' + fullweek.join(',')+ ' \', \' ' + (eachweek_cnt) + row + ' \');"> ' + (eachweek_cnt) + row + ' </td> </tr> <tr class="days">';
			fullweek = [];

		}
		
		//set class for different days
		if( days == day ) { classStyle = 'today'; } else if( first_day_month == 0 || first_day_month == 6 ) { classStyle = 'weekend'; } else { classStyle = 'day'; }
		//if( highlightDays.indexOf(days) > -1 ) { classStyle = 'highlight'; }
		
		idStyle = domID + 'C' + days + 'C' + month + 'C' + year;
		
		calendar += '<td id="' + idStyle + '"  class="' + classStyle + '" onClick="calendar_day(\'' + idStyle + '\');"> ' + days + ' </td>';
		
		fullweek[eachweek++] = idStyle;
		
	}
		
	for(w = 0; w < 7-first_day_month; w++ ) { calendar += '<td class="noday"> &nbsp; </td>'; }
		
	calendar += '<td class="fullweek" onClick="calendar_week(\' ' + fullweek.join(',')+ ' \', \' ' + (eachweek_cnt+1) + row + ' \');"> ' + (eachweek_cnt+1) + row + ' </td> </tr> </table>';
	
	document.getElementById(domID).innerHTML = calendar;
	
	calendar_hilite(domID, year, month);
	
}

function calendar_day( day ) { return false; }
function calendar_week( days, week ) { return false; }
function calendar_prev( domID, day, month, year, day_names_len, highlightDays  ) {
	
	var prev_month, prev_year;
	
	if( (month - 1) < 1 ) { prev_month = 12; prev_year = year - 1; } else { prev_month = month - 1; prev_year = year; }
	
	calendarJsMod(domID, day, prev_month, prev_year, 3);
	
}

function calendar_next( domID, day, month, year, day_names_len, highlightDays  ) {
	
	var next_month, next_year;
	
	if( (month + 1) > 12 ) { next_month = 1; next_year = year + 1; } else { next_month = month + 1; next_year = year; }
	
	calendarJsMod(domID, day, next_month, next_year, 3);
}
function calendar_hilite(domID, year, month) { 
	
	var hilite, idStyle;
	
	trace(domID+'-'+year+'-'+month);
	trace('year='+year+'&month='+month+'&action=hilite&tp_id='+tp_id);
	
	new Ajax.Request('http://sklad.cumc.columbia.edu/ent/calendar/events.php', 
	{ 
		method: 'post', 
		//asynchronous: false,  	
		parameters: 'year='+year+'&month='+month+'&action=hilite&tp_id='+tp_id,
		onComplete: function(transport){
			var response = transport.responseText || "";
			trace("<xmp>Success! \n\n" + response + "</xmp>");
			
			var today = new Date();
			
			for(h = 1; h <= 31; h++ ) { if(h != 20) { idStyle = domID + 'C' + h + 'C' + month + 'C' + year; $(idStyle).setStyle({ color: '#000000', fontWeight: 'normal' }); } }
			
			if( (hilite = response.split(',')) != '') {
			
				for(h = 0; h < hilite.length; h++ ) { idStyle = domID + 'C' + hilite[h] + 'C' + month + 'C' + year; $(idStyle).setStyle({ color: '#ff0000', fontWeight: 'bold' }); }
	
			}
			
			idStyle = domID + 'C' + 20 + 'C' + month + 'C' + year; $(idStyle).setStyle({ fontWeight: 'bold' });
			
		},
		onFailure: function(){ 
			trace('Something went wrong...'); 
		}
	});

}