
function CCalendar(sName, nX, nY)
{
	this.CalendarDinamicDraw = new CDinamicDraw("Calendar"+sName, nX, nY);
	this.m_sName = sName;
	var CalendarDate = new Date();
	this.m_nMonth = CalendarDate.getMonth();
	this.m_nYear = CalendarDate.getYear();
	this.m_nDate = CalendarDate.getDate();
	this.arCalendarDays = new Array("Su", "Mo", "Tu", "We","Th", "Fr", "Sa");
	this.UserDateHandler = "alert";
	this.Redraw = CCalendar_Redraw;
	this.SetMonth = CCalendar_SetMonth;
	this.SetYear = CCalendar_SetYear;
	this.SetDate = CCalendar_SetDate;
	this.SetNextDate = CCalendar_SetNextDate;
	this.GetNumDaysInMonth = CCalendar_GetNumDaysInMonth;

	this.CalendarDinamicDraw.Draw();
}

function CCalendar_Redraw()
{
	var sCalendar = '<table class=CalendarTable'+this.m_sName+'><tr class=CalendarDates'+this.m_sName+'>';
	for(i=0; i<7; i++)
	{
		sCalendar += '<td class=Calendar';
		if(i!=6 && i!=0) sCalendar += 'Workdays'; else sCalendar += 'Weekends';
		sCalendar += this.m_sName+'>'+this.arCalendarDays[i]+'</td>';
	}
	sCalendar += '</tr><tr><td colspan=7><table class=CalendarDatesTable'+this.m_sName+' border=1>';
	nDate = 1;
	for(i=0; i<42; i++)
	{
		if(i%7 == 0) sCalendar += '<tr class=CalendarDates'+this.m_sName+(i%7)+'>'
		var d = new Date(this.m_nYear, this.m_nMonth, nDate);
		if(i%7 == d.getDay() && nDate == d.getDate()) 
		{
			if(nDate == this.m_nDate) sCalendar += '<td class=CalendarCurrentDate'+this.m_sName+'>'+
								'<a href="" class=CalendarCurrentDate'+
								this.m_sName+' OnClick="'+this.UserDateHandler+
								'('+nDate+');return false;">'+nDate+'</a></td>';
			else sCalendar += '<td class=CalendarDates'+nDate+' border=1 cellpadding=1>'+
						'<a href="" class=CalendarWeekdates'+this.m_sName+' OnClick="'+
							this.UserDateHandler+'('+nDate+');return false;">'+nDate+'</a></td>';
			nDate++;
		}
		else sCalendar += '<td class=CalendarWeekdates'+this.m_sName+'>&nbsp;</td>';
		if(i%7 == 6) sCalendar += '</tr>';
	}
	sCalendar += '</table></td></tr></table>';
	this.CalendarDinamicDraw.Redraw(sCalendar);
}

function CCalendar_SetMonth(nMonth)
{
	this.m_nMonth = nMonth;
	var d = new Date(this.m_nYear, this.m_nMonth, this.m_nDate);
	this.m_nDate = d.getDate();
	this.Redraw();
}

function CCalendar_SetYear(nYear)
{
	this.m_nYear = nYear;
	var d = new Date(this.m_nYear, this.m_nMonth, this.m_nDate);
	this.m_nDate = d.getDate();
	this.Redraw();	
}

function CCalendar_SetDate(nDate)
{
	var d = new Date(this.m_nYear, this.m_nMonth, nDate);
	this.m_nDate = d.getDate();
	this.Redraw();
}

function CCalendar_SetNextDate()
{
	var d = new Date(this.m_nYear, this.m_nMonth, this.m_nDate);
	d.setTime(d.getTime()+86400000);
	this.m_nYear = d.getYear();
	this.m_nMonth = d.getMonth();
	this.m_nDate = d.getDate();
	this.Redraw();
}

function CCalendar_GetNumDaysInMonth(nMonth, nYear)
{
	var i=0;
	for(i=31; i>27; i--)
	{
		var d = new Date(nYear, nMonth, i);
		if(d.getDate() == i) break;
	}
	return i;
}
