

HkCalendar = function(day, month, year) 
{
	this.monthes = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	this.daysNormal = new Array('31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');
	this.daysLeap   = new Array('31', '29', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');
	this.daysCount  = 0;
	
	this.day = parseInt(day); // 1-31
	this.month = parseInt(month); // 1-12
	this.year = parseInt(year);


	this.initDay = this.day;
	this.initMonth = this.month;
	this.initYear = this.year;
	
	this.minYear = 1970;
	this.maxYear = 2038;

	this.selectedDates = new Array();
	
	this.checkEnabledDates = false;
	this.enabledDates = new Array(); //dates allowed to be clicked

	this.table = Object;
	this.startRow = 0;
	
	this.handlerClearDate = '';
	this.handlerSelectDate = '';

	this.classEmptyCell = 'cnone';
	this.classCell = 'cdeff';
	this.classCellSelected = 'cact1';
	this.classCellCurrentDay = 'cact2';
	this.classCellCurrentDaySelected = 'cact3';

	//home inner calendar fixed height
	this.useFixedRows = false;
	this.fixedRows = 0;
}

HkCalendar.prototype.Alert = function()
{
	alert(this.day + " " + this.month + " " + this.year);
}

HkCalendar.prototype.SetTable = function(table, startRow)
{
	this.table = table;
	this.startRow = startRow;
}


HkCalendar.prototype.InitMonth = function()
{
	
}

HkCalendar.prototype.InitDays = function()
{
	if (this.IsLeapYear())
	{
		this.daysCount = this.daysLeap[this.month-1];
	}
	else
	{
		this.daysCount = this.daysNormal[this.month-1];
	}
}

HkCalendar.prototype.IsLeapYear = function()
{
	if ((this.year % 4)==0) return true;
	else return false;
}

HkCalendar.prototype.PrevMonth = function()
{
	this.month--;
	if (this.month==0)
	{
		this.month = 12;
		this.year--;
	}
}

HkCalendar.prototype.NextMonth = function()
{
	this.month++;
	if (this.month==13)
	{
		this.month = 1;
		this.year++;
	}
}

HkCalendar.prototype.RenderMonth = function()
{
	this.InitDays();

	var date = new Date();
	date.setFullYear(this.year, this.month-1, 1);
	var weekDay = date.getDay(); // 0 sunday -> 6 saturday
	
	var emptyBegin = weekDay;
	var beginDays = 7 - weekDay;
	var days2 = this.daysCount - beginDays;
	var endDays = days2 % 7;
	
	var fullWeeks = (days2 - endDays)/7;
	var emptyEnd = 7 - endDays;

	// clear prev rows
	for (var i=this.startRow; i<this.table.rows.length; i++)
	{
		this.table.deleteRow(i);
		i--;
	}
	
	var rowNumber = this.startRow;

	// render first row
	var row = this.table.insertRow(rowNumber);
	for (var i=0; i<emptyBegin; i++)
	{
		this.RenderEmptyCell(row, i);
	}
	var day = 0;
	for (i; i<7; i++)
	{
		day++;
		this.RenderCell(row, i, day);
	}

	// render full weeks
	for (var j=0; j<fullWeeks; j++)
	{
		rowNumber++;
		var row = this.table.insertRow(rowNumber);
		for (i=0; i<7; i++)
		{
			day++;
			this.RenderCell(row, i, day);
		}
	}

	// render last row
	rowNumber++;
	var row = this.table.insertRow(rowNumber);
	for (i=0; i<endDays; i++)
	{
		day++;
		this.RenderCell(row, i, day);
	}
	var cellIndex = i;
	for (i=0; i<emptyEnd; i++)
	{
		day++;
		this.RenderEmptyCell(row, cellIndex);
		cellIndex++;
	}
	
	//make calendar height fixed
	if (this.useFixedRows && rowNumber<this.fixedRows)
	{	
		rowNumber2 = rowNumber+1;
		for (i=0; i<this.fixedRows-rowNumber; i++)
		{
			var row = this.table.insertRow(rowNumber2);
			for (j=0; j<7; j++ )
			{
				this.RenderEmptyCell(row, j);
			}
			rowNumber2++;
		}
	}
}

HkCalendar.prototype.RenderEmptyCell = function(row, idx)
{
	var cell = row.insertCell(idx);
	cell.className = this.classEmptyCell;
	cell.innerHTML = '&nbsp;';
}

HkCalendar.prototype.RenderCell = function(row, idx, day)
{
	var cell = row.insertCell(idx);
	var func = 'void(0)';

	if (this.IsDateEnabled(this.year, this.month, day))
	{
			if (this.IsDateSelected(this.year, this.month, day))
			{
				if (this.year==this.initYear && this.month==this.initMonth && day==this.initDay)
				{
					cell.className = this.classCellCurrentDaySelected;
				} 
				else
				{
					cell.className = this.classCellSelected;	
				}
				func = this.handlerClearDate + '(\'' + this.year + '\', \'' + this.month + '\', \'' + day + '\')';

			}
			else if (this.year==this.initYear && this.month==this.initMonth && day==this.initDay)
			{ 
				// current date
				cell.className = this.classCellCurrentDay;
				func = this.handlerSelectDate + '(\'' + this.year + '\', \'' + this.month + '\', \'' + day + '\')';
			}
			else
			{
				cell.className = this.classCell;
				func = this.handlerSelectDate + '(\'' + this.year + '\', \'' + this.month + '\', \'' + day + '\')';
			}
	}
	else
	{
		cell.className = this.classCell;
	}

	cell.innerHTML = '<a href="javascript:' + func + '">' + day + '</a>';
}

HkCalendar.prototype.GetMonthName = function()
{
	return this.monthes[this.month-1];
}

HkCalendar.prototype.SelectDate = function(year, month, day)
{
	if (!this.IsDateSelected(year, month, day))
	{
		var element = new Object();
		element.year = year;
		element.month = month;
		element.day = day;		
		this.selectedDates.push(element);
	}
}

HkCalendar.prototype.IsDateSelected = function(year, month, day)
{
	
	var len = this.selectedDates.length;
	for (var i=0; i<len; i++)
	{
		var element = this.selectedDates[i];
		if (!year || !element)
		{
			//alert(element);
			//alertObjProps(element, true);
			return;
		}
		if (element.year==year && element.month==month && element.day==day)
		{
			return true;
		}
	}
	return false;
}

HkCalendar.prototype.EnableDate = function(year, month, day)
{
	if (!this.IsDateEnabled(year, month, day))
	{
		var element = new Object();
		element.year = year;
		element.month = month;
		element.day = day;		
		this.selectedDates.push(element);
	}
}

HkCalendar.prototype.IsDateEnabled = function(year, month, day)
{
	if (!this.checkEnabledDates) return true;

	var len = this.enabledDates.length;
	for (var i=0; i<len; i++)
	{
		var element = this.enabledDates[i];
		if (element.year==year && element.month==month && element.day==day)
		{
			return true;
		}
	}
	return false;
}

HkCalendar.prototype.ClearDate = function(year, month, day)
{
	var len = this.selectedDates.length;
	for (var i=0; i<len; i++)
	{
		var element = this.selectedDates[i];
		if (element)
		{
			if (element.year==year && element.month==month && element.day==day)
			{
				this.selectedDates.splice(i, 1);
			}
		}
	}
}

HkCalendar.prototype.IsPrevButtonAllowed = function()
{
	if (this.year>this.minYear || (this.year==this.minYear && this.month>1)) return true;
	else return false;
}

HkCalendar.prototype.IsNextButtonAllowed = function()
{
	if (this.year<this.maxYear || (this.year==this.maxYear && this.month<12)) return true;
	else return false;
}