var calendar = null;

function getMonthArr()
{
	months = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,";
	return months.split(",");
}

function validateDate(objDate, mandatory)
{
	var sdate = objDate.value;
	var adate;
	var msg = sdate + ' is not a valid date';
	var time = null;
	
	if (!mandatory && (sdate == '' || sdate == ' '))
		return true;
	
	if (sdate == '' || sdate == ' ')
	{
		alert("Please enter a date");
		return false;
	}	
	if (sdate.indexOf("/") != -1)
	{
		adate = sdate.split("/");
	}
	else if (sdate.indexOf("-") != -1)
	{
		adate = sdate.split("-");
	}
	else if (sdate.indexOf(" ") != -1)
	{
		adate = sdate.split(" ");
	}
	else
	{
		alert(msg);
		return false;
	}
	if (adate.length < 3)
	{
		alert(msg);
		return false;
	}
	if (adate.length > 3)
		time = adate[3];
	else if (adate[2].split(" ").length > 1)
		time = adate[2].split(" ")[1];

	if (time != null)
	{
		atime = time.split(":");
		if (atime.length < 2 || atime.length > 3)
		{
			alert(time + " is not a valid time");
			return false;
		}
		var h = parseInt(atime[0], 10);
		var m = parseInt(atime[1], 10);
		var s = 0;
		if (atime.length == 3)
			s = parseInt(atime[2], 10);
		if (h < 0 || h > 23 || m < 0 || m > 59 || s < 0 || s > 59)
		{
			alert(time + " is not a valid time");
			return false;
		}
		time = h + ":" + ((m < 10) ? ("0" + m) : m);
		if (atime.length == 3)
			time += ":" + ((s < 10) ? ("0" + s) : s);
		
	}
	var monthArr = getMonthArr();
	
	if (adate[1] >= 0 && adate[1] <= 12)
		month = adate[1];
	else
	{
		months = months.toLowerCase();
		month = months.indexOf(adate[1].toLowerCase())
		if (month == -1)
		{
		  alert(msg);
			return false;
		}
		month = month / 4 + 1;
	}
	var year = parseInt(adate[2], 10);
	if (year < 100)
		year += 2000;

	var testDate = new Date(year, month - 1, adate[0]);
//	alert("testDate = " + testDate + ", testDateStr = " + testDateStr)
	if (testDate.getDate() != adate[0] || testDate.getMonth() != month - 1 || (testDate.getYear() != year && testDate.getFullYear() != year))
	{
		alert(msg);
		return false;
	}
	// Format the source object's date correctly so that Access doesn't screw it up if the user put in a 
	// date with an ambiguous date/month
	var newDateStr = testDate.getDate() + " " + monthArr[testDate.getMonth()] + ", " + testDate.getFullYear();
	if (time != null)
		 newDateStr += " " + time;
	objDate.value = newDateStr;
	return true;
}

// This function gets called when the end-user clicks on some date.
function onDatePickerDateSelected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked)
	  cal.callCloseHandler(); // Comment this out to force a double click to close the calendar
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  calendar = null;
}

// 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(btn, format, defaultDate, showsTime, time24, showWeekNumbers, showsOtherMonths, firstDayOfWeek, selectedHandler) 
{
  var el = btn.parentNode.getElementsByTagName("INPUT")[0];
  if (calendar != null) {
    // we already have some calendar created
    calendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(true, null, selectedHandler, closeHandler);
    cal.weekNumbers = showWeekNumbers;
    cal.showsTime = showsTime;
	cal.time24 = time24;
    cal.showsOthers = showsOtherMonths;
    cal.firstDayOfWeek = firstDayOfWeek;
    calendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  calendar.setDateFormat(format);    // set the specified date format
  calendar.parseDate(el.value);      // try to parse the text in field
  calendar.sel = el;                 // inform it what input field we use
  if (defaultDate && !isNaN(defaultDate) && typeof(defaultDate) != "undefined" && el.value == '')
	cal.setDate(defaultDate);
	
  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
//  calendar.showAtElement(el.nextSibling, "Br");        // show the calendar
  calendar.showAtElement(el, "Br");        // show the calendar
/*	if (!params.position)
		cal.showAtElement(params.button || params.displayArea || params.inputField, params.align);
	else
		cal.showAt(params.position[0], params.position[1]);*/
	return false;
}


