function reloadCalendar(month, year) {
	
	var months = [
		'Январь',
		'Февраль',
		'Март',
		'Апрель',
		'Май',
		'Июнь',
		'Июль',
		'Август',
		'Сентябрь',
		'Октябрь',
		'Ноябрь',
		'Декабрь'
		];
	
	var handler = function(result, errors) {
		if(result.result == 'true')
		{
/*			
			for(i in months)
			{
				alert(i + ': ' + months[i]);
			}
*/
			var table = '<table class="calendar">' +
					'<tr>' +
						'<td class="point_l"><a href="previous" onclick="currDate = subMonth(currDate);reloadCalendar(currDate.getMonth() + 1, currDate.getYear());return false;"><img src="/pic/left_point.gif" alt="" /></a></td>' +
						'<td class="month">' + months[month - 1] + ' ' + year + '</td>' +
						'<td class="point_r"><a href="next" onclick="currDate = addMonth(currDate);reloadCalendar(currDate.getMonth() + 1, currDate.getYear());return false;"><img src="/pic/right_point.gif" alt=""/></a></td>' +
					'</tr>' +
				'</table>' +
				'<table class="calendar">' +
					'<tr>' +
						'<td class="day"   width="14%">пн</td>' +
						'<td class="day"   width="14%">вт</td>' +
						'<td class="day"   width="14%">ср</td>' +
						'<td class="day"   width="14%">чт</td>' +
						'<td class="day"   width="14%">пт</td>' +
						'<td class="day"   width="15%">сб</td>' +
						'<td class="day"   width="15%">вc</td>' +
					'</tr>';
			for(var week in result.month)
			{
				table = table + '<tr>';
				for(var day in result.month[week])
				{
					if(result.month[week][day]['activity'] > 0)
					{
						table = table + '<td><a href="/blog/getdigest.html?date=' + year + '-' + month + '-' + result.month[week][day]['day'] + '">' + result.month[week][day]['day'] + '</a></td>';
					}
					else
						table = table + '<td>' + result.month[week][day]['day'] + '</td>';
				}
				table = table + '</tr>';
			}
			table = table + '</table>';
			$('ajax_calendar').innerHTML = table;
		}
		else
			alert(result.result);
	}
//	reloadDateCalendar(month, year);
//	return;
	if(year < 1000)
		year = year + 1900;
	JsHttpRequest.query('/calendar/index.wddx', {'month': month, 'year' : year}, handler, true);	
//	JsHttpRequest.query('/calendar/index.wddx?month=' + month + '&year=' + year, {}, handler, true);	
}



function reloadDateCalendar(month, year) {
	
	var months = [
		'Январь',
		'Февраль',
		'Март',
		'Апрель',
		'Май',
		'Июнь',
		'Июль',
		'Август',
		'Сентябрь',
		'Октябрь',
		'Ноябрь',
		'Декабрь'
		];
	
	var handler = function(result, errors) {
		if(result.result == 'true')
		{
/*			
			for(i in months)
			{
				alert(i + ': ' + months[i]);
			}
*/
			var table = '<table class="calendar">' +
					'<tr>' +
						'<td class="point_l"><a href="previous" onclick="currDate = subMonth(currDate);reloadDateCalendar(currDate.getMonth() + 1, currDate.getYear());return false;"><img src="/pic/left_point.gif" alt="" /></a></td>' +
						'<td class="month">' + months[month - 1] + ' ' + year + '</td>' +
						'<td class="point_r"><a href="next" onclick="currDate = addMonth(currDate);reloadDateCalendar(currDate.getMonth() + 1, currDate.getYear());return false;"><img src="/pic/right_point.gif" alt=""/></a></td>' +
					'</tr>' +
				'</table>' +
				'<table class="calendar">' +
					'<tr>' +
						'<td class="day"   width="14%">пн</td>' +
						'<td class="day"   width="14%">вт</td>' +
						'<td class="day"   width="14%">ср</td>' +
						'<td class="day"   width="14%">чт</td>' +
						'<td class="day"   width="14%">пт</td>' +
						'<td class="day"   width="15%">сб</td>' +
						'<td class="day"   width="15%">вc</td>' +
					'</tr>';
			for(var week in result.month)
			{
				table = table + '<tr>';
				for(var day in result.month[week])
				{
					if(result.month[week][day]['day'] != '&#160;')
					{
						if(((result.month[week][day]['events'] > 0) || (result.month[week][day]['birthdays'] > 0) || (result.month[week][day]['news'] > 0)))
						{
							var title = '';
							if(result.month[week][day]['birthdays'] > 0)
								title += "Дней рождения: " + result.month[week][day]['birthdays'] + " \n";
								
							if(result.month[week][day]['events'] > 0)
								title += "Событий: " + result.month[week][day]['events'] + " \n";
								
							if(result.month[week][day]['news'] > 0)
								title += "Новостей: " + result.month[week][day]['news'] + " \n";
								
//							var title = "Дней рождения: " + result.month[week][day]['birthdays'] + " \nСобытий: " + result.month[week][day]['events'] + "\nНовостей:" + result.month[week][day]['news'];
							table = table + '<td title="' + title + '"><a href="/calendar/index.html?date=' + year + '-' + month + '-' + result.month[week][day]['day'] + '">' + result.month[week][day]['day'] + '</a></td>';
						}
						else
							table = table + '<td>' + result.month[week][day]['day'] + '</td>';
					}
					else
						table = table + '<td>&#160;</td>';
				}
				table = table + '</tr>';
			}
			table = table + '</table>';
			$('ajax_calendar').innerHTML = table;
		}
	}
	if(year < 1000)
		year = year + 1900;
	JsHttpRequest.query('/calendar/calendar.wddx', {'month': month, 'year' : year}, handler, true);	
//	JsHttpRequest.query('/calendar/index.wddx?month=' + month + '&year=' + year, {}, handler, true);	
}





function subMonth(currDate)
{
	var month = currDate.getMonth();
	var year = currDate.getYear();
	if(year < 1000)
		year = year + 1900;

	month = month - 1;
	if(month < 0)
	{
		month = 11;
		year = year - 1;
	}
	
	if(year < 2005)
	{
		return currDate 
	}
	var newDate = new Date(year, month, 1);
	return newDate;
}


function addMonth(currDate)
{
	var month = currDate.getMonth();
	var year = currDate.getYear();
	if(year < 1000)
		year = year + 1900;
	month = month + 1;
	if(month > 11)
	{
		year = year + 1;
		month = 0;
	}
	
	if(year > 2045)
	{
		return currDate 
	}
	var newDate = new Date(year, month);
	return newDate;
}


