/* VERSJA 2.0 */
/* changelog: 2.0: dodanie rozpoznawania selectów */

function set_str(value, id)
{
	document.getElementById(id).innerHTML=value;
}

month_length=new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
weekday_names=new Array('pn', 'wt', 'śr', 'cz', 'pt', 'so', 'nd');
month_names=new Array('styczeń', 'luty', 'marzec', 'kwiecień', 'maj', 'czerwiec', 'lipiec', 'sierpień', 'wrzesień', 'październik', 'listopad', 'grudzień');

var actual=new Date();

var actual_day=actual.getDate();
var actual_year=actual.getYear();
var actual_month=actual.getMonth();
if(actual_year<1900) actual_year+=1900;
var last_id=0;

calendar_class.prototype.return_date=return_date;
calendar_class.prototype.calendar_get_form=calendar_get_form;
calendar_class.prototype.calendar=calendar;
calendar_class.prototype.close=close;

function calendar_class(class_id, form_date, form_day, form_month, form_year, x_offset, y_offset)
{
	if(!x_offset) x_offset=0;
	if(!y_offset) y_offset=0;

	this.id=class_id+'_div';
	this.class_id=class_id;

	if(form_date)
	{
		this.sel_day=Number(form_date.value.substring(8, 10));
		this.sel_month=Number(form_date.value.substring(5, 7));
		this.sel_year=Number(form_date.value.substring(0, 4));
	}
	else
	{
		this.sel_day=this.calendar_get_form(form_day);
		this.sel_month=this.calendar_get_form(form_month);
		this.sel_year=this.calendar_get_form(form_year);
	}

	if(this.sel_month) this.sel_month--;

	this.ret_form_date=form_date;
	this.ret_form_day=form_day;
	this.ret_form_month=form_month;
	this.ret_form_year=form_year;

	if(this.sel_day && this.sel_year) set_str(this.calendar(this.sel_year, this.sel_month), this.id);
	else set_str(this.calendar(actual_year, actual_month), this.id);
	document.getElementById(this.id).style.display='inline';
}

	function calendar_get_form(id)
	{
		if(id.options)
		{
			for(i=0;i<id.options.length;i++)
			{
				if(id.options[i].selected)
				{
					return Number(id.options[i].value);
				}
			}
			return 0;
		}
		else return Number(id.value);
	}
	function return_date(ret_day, ret_month, ret_year)
	{
		if(this.ret_form_date)
		{
			this.ret_form_date.value=ret_year + '-'+(ret_month<9?'0':'')+(ret_month+1)+'-' +(ret_day<10?'0':'')+ret_day;
		}
		else
		{
			if(this.ret_form_day)
			{
				if(this.ret_form_day.options)
				{
					for(i=0;i<this.ret_form_day.options.length;i++)
					{
						if(Number(this.ret_form_day.options[i].value)==ret_day) this.ret_form_day.options[i].selected=true;
						else this.ret_form_day.options[i].selected=false;
					}
				}
				else
				{
					ret_day=(ret_day<10?'0':'')+ret_day;
					this.ret_form_day.value=ret_day;
				}
			}
			if(this.ret_form_month)
			{
				if(this.ret_form_month.options)
				{
					for(i=0;i<this.ret_form_month.options.length;i++)
					{
						if(Number(this.ret_form_month.options[i].value)==(ret_month+1)) this.ret_form_month.options[i].selected=true;
						else this.ret_form_month.options[i].selected=false;
					}
				}
				else
				{
					ret_month=(ret_month<9?'0':'')+(ret_month+1);
					this.ret_form_month.value=ret_month;
				}
			}
			if(this.ret_form_year)
			{
				if(this.ret_form_year.options)
				{
					for(i=0;i<this.ret_form_year.options.length;i++)
					{
						if(Number(this.ret_form_year.options[i].value)==ret_year) this.ret_form_year.options[i].selected=true;
						else this.ret_form_year.options[i].selected=false;
					}
				}
				else this.ret_form_year.value=ret_year;
			}
		}
		this.close();
	}

	function calendar(year, month)
	{
		var i;
		var max_month_length;
		var cal=new Date();
		cal.setYear(year);
		cal.setMonth(month);

		cal.setDate(1);
		var first_day=cal.getDay();

		if((year%4==0 && year%100!=0 || year%400==0) && month==1) max_month_length=29;
		else max_month_length=month_length[month];

		if(month==11)
		{
			var n_month=0;
			var n_year=year+1;
			var p_month=10;
			var p_year=year;
		}
		else if(month==0)
		{
			var n_month=1;
			var n_year=year;
			var p_month=11;
			var p_year=year-1;
		}
		else
		{
			var n_month=month+1;
			var n_year=year;
			var p_month=month-1;
			var p_year=year;
		}

		var cal_str='<table class="title"><tr><td><span onclick="'+this.class_id+'.return_date('+actual_day+', '+actual_month+', '+actual_year+');">dziś</span></td><th><img src="/gfx/calendar/calendar_r.gif" alt="miesiąc w dół" onclick="set_str('+this.class_id+'.calendar('+p_year+', '+p_month+'), \''+this.id+'\')" /></th><th style="width: 100%">'+month_names[month]+'</th><th><img src="/gfx/calendar/calendar_f.gif" alt="miesiąc w góre" onclick="set_str('+this.class_id+'.calendar('+n_year+', '+n_month+'), \''+this.id+'\')" /></th><th><img src="/gfx/calendar/calendar_r.gif" alt="rok w dół" onclick="set_str('+this.class_id+'.calendar('+(year-1)+', '+month+'), \''+this.id+'\')" /></th><th>'+year+'</th><th><img src="/gfx/calendar/calendar_f.gif" alt="rok w góre" onclick="set_str('+this.class_id+'.calendar('+(year+1)+', '+month+'), \''+this.id+'\')" /></th><td><img src="/gfx/calendar/calendar_exit.gif" alt="zamknij kalendarz" onclick="'+this.class_id+'.close()" /></td></tr></table>';
		cal_str+='<table class="data"><tr>';
		for(i=0;i<7;i++) cal_str+=('<th'+(i==6?' class="cal_nd"':'')+'>'+weekday_names[i]+'</th>');
		if(first_day!=1) cal_str+='</tr><tr>';
		if(first_day==0) first_day=7;
		var bool_a;
		var bool_s;
		var bool_n;

		for(i=0;i<first_day-1;i++) cal_str+='<td>&nbsp;</td>';
		for(i=1;i<=max_month_length;i++)
		{
			if((first_day+i-2)%7==0) cal_str+='</tr><tr>';

			bool_a=(actual_day==i && actual_month==month && actual_year==year)?true:false;
			bool_s=(this.sel_day==i && this.sel_month==month && this.sel_year==year)?true:false;
			bool_n=((first_day+i-2)%7==6)?true:false;

			cal_str+='<td'+(bool_a||bool_s||bool_n?' class="'+(bool_s?' cal_sel':'')+(bool_a?' cal_act':'')+(bool_n?' cal_nd':'')+'"':'')+'><p onclick="'+this.class_id+'.return_date('+i+', '+month+', '+year+');">'+i+'</p></td>';
		}
		var last_days=(7-((i+first_day-2)%7))%7;
		for(i=0;i<last_days;i++) cal_str+='<td'+(i==last_days-1?' class="cal_nd"':'')+'>&nbsp;</td>';
		cal_str+='</table>';

		return cal_str;
	}

	function close()
	{
		document.getElementById(this.id).innerHTML='';
		document.getElementById(this.id).style.display='none';
	}

