/* -*-Java-*-
 *---
 *
 *	File:		EventCal.js
 *	CVS:		$Revision: 1.1 $
 *	Description:	Event Calendar JavaScript source
 *	Author:		Lew Gaiter III, StarFire Enterprises, Inc.
 *	Created:	Sun Jun  8 13:48:29 2003
 *	Modified:	Sun Jun  8 16:11:57 2003
 *	Modified by:	Lew Gaiter III - lew@StarFire.com
 *	Language:	Java
 *	Package:	N/A
 *	Status:		Experimental (Do Not Distribute)
 *
 *	(C) Copyright 2003, StarFire Enterprises, Inc., all rights reserved.
 *
 *---
 */

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

var calendar = null; // remember the calendar object so that we reuse
                     // it and avoid creating another

/*
 *	If the desired date is less than the reference date
 *	then it's not allowed. - return true
 */
var refDate = null;
function isDisabled(date)
{
    return refDate ? (date < refDate) : false;
}

// This function gets called when an end-user clicks on some date
function selected(cal, date)
{
    cal.sel.value = date; // just update the value of the input field
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks the "Close" (X) button.  It just hides the calendar without
// destroying it.
function closeHandler(cal)
{
    cal.hide();			// hide the calendar
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, prevId)
{
    var el = document.getElementById(id);
    var el2 = prevId ? document.getElementById(prevId) : null;
    var today = new Date();
    var prevDate;
    var minYear, maxYear;

    if (el2 && el2.value) {
	prevDate = new Date(el2.value);
    } else {
	prevDate = null;
    }
    if (prevDate) {
	minYear = prevDate.getFullYear();
    } else {
	minYear = today.getFullYear();
	if (!el2) minYear--;		// Allow last year if starting date
    }
    maxYear = minYear + 30;

    var dateStr = today.getMonth()+1 + "/" + today.getDate()
	        + "/" + today.getFullYear();
    /*
     *	Set the date to:
     *	   a) What's in the current box
     *	   b) What's in the prev date box (if we're setting end date)
     *	   c) Todays date
     */

    var theDate = el.value ? el.value : el2 ? el2.value : dateStr;
    refDate = prevDate;
    if (calendar != null) {
	// we already have one created, so just update it.
	calendar.hide();		// hide the existing calendar
	calendar.parseDate(theDate);	// set it to a new date
	calendar.setRange(minYear, maxYear);	// min/max year allowed
    } else {
	// first-time call, create the calendar
	var cal = new Calendar(true, theDate, selected, closeHandler);
	calendar = cal;		// remember the calendar in the global
	cal.setRange(minYear, maxYear);	// min/max year allowed
	// uncomment the following to hide the week numbers
	calendar.weekNumbers = false;
	cal.setDisabledHandler(isDisabled);
	calendar.create();		// create a popup calendar
    }
    calendar.setDateFormat('mm/dd/y');
    calendar.sel = el;		// inform it about the input field in use
    calendar.showAtElement(el);	// show the calendar next to the input field
    return false;
}
