var calendar_req = false;
var month_days = Array();
var calendar_days = Array();
var calendar_url = '/bydate.html?';

function GetMaxDays(month, year) {
	if (month==1 || month==3 || month==5 || month==7 ||
		month==8 || month==10 || month==12)
		return 31;
	if (month==4 || month==6 || month==9 || month==11)
		return 30;

	if (year%400==0 || (year%100!=0 && year%4==0))
		return 29;

	return 28;
}

function UpdateDays() {
	var month = document.getElementById('cur_month').value;
	var year = document.getElementById('cur_year').value;
	var maxdays = GetMaxDays(month, year);
	var firstday = new Date(year, month-1, 1);
	var weekday = firstday.getDay();
	
	var curday = 1;

	var table = document.getElementById('calendar_ajax_days');

	if (weekday==0) weekday = 7;

	var row = document.createElement('TR');	

	var maxdata = calendar_days.length;
	var curdata = 0;

	var current_date = new Date();

	for (var i=1;i<weekday;i++) {
		var elem = document.createElement('TD');
		elem.innerHTML = '&nbsp;';
		row.appendChild(elem);
	}
	for (i=weekday;i<=7;i++) {
		var elem = document.createElement('TD');

		if (month == current_date.getMonth()+1 &&
			year == current_date.getFullYear() &&
			curday == current_date.getDate())
			elem.className = 'current';

		if (curdata<maxdata && calendar_days[curdata]==curday) {
			elem.setAttribute('id','common');
			elem.innerHTML = '<a href="'+calendar_url+'current_date='+curday+'-'+month+'-'+year+'">'+curday+'</a>';
			curdata++;
		} else
			elem.innerHTML = curday;
		row.appendChild(elem);
		curday++;
	}

	table.appendChild(row);
	while(curday<=maxdays) {
		row = document.createElement('TR');
		for (i=0;i<7;i++) {
			var elem = document.createElement('TD');

			if (month == current_date.getMonth()+1 &&
				year == current_date.getFullYear() &&
				curday == current_date.getDate())
				elem.className = 'current';

			if (curday>maxdays)
				elem.innerHTML = '&nbsp;';
			else
				if (curdata<maxdata && calendar_days[curdata]==curday) {
					elem.setAttribute('id','common');
					elem.innerHTML = '<a href="'+calendar_url+'current_date='+curday+'-'+month+'-'+year+'">'+curday+'</a>';
					curdata++;
				} else
					elem.innerHTML = curday;
			row.appendChild(elem);
			curday++;
		}
		table.appendChild(row);
	}	
}

function SetDate(month, year) {
	var mn = Array();
	mn[1] = 'Январь';
	mn[2] = 'Февраль';
	mn[3] = 'Март';
	mn[4] = 'Апрель';
	mn[5] = 'Май';
	mn[6] = 'Июнь';
	mn[7] = 'Июль';
	mn[8] = 'Август';
	mn[9] = 'Сентябрь';
	mn[10] = 'Октябрь';
	mn[11] = 'Ноябрь';
	mn[12] = 'Декабрь';
}

function ClearDays() {
	var elem = document.getElementById('calendar_ajax_days');
	var children = elem.childNodes;
	
	while(children.length > 0) {
		elem.removeChild(children.item(0));
	}	
}

function OnLoadedDays() {
	if (calendar_req.readyState == 4) {
		var text = calendar_req.responseText;

		var month = document.getElementById('cur_month').value;
		var year = document.getElementById('cur_year').value;
		month_days = text.split(' ');

		m2 = 0;
		calendar_days = Array();
		for (m1=0;m1<month_days.length;m1++) {
			s = month_days[m1];
			m3 = s.indexOf('.');
			month1 = s.substring(0, m3);
			if (month1==month)
				calendar_days[m2++] = s.substring(m3+1, s.length);
		}
		
		UpdateDays();
	} else {
	    // still not ready
	}
}

function MonthChange() {
	ClearDays();
	
	var month = document.getElementById('cur_month').value;
	var year = document.getElementById('cur_year').value;

	m2 = 0;
	calendar_days = Array();
	for (m1=0;m1<month_days.length;m1++) {
		s = month_days[m1];
		m3 = s.indexOf('.');
		month1 = s.substring(0, m3);
		if (month1==month)
			calendar_days[m2++] = s.substring(m3+1, s.length);
	}
		
	UpdateDays();
}

function YearChange() {
	if (window.XMLHttpRequest) {
		calendar_req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		calendar_req = new ActiveXObject("Microsoft.XMLHTTP");
	}

	calendar_req.onreadystatechange = OnLoadedDays;

	var month = document.getElementById('cur_month').value;
	var year = document.getElementById('cur_year').value;

	megadate = '1-'+month+'-'+year;
	
	ClearDays();

	var days_xml_name = 'http://www.r-19.ru/getdays.html?current_date='+megadate;

	calendar_req.open('GET', days_xml_name, true);
	calendar_req.send(null);
}

function DaysRequest(dir) {

	if (window.XMLHttpRequest) {
		calendar_req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		calendar_req = new ActiveXObject("Microsoft.XMLHTTP");
	}

	calendar_req.onreadystatechange = OnLoadedDays;

	var megadate = document.getElementById('calendar_megadate').innerHTML;
	var m1 = megadate.indexOf('-');
	var m2 = megadate.indexOf('-', m1+1);
	var month = megadate.substring(m1+1, m2);
	var year = megadate.substring(m2+1, megadate.length);

	if (dir<0) {		
		month--;
		if (month<=0) {
			month = 12;
			year--;
		}
	} else if (dir>0) {
		month++;
		if (month>12) {
			month=1;
			year++;
		}
	}
	megadate = '1-'+month+'-'+year;
	document.getElementById('calendar_megadate').innerHTML = megadate;
	
	SetDate(month, year);
	ClearDays();

	var days_xml_name = 'http://www.r-19.ru/getdays.html?current_date='+megadate;

	calendar_req.open('GET', days_xml_name, true);
	calendar_req.send(null);

}
