/*  Copyright © 2001, 2002 OGMA Consulting Corp. */

/*
 * validDateFormat(s) : checks dates to ensure dates are the proper format YYYY/MM/DD
 */
function validDateFormat(s) {
  if (s.length != 10) {
    return false
  }

  for (j = 0; j < s.length; j++) {
    if ((j != 4) && (j != 7)) {
      if ((s.charAt(j) < "0") || (s.charAt(j) > "9")) {
        return false;
      }
    } else {
      if (s.charAt(j) != "/") {
        return false;
      }
    }
  }

  return true
} // validDateFormat(s)

/*
 * checkAllDates() : calls CheckDate for both the beg_date field and the
 *                    end_date field returning false if either are invalid
 */
function checkAllDates(errorWindow) {
  if (!CheckDate('beg_date','beg_date_help', errorWindow)) {
    return false
  }
  if (!CheckDate('end_date','end_date_help', errorWindow)) {
    return false
  }
  return true
}

/*
 * CheckDate(field,msg) : returns an error message msg if not a valid date.
 *                        used for the input field on change event.
 *                        Calls EditDate.
 */
function CheckDate(field, msg, errorWindow) {
  field1 = eval("document.forms[0]." + field)
  if ((field1.value != '') && (field1.value !=  ' ')) {
    if (!EditDate(field, msg, errorWindow)) {
      return false
    }
  }
  return true
}

/*
 * EditDate(s,msg) : returns an error message msg if not a valid date.
 *                   used for the input field on change event
 */
function EditDate(s, msg, errorWindow) {
  var message

  var checkvalue = eval("document.forms[0]." + s)

  if (checkvalue.value == "") { return true }

  if ((!validDateFormat(checkvalue.value)) || (!validDate(checkvalue.value))) {
    if (errorWindow) {
      validErrorWindow('showValidError', 'ValErr1001||<br><br>');
    } else {
      message = getValueByName(msg);  //eval("document.forms[0]." + msg + ".value")
      alert (message)
    }
    return false
//  } else if (!validDate(checkvalue.value)) {
//    message = getValueByName(msg);
//    alert (message)
//    return false
  }
   return true
}

/*
 * EditDateFuture(s,msg) : returns an error message msg if not a valid date.
 *                   used for the input field on change event
 */
function EditDateFuture(s, msg, errorWindow) {
  var message
  var checkvalue = eval("document.forms[0]." + s)

  if (checkvalue.value == "") { return true }

  if ((!validDateFormat(checkvalue.value)) || (!validDate(checkvalue.value)) || (!validDateFuture(checkvalue.value))) {
    if (errorWindow) {
      validErrorWindow('showValidError', 'ValErr1001||<br><br>'); //new error msg?
    } else {
      message = getValueByName(msg);
      alert (message)
    }
    return false
  }
  return true
}

/*
 * validDateFuture(s) : checks dates to ensure that dates are not in the past.
 */
function validDateFuture(s) {
  var now = new Date();
  var year = s.charAt(0) + s.charAt(1) + s.charAt(2) + s.charAt(3);
  var month = s.charAt(5) + s.charAt(6);
  var day = s.charAt(8) + s.charAt(9);
  var test = new Date(year, month-1, day);

  if (test >= now) {
    return true;
  } else {
    return false;
  }
}

/*
 * validDatePast(s) : checks dates to ensure that dates are in the past or right now.
 */
function validDatePast(s) {
  var now = new Date();
  var year = s.charAt(0) + s.charAt(1) + s.charAt(2) + s.charAt(3);
  var month = s.charAt(5) + s.charAt(6);
  var day = s.charAt(8) + s.charAt(9);
  var test = new Date(year, month-1, day);

  if (test <= now) {
    return true;
  } else {
    return false;
  }
}

/*
 * validDateToday(s) : checks dates to ensure that date is today.
 */
function validDateToday(s) {
  var now = new Date();
  var year = s.charAt(0) + s.charAt(1) + s.charAt(2) + s.charAt(3);
  var month = s.charAt(5) + s.charAt(6);
  var day = s.charAt(8) + s.charAt(9);
  var test = new Date(year, month-1, day);

  if (test == now) {
    return true;
  } else {
    return false;
  }
}


/*
 * validDate(s) : checks dates to ensure that dates are in the correct
 *                range ie. 2000/32/10 is not correct since there are not
 *                32 months. Assumes the date is already in the correct format
 */
function validDate(s) {
  var year  = "";
  var month = "";
  var day   = "";

  year = s.charAt(0) + s.charAt(1) + s.charAt(2) + s.charAt(3)

  if (!validYear(year)) {
    return false
  }

  month = s.charAt(5) + s.charAt(6)

  if (!validMonth(month)) {
    return false
  }

  day = s.charAt(8) + s.charAt(9)

  if (!validDay(day)) {
    return false
  }

  if (!validMonthDay(month, day)) {
    return false
  }

  return true
} // validDate(s)

/*
 * validYear(s) : returns true if the year is in the valid range
 */
function validYear(s) {
  if ((s >= "1900") && (s <= "3010") && (s.length == 4)) {
    return true
  }
  return false
} // validYear(s)

/*
 * validMonthDay(s) : returns true if the month/day combination is valid
 */
function validMonthDay(month, day) {
  if (month == "02") {
    if (day > "29") {
      return false
    }
  }
  if (month == "04") {
    if (day > "30") {
      return false
    }
  }
  if (month == "06") {
    if (day > "30") {
      return false
    }
  }
  if (month == "09") {
    if (day > "30") {
      return false
    }
  }
  if (month == "11") {
    if (day > "30") {
      return false
    }
  }
  return true
} // validMonthDay(s)

/*
 * validMonth(s) : returns true if the month is valid
 */
function validMonth(s) {
  if ((s > "00") && (s < "13")) {
    return true
  }
  return false
} // validMonth(s)

/*
 * validDay(s) : returns true if the day is valid
 */
function validDay(s) {
  if ((s > "00") && (s < "32")) {
    return true
  }
  return false
} // validDay(s)

/*
 * isDtABeforeDtB(dateA, timeA, dateB, timeB, msg, errorWindow) :
 *     Returns false and displays an error message (msg) if dateTimeA
 *     is after dateTimeB.  Calls getValueByCharTypeQualifier(...)
 *     which is in getSetValues.js.
 */
function isDtABeforeDtB(dateA, timeA, dateB, timeB, msg, errorWindow) {

	var dtA = getValueByCharTypeQualifier(dateA) + getValueByCharTypeQualifier(timeA);
	var dtB = getValueByCharTypeQualifier(dateB) + getValueByCharTypeQualifier(timeB);

	if (dtA < dtB) {
		return true;
	} else {

    if (errorWindow) {
      validErrorWindow('showValidError', 'ValErr1001||<br><br>');
    } else {
      alert(getValueByName(msg));
    }

		return false;
	}
}
