/*
 * This is a set of functions for date validation.
 *
 * Copyright (C) 2005 by John Glorioso, Zitego Solutions, LLC
 * <jglorioso@zitego.com>. All rights reserved.
 *
 * Redistribution and use in source forms, with or without modification, are
 * permitted provided that the following condition is met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * @author John Glorioso
 * @version $Id: date_validation.js,v 1.1 2008/02/20 15:19:35 jglorioso Exp $
 */

var _days = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

/**
 * Returns the number of days for February of the given year.
 *
 * @param year The year.
 * @return int
 */
function daysInFebruary (year)
{
	return ( ((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

/**
 * Returns whether the given date string is valid.
 *
 * @param dt The date string.
 * @return boolean
 */
function isDate(dt)
{
    if (dt.match(/^\d\d?\/\d\d?\/\d\d?\d?\d?$/) == null) return false;
	var index1 = dt.indexOf("/");
	var index2 = dt.indexOf("/", index1+1);
	var mon = dt.substring(0, index1);
	var day = dt.substring(index1+1, index2);
	var year = dt.substring(index2+1);
	if (mon < 1 || mon > 12) return false;
	if (day < 1 || day > 31 || (mon == 2 && day > daysInFebruary(year)) || day > _days[mon-1]) return false;
	if (year < 1) return false;

	return true;
}

/**
 * Returns whether the given date/time string is valid.
 *
 * @param dt The date/time string.
 * @return boolean
 */
function isDateTime(dt)
{
    if (dt.match(/^\d\d?\/\d\d?\/\d\d?\d?\d?\s\d\d?:\d\d?(:\d\d?)?$/) == null) return false;
    var d = dt.substring( 0, dt.indexOf(" ") );
    if ( !isDate(d) ) return false;
    var t = dt.substring(dt.indexOf(" ")+1);
	var index1 = t.indexOf(":");
	var index2 = t.indexOf(":", index1+1);
	var hour = t.substring(0, index1);
	if (index2 > -1)
	{
	    var min = t.substring(index1+1, index2);
	    var sec = t.substring(index2+1);
    	if (hour < 0 || hour > 23) return false;
    	if (min < 0 || min > 59) return false;
    	if (sec < 0 || sec > 59) return false;
	}
	else
	{
	    var min = t.substring(index1+1);
	    if (hour < 0 || hour > 23) return false;
    	if (min < 0 || min > 59) return false;
	}

	return true;
}