//<?php //ob_start("ob_gzhandler"); ?>

/****************************************************************************
 * Plik     : pickdate.js
 * Autor    : Bartosz Cichocki [BC] <bartek@stopklatka.pl>
 *          : Tomasz Cichocki [TC] <tomek@stopklatka.pl>
 * Wersja   : 2004.11.09 [BC] Pierwsza wersja
 *          : 2007.08.30 [TC] Dodanie możliwości wyboru domyślnej danty
 ****************************************************************************
 * Copyright (c) 2004 Stopklatka Sp. z o.o., Wszelkie prawa zastrzezone.
 ****************************************************************************
 * W kodzie strony nalezy zdefiniowac element:
 *   <div id="datepicker" style="position:absolute;display:none;"></div>
 * Przyklad uzycia do wyboru daty:
 *   <input name=data id=data_id>
 *   <input type=button onclick=javascript:pickdate('data_id'); value="...">
 * Aby wywolac z wyborem czasu nalezy dodac drugi parametr o wartosci true:
 *   <input name=dataczas id=dataczas_id>
 *   <input type=button onclick="javascript:pickdate('dataczas_id', true);"
 *     value="...">
 ***************************************************************************/

pickdate_month_name = new Array("", "Sty", "Lut", "Mar", "Kwi", "Maj", "Cze", "Lip", "Sie", "Wrz", "Paź", "Lis", "Gru");
pickdate_month_days = new Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

var pickdate_day, pickdate_month, pickdate_year, pickdate_element, prev_pickdate_day, prev_pickdate_month, prev_pickdate_year,
	cur_pickdate_day, cur_pickdate_month, cur_pickdate_year, pickdate_showtime, pickdate_reverse;

function pickdate_select(count) {
	day = document.getElementById("pickdate_" + count).innerHTML;
	month = pickdate_month;
	year = pickdate_year;
	if ((count < 7) && (day > 20))
		month--;
	else if ((count > 28) && (day < 15))
		month++;
	if (month < 1) {
		month = month + 12;
		year--;
	} else if (month > 12) {
		month = month - 12;
		year++;
	}
	if (month < 10)
		month = "0" + month;
	if (day < 10)
		day = "0" + day;
//	if (pickdate_reverse)
		value = day + "." + month + "." + year;
//	else
//		value = year + "." + month + "." + day;
	if (pickdate_showtime) {
		hour = document.getElementById("pickdate_hour_id").value;
		minute = document.getElementById("pickdate_minute_id").value;
		if ((hour != "") && (minute != "")) {
			hour = parseInt(hour);
			minute = parseInt(minute);
			if ((hour < 0) || (hour > 23) || isNaN(hour))
				hour = 0;
			if ((minute < 0) || (minute > 59) || isNaN(minute))
				minute = 0;
			if (hour < 10)
				hour = "0" + hour;
			if (minute < 10)
				minute = "0" + minute;
			value = value + " " + hour + ":" + minute;
		}
	}
	pickdate_element.value = value;
	document.getElementById("datepicker").style.display = "none";
}

function pickdate_monthdays(year, month) {
	if ((month == 2) && (!(year % 4) && ((year % 100) || !(year % 400))))
		return 29;
	return pickdate_month_days[month];
}

function pickdate_move(month_interval) {
	pickdate_month = pickdate_month + month_interval;
	if (pickdate_month < 1) {
		pickdate_month = pickdate_month + 12;
		pickdate_year--;
	} else if (pickdate_month > 12) {
		pickdate_month = pickdate_month - 12;
		pickdate_year++;
	}
	document.getElementById("pickdate_label").innerHTML = '<nobr>' + pickdate_month_name[pickdate_month] + " " + pickdate_year + '</nobr>';
	var date = new Date(pickdate_year, pickdate_month - 1, 1);
	weekday = date.getDay();
	if (weekday == 0)
		weekday = 7;
	start_weekday = weekday;
	month_days = pickdate_monthdays(pickdate_year, pickdate_month);
	prev_month_days = pickdate_monthdays(pickdate_month == 1 ? pickdate_year - 1 : pickdate_year, pickdate_month - 1 < 1 ? 12 : pickdate_month - 1);
	for (count = 1; count <= 7 * 6; count++) {
		month = pickdate_month;
		year = pickdate_year;
		color = "windowtext";
		if (count < weekday) {
			value = prev_month_days - start_weekday + count + 1;
			color = "#c0c0c0";
			month--;
			if (month < 1) {
				month = month + 12;
				year--;
			}
		} else {
			value = weekday - start_weekday + 1;
			weekday++;
			if (value > month_days) {
				month = month + 1;
				if (month > 12) {
					month = month - 12;
					year++;
				}
				value = value - month_days;
				color = "#c0c0c0";
			} else if (count % 7 == 0)
				color = "red";
			else if (count % 7 == 6)
				color = "#808080";
		}

		with (document.getElementById("pickdate_" + count)) {
			innerHTML = value;
			if (color != "")
				style.color = color;
			if ((year == prev_pickdate_year) && (month == prev_pickdate_month) && (value == prev_pickdate_day))
				style.border = "1px solid red";
			else
				style.border = "0px";
			if ((year == cur_pickdate_year) && (month == cur_pickdate_month) && (value == cur_pickdate_day))
				style.fontWeight = "bold";
			else
				style.fontWeight = "normal";
		}
	}
}

function pickdate_hourchange() {
//	if (document.getElementById("pickdate_hour_id").value.length > 1)
//		document.getElementById("pickdate_minute_id").focus();
}

function pickdate(element, showtime, dx, dy, reverse, startyear, startmonth, startday) {
	pickdate_reverse = reverse;
	pickdate_showtime = showtime;
	if (document.getElementById(element))
		element = document.getElementById(element);
	pickdate_element = element;
	object = document.getElementById("datepicker");
	if ((object.style.display == "") && (element == pickdate_element))
		object.style.display = "none";
	else {
		for (obj = element, curleft = 0; obj.offsetParent; obj = obj.offsetParent)
			curleft += obj.offsetLeft;
		for (obj = element, curtop = 0; obj.offsetParent; obj = obj.offsetParent)
			curtop += obj.offsetTop;

		if (self.innerHeight)
			winheight = self.innerHeight;
		else if (document.documentElement && document.documentElement.clientHeight)
			winheight = document.documentElement.clientHeight;
		else if (document.body)
			winheight = document.body.clientHeight;

		if (self.pageYOffset)
			wintop = self.pageYOffset;
		else if (document.documentElement && document.documentElement.scrollTop)
			wintop = document.documentElement.scrollTop;
		else if (document.body)
			wintop = document.body.scrollTop;


		if (!(isNaN(dx) && isNaN(dy))) {
			curtop += dy;
			curleft += dx;
		}

		with (object.style) {
			left = curleft + "px";
			top = curtop + 21 + "px";
		}

		content  = "<table border=0 cellspacing=2 cellpadding=1><tr>";
		content += "<td width=14% align=center><a href=# onclick='javascript:pickdate_move(-12);return false;'>&lt;&lt;</td>"
				 + "<td width=14% align=center><a href=# onclick='javascript:pickdate_move(-1);return false;'>&lt;</td>"
				 + "<td width=44% colspan=3 align=center><div id=pickdate_label></div></td>"
				 + "<td width=14% align=center><a href=# onclick='javascript:pickdate_move(1);return false;'>&gt;</td>"
				 + "<td width=14% align=center><a href=# onclick='javascript:pickdate_move(12);return false;'>&gt;&gt;</td>"
				 + "</tr><tr>"
				 + "<td align=center><b>Pn</b></td>"
				 + "<td align=center><b>Wt</b></td>"
				 + "<td align=center><b>Śr</b></td>"
				 + "<td align=center><b>Cz</b></td>"
				 + "<td align=center><b>Pt</b></td>"
				 + "<td align=center><b>So</b></td>"
				 + "<td align=center><b>Nd</b></td>"
		         + "</tr><tr>";
		for (count = 1; count <= 7 * 6; count++) {
			content += "<td align=center onclick=javascript:pickdate_select(" + count + "); style=cursor:hand><div id=pickdate_" + count;
			content += "></div></td>";
			if (count % 7 == 0)
				content += "</tr><tr>";
		}
		if (pickdate_showtime) {
			content += "<td colspan=4 align=right>Godzina:&nbsp;</td><td colspan=3><input onkeyup=javascript:pickdate_hourchange(); id=pickdate_hour_id type=text size=2 style='width:20;border: 1px solid windowtext'>:<input id=pickdate_minute_id type=text size=2 style='width:20;border: 1px solid windowtext'></tr>";
		}
		content += "</table><span id=pickdate_bottom_id></span>";
		object.innerHTML = content;

		value = element.value;
		if (value != "") {
			value = value.replace("-", ".");
			value = value.replace(",", ".");
			value = value.replace(":", ".");
			value = value.replace(" ", ".");
			table = value.split(".");
			if (table.length < 3)
				value = "";
			else {
				if (table[2].length > 2) {
					pickdate_day = parseInt(table[0]);
					pickdate_month = parseInt(table[1]);
					pickdate_year = parseInt(table[2]);
				} else {
					pickdate_year = parseInt(table[0]);
					pickdate_month = parseInt(table[1]);
					pickdate_day = parseInt(table[2]);
				}
				if (pickdate_showtime) {
					if (table.length > 3)
						hour = table[3];
					else
						hour = 0;
					if (table.length > 4)
						minute = table[4];
					else
						minute = 0;
					if ((hour != "") && (minute != "")) {
						hour = parseInt(hour);
						minute = parseInt(minute);
						if ((hour < 0) || (hour > 23))
							hour = 0;
						if ((minute < 0) || (minute > 59))
							minute = 0;
						if (hour < 10)
							hour = "0" + hour;
						if (minute < 10)
							minute = "0" + minute;
						document.getElementById("pickdate_hour_id").value = hour;
						document.getElementById("pickdate_minute_id").value = minute;
					}
				}

				if ((pickdate_year < 1900) || (pickdate_year > 2100) || (pickdate_month < 1) ||
				   (pickdate_month > 12) || (pickdate_day < 1))
					value = "";
				else {
					if (pickdate_day > pickdate_monthdays(pickdate_year, pickdate_month))
						value = "";
				}
			}
		}
		var date = new Date();
		if (startday) {
			cur_pickdate_day = startday;
		} else {
			cur_pickdate_day = date.getDate();
		}
		if (startmonth) {
			cur_pickdate_month = startmonth;
		} else {
			cur_pickdate_month = date.getMonth() + 1;
		}
		if (startyear) {
			cur_pickdate_year = startyear;
		} else {
			cur_pickdate_year = date.getFullYear();
		}

		if (value == "") {
			pickdate_day   = cur_pickdate_day;
			pickdate_month = cur_pickdate_month;
			pickdate_year  = cur_pickdate_year;
			prev_pickdate_day   = "";
			prev_pickdate_month = "";
			prev_pickdate_year  = "";
		} else {
			prev_pickdate_day   = pickdate_day;
			prev_pickdate_month = pickdate_month;
			prev_pickdate_year  = pickdate_year;
		}
		pickdate_move(0);

		with (object.style) {
			backgroundColor = "window";
			border = "2px inset";
			display = "";
			//if (pickdate_showtime)
			//	document.getElementById("pickdate_hour_id").focus();
		}

		for (obj = document.getElementById("pickdate_bottom_id"), bottom = 0; obj.offsetParent; obj = obj.offsetParent)
			bottom += obj.offsetTop;

		if (bottom > (wintop + winheight))
			object.style.top = curtop - (bottom - curtop) + 14;

	}
}

/****************************************************************************
 * Koniec pliku pickdate.js
 ***************************************************************************/