var calendars = new Array();
var calendarcount = 0;


function kolor_tla()
{
	//if(season=='wiosna') return  '#648E44';
	//if(season=='jesien') return  '#762828';
	//if(season=='zima') return  '#23A9FE';
//	if(season=='lato') 
	return  '#762828';
}

function calendarHighlight(obj, set)
{
	var kolor = '#762828';
	/*if(season=='wiosna') kolor = '#537D33';
	if(season=='lato') kolor = '#C21D1D';
	if(season=='jesien') kolor = '#C21D1D';
	if(season=='zima') kolor = '#1C437A'; */


	if(!obj.highlighted && set)
	{
		obj.highlighted = true;
		obj.backgroundColor = obj.style.backgroundColor;
		if(obj.style.backgroundColor != "#9b9b9b" && obj.style.backgroundColor != "")
			obj.style.backgroundColor = kolor;
//			obj.style.backgroundColor = "#C21D1D";
		else
			obj.style.backgroundColor = "#CFD1C8";
	}
	if(obj.highlighted && !set)
	{
		obj.highlighted = false;
		obj.style.backgroundColor = obj.backgroundColor;
	}
}

function __Month(id, name, sel)
{
	ret = '<option value="' + id + '"';
	if(sel == id)
		ret = ret + ' selected="true"';
	ret = ret + '>' + name;
	return ret;
}

function __daysInMonth(date)
{
	dt = new Date(date);
	dt.setDate(29);
	if(dt.getMonth() != date.getMonth())
		return 28;
	dt.setDate(30);
	if(dt.getMonth() != date.getMonth())
		return 29;
	dt.setDate(31);
	if(dt.getMonth() != date.getMonth())
		return 30;
	return 31;
}

function Calendar(div)
{
	this.div = div;
	this.id = calendarcount;
	calendars[calendarcount++] = this;
	first=1;

	this.recreateTable = function()
					{
						year = this.date.getFullYear();
						if (year<100) //dla lat 1900 .. 2000 // uwaga: dla lat < 1900 nie dziala poprawnie
							year = '19'+year;
						if (parseInt(year)<1000) //dla mozilli
							year = parseInt(year)+1900;
						html = '<table width="150" cellspacing="0" cellpadding="1" border="0" style="border:0px groove black;">';
						html = html + '<tr><td colspan="7" align="center"><select class="form-select" onchange="calendarYear(' + this.id + ', this.value)">';
						for(x = 1980; x < 2021; x++)
						{
							html = html + '<option value="' + x + '"'
							if(x == year)
								html = html + 'selected="true"';
							html = html + '>' + x;
						}
						html = html + '</select>';
						html = html + '<select class="form-select" onchange="calendarMonth(' + this.id + ', this.value)">';
						html = html + __Month(0, "Styczen", this.date.getMonth());
						html = html + __Month(1, "Luty", this.date.getMonth());
						html = html + __Month(2, "Marzec", this.date.getMonth());
						html = html + __Month(3, "Kwiecien", this.date.getMonth());
						html = html + __Month(4, "Maj", this.date.getMonth());
						html = html + __Month(5, "Czerwiec", this.date.getMonth());
						html = html + __Month(6, "Lipiec", this.date.getMonth());
						html = html + __Month(7, "Sierpien", this.date.getMonth());
						html = html + __Month(8, "Wrzesien", this.date.getMonth());
						html = html + __Month(9, "Pazdziernik", this.date.getMonth());
						html = html + __Month(10, "Listopad", this.date.getMonth());
						html = html + __Month(11, "Grudzien", this.date.getMonth());
						html = html + '</select>';
						html = html + '</td></tr>';
						
						html = html + '<tr>';
						html = html + '<td width="14%" class="calendar"><b>Nie</b></td>';
						html = html + '<td width="14%" class="calendar"><b>Pon</b></td>';
						html = html + '<td width="14%" class="calendar"><b>Wto</b></td>';
						html = html + '<td width="14%" class="calendar"><b>Sro</b></td>';
						html = html + '<td width="14%" class="calendar"><b>Czw</b></td>';
						html = html + '<td width="14%" class="calendar"><b>Pia</b></td>';
						html = html + '<td width="14%" class="calendar"><b>Sob</b></td>';
						html = html + '</tr>';
						html = html + '<tr>';
						
						ident = "day" + this.id + "-";
						tmpdate = new Date(this.date.getFullYear(), this.date.getMonth(), 1);
						dow = tmpdate.getDay();
						
						for(x = 0; x < dow; x++)
						{
							if(x == 0)
								html = html + '<td width="14%" class="calendar">&nbsp;</td>';
							else
								html = html + '<td width="14%" class="calendar">&nbsp;</td>';
						}
							
						day = 1;
						var tlo_kolor;
						tlo_kolor = kolor_tla();
						for(x = dow; x < 7; x++)
						{
							if(x == 0)
							{
								if(day == this.date.getDate())
									html = html + '<td id="' + ident + day + '" width="14%" onmouseover="calendarHighlight(this, true)" onmouseout="calendarHighlight(this, false)" onmousedown="calendarDown(' + this.id + ', ' + day + ', ' + this.date.getMonth() + ')" align="center" class="calendar" style="cursor:pointer;background-color:' + tlo_kolor + '; color: #ffffff;"';
								else
									html = html + '<td id="' + ident + day + '" width="14%" onmouseover="calendarHighlight(this, true)" onmouseout="calendarHighlight(this, false)" onmousedown="calendarDown(' + this.id + ', ' + day + ', ' + this.date.getMonth() + ')" align="center" class="calendar"';
							}
							else
							{
								if(day == this.date.getDate())
									html = html + '<td id="' + ident + day + '" width="14%" onmouseover="calendarHighlight(this, true)" onmouseout="calendarHighlight(this, false)" onmousedown="calendarDown(' + this.id + ', ' + day + ', ' + this.date.getMonth() + ')" align="center" class="calendar" style="cursor:pointer;background-color:' + tlo_kolor + '; color: #ffffff;"';
								else
									html = html + '<td id="' + ident + day + '" width="14%" onmouseover="calendarHighlight(this, true)" onmouseout="calendarHighlight(this, false)" onmousedown="calendarDown(' + this.id + ', ' + day + ', ' + this.date.getMonth() + ')" align="center" class="calendar"';
							}
							html = html + '>' + day;
							html = html + '</td>';
							day++;
						}
						html = html + "</tr>";
						dow = 0;
						daysinmonth = __daysInMonth(this.date);
						while(day <= daysinmonth)
						{
							if(dow == 0)
							{
								html = html + "<tr>";
								if(day == this.date.getDate())
									html = html + '<td id="' + ident + day + '" width="14%" onmouseover="calendarHighlight(this, true)" onmouseout="calendarHighlight(this, false)" onmousedown="calendarDown(' + this.id + ', ' + day + ', ' + this.date.getMonth() + ')" align="center" class="calendar" style="cursor:pointer;background-color:' + tlo_kolor + '; color: #ffffff;"';
								else
									html = html + '<td id="' + ident + day + '" width="14%" onmouseover="calendarHighlight(this, true)" onmouseout="calendarHighlight(this, false)" onmousedown="calendarDown(' + this.id + ', ' + day + ', ' + this.date.getMonth() + ')" align="center" class="calendar"';
							}
							else
							{
								if(day == this.date.getDate())
									html = html + '<td id="' + ident + day + '" width="14%" onmouseover="calendarHighlight(this, true)" onmouseout="calendarHighlight(this, false)" onmousedown="calendarDown(' + this.id + ', ' + day + ', ' + this.date.getMonth() + ')" align="center" class="calendar" style="cursor:pointer;background-color:' + tlo_kolor + '; color: #ffffff;"';
								else
									html = html + '<td id="' + ident + day + '" width="14%" onmouseover="calendarHighlight(this, true)" onmouseout="calendarHighlight(this, false)" onmousedown="calendarDown(' + this.id + ', ' + day + ', ' + this.date.getMonth() + ')" align="center" class="calendar"';
							}
							html = html + '>' + day;
							html = html + '</td>';
							if(++dow > 6)
							{
								html = html + "</tr>";
								dow = 0;
							}
							day++;
						}
						if(dow > 0)
							for(x = dow; x < 7; x++)
								html = html + '<td width="14%" class="calendar">&nbsp;</td>';

						html = html + '</tr>';
						html = html + '</table>';
						//alert(html);
						if (first)
							writeDivHTML(this.div,0,1,html);
						else
							writeDivHTML(this.div,1,1,html);
						first=0;
//						this.div.innerHTML = html;
return;
					}
	this.setNewDate = function(date)
					{
						olddate = new Date(this.date);
						this.date = date;
						this.day = date.getDate();
						this.month = date.getMonth() + 1;
						this.year = date.getFullYear();
						if(this.month != olddate.getMonth() + 1 || this.year != olddate.getFullYear())
							this.recreateTable();
					}
	this.dayClick = function(day, month)
					{
						oldtd=document.getElementById("day" + this.id + "-" + this.date.getDate());
//						oldtd = document.all["day" + this.id + "-" + this.date.getDate()];
						if(oldtd)
						{
							oldtd.color = ""
							oldtd.style.color = ""
							oldtd.backgroundColor = "";
							oldtd.style.backgroundColor = "";
						}
						newtd = document.getElementById("day" + this.id + "-" + day);
//						newtd = document.all["day" + this.id + "-" + day];
						if(newtd)
						{
							newtd.color = "#ffffff"
							newtd.style.color = "#ffffff"
							newtd.backgroundColor = kolor_tla();
							newtd.style.backgroundColor = kolor_tla();
						}
						this.date.setDate(day);
						this.date.setMonth(month);
						this.setNewDate(this.date);
					}
	this.monthChange = function(month)
					{
						this.date.setMonth(month);
						this.setNewDate(this.date);
						this.recreateTable();
					}
	this.yearChange = function(year)
					{
						this.date.setFullYear(year);
						this.setNewDate(this.date);
						this.recreateTable();
					}
	this.getCurrentDate = function(){
						return this.year + "-" + this.month + "-" + this.day;
					}
	this.getCurrentIntDate = function(){
						return this.date.valueOf();
					}
	if(arguments.length > 1)
		this.setNewDate(arguments[1]);
	else
		this.setNewDate(new Date());
	//this.recreateTable();
	
	return this;
}

function calendarDown(id, day, month)
{
	calendars[id].dayClick(day, month);
}

function calendarMonth(id, month)
{
	calendars[id].monthChange(month);
}

function calendarYear(id, year)
{
	calendars[id].yearChange(year);
}

