function DateTimeValidator(fromDate,toDate,fromTime,toTime)
{
  this.fromDate = fromDate;
  this.formattedFromDate = null;
  this.formattedToDate = null;
  this.toDate = toDate;
  this.fromTime = fromTime;
  this.toTime = toTime;
  this.nToTime = 0;
  this.nFromTime = 0;
  this.strError = new String("");
  this.validDateString = /^(\d{1,2})(\/|-|\.)(\d{1,2})(\/|-|\.)(\d{2}|\d{4})$/
  this.dateFormatErrSt = " must be in one of the following formats: MM/DD/YY, MM/DD/YYYY, MM-DD-YY, MM-DD-YYYY, MM.DD.YY, or MM.DD.YYYY";
  this.formatDate = DateTimeValidator_formatDate;
  this.isDateValid = DateTimeValidator_isDateValid;
  this.isTimeValid = DateTimeValidator_isTimeValid;
  this.getFromDate = DateTimeValidator_getFromDate;
  this.getToDate = DateTimeValidator_getToDate;
  this.getDateRange = DateTimeValidator_getDateRange;
  this.getTimeRange = DateTimeValidator_getTimeRange;
  this.isDateFormatValid = DateTimeValidator_isDateFormatValid;
  this.getErrorMsg = DateTimeValidator_getErrorMsg;
}

function DateTimeValidator_isDateValid()
{
  var bValid = true;
  this.strError = "";
  
  if ( !(this.isDateFormatValid(this.fromDate)) ) {
    this.strError += "- Accident From Date" + this.dateFormatErrSt + "\n";
    bValid = false;

  } else {
    this.formattedFromDate = this.formatDate(this.fromDate);
    if (this.formattedFromDate == null) {
      this.strError += "- Accident From Date is not a valid date.\n";
      bValid=false;
    }
  }

  if ( !(this.isDateFormatValid(this.toDate)) ) {
    this.strError += "- Accident To Date" + this.dateFormatErrSt + "\n";
    bValid = false;

  } else {
    this.formattedToDate = this.formatDate(this.toDate);
    if (this.formattedToDate == null) {
      this.strError += "- Accident To Date is not a valid date.\n";
      bValid=false;
    }
  }

  if (this.formattedFromDate!=null && this.formattedToDate!=null) {
    fDateArray = this.formattedFromDate.split("/");
		tDateArray = this.formattedToDate.split("/");
		if (tDateArray[2] < fDateArray[2] ||
        (tDateArray[2] == fDateArray[2] &&
         tDateArray[0] < fDateArray[0]) ||
         (tDateArray[2] == fDateArray[2] &&
          tDateArray[0] == fDateArray[0] &&
          tDateArray[1] < fDateArray[1])) {
      this.strError += "- Accident To Date cannot be earlier than Accident From Date\n";
      bValid = false;
    }
  }

  return bValid;
}

function DateTimeValidator_isTimeValid()
{
  var bValid = true;
  this.strError = "";

  if ( (this.fromTime.length>0 && this.toTime.length<=0) ||
       (this.fromTime.length<=0 && this.toTime.length>0) ) {
    this.strError += "- enter from and to times\n"; 
    bValid=false;
  }

  if (this.fromTime.length>0 &&
      this.toTime.length>0) {
     this.nFromTime = parseInt(this.fromTime,10);
     this.nToTime = parseInt(this.toTime,10);

    // Check for valid time values with the date specified.
    if (this.formattedFromDate!=null &&
        this.formattedToDate!=null   &&
        this.formattedFromDate == this.formattedToDate &&
        this.nFromTime >= this.nToTime) {

      this.strError += "- to time must be greater than from time."
      bValid = false;
    }
  }

	return bValid;
}

function DateTimeValidator_getFromDate()
{
  if (this.formattedFromDate != null)
    return this.formattedFromDate;
  else
    return this.fromDate;
}

function DateTimeValidator_getToDate()
{
  if (this.formattedToDate != null)
    return this.formattedToDate;
  else
    return this.toDate;
}

function DateTimeValidator_getDateRange()
{
    var dateArrayFrom = this.fromDate.match(this.validDateString);
    var monthFrom = dateArrayFrom[1];
    var dayFrom = dateArrayFrom[3];
    var yearFrom = dateArrayFrom[5];
    var dateArrayTo = this.toDate.match(this.validDateString);
    var monthTo = dateArrayTo[1];
    var dayTo = dateArrayTo[3];
    var yearTo = dateArrayTo[5];
    var newdateFrom = new Date(yearFrom , (monthFrom - 1), dayFrom);
    var newdateTo = new Date(yearTo , (monthTo - 1), dayTo);
    var OneDay = 86400000;
    var date1_ms = newdateFrom.getTime();
    var date2_ms = newdateTo.getTime();
    var dateRange = Math.round(Math.abs(date1_ms - date2_ms)/OneDay);
    return dateRange
}

function DateTimeValidator_getTimeRange()
{
  var timeRange = 0;
  if (this.formattedFromDate!=null &&
      this.formattedToDate!=null ) {
    var dateRange = this.getDateRange();
    if (dateRange == 0) {
      timeRange = this.nToTime - this.nFromTime;

    } else {
      timeRange = (24-this.nFromTime) + this.nToTime + (24*(dateRange-1));
    }   
  }

  return timeRange;
}

function DateTimeValidator_getErrorMsg()
{
  return this.strError;
}

function DateTimeValidator_isDateFormatValid(date)
{
  if (!date.match(this.validDateString))
    return false;
  else
   return true;
}

function DateTimeValidator_formatDate(date)
{
  if ( !(this.isDateFormatValid(date)) )
    return null;

  var dateArray = date.match(this.validDateString);
  var month = dateArray[1];
  var day = dateArray[3];
  var year = dateArray[5];

  if (month < 1 || month >12)
    return null;

  if (day < 1 || day > 31)
		return null;

	if ( (month == 4 ||
        month == 6 || 
        month == 9 ||
        month == 11) &&
       day == 31)
     return null;

	if (month == 2) {
		var LeapYear = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day == 29 && !LeapYear)) {
			return null;
    }
	}

	if (day.length < 2)
		day = "0" + day;

	if (month.length < 2)
		month = "0" + month;

	if (year.length == 2) {

	  var now = new Date();
	  var theYear = now.getYear();
	  if (theYear < 1000) {
	    	theYear += 1900;
	  }

		if ("20" + year <= theYear) {
			year = "20" + year;
		} else {
			year = "19" + year;
		}
	}

	return (month + "/" + day + "/" + year);
}