// form_date.js - JavaScript support for PHP-based form library functions.

//	FST Application Framework, Version 3.3
//	Copyright 2004-07, Norman Lippincott Jr, Saylorsburg PA USA
//	All Rights Reserved
//
//	The FST Application Framework, and its associated libraries, may
//	be used only with the expressed permission of the copyright holder.
//	Usage without permission is strictly prohibited.

// Revision history:
//	2007-06-18 (v 3.3) - Re-written to accommodate changes to accompanying
//		PHP functions.
//	2007-06-29 (v 3.3) - Added support for user-defined change function for
//		date field.

// form_date_calendar - Sets up the calendar table for a given month, with
//	date links calling form_date_set (which requires id and fmt).
function form_date_calendar (y, m, id, fmt, fcn)
{
	var dt = new Date(y, m-1, 1);

	y = dt.getFullYear();
	m = dt.getMonth() + 1;

	html = new String();

	html += '<table cellspacing="0" cellpadding="0">';

	html += '<tr>';

	html += '<th class="month" colspan="4">';
	html += form_date_format(dt, 'F Y');
	html += '</th>';

	html += '<th class="nav" colspan="3">';

	html += '<a href="#" ';
	html += 'title="Previous Month" ';
	html += 'onclick="form_date_calendar(';
	html += String(y) + ', ' + String(m-1);
	html += ', \'' + id + '\', \'' + fmt + '\'';
	html += ', \'' + fcn + '\'';
	html += ');';
	html += 'return false">';
	html += 'Prev';
	html += '</a>';

	html += ' ';

	html += '<a href="#" ';
	html += 'title="Next Month" ';
	html += 'onclick="form_date_calendar(';
	html += String(y) + ', ' + String(m+1);
	html += ', \'' + id + '\', \'' + fmt + '\'';
	html += ', \'' + fcn + '\'';
	html += '); return false">';
	html += 'Next';
	html += '</a>';

	html += ' ';

	html += '<a href="#" onclick="form_date_close(); return false">';
	html += 'Close';
	html += '</a>';

	html += '</th>';
	html += '</tr>';

	html += '<tr>';
	html += '<th class="day">Sun</th>';
	html += '<th class="day">Mon</th>';
	html += '<th class="day">Tue</th>';
	html += '<th class="day">Wed</th>';
	html += '<th class="day">Thu</th>';
	html += '<th class="day">Fri</th>';
	html += '<th class="day">Sat</th>';
	html += '</tr>';

	html += '<tr>';

	for (i = 0; i < dt.getDay(); i++) {
		html += '<td>&nbsp;</td>';
	}

	while (dt.getMonth() == m-1) {

		if (dt.getDate() > 1 && dt.getDay() == 0) {
			html += '</tr><tr>';
		}

		html += '<td>';
		html += '<a href="#" ';
		html += 'onclick="form_date_set(\'' + id + '\', \'' +
			form_date_format(dt, 'Y-m-d') + '\', \'' + fmt + '\'); ';
		if (fcn != '') {
			html += fcn + '(); ';
		}
		html += 'return false">';
		html += dt.getDate();
		html += '</a>';
		html += '</td>';

		dt.setDate(dt.getDate() + 1);
	}

	while (dt.getDay() != 0) {
		html += '<td>&nbsp;</td>';
		dt.setDate(dt.getDate() + 1);
	}

	html += '</tr>';

	html += '</table>';

	document.getElementById('form_date').innerHTML = html;
}

// form_date_close - Closes (or hides) the date picker.
function form_date_close ()
{
	// Hide the date-picker.
	document.getElementById('form_date').style.display = 'none';
}

// form_date_open - Opens the date picker to set the date for the given
//	id. Also, format is specified for setting the associated anchor.
function form_date_open (id, fmt, fcn)
{
	// If time-picker exists and is visible, hide it.
	if (document.getElementById('form_time'))
		document.getElementById('form_time').style.display = 'none';

	// Get month and year based on values found in the hidden input.
	var tmp = document.getElementById(id).value.split('-');
	var year = Number(tmp[0]);
	var month = Number(tmp[1]);

	// Set up the calendar based on current month.
	form_date_calendar(year, month, id, fmt, fcn);

	// Find X and Y coordinates of the anchor.
	var obj = document.getElementById(id + '_a');

	var x = 0;
	var y = 0;
	if (obj.offsetParent)
		while (obj.offsetParent) {
			x += obj.offsetLeft;
			y += obj.offsetTop;
			obj = obj.offsetParent;
		}
	else if (obj.x) {
		x = obj.x;
		y = obj.y;
	}

	// Set X and Y coordinates of the date-picker and show it.
	var fd = document.getElementById('form_date');
	fd.style.left = x + 'px';
	fd.style.top = y + 'px';
	fd.style.display = 'block';
}

// form_date_format - Formats a date to a given format. Valid formats are a
//	subset of those recognized by the PHP date function (date-related format
//	codes only - no time codes).
function form_date_format (dt, fmt)
{
	// Get year, month, day, and weekday from date.
	var y = dt.getFullYear();
	var m = dt.getMonth() + 1;
	var d = dt.getDate();
	var w = dt.getDay();

	// Set up 2-digit versions of month, day, and year.
	var mm = m;
	if (m < 10)
		mm = '0' + m;
	var dd = d;
	if (d < 10)
		dd = '0' + d;

	var yy = y % 100;
	if (yy < 10)
		yy = '0' + yy;

	// Set up constants for date formatting.
	var mshort = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
		"Aug", "Sep", "Oct", "Nov", "Dec");
	var mlong = new Array("January", "February", "March", "April", "May",
		"June", "July", "August", "September", "October", "November",
		"December");

	var dshort = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
		"Sat");
	var dlong = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
		"Thursday", "Friday", "Saturday");

	// Format date string based on format specified in variable fmt.
	var dts = '';
	for (i = 0; i < fmt.length; i++) {

		var c = fmt.charAt(i);

		switch (c) {
		case 'd': dts = dts + dd; break;
		case 'D': dts = dts + dshort[w]; break;
		case 'F': dts = dts + mlong[m-1]; break;
		case 'j': dts = dts + d; break;
		case 'l': dts = dts + dlong[w]; break;
		case 'm': dts = dts + mm; break;
		case 'M': dts = dts + mshort[m-1]; break;
		case 'n': dts = dts + m; break;
		case 'S':
			if (d >= 11 && d <= 13)
				dts = dts + 'th';
			else {
				switch (d % 10) {
				case 1: dts = dts + 'st'; break;
				case 2: dts = dts + 'nd'; break;
				case 3: dts = dts + 'rd'; break;
				default: dts = dts + 'th';
				}
			}
			break;
		case 'Y': dts = dts + y; break;
		case 'y': dts = dts + yy; break;
		case '\\': i++; dts = dts + fmt.charAt(i); break;
		default: dts = dts + c;
		}
	}

	// Return the formatted date.
	return dts;
}

// form_date_set - Set the hidden input with given id to the given date, and
//	set its associated anchor to the given date with the given format.
function form_date_set (id, ymd, fmt)
{
	document.getElementById(id).value = ymd;

	var tmp = ymd.split('-');
	var dt = new Date(Number(tmp[0]), Number(tmp[1]) - 1, Number(tmp[2]));
	document.getElementById(id + "_a").innerHTML = form_date_format(dt, fmt);

	form_date_close();
}
