// 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

var vWinCal = '';

function show_calendar(str_target, str_datetime, strPath, checkTarget) {
	var n_weekstart = 1; // day week starts from (normally 0 or 1)

	var strIcons = strPath.replace('images','icons');
	var strCss = strPath.replace('media/images','css');
    str_LinkTarget = str_target.replace(/\'/g,"\\\'");
	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);
	
	// html generation (feel free to tune it for your particular application)
	// print calendar header
	var str_buffer = new String (
		"<html>\n"+
		"<link rel=\"stylesheet\" href=\""+strCss+"style.css\" type=\"text/css\">\n"+
		"<head>\n"+
		"	<title>"+txt_calendar+"</title>\n"+
		"</head>\n"+
		"<body style=\"background-image:url("+strPath+"kachel_px.gif);\">\n"+
		"<table class=\"tdheader\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
		"<tr><td>\n"+
		"<table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
		"<tr>\n	<td bgcolor=\"#FFFFFF\"><a href=\"javascript:window.opener.show_calendar('"+
		str_LinkTarget+"', '"+ dt2dtstr(dt_prev_month)+"'+document.cal.time.value,'"+strPath+"');\">"+
		"<img src=\""+strIcons+"/i16_zurueck3.gif\" width=\"16\" height=\"16\" border=\"0\""+
		"></a></td>\n"+
		"	<td bgcolor=\"#FFFFFF\" align=\"center\" colspan=\"5\">"+
		""+arr_months[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</td>\n"+
		"	<td bgcolor=\"#FFFFFF\" align=\"right\"><a href=\"javascript:window.opener.show_calendar('"
		+str_LinkTarget+"', '"+dt2dtstr(dt_next_month)+"'+document.cal.time.value,'"+strPath+"');\">"+
		"<img src=\""+strIcons+"/i16_vor3.gif\" width=\"16\" height=\"16\" border=\"0\""+
		"></a></td>\n</tr>\n"
	);

	var dt_current_day = new Date(dt_firstday);
	// print weekdays titles
	str_buffer += "<tr>\n";
	for (var n=0; n<7; n++)
		str_buffer += "	<td class=\"tdheader\">"+
		"<font color=\"white\" face=\"verdana,arial,helvetica\" size=\"2\">"+
		week_days[(n_weekstart+n)%7]+"</font></td>\n";
	// print calendar table
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		// print row heder
		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())
					// print current date
					str_buffer += "	<td bgcolor=\"#FFB6C1\" align=\"center\">";
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// weekend days
					str_buffer += "	<td class=\"tdcontent\" align=\"center\">";
				else
					// print days of other months
					str_buffer += "	<td class=\"tdcontentweiss\" align=\"center\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// print days of current month
					str_buffer += "<a href=\"#\" onClick=\"{window.opener.saveDatetime('"+str_LinkTarget+"','"
						+dt2dtstr(dt_current_day)+"'+document.cal.time.value);window.opener."+checkTarget+".checked=true;}\">";
				else 
					// print days of other months
					str_buffer += "<a color=\"gray\" href=\"#\" onClick=\"{window.opener.saveDatetime('"+str_LinkTarget+"','"
					    +dt2dtstr(dt_current_day)+"'+document.cal.time.value);window.opener."+checkTarget+".checked=true;}\">";

				str_buffer += dt_current_day.getDate()+"</a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		// print row footer
		str_buffer += "</tr>\n";
	}
	// print calendar footer
	str_buffer +=
		"<form name=\"cal\">\n<tr><td colspan=\"7\" class=\"tdheader\">"+
		"<font color=\"White\" face=\"Verdana, Arial, Helvetica\" size=\"2\">"+
		txt_time+": <input type=\"text\" name=\"time\" value=\""+dt2tmstr(dt_datetime)+
		"\" size=\"5\" maxlength=\"5\"></font></td></tr>\n<tr><td colspan=\"3\" class=\"tdcontentweiss\">"+
		"<a href=\"javascript:window.opener.resetCalendar('"+str_LinkTarget+"','"+strPath+"')\"><img border=\"0\" src=\""+strIcons+"/i16_zurueck3.gif\">"+txt_reset_calendar+
		"</a></span></td><td colspan=\"4\" class=\"tdcontentweiss\" align=\"right\">"+
		"<a href=\"#\" onClick=\"{window.opener.saveDatetime('"+str_LinkTarget+"','"+dt2dtstr(dt_datetime)+"'+document.cal.time.value);window.opener."+checkTarget+".checked=true;}\">"+txt_update_changes+
		"<img border=\"0\" src=\""+strIcons+"/i16_vor3.gif\"></a></span></td></tr>\n</form>\n" +
		"</table>\n" +
		"</tr>\n</td>\n</table>\n" +
		"</body>\n" +
		"</html>\n";

	vWinCal = window.open("", txt_calendar, 
		"width=450,height=250,status=no,resizable=no,top=200,left=200");
	vWinCal.opener = self;
	var calc_doc = vWinCal.document;
	calc_doc.write (str_buffer);
	calc_doc.close();
	vWinCal.focus();
}

// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt (str_datetime) {
	/*var re_date = /^(\d+)\.(\d+)\.(\d+)\s+(\d+)\:(\d+)$/;
	if (!re_date.exec(str_datetime))
	{
		return alert("Das Format ist ungültig (TT.MM.JJJJ HH:MI): "+ str_datetime);
	}
	else
	{
//		return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6));
	}
*/
	aDateTime = checkDate(str_datetime);
    strDateTime = aDateTime.join (", ");
	eval ('oDate = new Date (' + strDateTime + ')');
	if (oDate)
		return oDate;
}

function dt2dtstr (dt_datetime) {
	var strDay  = ((dt_datetime.getDate() < 10) ? "0" + dt_datetime.getDate() : dt_datetime.getDate());
	var strMonth  = ((dt_datetime.getMonth() + 1 < 10) ? "0" + (dt_datetime.getMonth() + 1) : (dt_datetime.getMonth() + 1));

	return (new String (
			strDay+"."+strMonth+"."+dt_datetime.getFullYear()+" "));
}
function dt2tmstr (dt_datetime) {
	var strHours  = ((dt_datetime.getHours() < 10) ? "0" + dt_datetime.getHours() : dt_datetime.getHours());
	var strMinutes  = ((dt_datetime.getMinutes() < 10) ? "0" + dt_datetime.getMinutes() : dt_datetime.getMinutes());
 	
	return (new String (
			strHours+":"+strMinutes));
}

function checkDate(str_datetime)
{
	var re_date = /^(\d+)\.(\d+)\.(\d+)\s+(\d+)\:(\d+)$/;
	if (!re_date.exec(str_datetime))
	{
		return alert(txt_wrong_date_format);
	}
	else
	{
		return (new Array(RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5));
	}
}

function saveDatetime(str_target, strDatetime)
{
	var dt = str2dt(strDatetime);
	var now = new Date();
	if ( dt.getTime() < now.getTime() )
		strDatetime = dt2dtstr(now)+dt2tmstr(now);

	aDateParts = checkDate(strDatetime)

	if (aDateParts)
	{
		if (aDateParts[3] < 24 && aDateParts[4] <= 59)
		{
			oElement = eval(str_target);
			if (oElement)
			{
				oElement.value = strDatetime;
				vWinCal.close();
			}
		}
		else
		{
			alert (txt_wrong_time_format);
			vWinCal.focus();
		}
	}
	else
	{
		vWinCal.focus();
	}
}

function resetCalendar(str_target, str_path)
{
    show_calendar(str_target,'',str_path);
}