
// Utility functions for dealing with dates
function DateUtils() {}

DateUtils.DATE_REGEX = /^(([1-9])|(0[1-9])|(1[0-2]))\/(([1-9])|(0[1-9])|([1-2][0-9])|(3[0-1]))\/(\d{2}|\d{4})$/;
DateUtils.DATE_TIME_REGEX =/^(([1-9])|(0[1-9])|(1[0-2]))\/(([1-9])|(0[1-9])|([1-2][0-9])|(3[0-1]))\/(\d{2}|\d{4}) (([0-9])|(0[0-9])|(1[0-9])|(2[0-3])):(([0-5][0-9]))(:(([0-5][0-9])))?$/;

/*
// checks that a 'To' date string is later than a 'From' date string
//
// Arguments: a string in a format acceptable to the Date constructor
//  
// returns: 'true' if either of the arguments is not provided, null, or empty,
//   or toDateString is before fromDateString  
//
*/
DateUtils.toDateStringAfterFrom = function( fromDateString, toDateString ) {

   if (  arguments.length < 2 || 
        fromDateString === null || fromDateString === "" ||
        toDateString === null || toDateString === "" ) {
      return true;
   } else {
      return DateUtils.toDateAfterFrom( new Date( fromDateString ),
                                      new Date( toDateString ) );
   }
};

/*
// checks that a 'To' date is later than a 'From' date.
//
// Arguments: the dates to check
//
// returns: 'true' if either of the arguments is not provided, empty, or
// null,  or toDate is before fromDate
//
*/
DateUtils.toDateAfterFrom = function( fromDate, toDate ) {

    if ( arguments.length < 2  ||
        fromDate === null || toDate === null  ) {
       return true;
    } else {
       return fromDate <= toDate;
	}
};

/*
//standardizes a date string to MM/DD/YYYY format by
//prepending 0's to 1-digit month and day and 20 to a 
//2-digit year (or supply the current year if none is supplied)
//
//Arguments - a date string
//
//returns the standardize string. Examples of input and return values are:
// input                returned
// ----------------------------------------
// 1/1/07              01/01/2007
//12/25/2008        12/25/2008
//11/4/2007          11/04/2007
//  1/1                  01/01/2008 (if this year is 2008)
//
*/

DateUtils.standardizeDate =  function( dateString ) {
    var standardized = trim(dateString);
    if ( dateString.length > 0 ) {
       var delimiter = "/";
       var pieces = dateString.split( delimiter );
	     if ( pieces.length > 1 ) {    
               var month = trim( pieces[ 0 ] );
              if ( month.length === 1 ) {
                   month = "0" + month;
              }
              var day = trim( pieces[ 1 ] );
              if ( day.length === 1 ) {
                 day = "0" + day;
              }
              var year = trim( pieces[ 2 ] );
              if ( year === null || year === undefined ) {
                  year = new Date().getFullYear();
              } else {
                 if ( year.length === 2 ) {
                    year = "20" + year;
                 }
              }
           }
    }
    if ( ! ( isEmpty( month ) ||
             isEmpty( day ) ||
             isEmpty( year ) ) ) {
       standardized =  month + delimiter + day + delimiter + year;
    }
    return standardized;
};

/*
//standardizes a date/time string to MM/DD/YYYY HH:mm:ss format by
//prepending 0's to 1-digit month and day and 20 to a 
//2-digit year (or supply the current year if none is supplied)
//and 1-digit hours, 
//checks and sets two-digits minutes and seconds
//Arguments - a date/time string
//
//returns the standardize string. Examples of input and return values are:
//input                returned
//----------------------------------------
//1/1/07 1:02:03             01/01/2007 01:02:03
//12/25/2008 12:23:23        12/25/2008 12:23:23
//11/4/2007 23:33:33         11/04/2007 23:33:33
//1/1/08                     01/01/2008 00:00:01 
//
*/
DateUtils.standardizeDateTime =  function( dateTimeString ) {
	var standardized = "";
	dateTimeString = trim(dateTimeString);
	if ( dateTimeString.length > 0 ) {
		var posSpace = dateTimeString.indexOf(" ");
		var standardizedDate = "";
		var standardizedTime = "";
		if(posSpace != -1){
			 var datestr = dateTimeString.substr(0,posSpace);
	    	 var timestr = dateTimeString.substr(posSpace);
	    	 //standardized Date
	    	 standardizedDate = DateUtils.standardizeDate(datestr); 
	    	 //standardizedTime
	    	 var delimiter = ":";
	         var pieces = timestr.split( delimiter );
		     if ( pieces.length > 1 ) {
	                var hour = trim( pieces[ 0 ] );
	                if ( hour.length === 1 ) {
	                	hour = "0" + hour;
	                }
	                var min = trim( pieces[ 1 ] );
	                if ( min.length != 2 ) {
	                	min = "" ;
	                } 
	                var sec = "00";
	                if (pieces.length > 2){
		                sec = trim( pieces[ 2 ] );
		                if ( sec.length != 2 ) {
		                	sec = "" ;
		                }
	                }
	                if ( ! ( isEmpty( hour ) ||
	                        isEmpty( min ) ||
	                        isEmpty( sec ) ) ) {
	            	standardizedTime =  hour + delimiter + min + delimiter + sec;
	               }
		     }
		}
		else{
			standardizedDate = DateUtils.standardizeDate(dateTimeString);
			standardizedTime = "00:00:00"; 
		}
		standardized = standardizedDate+ " " + standardizedTime;
	}
	return standardized;
};
    
/*
 Trims extra white space and checks if dateString is in valid format.
 Arguments: the dateString to check for (MM/dd/yyyy or M/d/yy)
 returns: 'true' if dateString is in valid format, false otherwise
 */
DateUtils.isValidDateSyntax = function (dateString) {
	dateString = trim (dateString);
	return DateUtils.DATE_REGEX.test( dateString );
};

/*
 Trims extra white space and checks if dateString is in valid format.
 Arguments: the dateString to check for (MM/dd/yyyy hh:mm:ss or M/d/yy h:mm)
 returns: 'true' if dateString is in valid format, false otherwise
*/
DateUtils.isValidDatetimeSyntax = function (dateString) {
	dateString = trim (dateString);
	return DateUtils.DATE_TIME_REGEX.test( dateString );
};

/*
//checks that a 'dateString' is equal or later than today.
//Arguments: the date to check
//returns: 'true'  or 'false'
*/
DateUtils.isDateCurrentOrFuture = function( dateString ) {
	   dateString = DateUtils.standardizeDateTime(dateString);  // changes M/D/YY (if received in this format) to MM/DD/YYYY
	   var date = new Date(dateString);
	   
	   date.setHours( 00 );
	   date.setMinutes( 00 );
	   date.setSeconds( 00 );
	   date.setMilliseconds( 000 );
	   
	   var today = new Date();
	   today.setHours( 00 );
	   today.setMinutes( 00 );
	   today.setSeconds( 00 );
	   today.setMilliseconds( 000 );
	   
	   return date >= today;
};

/*
//checks that a 'dateString' is equal or later than today fate and time.
//Arguments: the date to check
//returns: 'true'  or 'false'
*/
DateUtils.isDateTimeCurrentOrFuture = function( dateString ) {
	   dateString = DateUtils.standardizeDateTime(dateString); 
	   var date = new Date(dateString);    // used to format date received and today's date in similar GMT type format
	   var today = new Date(); 

	   return date >= today;
};


