// Some functions to help with date validation and other date stuff.
// Will probably only work in IE4, NN4 and above.

// @param int day - the day of the month
// @param int month - the month of the year (1 to 12)
// @param int year - the year
// @return Date - null if the date specified is invalid
function DateUtil_newDate(year, month, day){
    // Javascript Date month is 0 to 11, so subtract 1
    var date = new Date(year, (month - 1), day);
    if (
        (year  == date.getFullYear()) &&
        (month == date.getMonth() + 1) &&
        (day   == date.getDate())
    ) {
        return date; // date specified is valid
    } else {
        return null; // date specified is invalid
    }
}

// @param String source - value to parse
// @return boolean - true if source represents a +ve int
function DateUtil_isInt(source) {
    var charAt;
    for (var i = 0, n = source.length; i < n; i++) {
        charAt = source.charAt(i);
        if (charAt < '0' || charAt > '9') {return false;}
    }
    return true;
}

// Parse date with format "day/month/year".
// @param String source - the value to test
// @param boolean emptyOK
// @param int minYear
// @param int maxYear
// @return boolean - true if the field contains date within range inclusive
function DateUtil_dateIsInRange(source, emptyOK, yearMin, yearMax) {

    var length = source.length;

    if (length == 0) {
        return emptyOK; // empty String
    }

    var delimiter = "/";
    var sep1Pos   = source.indexOf(delimiter);
    var sep2Pos   = source.lastIndexOf(delimiter);

    if (sep1Pos == -1) {
        return false; // no delimiters found!
    }

    if (sep1Pos == sep2Pos) {
        return false; // only one delimiter found!
    }

    var dayStr   = source.substring(0,           sep1Pos);
    var monthStr = source.substring(sep1Pos + 1, sep2Pos);
    var yearStr  = source.substring(sep2Pos + 1, length );

    var dayIsInt   = DateUtil_isInt(dayStr);
    var monthIsInt = DateUtil_isInt(monthStr);
    var yearIsInt  = DateUtil_isInt(yearStr);

    if (!dayIsInt || !monthIsInt || !yearIsInt) {
        return false; // not all +ve int values
    }

    var year = parseInt(yearStr, 10);

    if (year < yearMin || year > yearMax) {
        return false; // year not within yearMin and yearMax inclusive
    }

    return DateUtil_newDate(year, parseInt(monthStr, 10), parseInt(dayStr, 10)) != null;
}

// Parse date with format "hh/mm/ss".
// @param String source - the value to test
// @return boolean - true if the field contains a valid time
function DateUtil_timeIsValue(source) {

    var length = source.length;

    if (length == 0) {
        return false; // empty String
    }

    var delimiter = ":";
    var sep1Pos   = source.indexOf(delimiter);
    var sep2Pos   = source.lastIndexOf(delimiter);

    if (sep1Pos == -1) {
        return false; // no delimiters found!
    }

    if (sep1Pos == sep2Pos) {
        return false; // only one delimiter found!
    }

    var hourStr = source.substring(0,           sep1Pos);
    var minStr  = source.substring(sep1Pos + 1, sep2Pos);
    var secStr  = source.substring(sep2Pos + 1, length );

    var hourIsInt = DateUtil_isInt(hourStr);
    var minIsInt  = DateUtil_isInt(minStr);
    var secIsInt  = DateUtil_isInt(secStr);

    if (!hourIsInt || !minIsInt || !secIsInt) {
        return false; // not all +ve int values
    }

    if (parseInt(hourStr, 10) > 99) {
        return false; // hours not valid
    }

    if (parseInt(minStr, 10) > 59) {
        return false; // minutes not valid
    }

    if (parseInt(secStr, 10) > 59) {
        return false; // seconds not valid
    }

    return true; // all valid
}

