function Calendar(oDate, oTrgt, WDaysP, aMonthsP, onDataClick)
{

	var  oContainer = document.getElementById('oCalendarContainer')

	var oCurrentDate = new Date();
	var oRequestDate = oDate; //(!oDate) ? new Date() : oDate;
	var oPrevMonthDate = new Date((!oRequestDate) ? new Date() : oRequestDate);
		oPrevMonthDate.setDate(1);
		oPrevMonthDate.setMonth(((!oRequestDate) ? new Date() : oRequestDate).getMonth()-1);
	var prevMonthDaysCount = Calendar.prototype.getLastDay(oPrevMonthDate);
	var emptyDays = Calendar.prototype.getEmptyWDays((!oRequestDate) ? new Date() : oRequestDate);
	var prevMonthDayStart = (prevMonthDaysCount-emptyDays) + 1;
	var currentMonthDaysCount = Calendar.prototype.getLastDay((!oRequestDate) ? new Date() : oRequestDate);

	Calendar.prototype.currentDate = oRequestDate;

	Calendar.prototype.onDataClick = onDataClick;


	if(!WDaysP)
		var aWeekDays = new Array("Д", "С", "Ч", "П", "&#1174;", "Ш", "Я");
	else
		var aWeekDays = WDaysP;

	if(!aMonthsP)
		var aMonths = new Array("Гыйнвар", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь");
	else
		var aMonths = aMonthsP;

	var oMainTable = document.createElement('TABLE');
		oMainTable.id = 'oCalendarTable';
		oMainTable.cellPadding = 0;
		oMainTable.cellSpacing = 0;
		oMainTable.border = 0;
		oMainTable.className = 'Calendar_MainTable';
	var oMainRow = oMainTable.insertRow(oMainTable.rows.length);

	var oMainCell = oMainRow.insertCell(oMainRow.cells.length);
		oMainCell.style.width = '100px';
		oMainCell.className = 'Calendar_Sliders';

		var oMonthYearTable = document.createElement('TABLE');
			oMonthYearTable.id = 'oCalendarMonthYearTable';
			oMonthYearTable.cellPadding = 0;
			oMonthYearTable.cellSpacing = 1;
			oMonthYearTable.border = 0;
			oMonthYearTable.width="100%";

		oMainCell.appendChild(oMonthYearTable);

		var oMonthYearTableRow = oMonthYearTable.insertRow(oMonthYearTable.rows.length);

		var oMonthCell = oMonthYearTableRow.insertCell(oMonthYearTableRow.cells.length);
		oMonthCell.width="60%";

		var oSelectMonth = document.createElement("SELECT");
		oSelectMonth.className = "oCalendar_month_selector"
		//oSelectMonth.style.width="100%";
		oMonthCell.appendChild(oSelectMonth);
		for(var i=0; i<aMonths.length;i++)
		{
			var oMonth = document.createElement("OPTION");
			oSelectMonth.appendChild(oMonth);
			oMonth.value = i;
			oMonth.name = "M" + i;
			oMonth.innerHTML = aMonths[i];
			if( ((!oRequestDate)?new Date():oRequestDate).getMonth() == i )
				oMonth.selected = true;
		}
		oSelectMonth.selectedIndex = ( (!oRequestDate)?new Date():oRequestDate ).getMonth();

		oSelectMonth.onchange = function() {
			var oCurrentDate = new Date((!Calendar.prototype.currentDate)?new Date():Calendar.prototype.currentDate);
			var oNewDate = new Date(oCurrentDate);
			vall = this.value
			if(vall < 0)
				vall = oCurrentDate.getMonth();
			oNewDate.setMonth(vall);
			Calendar(oNewDate,null,null,null,Calendar.prototype.onDataClick);
		}

		var oYearCell = oMonthYearTableRow.insertCell(oMonthYearTableRow.cells.length);
		oYearCell.width="40%";

		var oSelectYear = document.createElement("SELECT");
		oSelectYear.className = "oCalendar_year_selector"
		//oSelectYear.style.width="100%";
		oYearCell.appendChild(oSelectYear);
		for(var i=1977; i<2099;i++)
		{
			var oYear = document.createElement("OPTION");
			oSelectYear.appendChild(oYear);
			oYear.value = i;
			oYear.name = "Y" + i;
			oYear.innerHTML = i;
			if( ((!oRequestDate)?new Date():oRequestDate ).getFullYear() == i)
				oYear.selected = true;
		}
		oSelectYear.selectedIndex = ( (!oRequestDate)?new Date():oRequestDate ).getFullYear() - 1977;

		oSelectYear.onchange = function() {
			var oCurrentDate = new Date((!Calendar.prototype.currentDate)?new Date():Calendar.prototype.currentDate);
			var oNewDate = new Date(oCurrentDate);
			vall = this.value
			if(vall == 0)
				vall = oCurrentDate.getFullYear();
			oNewDate.setFullYear(vall);
			Calendar(oNewDate,null,null,null,Calendar.prototype.onDataClick);
		}

	var oMainRow = oMainTable.insertRow(oMainTable.rows.length);
	var oMainCell = oMainRow.insertCell(oMainRow.cells.length);
		oMainCell.colSpan = 3;

	var oNewTable = document.createElement('TABLE');
	oNewTable.cellPadding = 0;
	oNewTable.cellSpacing = 0;
	oNewTable.className = 'Calendar_Table';

	myNewRow = oNewTable.insertRow(oNewTable.rows.length);
	for (i=0; i<7; i++)
	{
		myNewCell = myNewRow.insertCell(myNewRow.cells.length);
		if(myNewRow.cells.length > 5)
			myNewCell.className = 'Calendar_days_wdays_weekend';
		else
			myNewCell.className = 'Calendar_days_wdays';
		myNewCell.innerHTML = aWeekDays[i];

	}

	myNewRow = oNewTable.insertRow(oNewTable.rows.length);
	for (var i=0; i<emptyDays; i++)
	{
		myNewCell = myNewRow.insertCell(myNewRow.cells.length);
		if(i > (emptyDays + prevMonthDayStart > 30 ? 4 : 3))
			myNewCell.className = 'Calendar_days_out_weekend';
		else
			myNewCell.className = 'Calendar_days_out';

		myNewCell.innerHTML = prevMonthDayStart;
		prevMonthDayStart++;
		myNewCell.onmousedown = function()
		{
			var oCurrentDate = (!Calendar.prototype.currentDate)?new Date():new Date(Calendar.prototype.currentDate);
			var oNewDate = new Date(oCurrentDate);
			oNewDate.setMonth(oCurrentDate.getMonth()-1);
			oNewDate.setDate(parseInt(this.innerHTML));
			Calendar(oNewDate,null,null,null,Calendar.prototype.onDataClick);
		}
	}

	// Anoaaeyai aie oaeouaai ianyoa
	for (var i=1; i<=currentMonthDaysCount; i++)
	{
		if (!(myNewRow.cells.length % 7))
		{
			myNewRow = oNewTable.insertRow(oNewTable.rows.length);
		}

		myNewCell = myNewRow.insertCell(myNewRow.cells.length);

		if(myNewRow.cells.length > 5) {
			if(!oRequestDate)
				myNewCell.className = 'Calendar_days_all_weekend';
			else
				myNewCell.className = (i == ((!oRequestDate)?new Date():oRequestDate).getDate()) ? 'Calendar_days_currday_weekend' : 'Calendar_days_all_weekend';

			if (i == oCurrentDate.getDate() && ((!oRequestDate)?new Date():oRequestDate).getMonth() == oCurrentDate.getMonth() && ((!oRequestDate)?new Date():oRequestDate).getFullYear() == oCurrentDate.getFullYear())  {
				myNewCell.className = 'Calendar_days_today_weekend';
			}
		}
		else {
			if(!oRequestDate)
				myNewCell.className = 'Calendar_days_all';
			else
				myNewCell.className = (i == ((!oRequestDate)?new Date():oRequestDate).getDate()) ? 'Calendar_days_currday' : 'Calendar_days_all';

			if (i == oCurrentDate.getDate() && ((!oRequestDate)?new Date():oRequestDate).getMonth() == oCurrentDate.getMonth() && ((!oRequestDate)?new Date():oRequestDate).getFullYear() == oCurrentDate.getFullYear())  {
				myNewCell.className = 'Calendar_days_today';
			}
		}

		myNewCell.innerHTML = i;
		myNewCell.onmousedown = function()
		{
			var oCurrentDate = (!Calendar.prototype.currentDate)?new Date():new Date(Calendar.prototype.currentDate);
			var oNewDate = new Date(oCurrentDate);
			oNewDate.setDate(parseInt(this.innerHTML));
			if(Calendar.prototype.onDataClick)
				Calendar.prototype.onDataClick(oNewDate);
			Calendar(oNewDate,null,null,null,Calendar.prototype.onDataClick);
		}
	}

	var is31 = i == 31;

	// Anoaaeyai aie neaao?uaai ianyoa
	var nextMonthDaysCount = (7 - myNewRow.childNodes.length);
	for (var i=1; i<=nextMonthDaysCount; i++)
	{
		myNewCell = myNewRow.insertCell(myNewRow.cells.length);
		if(myNewRow.cells.length > 5)
			myNewCell.className = 'Calendar_days_out_weekend';
		else
			myNewCell.className = 'Calendar_days_out';
		myNewCell.innerHTML = i;
		myNewCell.onmousedown = function()
		{
			var oCurrentDate = (!Calendar.prototype.currentDate)?new Date():new Date(Calendar.prototype.currentDate);
			var oNewDate = new Date(oCurrentDate);
			oNewDate.setMonth(oCurrentDate.getMonth()+1);
			oNewDate.setDate(parseInt(this.innerHTML));
			Calendar(oNewDate,null,null,null,Calendar.prototype.onDataClick);
		}
	}


	oMainCell.appendChild(oNewTable);




	// Anoaaeyai eaeaiaa?u a eiioaeia?
	if (oContainer.firstChild) {
		oContainer.removeChild(oContainer.firstChild);
	}

	oContainer.appendChild(oMainTable);

}


Calendar.prototype.getLastDay = function(oDate)
{
	var myDate = new Date(oDate.getYear(), oDate.getMonth()+1, -1);
	return myDate.getDate() + 1;
}

Calendar.prototype.getEmptyWDays = function (oDate)
{
	var myDate = new Date(oDate)
	myDate.setDate(1);
	return (myDate.getDay()) ? myDate.getDay()-1  : 6;
}


