/*
 * calendar	Setaday
 * written by Eugene Ostroukhov
 * 2007
 */
Calendar = function(div)
{
	this.init(div);
}

Calendar.prototype =
{
	init: function (div, day, month, year)
	{
		this.sign =	'Calendar Setaday, version 1.3';
		var $D = document;
		this.elementId = div;
		this.div = $D.getElementById(div);
		this.div.innerHTML = "<table class='calendarLayout' cellspacing='0' cellpadding='0' border='0'><tr><td width='19' class='monthLeft' id='"+div+"calml' title='< Month'><img src='/images/arrow75b.gif' alt='<' width='18' height='16' border='0'></td><td class='header' id='"+div+"header'></td><td  class='monthRight' id='"+div+"calmr' title='Month >'><img src='/images/arrow75.gif' alt='>' width='18' height='16' border='0'></td></tr><tr><td colspan='3' class='mainSpace' id='"+div+"mainSpace'></td></tr><tr><td width='19' class='dayLeft' id='"+div+"caldl' title='< Day'><b>&lt;</b></td><td class='footer' id='"+div+"footer'></td><td class='dayRight' id='"+div+"caldr' title='Day >'><b>&gt;</b></td></tr></table>";

/*		this.div.innerHTML = "<table class='calendarLayout' cellspacing='0' cellpadding='0' border='0'><tr><td width='19' class='monthLeft' id='"+div+"calml' title='< Month'></td><td class='header' id='"+div+"header'></td><td  class='monthRight' id='"+div+"calmr' title='Month >'></td></tr><tr><td colspan='3' class='mainSpace' id='"+div+"mainSpace'></td></tr><tr><td width='19' class='dayLeft' id='"+div+"caldl' title='< Day'></td><td class='footer' id='"+div+"footer'></td><td class='dayRight' id='"+div+"caldr' title='Day >'></td></tr></table>"; */
		var el;
		var _this = this;
		this.weekOffset	= 0;

		el = $D.getElementById(div+'calml');
// month left
		el.onclick = function()	{
			var m =	_this.month, y = _this.year, d = _this.date;
			m --;
			if(m ==	-1) {
				m = 11;
				y --;
			}
			if((_this.minYear >= y && _this.minMonth > m)) return;
			if(d > _this.getDaysCount(y, m)) d = _this.getDaysCount(y, m);

			if(_this.minYear == y && _this.minMonth	>= m &&	_this.minDate >= d) d =	_this.minDate +	1;

			_this.year = y;	_this.month = m; _this.date = d;
			_this.setDay();
			_this.draw();
			_this._onSelectDate();
		}

// month right
		el = $D.getElementById(div+'calmr');
		el.onclick = function()	{
			_this.month++;
			if(_this.month == 12) {
				_this.month = 0;
				_this.year ++;
			}
			if(_this.date >	_this.getDaysCount(_this.year, _this.month)) _this.date	= _this.getDaysCount(_this.year, _this.month);
			_this.setDay();
			_this.draw();
			_this._onSelectDate();
		}

// day left
		el = $D.getElementById(div+'caldl');
		el.onclick = function()	{
			var d =	_this.date, m =	_this.month, y = _this.year;
			if(--d < 1)
			{
				if(--m < 0)
				{
					y--;
					m = 11;
				}
				d = _this.getDaysCount(y, m);
			}
			if((_this.minYear >= y && _this.minMonth > m) || (_this.minYear	>= y &&	_this.minMonth >= m && d <= _this.minDate)) return;
			_this.year = y;	_this.month = m; _this.date = d;
			_this.setDay();
			_this.draw();
			_this._onSelectDate();
		}

// right left
		el = $D.getElementById(div+'caldr');
		el.onclick = function()	{
			if(++_this.date	> _this.getDaysCount(_this.year, _this.month)) {
				if(++_this.month     > 11)
				{
					_this.month = 0;
					_this.year++;
				}
				_this.date = 1
			}
			_this.setDay();
			_this.draw();
			_this._onSelectDate();
		}

		var date = new Date;
		this.year = date.getFullYear();
		this.month = date.getMonth();
		this.date = date.getDate();
		this.day = date.getDay();

		if(typeof year != 'undefined') this.year = year;
		if(typeof month	!= 'undefined') this.month = month;
		if(typeof day != 'undefined') this.date = day;
		this.setDay();

		this.outputFormat =	"%d/%M/%Y";
		this.outputField = null;
	},

	setDate: function(day, month, year)
	{
		this.year =	year;
		this.month = month - 1;
		this.date =	day;
		this.setDay();
	},

	timestamp: function()
	{
		return Date.UTC(this.year, this.month, this.date);
	},

	setAlign: function(id, horAlign, vertAlign)	{
		this.alignTo = id;
		this.horAlign =	horAlign;
		this.vertAlign = vertAlign;
	},

	changeClassHover: function(ev)
	{
		var	ev = window.event || ev;
		var	target=window.event? window.event.srcElement: ev.target;
		switch(ev.type)
		{
			case 'mouseover' : target.className = 'Hovered'; break;
			case 'mouseout' : target.className = 'hNormal'; break;
		}

	},
	dateToString: function(format)
	{
		var	tmp;
		format = format.replace("%d", this.date);
		format = format.replace("%m", this.month);
		format = format.replace("%Y", this.year);
		tmp	= (this.date < 10) ? "0" : "";
		format = format.replace(/%D/, tmp+this.date);
		var	month =	this.month + 1;
		tmp	= (month < 10) ? "0" : "";
		format = format.replace(/%M/, tmp+(month));
		tmp	= this.year.toString();
		format = format.replace("%y", tmp.substring(2, 4));
		format = format.replace("%O", calendarMonth[this.month]);
		format = format.replace("%o", calendarShortMonth[this.month]);
		format = format.replace("%s", calendarShortDays[this.day]);
		format = format.replace("%l", calendarDays[this.day]);
		return format;
	},

	isLeapYear:	function(year)
	{
		if(Math.abs((year) % 4)	== 0) return true;
		return false;
	},

	getDaysCount: function(year, month)
	{
		if(this.isLeapYear(year))
			var	days_in_month =	[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
		else
			var	days_in_month =	[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

		return days_in_month[month];
	},

	getFirstMonthDay: function(year, month)	// return :	index;
	{
		var	days = 0;
		var	ret;
		ret	= 5	* parseInt(year	/ 4, 10);
		if(year	% 4 > 0) ret +=	2 + (year % 4) - 1;
		ret += 5;
		for(i=0; i < month; i++) days += this.getDaysCount(this.year, i);

		return (ret + days - this.weekOffset) %	7;
	},

	setMinDate: function(date, month, year)
	{
		if(arguments.length == 0)
		{
			var d =	new Date;

			date = d.getDate() - 1;
			month =	d.getMonth();
			year = d.getFullYear();
		}
		this.minYear = year;
		this.minMonth =	month;
		this.minDate = date;
	},

	draw: function()
	{
		var parent = document.getElementById(this.elementId+'mainSpace');
		if(parent.childNodes.length)
			parent.removeChild(parent.childNodes[0]);

		var  table = document.createElement("TABLE");
		table.setAttribute("cellSpacing", '0px');
		table.setAttribute("cellPadding", '0px');
		table.className	= 'monthLattice';
		var row	= table.insertRow(0);
		var cell;

		var j =	this.weekOffset;
		for(var	i=0; i<	7; i++)	{
			s = calendarShortDays[i];
			cell = row.insertCell(i);
			cell.className = 'dayofweek';
			cell.innerHTML = calendarShortDays[j].substr(0,	2);
			if(j ==	6) j = 0;
			else j++;
		}
		var i =	0, j = 0, cnt =	0;
		var k =	0;


		if(this.minYear	&& this.minMonth && this.minDate)

		var disabledEntire = false;
		var currentMonth = false;


		if(this.year <=	this.minYear &&	this.month < this.minMonth)  
			disabledEntire = true;
		else if(this.year == this.minYear && this.month	== this.minMonth)  
			currentMonth = true

		while(true)
		{
			if(!(i % 8)) {
				row = table.insertRow(j+1); 
				k = 0;
			}
			else
			{
				if(i > this.getFirstMonthDay(this.year,	this.month))
				{
					var _this = this;

					if( disabledEntire || (	currentMonth &&	cnt < this.minDate))
					{
						cnt ++;
						cell = row.insertCell(k);
						cell.className = 'dateCellDisabled';
						cell.innerHTML = cnt;
						k++; i++;

						continue;
					}

					cnt ++;
					if(cnt == this.date)
					{
						cell = row.insertCell(k);
						cell.className = 'dateCellSelected';
						cell.id	= this.elementId + "caldtd" + cnt;
						cell.innerHTML = cnt;
						this.selectedDate = 'caldtd' + cnt;
					}
					else 
					{
						cell = row.insertCell(k);
						cell.className = 'dateCell';
						cell.id	= this.elementId + "caldtd" + cnt;
						cell.innerHTML = cnt;
					}
					cell.onclick = function() 
					{
						if(_this.date == this.innerHTML) return;
						if(this.innerHTML == '') return;

						_this.date = this.innerHTML;
						document.getElementById(_this.elementId	+ _this.selectedDate).className	= 'dateCell';
						_this.setDay();
						_this.selectedDate = 'caldtd' + this.innerHTML;
						document.getElementById(_this.elementId+'header').innerHTML = _this.dateToString("%O %Y");
						document.getElementById(_this.elementId+'footer').innerHTML = _this.dateToString("%l %d");
						this.className = 'dateCellSelected';
						_this._onSelectDate();
					}
					k++;
				}
				else {		      
					cell = row.insertCell(k);
					cell.className = 'dateCell';
					k++;
				}
			}
			if(cnt >= this.getDaysCount(this.year, this.month)) break;
			if(!(i % 8)) j ++; i ==	0;
			i++;
		}

		parent.appendChild(table);
		parent.childNodes[0].setAttribute('align', 'center');
		document.getElementById(this.elementId+'header').innerHTML = this.dateToString("%O %Y");
		document.getElementById(this.elementId+'footer').innerHTML = this.dateToString("%l %d");
	},
	setDay:	function()
	{
		var d =	new Date();
		d.setDate(this.date);
		d.setMonth(this.month);
		d.setYear(this.year);
		this.day = d.getDay();
	},
	_onSelectDate: function()
	{
		if(this.onSelectDate) this.onSelectDate(this.dateToString(this.outputFormat))
	},

	getDate: function() 
	{
		return this.dateToString(this.outputFormat);
	}
}
