/*
	Definte Regular Expression for easier validation
*/
var regexps    = new Object();

/*
 Define the valid format of date in YYYY-MM-DD or YYYYMMDD or YYMMDD
 */
regexps.date		= /^((\d{2})|([123456789]\d{3}))-(([0]{0,1}[123456789])|([1][012]))-(([0]{0,1}[123456789])|([12]\d)|([3][01]))$/;
regexps.datedelim	= "-";
regexps.posinteger	= /^\d+$/

/**
 * Validate date format MM-DD-YY
 * @return Date object representing obj if validation is successful; false otherwise
 * @param obj HTML object containing date string to be validated
 */

function validateDateMMDDYY(obj) {
    // If obj.value is plain number, insert default deliminator between year, month and day
    var l_str = obj.value.substr(obj.value.lastIndexOf("-")+1) + "-" + obj.value.substr(0,obj.value.lastIndexOf("-"));
	
    // check if the modify date string pass the valid format of date
    if ( regexps.date.test( l_str ) == false ) {
        //switchErrorClass(obj,true);
        alert( obj.value + " is not a valid date format! (i.e. MM-DD-YY)" );
        obj.value = "";
        obj.focus();
        return false;
    }
	
    // prepare the three values for valid date checking
    // l_tmpd	: temporary storage for formatting the string
    // l_yy		: store the value of year
    // l_mm		: store the value of month
    // l_dd		: store the value of day
    // l_yyyy	: store the value of system time -> year in full format YYYY
    var l_tmpd, l_yy, l_mm, l_dd, l_yyyy;

    l_yyyy	= new Date().getFullYear();
    l_tmpd	= l_str;
    l_yy 		= l_tmpd.substring( 0, l_tmpd.indexOf( regexps.datedelim ) );
    l_tmpd	= l_tmpd.substring( l_tmpd.indexOf( regexps.datedelim ) + 1, l_tmpd.length );
    l_mm		= l_tmpd.substring( 0, l_tmpd.indexOf( regexps.datedelim ) );
    l_tmpd	= l_tmpd.substring( l_tmpd.indexOf( regexps.datedelim ) + 1, l_tmpd.length );
    l_dd		= l_tmpd;

    // convert 2 digit year to 4 digit year
    if ( l_yy.length == 2 ) {
        /*
        l_yy = ( l_yyyy - ( l_yyyy % 100 ) ) + parseInt( l_yy );

        // read 4 digit year between (current year - 79, current year + 20)
        if ( l_yyyy < l_yy - 20 ) {
            l_yy -= 100;
        }
        */

        // Amended by Edward (2007-6-14): Fix error with future dates
         var testYear = Math.round(l_yyyy/100)+""+l_yy;
         if (Number(testYear)-l_yyyy < 20) l_yy = Math.round(l_yyyy/100)+""+l_yy;
         else l_yy = Math.round(l_yyyy/100)-1+""+l_yy;
    }

    // eliminate the error occur in parseInt, if first character is '0', eliminate it
    if ( l_mm.charAt(0) == '0' )
    {
        l_mm = l_mm.substring(1,l_mm.length);
    }

    // eliminate the error occur in parseInt, if first character is '0', eliminate it
    if ( l_dd.charAt(0) == '0' )
    {
        l_dd = l_dd.substring(1,l_dd.length);
    }

    l_mm	= parseInt(l_mm);
    l_dd	= parseInt(l_dd);

    // build the date into standard date format as YYYY-MM-DD
    l_str = l_yy + regexps.datedelim + (l_mm<10?"0"+l_mm:l_mm) + regexps.datedelim + (l_dd<10?"0"+l_dd:l_dd);


    // if the date is invalid, return error
    if ( isDate( l_yy, l_mm, l_dd ) == false )
    {
        //switchErrorClass(obj,true);
        obj.value = "";
        obj.focus();
        return false;
    }

    return new Date(l_yy,l_mm-1,l_dd);
}

/**
 * Added by Edward (2007-1-25)
 * Validate date format YYYY-MM-DD
 * @return Date object representing obj if validation is successful; false otherwise
 * @param obj HTML object containing date string to be validated
 */

function validateDateMMDDYYYY(obj) {
    if(obj.value=="") return;
    var l_str = obj.value
	
    // check if the modify date string pass the valid format of date
    if ( regexps.date.test( l_str ) == false ) {
        //switchErrorClass(obj,true);
        alert( obj.value + " is not a valid date format! (i.e. MM-DD-YYYY)" );
        obj.value = "";
        obj.focus();
        return false;
    }
	
    // prepare the three values for valid date checking
    // l_tmpd	: temporary storage for formatting the string
    // l_yy		: store the value of year
    // l_mm		: store the value of month
    // l_dd		: store the value of day
    // l_yyyy	: store the value of system time -> year in full format YYYY
    var l_tmpd, l_yy, l_mm, l_dd;

    l_tmpd	= l_str;
    l_yy 		= l_tmpd.substring( 0, l_tmpd.indexOf( regexps.datedelim ) );
    l_tmpd	= l_tmpd.substring( l_tmpd.indexOf( regexps.datedelim ) + 1, l_tmpd.length );
    l_mm		= l_tmpd.substring( 0, l_tmpd.indexOf( regexps.datedelim ) );
    l_tmpd	= l_tmpd.substring( l_tmpd.indexOf( regexps.datedelim ) + 1, l_tmpd.length );
    l_dd		= l_tmpd;


    // eliminate the error occur in parseInt, if first character is '0', eliminate it
    if ( l_mm.charAt(0) == '0' )
    {
        l_mm = l_mm.substring(1,l_mm.length);
    }

    // eliminate the error occur in parseInt, if first character is '0', eliminate it
    if ( l_dd.charAt(0) == '0' )
    {
        l_dd = l_dd.substring(1,l_dd.length);
    }

    l_mm	= parseInt(l_mm);
    l_dd	= parseInt(l_dd);


    // if the date is invalid, return error
    if ( isDate( l_yy, l_mm, l_dd ) == false )
    {
        //switchErrorClass(obj,true);
        obj.value = "";
        obj.focus();
        return false;
    }

    return new Date(l_yy,l_mm-1,l_dd);
}

/**
 * Retrun Date object for MMDDYY format string
 * @return Date object representing obj if validation is successful; false otherwise
 * @param str String of Date format
 */
function returnDateMMDDYY(str) {
	
    // check if the modify date string pass the valid format of date
    /*if ( regexps.date.test( str ) == false ) {
        return false
    }*/
	
    // prepare the three values for valid date checking
    // l_tmpd	: temporary storage for formatting the string
    // l_yy		: store the value of year
    // l_mm		: store the value of month
    // l_dd		: store the value of day
    var l_tmpd,l_yy, l_mm, l_dd,l_yyyy;

    l_yyyy	= new Date().getFullYear();
    l_tmpd	= str;
    l_mm 		= l_tmpd.substring( 0, str.indexOf( regexps.datedelim ) );
    l_tmpd	= l_tmpd.substring( l_tmpd.indexOf( regexps.datedelim ) + 1, l_tmpd.length );
    l_dd		= l_tmpd.substring( 0, l_tmpd.indexOf( regexps.datedelim ) );
    l_tmpd	= l_tmpd.substring( l_tmpd.indexOf( regexps.datedelim ) + 1, l_tmpd.length );
    l_yy		= l_tmpd;

    // convert 2 digit year to 4 digit year
    if ( l_yy.length == 2 ) {
        l_yy = ( l_yyyy - ( l_yyyy % 100 ) ) + parseInt( l_yy );

        // read 4 digit year between (current year - 79, current year + 20)
        if ( l_yyyy < l_yy - 20 ) {
            l_yy -= 100;
        }
    }

    // eliminate the error occur in parseInt, if first character is '0', eliminate it
    if ( l_mm.charAt(0) == '0' )
    {
        l_mm = l_mm.substring(1,l_mm.length);
    }

    // eliminate the error occur in parseInt, if first character is '0', eliminate it
    if ( l_dd.charAt(0) == '0' )
    {
        l_dd = l_dd.substring(1,l_dd.length);
    }

    l_mm	= parseInt(l_mm);
    l_dd	= parseInt(l_dd);
    
    //If date/month/year is not a number than it is not a valid date
    if(isNaN(l_yy) || isNaN(l_mm) || isNaN(l_dd))
    {
	return false;
    }

    // if the date is invalid, return error
    if ( isDate( l_yy, l_mm, l_dd ) == false )
    {
        return false;
    }
    return new Date(l_yy,l_mm-1,l_dd);
}

/*
* Formatting Date to MM-DD-YY
* @return String of the formated date
* @param formatDate the date that need to format
*/
function FormatDateMMDDYY(formatDate)
{
    var Yr=formatDate.getFullYear()+""
    return (formatDate.getMonth()+1)+"-"+(formatDate.getDate())+"-"+Yr.substring(2)
}

/* Check if date is a valid date */
function isDate( year, month, day )
{
	// l_mname	: for the purpose of display the name of a month
	// l_mdays	: assign maximum number of days for each month into an array
	var l_mname	= new Array( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" );
	var l_mdays		= new Array( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );

	// change feburary to have a max of days of 29 depends on the parameter year
	var count = 1;
	l_mdays[1] = ( (year%4==0) && ( !(year%100==0) || (year%400==0) ) ) ? 29 : 28;

	// if the parameter day is larger than max days of a month, return error.
	if ( l_mdays[month-1] < day )
	{
		window.alert( l_mname[month-1] + " doesn't have " + day + " days in year " + year + "." );
		return false;
	}
	return true;
}
