/*
 * $Id: datetime.js 45 2009-09-27 07:19:26Z wru $
 */

Date.adjustYear = function (theyear, theoption) // theoption: -1=-99-0, 0=-49-50, 1=0-99
{
    if (typeof theoption == "undefined")
        theoption = 0;
    if (theyear instanceof String)
        theyear = theyear.valueOf ();
    if (typeof theyear != "string")
        return (theyear);
    var year    = theyear;
    var yearval = parseInt (year, 10);
    if (year.length < 4) {
        var thisyear = (new Date ()).getFullYear ();
        if (year.length > 0) {
            var pow = Math.pow (10, year.length);
            var tmp = thisyear - thisyear % pow + yearval;
            if (theoption != -1 && tmp < thisyear - (theoption == 0 ? pow / 2 + 1 : 0))
                tmp += pow;
            else
            if (theoption != 1 && tmp > thisyear + (theoption == 0 ? pow / 2 : 0))
                tmp -= pow;
            thisyear = tmp;
        }
        yearval = thisyear;
    }
    year = String (yearval);
    while (year.length < 4)
        year = "0" + year;
    return (year);
}

Date.isValid = function (theyear, themonth, theday)
{
    theyear  = parseInt (theyear , 10);
    themonth = parseInt (themonth, 10);
    theday   = parseInt (theday  , 10);
    if (theyear == 0 || themonth < 1 || themonth > 12 || theday < 1 || theday > 31)
        return (false);
    var isleap = (theyear < 0 ? (- theyear - 1) % 4 == 0 : theyear % 4 == 0 && (theyear % 100 != 0 || theyear % 400 == 0 || theyear < 1700));
    var maxday = 31;
    switch (themonth) {
        case 2: maxday = (isleap ? 29 : 28); break;
        case 4: case 6: case 9: case 11: maxday = 30; break;
    }
    return (theday <= maxday);
}
