// Title: Timestamp picker
// Description: See the demo at url
// URL: http://us.geocities.com/tspicker/
// Script featured on: http://javascriptkit.com/script/script2/timestamp.shtml
// Version: 1.0
// Date: 12-05-2001 (mm-dd-yyyy)
// Author: Denis Gritcyuk <denis@softcomplex.com>; <tspicker@yahoo.com>
// Notes: Permission given to use this script in any kind of applications if
//    header lines are left unchanged. Feel free to contact the author
//    for feature requests and/or donations

function show_calendar(str_target, str_date_y, str_date_m, str_date_d, godzina_tak_nie, godz_hh, godz_hm) {
	var str_datetime = new String(str_date_y + "-" + str_date_m + "-" + str_date_d);
	var arr_months = ["Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec",
		"Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień"];
	var week_days = ["Ni", "Po", "Wt", "Śr", "Cz", "Pi", "So"];
	var n_weekstart = 1; // day week starts from (normally 0 or 1)

	var dt_datetime = (str_datetime == null || str_datetime == "" ?  new Date() : str2dt(str_datetime));
	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setMonth(dt_datetime.getMonth() - 1);
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth() + 1);
	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1 - (7 + dt_firstday.getDay() - n_weekstart)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);

//	utworzenie skryptu przenoszacego dane
	var skrypt = new String("<script type='text/javascript'>\n"
	+ "function przenies_date(nazwa_formularza, data_y, data_m, data_d, godz_t_n) \n{\n"
	+ "   data_m ++;"
	+ "   window.opener." + str_target + "_y.value = data_y;\n"
	+ "   window.opener." + str_target + "_m.value = data_m;\n"
	+ "   window.opener." + str_target + "_d.value = data_d;\n"
	+ "   if(godz_t_n == 1)\n{\n"
	+ "   window.opener." + str_target + "_hh.value = document.getElementById('hh').value;\n"
	+ "   window.opener." + str_target + "_hm.value = document.getElementById('hm').value;\n"
	+ "   }\n"
	+ "   window.close();\n}\n"
	+ "function przenies_minuty(minuty)\n{\n"
	+ "   var pole_minut = document.getElementById('hm');\n"
	+ "   pole_minut.value = minuty;\n"
	+ " }\n"
	+ "function przenies_godziny(godziny)\n{\n"
	+ "   var pole_godzin = document.getElementById('hh');\n"
	+ "   pole_godzin.value = godziny;\n"
	+ " }\n"
	+ "</script>\n");

//	 tworzenie html
//	 naglowek kalendarza
	var poprzedni_miesiac = dt_prev_month.getMonth() + 1;
	var nastepny_miesiac = dt_next_month.getMonth() + 1;
	var str_buffer = new String (
		"<html><head><meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />" +
		"<link rel=\"stylesheet\" type=\"text/css\" href=\"style/podstawowy1/kalendarz.css\" />" +
		skrypt +
		"<title>Wybierz datę</title></head><body>\n" +
		"<table>\n" +
		"<tr><td class=\"poprzedni_miesiac\" title=\"Poprzedni miesiąc\" onclick=\"javascript:window.opener.show_calendar('" + str_target + "', '" + dt_prev_month.getFullYear() + "', '" + poprzedni_miesiac + "', '" + dt_prev_month.getDate() + "'," + godzina_tak_nie + ", " + godz_hh + ", " + godz_hm + ");\">&laquo;</td>"
		+ "<td class=\"miesiac_rok\" colspan=\"5\">" + arr_months[dt_datetime.getMonth()] + " " + dt_datetime.getFullYear() + "</td>" +
		"<td class=\"nastepny_miesiac\" title=\"Następny miesiąc\" align=\"right\" onclick=\"javascript:window.opener.show_calendar('" + str_target + "', '" + dt_next_month.getFullYear() + "', '" + nastepny_miesiac + "', '" + dt_next_month.getDate() + "'," + godzina_tak_nie + ", " + godz_hh + ", " + godz_hm + ");\">" + "&raquo;</td></tr>\n"
	);

	var dt_current_day = new Date(dt_firstday);
//	 naglowek tygodni
	str_buffer += "<tr>";
	for (var n = 0; n < 7; n++)
	{
		str_buffer += "<td class=\"dni_tygodnia\">" + week_days[(n_weekstart+n)%7] + "</td>";
	}
//	 tabela kalendarza
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_datetime.getMonth() || dt_current_day.getMonth() == dt_firstday.getMonth())
	{
//		początek wiersza
		str_buffer += "<tr>\n";
		for (var n_current_wday = 0; n_current_wday < 7; n_current_wday++) {
			if (dt_current_day.getDate() == dt_datetime.getDate() && dt_current_day.getMonth() == dt_datetime.getMonth()) {
				str_buffer += "	<td class=\"dzis\" "; //				biezaca data
			}
			else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6) {
				str_buffer += "	<td class=\"weekend\" "; //					weekend
			}
			else {
				str_buffer += "	<td class=\"biezacy_miesiac\" "; //					 dni robocze biezacego miesiaca
			}

			if (dt_current_day.getMonth() == dt_datetime.getMonth()) {
//					dni miesiaca
					str_buffer += " onclick=\"przenies_date('" + str_target + "', " + dt_current_day.getFullYear() + ", " + dt_current_day.getMonth() + ", " + dt_current_day.getDate() + ", " + godzina_tak_nie + "); window.close();\">";
			}
			else {
//				dni innych miesiecy
				str_buffer += " style=\"background-color: #f0f0f0; color: #505050\" onclick=\"przenies_date('" + str_target + "', " + dt_current_day.getFullYear() + ", " + dt_current_day.getMonth() + ", " + dt_current_day.getDate() + ", " + godzina_tak_nie + "); window.close();\">";
			}
			str_buffer += dt_current_day.getDate() + "</td>\n";
			dt_current_day.setDate(dt_current_day.getDate() + 1);
		}
//		zakonczenie wiersza
		str_buffer += "</tr>\n";
	}
//	zakonczenie kalendarza
	str_buffer +=
		"</table>\n</tr>\n</td>\n";
//	tabelka wyboru godziny
	if(godzina_tak_nie == 1) {
		str_buffer += "<table class='wybor_godziny'>";
		var godz = 0;
//		tabela godzin
		for(var godz_wiersz = 0; godz_wiersz <= 3; godz_wiersz++) {
			str_buffer += "<tr>";
			for(var godz_kolumna = 0; godz_kolumna <= 5; godz_kolumna++) {
				str_buffer += "<td class=\"godzina\" onclick=\"przenies_godziny('" + godz + "');\">" + godz + "</td>";
				godz++;
			}
			str_buffer += "</tr>";
		}
//		tabela minut
		str_buffer += "<tr><td class=\"pusta\"></td>"
		+ "<td class=\"minuta\" onclick=\"przenies_minuty('00');\">00</td>"
		+ "<td class=\"minuta\" onclick=\"przenies_minuty('15');\">15</td>"
		+ "<td class=\"minuta\" onclick=\"przenies_minuty('30');\">30</td>"
		+ "<td class=\"minuta\" onclick=\"przenies_minuty('45');\">45</td>"
		+ "<td class=\"pusta\"></td>"
		+ "</tr><tr>"
		+ "<td colspan=\"6\" class=\"pole_wpisu\">"
		+ "<input type=\"text\" id=\"hh\" name=\"hh\" value=\"" + godz_hh + "\" maxlength=\"2\" style=\"width: 2em;\" />"
		+ " : <input type=\"text\" id=\"hm\" name=\"hm\" value=\"" + godz_hm + "\" maxlength=\"2\" style=\"width: 2em;\" /></td>"
		+ "</tr>"
		;
		str_buffer += "</table>";
	}
	str_buffer += "</body></html>\n";

//	status  	The status bar at the bottom of the window.
//toolbar 	The standard browser toolbar, with buttons such as Back and Forward.
//location 	The Location entry field where you enter the URL.
//menubar 	The menu bar of the window
//directories 	The standard browser directory buttons, such as What's New and What's Cool
//resizable 	Allow/Disallow the user to resize the window.
//scrollbars 	Enable the scrollbars if the document is bigger than the window
//height 	Specifies the height of the window in pixels. (example: height='350')
//width 	Specifies the width of the window in pixels.

	if(godzina_tak_nie == 1) {
		var parametry = "width=200,height=340,status=no,menubar=no,location=no,resizable=yes,top=200,left=200";
	}
	else {
		var parametry = "width=200,height=190,status=yes,resizable=yes,top=200,left=200";
	}
	var vWinCal = window.open("", "Wybierz datę", parametry);
	vWinCal.opener = self;
	var calc_doc = vWinCal.document;
	calc_doc.write (str_buffer);
	calc_doc.close();
}
// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt (str_datetime) {
//	polaczenie pol daty
	var re_date = /^(\d+)\-(\d+)\-(\d+)$/;
	re_date.exec(str_datetime)
//	if (!re_date.exec(str_datetime))
//		return alert("Niewłaściwy format daty: "+ str_datetime);
	var biezacadata = new Date();
	rok = RegExp.$1;
	if(rok <= 0)
	{
		rok = biezacadata.getFullYear();
	}
	miesiac = RegExp.$2-1;
	if(miesiac < 0 || miesiac > 11)
	{
		miesiac = biezacadata.getMonth();
	}
	dzien = RegExp.$3;
	if(dzien < 1 || dzien > 31)
	{
		dzien = biezacadata.getDate();
	}
	return (new Date (rok, miesiac, dzien));
//	return (new Date (RegExp.$1, RegExp.$2-1, RegExp.$3));
}
function dt2dtstr (dt_datetime) {
	return (new String (dt_datetime.getFullYear() + "-" + (dt_datetime.getMonth() + 1) + "-" + dt_datetime.getDate()));
}
function dt2tmstr (dt_datetime) {
//	return (new String (dt_datetime.getHours() + ":" + dt_datetime.getMinutes() + ":" + dt_datetime.getSeconds()));
}

