/** ---------------------------------------------------------------------------
 * PROGRAM ID  : date
 * DESCRIPTION : To do validation of dates
 * AUTHOR      : Linda Tay
 * DATE        : 09-05-2005 (Monday)
 * VERSION NO  : 1.0
 * COMMENTS    :
 * ----------------------------------------------------------------------------
 * CHANGE LOG  :
 * CHANGED BY  :
 * DATE        :
 * VERSION NO  :
 * CHANGES     :
---------------------------------------------------------------------------- */

var RE_NUM = /^\-?\d+$/;
// if two digit year input dates after this year considered 20 century.
var NUM_CENTYEAR = 30;
var ARRAY_MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

/////////////////////////////////////////////////////////////////////////////

function checkMonthLength(mm,dd)
{
    var months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December");

    if((mm == 4 ||mm == 6 || mm == 9 || mm == 11) && dd > 30)
    {
        //alert(months[mm] + " has only 30 days");
        return(false);
    }
    else if (dd > 31)
    {
        //alert(months[mm] + " has only 31 days");
        return(false);
    }
    return(true);
}

/////////////////////////////////////////////////////////////////////////////

function checkLeapMonth(mm,dd,yyyy)
{
    if(yyyy%4 > 0 && dd > 28)
    {
        //alert("February of " + yyyy + " has only 28 days")
        return(false);
    }
    else if (dd > 29)
    {
        //alert("February of " + yyyy + " has only 29 days");
        return(false);
    }
    return(true);
}

/////////////////////////////////////////////////////////////////////////////

function checkDate(sDate)
{
    if (sDate.length != 8)
    {
        //alert("Please check your date format. It should be in YYYYMMDD");
        return(false);
    }
    
    var yyyy = parseInt(sDate.substring(0,4),10);
    var mm = parseInt(sDate.substring(4,6),10);
    var dd = parseInt(sDate.substring(6,sDate.length),10);
    
    if(isNaN(mm) || isNaN(dd) || isNaN(yyyy))
    {
        //alert("Please check your date format. It should be in YYYYMMDD. It should not contains letters.");
        return(false);
    }

    if(mm < 1 || mm > 12)
    {
        //alert("Months must be between 01 and 12");
        return(false);
    }

    if(dd < 1 || dd >31)
    {
        //alert("Days must be between 01 and 31 depending on the month and year");
        return(false);
    }
    
    if(yyyy<100)
    {
        if(yyyy >=30)
        {
            yyyy += 1900;
        }
        else
        {
            yyyy+=2000;
        }
    }

    if(!checkMonthLength(mm,dd))
    {
        return(false);
    }

    if(mm == 2)
    {
        if(!checkLeapMonth(mm,dd,yyyy))
        {
            return(false);
        }
    }
    
    return(true);    
}

/////////////////////////////////////////////////////////////////////////////

function checkDateCompareDate(sDate, cDate) 
{
    var cyyyy = parseInt(cDate.substring(0,4),10);
    var cmm = parseInt(cDate.substring(4,6),10);
    var cdd = parseInt(cDate.substring(6,8),10);
    
    if (checkDate(sDate)) {
        var yyyy = parseInt(sDate.substring(0,4),10);
        var mm = parseInt(sDate.substring(4,6),10);
        var dd = parseInt(sDate.substring(6,8),10);
        if (yyyy<cyyyy) {
            return (false);
        }
        else if (yyyy==cyyyy) {
            if (mm>=cmm && dd>=cdd) {
                return (true);
            }
            else {
                return (false);
            }
        }
        else {
            return (true);
        }
    }
    else {
        return (false);
    }
}

/////////////////////////////////////////////////////////////////////////////

function checkDate2(sDate)
{

    var dd = sDate.substring(0,2);
    var mm = sDate.substring(2,4);
    var yy = parseInt(sDate.substring(4,6),10);    
    if(yy<100)
    {
        if(yy >=30)
        {
            yy += 1900;
        }
        else
        {
            yy +=2000;
        }
    }
    yy = yy.toString();
    var cDate = yy.concat(mm).concat(dd);
    return checkDate(cDate);
}

/////////////////////////////////////////////////////////////////////////////

