/*
'**************************************************************
' Copyright (c) Epic Systems Corporation 1999-2004
' Name: dateFormat.js
' Author:
'
' Description: Javascript functions to format the date
'
' Revision History
' *kad 3/04 71871 created
'***************************************************************
*/

//Formats the date, may display an error if it could not format it
// objDateField - is the input field
// monthPc - is the number 1-3 piece that the month occupies
// yearPc - is the number 1-3 piece that the year occupies
// validType - is an indicator of the type of date that should be accepted
//   0 - all valid dates
//   1 - past dates only
//   2 - future dates only
//   3 - future or today only
//   4 - past or today only
function formatDate(objDateField,monthPc,yearPc,validType) {
  if (objDateField.value == "") {
    return "";
  }

  switch(yearPc) {
  case 1:   // yyyy/xx/xx
    switch (monthPc) {
    case 3:   // yyyy/dd/mm
      formatYearDayMonth(objDateField,validType);
      break;
    default:  // yyyy/mm/dd
      formatYearMonthDay(objDateField,validType);
    }
    break;
  default:  // xx/xx/yyyy
    switch (monthPc) {
    case 2:   // dd/mm/yyyy
      formatDayMonthYear(objDateField,validType);
      break;
    default:  // mm/dd/yyyy
      formatMonthDayYear(objDateField,validType);
    }
  }
  return "";
} //End of formatDate

// Formats and checks the date for yyyy/dd/mm settings
function formatYearDayMonth(objDateField,validType) {
  var sDate = objDateField.value
  var dDate,sFormat,sMatch
  sMatch = sDate.match(/^\s*(\d+)\s*\D\s*(\d+)\s*$/);
  if (sMatch != null) {  // dd/mm with delimiters
    dDate = makeDateObj(sDate,"yyyy/dd/mm",sMatch[1],sMatch[2],"");
  }
  else {
    sMatch = sDate.match(/^\s*(\d{2})\s*(\d{2})\s*$/);
    if (sMatch != null) {  // ddmm no delimiters, digits only
      dDate = makeDateObj(sDate,"yyyy/dd/mm",sMatch[1],sMatch[2],"");
    }
    else {
      sMatch = sDate.match(/^\s*(\d{2}(\d{2})?)\s*\D\s*(\d+)\s*\D\s*(\d+)\s*$/);
      if (sMatch != null) {  // full with delimiters
        dDate = makeDateObj(sDate,"yyyy/dd/mm",sMatch[3],sMatch[4],sMatch[1]);
      }
      else {
        sMatch = sDate.match(/^\s*(\d{2}(\d{2})?)\s*(\d{2})\s*(\d{2})\s*$/);
        if (sMatch != null) {  // full no delimiters, digits only
          dDate = makeDateObj(sDate,"yyyy/dd/mm",sMatch[3],sMatch[4],sMatch[1]);
        }
      }
    }
  }
  if (sMatch == null) {
    noMatch(sDate,"yyyy/dd/mm");
    objDateField.focus();
    objDateField.select();
    return "";
  }
  if (dDate == null) {
    objDateField.focus();
    objDateField.select();
    return "";
  }
  sFormat = dDate.getFullYear() + "/" + addZero(dDate.getDate()) + "/" + addZero(dDate.getMonth()+1);
  if (checkDate(dDate,sFormat,validType)) {
    objDateField.focus();
    objDateField.select();
    return "";
  }
  objDateField.value = sFormat;
  return "";
} //End of formatYearDayMonth

// Formats and checks the date for dd/mm/yyyy settings
function formatYearMonthDay(objDateField,validType) {
  var sDate = objDateField.value
  var dDate,sFormat,sMatch
  sMatch = sDate.match(/^\s*(\d+)\s*\D\s*(\d+)\s*$/);
  if (sMatch != null) {  // mm/dd with delimiters
    dDate = makeDateObj(sDate,"yyyy/mm/dd",sMatch[2],sMatch[1],"");
  }
  else {
    sMatch = sDate.match(/^\s*(\d{2})\s*(\d{2})\s*$/);
    if (sMatch != null) {  // mmdd no delimiters, digits only
      dDate = makeDateObj(sDate,"yyyy/mm/dd",sMatch[2],sMatch[1],"");
    }
    else {
      sMatch = sDate.match(/^\s*(\d{2}(\d{2})?)\s*\D\s*(\d+)\s*\D\s*(\d+)\s*$/);
      if (sMatch != null) {  // full with delimiters
        dDate = makeDateObj(sDate,"yyyy/mm/dd",sMatch[4],sMatch[3],sMatch[1]);
      }
      else {
        sMatch = sDate.match(/^\s*(\d{2}(\d{2})?)\s*(\d{2})\s*(\d{2})\s*$/);
        if (sMatch != null) {  // full no delimiters, digits only
          dDate = makeDateObj(sDate,"yyyy/mm/dd",sMatch[4],sMatch[3],sMatch[1]);
        }
      }
    }
  }
  if (sMatch == null) {
    noMatch(sDate,"yyyy/mm/dd");
    objDateField.focus();
    objDateField.select();
    return "";
  }
  if (dDate == null) {
    objDateField.focus();
    objDateField.select();
    return "";
  }
  sFormat = dDate.getFullYear() + "/" + addZero(dDate.getMonth()+1) + "/" + addZero(dDate.getDate());
  if (checkDate(dDate,sFormat,validType)) {
    objDateField.focus();
    objDateField.select();
    return "";
  }
  objDateField.value = sFormat;
  return "";
} //End of formatYearMonthDay

// Formats and checks the date for dd/mm/yyyy settings
function formatDayMonthYear(objDateField,validType) {
  var sDate = objDateField.value
  var dDate,sFormat,sMatch
  sMatch = sDate.match(/^\s*(\d+)\s*\D\s*(\d+)\s*(\D\s*(\d{2}(\d{2})?))?\s*$/);
  if (sMatch != null) {  // with delimiters
    dDate = makeDateObj(sDate,"dd/mm/yyyy",sMatch[1],sMatch[2],sMatch[4]);
  }
  else {
    sMatch = sDate.match(/^\s*(\d{2})\s*(\d{2})\s*(\d{2}(\d{2})?)?\s*$/);
    if (sMatch != null) {  // no delimiters, digits only
      dDate = makeDateObj(sDate,"dd/mm/yyyy",sMatch[1],sMatch[2],sMatch[3]);
    }
  }
  if (sMatch == null) {
    noMatch(sDate,"dd/mm/yyyy");
    objDateField.focus();
    objDateField.select();
    return "";
  }
  if (dDate == null) {
    objDateField.focus();
    objDateField.select();
    return "";
  }
  sFormat = addZero(dDate.getDate()) + "/" + addZero(dDate.getMonth()+1) + "/" + dDate.getFullYear();
  if (checkDate(dDate,sFormat,validType)) {
    objDateField.focus();
    objDateField.select();
    return "";
  }
  objDateField.value = sFormat;
  return "";
} //End of formatDayMonthYear

// Formats and checks the date for mm/dd/yyyy settings
function formatMonthDayYear(objDateField,validType) {
  var sDate = objDateField.value;
  var dDate,sFormat,sMatch
  sMatch = sDate.match(/^\s*(\d+)\s*\D\s*(\d+)\s*(\D\s*(\d{2}(\d{2})?))?\s*$/);
  if (sMatch != null) {  // with delimiters
    dDate = makeDateObj(sDate,"mm/dd/yyyy",sMatch[2],sMatch[1],sMatch[4]);
  }
  else {
    sMatch = sDate.match(/^\s*(\d{2})\s*(\d{2})\s*(\d{2}(\d{2})?)?\s*$/);
    if (sMatch != null) {  // no delimiters, digits only
      dDate = makeDateObj(sDate,"mm/dd/yyyy",sMatch[2],sMatch[1],sMatch[3]);
    }
  }
  if (sMatch == null) {
    noMatch(sDate,"mm/dd/yyyy");
    objDateField.focus();
    objDateField.select();
    return "";
  }
  if (dDate == null) {
    objDateField.focus();
    objDateField.select();
    return "";
  }
  sFormat = addZero(dDate.getMonth()+1) + "/" + addZero(dDate.getDate()) + "/" + dDate.getFullYear();
  if (checkDate(dDate,sFormat,validType)) {
    objDateField.focus();
    objDateField.select();
    return "";
  }
  objDateField.value = sFormat;
  return "";
} //End of formatMonthDayYear

// Returns a date object based on sDay, sMonth, and sYear passed in
// Defaults today for any information not passed in
// Defaults 19xx for the two digit year if xx >= 30, otherwise 20xx
// May display error alerts if invalid sDay or sMonth are passed in
// sDate and sFormat are used for error messages
function makeDateObj(sDate,sFormat,sDay,sMonth,sYear){
  var dDate = new Date();
  if (sDay == null || sDay == "") {
    sDay = dDate.getDate();
  }
  if (sMonth == null || sMonth == "") {
    sMonth = dDate.getMonth() + 1;
  }
  if (sYear == null || sYear == "") {
    sYear = dDate.getFullYear();
  }
  if (sYear.length == 2) {
    if (sYear < 30) {
      sYear = "20" + sYear;
    }
    else {
      sYear = "19" + sYear;
    }
  }
  if ((sMonth < 1) || (sMonth > 12)) {
    alert(sDate + " is an invalid date.\nThe month must be in the range 1-12.\nPlease reenter the date in " + sFormat + " format.");
    return null;
  }
  dDate = new Date(sYear,sMonth-1,sDay);
  if (sDay != dDate.getDate()) {
    alert(sDate + " is an invalid date.\nThe day is not valid for the month.\nPlease reenter the date in " + sFormat + " format.");
    return null;
  }
  return dDate;
} //End of makeDateObj

// Displays an alert
// Intended to be used when the formatter cannot figure out the date format.
function noMatch(sDate,sFormat) {
  alert(sDate + " is not a recognized date format.\nPlease reenter the date in " + sFormat + " format.");
  return "";
} //End of noMatch

// Adds a zero to the front of the string if the number is < 10
// Intended to pad date days and months
function addZero(sNum) {
  if (sNum < 10) {
    return "0" + sNum;
  }
  return sNum;
} //End of addZero

// Does any other checking once the date has been formatted.
// validType - is an indicator of the type of date that should be accepted
//   0 - all valid dates
//   1 - past dates only
//   2 - future dates only
//   3 - future or today only
//   4 - past or today only
// Returns true if the date is NOT valid
function checkDate(dDate,sDate,validType) {
  var dToday = new Date();
  switch (validType) {
  case 1:  // past dates only
    if ((dToday.getFullYear() < dDate.getFullYear()) || 
       ((dToday.getFullYear() == dDate.getFullYear()) && 
       ((dToday.getMonth() < dDate.getMonth()) || 
       ((dToday.getMonth() == dDate.getMonth()) && 
       (dToday.getDate() <= dDate.getDate()))))) {
      alert(sDate + " is not valid.\nThe date must be in the past.")
      return true;
    }
    break;
  case 2:  // future dates only
    if ((dToday.getFullYear() > dDate.getFullYear()) || 
       ((dToday.getFullYear() == dDate.getFullYear()) && 
       ((dToday.getMonth() > dDate.getMonth()) || 
       ((dToday.getMonth() == dDate.getMonth()) && 
       (dToday.getDate() >= dDate.getDate()))))) {
      alert(sDate + " is not valid.\nThe date must be in the future.")
      return true;
    }
    break;
  case 3:  // future or today only
    if ((dToday.getFullYear() > dDate.getFullYear()) || 
       ((dToday.getFullYear() == dDate.getFullYear()) && 
       ((dToday.getMonth() > dDate.getMonth()) || 
       ((dToday.getMonth() == dDate.getMonth()) && 
       (dToday.getDate() > dDate.getDate()))))) {
      alert(sDate + " is not valid.\nThe date must be today or in the future.")
      return true;
    }
    break;
  case 4:  // past or today only
    if ((dToday.getFullYear() < dDate.getFullYear()) || 
       ((dToday.getFullYear() == dDate.getFullYear()) && 
       ((dToday.getMonth() < dDate.getMonth()) || 
       ((dToday.getMonth() == dDate.getMonth()) && 
       (dToday.getDate() < dDate.getDate()))))) {
      alert(sDate + " is not valid.\nThe date must be today or in the past.")
      return true;
    }
    break;
  }
  return false;
} //End of checkDate