// 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
//
// Modification Log:
//			Date:	16 February 2005
//			By:		Guy Stevens
//			Desc:	Changed colour scheme, fonts, positioning, etc. to agree with Department standards, based on Oracle's BLAF specs.
//						See http://www.oracle.com/technology/tech/blaf/specs/datePicker.html
//
//			Date:	03 March 2005
//			By:		Guy Stevens
//			Desc:	Added drop-down selection lists for month and year.
function show_calendar(str_target, str_datetime, styling) {
  var daysOfWkBg='#cccc99';
  var daysOfWkFg='#336699';
  var daySelectedBg='#999966';
  var dayBg='#f7f7e7';
  if (styling=='ssi') {
    daysOfWkBg='#006699';
    daysOfWkFg='#FFFFFF';
    daySelectedBg='#86B5C5';
    dayBg='#d2e2ec';
  }
  
	var arr_months = ["January", "February", "March", "April", "May", "June",
		"July", "August", "September", "October", "November", "December"];
	var week_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
	var n_weekstart = 1; // day week starts from (normally 0 or 1)
	var dt_today = new Date();

	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 (
	  "<!DOCTYPE HTML>\n" +
		"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"+
		"<head>\n"+
		"<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>\n"+
		"<title>Pick a date</title>\n"+
		"<script language=\"Javascript\" src=\"/static/javascript/utils/dcmnr_datePicker.js\"></script>\n"+
		"</head>\n"+
		"<body bgcolor=\"#ffffff\">\n"+
		"<form name=\"cal\">\n"+
		"<div align=\"center\" style=\"font-size:small;\">\n"+
		"<select name=\"desiredMonth\" onchange=\"javascript:window.opener.show_calendar('"+
		str_target+"',(dt2dtstr(getDesiredDate(document.cal.desiredMonth.value, document.cal.desiredYear.value))+document.cal.time.value), '" + styling + "');\">\n");
		str_buffer +=
		"  <option value=\"0\""+(0==dt_datetime.getMonth()?" selected=\"selected\"":" ")+">January</option>\n"+
		"  <option value=\"1\""+(1==dt_datetime.getMonth()?" selected=\"selected\"":" ")+">February</option>\n"+
		"  <option value=\"2\""+(2==dt_datetime.getMonth()?" selected=\"selected\"":" ")+">March</option>\n"+
		"  <option value=\"3\""+(3==dt_datetime.getMonth()?" selected=\"selected\"":" ")+">April</option>\n"+
		"  <option value=\"4\""+(4==dt_datetime.getMonth()?" selected=\"selected\"":" ")+">May</option>\n"+
		"  <option value=\"5\""+(5==dt_datetime.getMonth()?" selected=\"selected\"":" ")+">June</option>\n"+
		"  <option value=\"6\""+(6==dt_datetime.getMonth()?" selected=\"selected\"":" ")+">July</option>\n"+
		"  <option value=\"7\""+(7==dt_datetime.getMonth()?" selected=\"selected\"":" ")+">August</option>\n"+
		"  <option value=\"8\""+(8==dt_datetime.getMonth()?" selected=\"selected\"":" ")+">September</option>\n"+
		"  <option value=\"9\""+(9==dt_datetime.getMonth()?" selected=\"selected\"":" ")+">October</option>\n"+
		"  <option value=\"10\""+(10==dt_datetime.getMonth()?" selected=\"selected\"":" ")+">November</option>\n"+
		"  <option value=\"11\""+(11==dt_datetime.getMonth()?" selected=\"selected\"":" ")+">December</option>\n"+
		"</select>&#160;&#160;"+
		"<select name=\"desiredYear\" onchange=\"javascript:window.opener.show_calendar('"+
		str_target+"',(dt2dtstr(getDesiredDate(document.cal.desiredMonth.value, document.cal.desiredYear.value))+document.cal.time.value), '" + styling + "');\">\n";
	for (var y=dt_today.getFullYear()-100; y<=dt_today.getFullYear()+50; y++) {
		var selected = '';
		if (y==dt_datetime.getFullYear()) {
			selected = " selected=\"selected\"";
		}
		str_buffer += "  <option value=\""+y+"\""+selected+">"+y+"</option>\n";
	}
	str_buffer += "</select>&#160;&#160;"+
		//"<input type=\"button\" value=\"Go\" onClick=\"javascript:window.opener.show_calendar('"+
		//str_target+"',(dt2dtstr(getDesiredDate(document.cal.desiredMonth.value, document.cal.desiredYear.value))+document.cal.time.value));\"></input>\n"+
		"</div>\n"+
		"<div>&#160;</div>\n"+
		"<table class=\"clsOTable\" cellspacing=\"0\" border=\"0\" width=\"100%\" style=\"font-size:small;\">\n"+
		"<tr><td bgcolor=\"#FFFFFF\">\n"+
		"<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">\n"+
		"<tr>\n	<td bgcolor=\"#FFFFFF\" colspan=\"7\" align=\"center\"><a href=\"javascript:window.opener.show_calendar('"+
		str_target+"', '"+dt2dtstr(dt_prev_month)+"'+document.cal.time.value, '" + styling + "');\">"+
		"<img src=\"/static/images/datepicker_lgarrowleft_enabled.gif\" width=\"20\" height=\"20\" border=\"0\" align=\"absmiddle\""+
		" alt=\"Previous month\"></a>\n"+
		"	"+
		"<font color=\"#336699\" style=\"font-family:Arial,Helvetica,Geneva,sans-serif;font-size:small;font-weight:bold;\">"+
		arr_months[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</font>\n"+
		"	<a href=\"javascript:window.opener.show_calendar('"+
		str_target+"', '"+dt2dtstr(dt_next_month)+"'+document.cal.time.value, '" + styling + "');\">"+
		"<img src=\"/static/images/datepicker_lgarrowright_enabled.gif\" width=\"20\" height=\"20\" border=\"0\" align=\"absmiddle\""+
		" alt=\"Next month\"></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 bgcolor=\"" + daysOfWkBg + "\" align=\"center\">"+
		"<font color=\"" + daysOfWkFg + "\" style=\"font-family:Arial,Helvetica,Geneva,sans-serif;font-size:small;font-weight:bold;\">"+
		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 header
		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=\"" + daySelectedBg +"\" align=\"right\">";
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// weekend days
					str_buffer += "	<td bgcolor=\"" + dayBg + "\" align=\"right\">";
				else
					// print working days of current month
					str_buffer += "	<td bgcolor=\"" + dayBg +"\" align=\"right\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// print days of current month
					str_buffer += "<a href=\"javascript:window.opener."+str_target+
					".value='"+dt2dtstr(dt_current_day)+"'+document.cal.time.value; window.close(); \" style=\"text-decoration:none;\">"+
					// if current day, use different font colour
					((dt_current_day.getDate() == dt_datetime.getDate()) ?
							"<font color=\"#ffffff\" style=\"font-family:Arial,Helvetica,Geneva,sans-serif;font-size:small;text-decoration:none;\">" :
							"<font color=\"#336699\" style=\"font-family:Arial,Helvetica,Geneva,sans-serif;font-size:small;text-decoration:none;\">");
				else
					// print days of other months
					str_buffer += "<a href=\"javascript:window.opener."+str_target+
					".value='"+dt2dtstr(dt_current_day)+"'+document.cal.time.value; window.close();\" style=\"text-decoration:none;\">"+
					"<font color=\"#999999\" style=\"font-family:Arial,Helvetica,Geneva,sans-serif;font-size:small;text-decoration:none;\">";
				str_buffer += dt_current_day.getDate()+"</font></a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		// print row footer
		str_buffer += "</tr>\n";
	}

	// print calendar footer

	/* TIME IS HIDDEN UNTIL WE HAVE NEED FOR IT. WHEN READY, REPLACE THE NEXT SECTION WITH...
	*/
	str_buffer +=
		"<tr><td colspan=\"7\" bgcolor=\"#f7f7e7\">\n"+
		"<font color=\"#336699\" style=\"font-family:Arial,Helvetica,Geneva,sans-serif;font-size:small;font-weight:bold;\">"+
		"<input type=\"hidden\" name=\"time\" value=\""+dt2tmstr(dt_datetime)+"\" size=\"8\" maxlength=\"8\"></font>"+
		"</td></tr>\n";
	/* ...THE FOLLOWING SECTION
	str_buffer +=
		"<tr><td colspan=\"7\" bgcolor=\"#f7f7e7\">"+
		"<font color=\"#336699\" style=\"font-family:Arial,Helvetica,Geneva,sans-serif;font-size:small;font-weight:bold;\">"+
		"Time: <input type=\"text\" name=\"time\" value=\""+dt2tmstr(dt_datetime)+"\" size=\"8\" maxlength=\"8\"></font>"+
		"</td></tr>\n</form>\n";
	*/

	// finish up table and html
	str_buffer +=
		"</table>\n" +
		"</td>\n</tr>\n</table>\n" +
		"</form>\n" +
		"</body>\n" +
		"</html>\n";

	var vWinCal = window.open("", "Calendar",
		"width=220,height=232,status=no,resizable=no,menubar=no,toolbar=no,scrollbars=no,location=no,top=270,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+)\:(\d+)$/;
	var re_date = /^(\d+)[\-\/](\d+)[\-\/](\d+)$/;

	if (!re_date.exec(str_datetime)) {
		alert("Invalid Datetime format: "+ str_datetime);
		return new Date();
        }
	return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, "01", "01", "01"));
}

function dt2dtstr (dt_datetime) {
	var dateDay;
	var dateMonth;
	if (dt_datetime.getMonth()<9)
	{
		dateMonth = "0" + (dt_datetime.getMonth()+1);
	}
	else
	{
		dateMonth = "" + (dt_datetime.getMonth()+1);
	}
	if (dt_datetime.getDate()<10)
	{
		dateDay = "0" + dt_datetime.getDate();
	}
	else
	{
		dateDay = "" + dt_datetime.getDate();
	}
	return (new String (
			dateDay+"-"+dateMonth+"-"+dt_datetime.getFullYear()));
}

function dt2tmstr (dt_datetime) {
	return (new String (""));
//			dt_datetime.getHours()+":"+dt_datetime.getMinutes()+":"+dt_datetime.getSeconds()));
}

function getDesiredDate (desiredMonth, desiredYear) {
	dt_desired_date = new Date();
	dt_desired_date.setMonth(desiredMonth);
	dt_desired_date.setFullYear(desiredYear);
	return dt_desired_date;
}

