﻿// JScript File

function ValidDate(gField, delim, y2kYear, yrDisp){
//
// Validate date and format date with specified delimiters. Date can be
// space, dash, period, or slash delimited. Dates are returned as
// MMDDYYYY or MMDDYY depending on the parameter passed in yrDisp.
//
//   yrDisp = C = MMDDYYYY
//   yrDisp = Y = MMDDYY
//
   if (gField.value == ""){return}

	// convert hyphen,space,period delimiters to specified date delimiter
   var inputStr = gField.value
   newStr = ""
   newStr = ReplaceString(inputStr,"-",delim)
   inputStr = newStr
   newStr = ReplaceString(inputStr," ",delim)
   inputStr = newStr
   newStr = ReplaceString(inputStr,".",delim)
   inputStr = newStr

   var delim1 = inputStr.indexOf(delim)
   var delim2 = inputStr.lastIndexOf(delim)
   if (delim1 != -1) {
      // there are delimiters; extract component values
      var mm = parseInt(inputStr.substring(0,delim1),10)
      var dd = parseInt(inputStr.substring(delim1 + 1,delim2),10)
      var yyyy = parseInt(inputStr.substring(delim2 + 1, inputStr.length),10)
   } else {
      // there are no delimiters; extract component values
      var mm = parseInt(inputStr.substring(0,2),10)
      var dd = parseInt(inputStr.substring(2,4),10)
      var yyyy = parseInt(inputStr.substring(4,inputStr.length),10)
   }
   if (isNaN(mm) || isNaN(dd) || isNaN(yyyy)) {
      // there is a non-numeric character in one of the component values
      alert("The date entry is not in an acceptable format.\n\nYou can enter dates in the following formats: mmddyyyy, mm/dd/yyyy, mm dd yyyy or mm-dd-yyyy.")
      gField.value = ""
	  gField.focus()
      gField.select()
      return false
   }
   if (mm < 1 || mm > 12) {
      // month value is not 1 thru 12
      alert("Months must be entered between the range of 01 (January) and 12 (December).")
      gField.value = ""
	  gField.focus()
      gField.select()
      return false
   }
   // validate year
   if (yyyy < 100) {
      // entered value is two digits
      if (yyyy > y2kYear) {
         yyyy += 1900
      }else{
         yyyy += 2000
      }
   }

   var today = new Date()
   // If day exceed highest day of month, set to highest day of month
   monthMax = new Array(31,31,29,31,30,31,30,31,31,30,31,30,31)
   mMax = monthMax[mm]
   if (dd > mMax){
      dd = monthMax[mm]
   }
   // Adjust day of February for leap years
   if (mm == 2 && dd >= 29) {
      dd = (((yyyy %4 == 0) && ((!(yyyy % 100 == 0)) || (yyyy % 400 == 0))) ? 29 : 28)
   }
   // Format MM and DD with preceeding "0"s
   mm = mm.toString()
   if (mm.length != 2){
       mm = "0" + mm
   }
   dd = dd.toString()
   if (dd.length != 2){
       dd = "0" + dd
   }
   // Format the year based on the yrDisp value. "Y" = 2 digit year.
   yyyy = yyyy.toString()
   if (yrDisp == "Y"){
       yyyy = yyyy.substr(2,2)
   }

   if (yyyy>2099 || yyyy<1900) {
      alert("Invalid Year!")
      //gField.value = ""
	  gField.focus()
      gField.select()
      return false	
	}

   gField.value = mm + delim + dd + delim + yyyy.toString()
   return true
   
}

function ReplaceString(strData, lookFor, replaceWith){
//
// Replace all occurances of a substring with a specified substring
// within the data object.
//
    newStr = ""
    var testStr = strData
    if (testStr.indexOf(lookFor) != -1) {
       myArray = testStr.split(lookFor)
       for (i=0; i < myArray.length; i++){
          if (i != myArray.length-1){
             newStr += myArray[i] + replaceWith
          }else{
             newStr += myArray[i]
          }
       }
       return newStr
   }else{
       return strData
   }
}