// Title: Timestamp picker
// Description: See the demo at url
// URL: http://www.geocities.com/tspicker/
// Version: 1.0.a (Date selector only)
// Date: 12-12-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: Dave Robertson (dave@factory44.net):  Added drop-down menus
//    for quickly changing months and years.  NS4x must reload the popup,
//    otherwise the onChange= MenuItems handler will fail.  stupid netscape.

// Modification: Jim Robbins (jrobbins@jplprod.com):  Added link to external
//	css file for ease of layout update.

function showCalendar(pFormField, pDateValue) {

	var l_aryMonths = ["January", "February", "March", "April", "May", "June",
		"July", "August", "September", "October", "November", "December"];
	var l_aryWeekDays = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
	var l_numWeekStart = 0; // day week starts from (normally 0 or 1)

	if (!strToDate(pDateValue) && pDateValue != "") return;
	var l_dtmDateValue = (pDateValue == null || pDateValue =="" ?  new Date() : strToDate(pDateValue));
	var l_dtmPrevMonth = new Date(l_dtmDateValue);
	l_dtmPrevMonth.setMonth(l_dtmDateValue.getMonth()-1);
	var l_dtmNextMonth = new Date(l_dtmDateValue);
	l_dtmNextMonth.setMonth(l_dtmDateValue.getMonth()+1);
	var l_dtmFirstDay = new Date(l_dtmDateValue);
	l_dtmFirstDay.setDate(1);
	l_dtmFirstDay.setDate(1-(7+l_dtmFirstDay.getDay()-l_numWeekStart)%7);
	var l_dtmLastDay = new Date(l_dtmNextMonth);
	l_dtmLastDay.setDate(0);

	// html generation (feel free to tune it for your particular application)
	// print calendar header
	var l_strBuffer = new String (
		"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"  \"http://www.w3.org/TR/html4/loose.dtd\">\n"+
        "<html>\n"+
		"<head>\n"+
		"	<title>Calendar</title>\n"+
		"	<link rel=\"stylesheet\" href=\"/cm/lib/js/calendar/calendar.css\" type=\"text/css\"/>\n"+
		"</head>\n"+
		"<body>\n"+
		"<table cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
		"<tr><td class=\"calendarTable\">\n"+
		"<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
		"<tr>\n	<td><a href=\"#\" onclick=\"window.opener.showCalendar('"+
		pFormField+"', '"+ dateToStr(l_dtmPrevMonth)+"');\">"+
		"<img src=\"/cm/lib/js/calendar/arrow_prev.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"\" title=\"previous month\"></a></td>\n"+
		"	<td colspan=\"5\" class=\"monthYearHeader\">"+l_aryMonths[l_dtmDateValue.getMonth()]+" "+l_dtmDateValue.getFullYear()+"</td>\n"+
		"	<td align=\"right\"><a href=\"#\" onclick=\"window.opener.showCalendar('"
		+pFormField+"', '"+dateToStr(l_dtmNextMonth)+"');\">"+
		"<img src=\"/cm/lib/js/calendar/arrow_next.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"\" title=\"next month\"></a></td>\n</tr>\n"
	);

	var l_dtmCurrentDay = new Date(l_dtmFirstDay);
	// print weekdays titles
	l_strBuffer += "<tr>\n";
	for (var n=0; n<7; n++)
		l_strBuffer += "	<td class=\"weekdayHeaders\">"+
		l_aryWeekDays[(l_numWeekStart+n)%7]+"</td>\n";
	// print calendar table
	l_strBuffer += "</tr>\n";
	while (l_dtmCurrentDay.getMonth() == l_dtmDateValue.getMonth() ||
		l_dtmCurrentDay.getMonth() == l_dtmFirstDay.getMonth()) {
		// print row heder
		l_strBuffer += "<tr>\n";
		for (var l_numCurrentWeekDay=0; l_numCurrentWeekDay<7; l_numCurrentWeekDay++) {
				if (l_dtmCurrentDay.getDate() == l_dtmDateValue.getDate() &&
					l_dtmCurrentDay.getMonth() == l_dtmDateValue.getMonth())
					// print current date
					l_strBuffer += "	<td class=\"selected\">";
				else if (l_dtmCurrentDay.getDay() == 0 || l_dtmCurrentDay.getDay() == 6)
					// weekend days
					l_strBuffer += "	<td class=\"weekend\">";
				else
					// print working days of current month
					l_strBuffer += "	<td class=\"weekday\">";

				if (l_dtmCurrentDay.getMonth() == l_dtmDateValue.getMonth())
					// print days of current month
					l_strBuffer += "<a href=\"#\" onClick=\"window.opener."+pFormField+
					".value='"+dateToStr(l_dtmCurrentDay)+"'; window.close();\">";
				else
					// print days of other months
					l_strBuffer += "<a href=\"#\" onClick=\"window.opener."+pFormField+
					".value='"+dateToStr(l_dtmCurrentDay)+"'; window.close();\" class=\"prevMonthDays\">";
				l_strBuffer += l_dtmCurrentDay.getDate()+"</a></td>\n";
				l_dtmCurrentDay.setDate(l_dtmCurrentDay.getDate()+1);
		}
		// print row footer
		l_strBuffer += "</tr>\n";
	}
	// print calendar footer
	l_strBuffer +=
		"</table>\n" +
		"</td>\n</tr>\n</table>\n" +
        "<br/><form action=\"#\" method=\"POST\">\n" +
        "<table width=100% border=0 cellpadding=0 cellspacing=0>\n<tr>\n" +
        "<td>\n"+
        "<select name='jumpMonth' onChange =\"window.opener.showCalendar('"+pFormField+"', this.options[this.options.selectedIndex].value + '/" + l_dtmDateValue.getDate() + "/" + l_dtmDateValue.getFullYear() + "'); if (document.layers) history.go(0)\">\n"
        for (mi = 0; mi < l_aryMonths.length; mi++) {
            l_strBuffer += "<option value='"+(mi+1)+"'"
            if (l_aryMonths[l_dtmDateValue.getMonth()] == l_aryMonths[mi])
                l_strBuffer += " selected";
            l_strBuffer += ">"+l_aryMonths[mi]+"</option>\n"
        }
        l_strBuffer += "</select>\n</td>\n" +

        "<td align=\"right\">\n"+
        "<select name='jumpYear' onChange =\"window.opener.showCalendar('"+pFormField+"', '" + (l_dtmDateValue.getMonth()+1) + "/" + l_dtmDateValue.getDate() + "/' + this.options[this.options.selectedIndex].value); if (document.layers) history.go(0)\">\n"
        for (yi = (l_dtmDateValue.getFullYear() - 5); yi < (l_dtmDateValue.getFullYear() + 6); yi++) {
            l_strBuffer += "<option value='" + yi + "'"
            if (yi == l_dtmDateValue.getFullYear())
                l_strBuffer += " selected";
            l_strBuffer += ">" + yi + "</option>\n"
        }
        l_strBuffer += "</select>\n" +
        "</td>\n" +
        "</tr>\n</table>\n</form>\n" +
		"</body>\n" +
		"</html>\n";


	var vWinCal = window.open("", "Calendar", "width=200,height=250,status=no,resizable=yes,top=200,left=200");
	vWinCal.opener = self;
	vWinCal.focus();
	var l_docCalendar = vWinCal.document;
	l_docCalendar.write (l_strBuffer);
	l_docCalendar.close();
}
// datetime parsing and formatting routimes. modify them if you wish other datetime format
function strToDate (pDateValue) {
	var l_regExpDate = /^(\d+)\/(\d+)\/(\d+)$/;
	if (!l_regExpDate.exec(pDateValue) && pDateValue != "") {
		alert("Invalid Datetime format: "+ pDateValue); return false
    }

    // Patch for non-y2k compliant systems.
    var l_numSelectedYear = parseInt(RegExp.$3)
    if (l_numSelectedYear<50) {l_numSelectedYear += 2000}
    if (l_numSelectedYear<100 && l_numSelectedYear>=50){l_numSelectedYear += 1900}
	return (new Date (l_numSelectedYear, RegExp.$1-1, RegExp.$2));
}

function dateToStr (pDateValue) {
	return (new String (
			(pDateValue.getMonth()+1)+"/"+pDateValue.getDate()+"/"+pDateValue.getFullYear()));
}

