
	function Calendar(url, times, target, month, year, lang) {

		this.url = url;
		this.times = times.split(',').sort();
		this.target = target;
		this.lang = lang;
		if (lang == 'en') {
			this.rusMonths = {1:'january',2:'february',3:'march',4:'april',5:'may',6:'june',7:'july',8:'august',9:'september',10:'october',11:'november',12:'december'};
		} else {
			this.rusMonths = {1:'Январь',2:'Февраль',3:'Март',4:'Апрель',5:'Май',6:'Июнь',7:'Июль',8:'Август',9:'Сентябрь',10:'Октябрь',11:'Ноябрь',12:'Декабрь'};
		}
		var nowDate = new Date();
		var curMonth = nowDate.getMonth() + 1;
		var curYear = nowDate.getFullYear();
		if ((year != 0) && (month != 0)) {
			curMonth = month;
			curYear = year;
		} 
		this.curDate = new Date(curYear, curMonth - 1, 1);
		this.Draw();		
	}	
	
	Calendar.prototype.CreateMonthsSelect = function () {
		var oSel = document.createElement('SELECT');
		
		var text = '';
		for (i = 1; i <= 12; i++) {
			text += '<option ';
			text += 'value = "' + i + '"';
			if (i == (this.curDate.getMonth() + 1)) {
				text += ' selected';
			}
			text += '>' + this.rusMonths[i] + '</option>';
		}
				
		return text;
	}
	
	
	Calendar.prototype.CreateYearsSelect = function () {
		var years = this.GetAllYears();
		
		var text = '';
		for (i = 0; i < years.length; i++) {
			text += '<option value="' + years[i] + '"';
			if (years[i] == this.curDate.getFullYear()) {
				text += ' selected';
			}
			text += '>';
			text += years[i] + '</option>'
		}
		return text;
	}
	Calendar.prototype.GetAllYears = function() {
		var res = new Array();
		res.push(this.curDate.getFullYear());
		for (i = 0;  i < this.times.length; i++) {
			var t = new Date();
			t.setTime(this.times[i]*1000);
			ty = t.getFullYear();
			var f = true;
			for (j = 0; j < res.length; j++) {
				if (res[j] == ty) {
					f = false;
				}
			}
			if (f) {
				res.push(ty);
			}
		}
		
		return res.sort();
	}
	
	Calendar.prototype.selectOption = function(oSel, value) {
		oSel.value = value;
		return;
		for (i = 0; i < oSel.options.length; i++) {
			oSel.options[i].selected = oSel.options[i].value == value;
		}
				
	}
	
	
	Calendar.prototype.Draw = function() {	

		Text = '<table border=0 width=158 cellpadding=0 cellspacing=0 style="margin: 5 0 5 3;"><tr><td width=19><a href="javascript: void(0);" onclick="onMinusMonth();"><img src="/i/calendar_left.gif" width="15" height="12" alt="" border="0"></a></td>';
		Text +=	'<td align=center class=bluetext width=119>' + this.rusMonths[this.curDate.getMonth() + 1] + ' ' + this.curDate.getFullYear() + '</td>';
		Text += '<td><a href="javascript: void(0);" onclick="onPlusMonth();" align=right  width=19><img src="/i/calendar_right.gif" width="15" height="12" alt="" border="0"></a></td>';
		Text += '</tr></table>';
		Text += '<table cellpadding="3" cellspacing="3"  class="calendar" border=0>';		

		var days = this.GetLastDay(this.curDate);
		var startWeekDay = (this.curDate.getDay() +6) % 7;			
		
		var tDate = new Date(this.curDate.getFullYear(), this.curDate.getMonth(), 1);
		for (i = 1 - startWeekDay; i <= days; i+=7) {			
			Text += '<tr>' + this.CreateRow(i, days, tDate).innerHTML + '</tr>';
					
			if ((i + 7) <= days) {
				Text += '';
			} else {
				Text += '';
			}
		}
		
		
		Text += "</table>";
		
		this.target.innerHTML = Text;
	}
	
		
	Calendar.prototype.CreateRow = function(startDay, lastDate, td) {
		oTr = document.createElement('TR');

		//otd = document.createElement('TD');
		//otd.innerHTML = '&nbsp;';
		//oTr.appendChild(otd); 
		ntd = new Date();
		
		for(j = 1; j <= 7; j++) {
			var cur = startDay + j - 1;
			otd1 = document.createElement('TD');
			otd1.align = 'center';
			if ((cur < 1) || (cur > lastDate)) {
				otd1.innerHTML = '&nbsp;';
			} else { 
				
				ctd = new Date(td.getTime());
				ctd.setDate(cur);
				if (this.HasEvent(ctd)) {
					otd1.innerHTML = '<a href="'+this.GetUrl(ctd)+'" >'+ctd.getDate()+'</a>';
				} else {
					otd1.innerHTML = ctd.getDate();
				}
			}
			
			if (j == 7) {
				otd1.className = 'weekend';
			}
			
			oTr.appendChild(otd1);
		}
		
		//oTr.appendChild(otd.cloneNode(false));		
		return oTr;
	}
	
	Calendar.prototype.GetUrl = function(td) {
		return this.url + '&day=' + td.getDate() + '&month=' + (td.getMonth() + 1) + '&year=' + td.getFullYear();
	}
	
	Calendar.prototype.GetSmallUrl = function() {
		return this.url + '&month=' + (this.curDate.getMonth() + 1) + '&year=' + this.curDate.getFullYear();
	}
	
	Calendar.prototype.HasEvent = function(dd) {
		var tom = new Date(dd.getFullYear(), dd.getMonth(), dd.getDate() + 1);
		for (var z = 0; z < this.times.length; z++) {
			c = this.times[z];			 
			if (((dd.getTime() / 1000) <= c) && (c < (tom.getTime() / 1000))) {
				return true;
			}
		}
		
		return false;
	}
		
	Calendar.prototype.GetLastDay = function(d) {
		var lastDate = new Date(d.getFullYear(), d.getMonth() + 1, 1);
		lastDate.setDate(0);
		return lastDate.getDate();
	}
	


	Calendar.prototype.getMS = function() {
		sels = this.target.getElementsByTagName('SELECT')
		return sels[1];
	}
	Calendar.prototype.getYR = function() {
		sels = this.target.getElementsByTagName('SELECT')
		return sels[0];
	}
	
	Calendar.prototype.ChangeMonth = function(month) {
		this.curDate.setMonth(month - 1);
		this.CreateMonthsSelect();
		this.Draw();
	}
	
	Calendar.prototype.ChangeYear = function(year) {
		this.curDate.setFullYear(year);
		this.CreateYearsSelect();
		this.Draw();
	}	
	
	function onMinusMonth() {
		oCal.ChangeMonth(oCal.curDate.getMonth());
	}
		
	function onPlusMonth() {
		oCal.ChangeMonth(oCal.curDate.getMonth() + 2);
	}
		
	function onChangeMonth(obj) {
		oCal.ChangeMonth(obj.value);
	}
	
	function onChangeYear(obj) {
		oCal.ChangeYear(obj.value);
	}
	
	
	function showCalendar(url, times, target, month, year, lang) {
		oCal = new Calendar(url, times, target, month, year, lang);
	}

