var strFormattedData;

function DateFormat(vDateName, vDateValue, e, dateCheck, dateType)
{
   // vDateName = object name
   // vDateValue = value in the field being checked
   // e = event
   // dateCheck
   // True  = Verify that the vDateValue is a valid date
   // False = Format values being entered into vDateValue only
   // vDateType
   // 1 = mm/dd/yyyy
   // 2 = yyyy/mm/dd - not supported at this time
   // 3 = dd/mm/yyyy - not supported at this time
   // 4 = hh:mm:ss am
   // 5 = mm/dd/yyyy hh:mm:ss am
   //Enter a tilde sign for the first number and you can check the variable information.
// Check browser version
var isNav4 = false;
var isNav5 = false;
var isIE4 = false;
var strSeparator = "/";
var strTimeSeparator = ":";
var strSpace = " ";
var vDateType = 1; // Global value for type of date format
//                1 = mm/dd/yyyy
//                2 = yyyy/dd/mm - not supported at this time
//                3 = dd/mm/yyyy - not supported at this time
//                4 = hh:mm:ss am
//                5 = mm/dd/yyyy hh:mm:ss am
var vYearType = 4; //Set to 2 or 4 for number of digits in the year for Netscape
var vYearLength = 2; // Set to 4 if you want to force the user to enter 4 digits for the year before validating.
var err = 0; // Set the error code to a default of zero


if (navigator.appName == "Netscape")
{
   if (navigator.appVersion < "5")
   {
      isNav4 = true;
      isNav5 = false;
   }
   else
   {
      isNav4 = false;
      isNav5 = true;
   }
}
else
{
   isIE4 = true;
}

   vDateType = dateType;

   if (vDateValue == "~")
   {
      alert("AppVersion = "+navigator.appVersion+" \nNav. 4 Version = "+isNav4+" \nNav. 5 Version = "+isNav5+" \nIE Version = "+isIE4+" \nYear Type = "+vYearType+" \nDate Type = "+vDateType+" \nSeparator = "+strSeparator);
      vDateName.value = "";
      vDateName.focus();
      return true;
   }
   var whichCode = (window.Event) ? e.which : e.keyCode;


   if (dateCheck)
   {
      if (dateType == 1)
      {
         if (CheckDate(vDateName, vDateValue, isNav4))
         {
            if (vDateValue.length != 0)
               vDateName.value = strFormattedData;
            return true;
         }
         else
            return false;
      }

      if (dateType == 4) // time
      {
         if (CheckTime(vDateName, vDateValue, isNav4))
            return true;
         else
            return false;
      }

      if (dateType == 5) // datetime
      {
         if (vDateValue.length == 0)
            return true;

         // first check length of string
         if (((vDateValue.length != 18) && (vDateValue.length != 20) && (vDateValue.length != 22) &&
              (vDateValue.length != 17) && (vDateValue.length != 19) && (vDateValue.length != 21)) ||
             (!isNav4 && vDateValue.length == 18))
         {
            alert("Invalid value\nPlease re-enter");
            vDateName.focus();
            return false;
         }

var iSpacePos;
var strDatePart, strTimePart;

         // now split string into date and time
         iSpacePos = vDateValue.indexOf(" ");

         // if len is 18, space should be at 6
         if (vDateValue.length == 18 || vDateValue.length == 17)
         {
            if (iSpacePos != 6)
            {
               alert("Invalid value\nPlease re-enter");
               vDateName.focus();
               return false;
            }
            else
            {
               // split up date and time
               strDatePart = vDateName.value.substr(0,6);
               strTimePart = vDateName.value.substr(7);
            }
         }
         // if len is 20, space should be at 8
         if (vDateValue.length == 20 || vDateValue.length == 19)
         {
            if (iSpacePos != 8)
            {
               alert("Invalid value\nPlease re-enter");
               vDateName.focus();
               return false;
            }
            else
            {
               // split up date and time
               strDatePart = vDateName.value.substr(0,8);
               strTimePart = vDateName.value.substr(9);
            }
         }
         // if len is 22, space should be at 10
         if (vDateValue.length == 22 || vDateValue.length == 21)
         {
            if (iSpacePos != 10)
            {
               alert("Invalid value\nPlease re-enter");
               vDateName.focus();
               return false;
            }
            else
            {
               // split up date and time
               strDatePart = vDateName.value.substr(0,10);
               strTimePart = vDateName.value.substr(11);
            }
         }

         // validate date and time
         if (CheckDate(vDateName, strDatePart, isNav4))
         {
            if (CheckTime(vDateName, strTimePart, isNav4))
            {
               var strTempData;
               strTempData = strFormattedData + " " + strTimePart;
               vDateName.value = strTempData;
               return true;
            }
            else
               return false;
         }
         else
            return false;
      }
   }
   else
   {
      // do alpha check - needed???
      //Eliminate all the ASCII codes that are not valid
      var alphaCheck = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-";
      if (alphaCheck.indexOf(vDateValue) >= 1)
      {
         undoLast(vDateName, vDateValue, isNav4);
         return false;
      }
      if ((whichCode == 8) || (whichCode == 16)) //Ignore the Netscape value for backspace. IE has no value
         return false;                           // also ignore shift keys

      // no auto-format for netscape4
      if (isNav4)
         return true;

      var strCheck = '47,48,49,50,51,52,53,54,55,56,57,58,59,95,96,97,98,99,100,101,102,103,104,105';
      var strTimeCheck = '65,80,97,112'; // for am/pm time

      if (dateType == 1)
      {
         // check whether the current key code is valid - if not, reset string back by 1 char
         // if valid, and browser is not nav4, and if vDateValue.length is 2 or 5, add separator
         // and reset field, then return true
         if (strCheck.indexOf(whichCode) != -1)
         {
            if (vDateValue.length == 2)
            {
               vDateName.value = vDateValue+strSeparator;
            }
            if (vDateValue.length == 5)
            {
               vDateName.value = vDateValue+strSeparator;
            }
            return true;
         }
         else
         {
            // If the value is not in the string return the string minus the last
            // key entered.
            undoLast(vDateName, vDateValue, isNav4);
            return false;
         }
      }

      if (vDateType == 5)
      {
         var iLength;

         iLength = vDateValue.length;

         if (iLength <= 8)
         {
            if (strCheck.indexOf(whichCode) != -1)
            {
               if ((vDateValue.length == 2) || (vDateValue.length == 5))
                  vDateName.value = vDateValue+strSeparator;
               return true;
            }
            else
            {
               // If the value is not in the string return the string minus the last
               // key entered.
               undoLast(vDateName, vDateValue, isNav4);
               return false;
            }
         }
         else
         {
            if ((iLength == 9) && (whichCode == 32)) // allow space in 8th pos
            {
               return true;
            }

            // find position of first space - there should be one if length is more than 10
            iSpacePos = vDateValue.indexOf(" ");

            if (iSpacePos == 9)  // can't have space in 9th pos
            {
               undoLast(vDateName, vDateValue, isNav4);
               return false;
            }

            if (iSpacePos == -1) // no space found
            {
               if (iLength <= 10) // just validate key and exit
               {
                  if (strCheck.indexOf(whichCode) != -1)
                  {
                     if (vDateValue.length == 10)
                        vDateName.value = vDateValue + " ";
                     return true;
                  }
                  else
                  {
                     // If the value is not in the string return the string minus the last
                     // key entered.
                     undoLast(vDateName, vDateValue, isNav4);
                     return false;
                  }
               }
            }
            else
            {
               if (iSpacePos == 8)
               {
                  if (iLength <= 17)
                  {
                     if (strCheck.indexOf(whichCode) != -1)
                     {
                        if ((iLength == 11) || (iLength == 14))
                           vDateName.value = vDateValue+strTimeSeparator;
                        if (iLength == 17)
                           vDateName.value = vDateValue + " ";
                        return true;
                     }
                     else
                     {
                        // If the value is not in the string return the string minus the last
                        // key entered.
                        undoLast(vDateName, vDateValue, isNav4);
                        return false;
                     }
                  }
                  else
                  {
                     if (iLength == 18)
                     {
                        // only space allowed
                        if (whichCode != 32)
                        {
                           undoLast(vDateName, vDateValue, isNav4);
                           return false;
                        }
                        else
                           return true;
                     }
                     else
                     {
                        if (iLength > 20)
                        {
                           // validate time and exit
                           return true;
                        }
                        else
                        {
                           // last 2 chars should be am or pm
                           if (iLength == 19)
                           {
                              if (strTimeCheck.indexOf(whichCode) != -1)
                              {
                                 return true;
                              }
                              else
                              {
                                 // If the value is not in the string return the string minus the last
                                 // key entered.
                                 undoLast(vDateName, vDateValue, isNav4);
                                 return false;
                              }
                           }
                           else
                           {
                              // last character should be m or M (am/pm)
                              var strMm = '77,109';

                              if (strMm.indexOf(whichCode) != -1)
                              {
                                 // validate time
                                 return true;
                              }
                              else
                              {
                                 // If the value is not in the string return the string minus the last
                                 // key entered.
                                 undoLast(vDateName, vDateValue, isNav4);
                                 return false;
                              }
                           }
                        }
                     }
                  }
               }
               if (iSpacePos == 10)
               {
                  if (iLength <= 19)
                  {
                     if (strCheck.indexOf(whichCode) != -1)
                     {
                        if ((iLength == 13) || (iLength == 16))
                           vDateName.value = vDateValue+strTimeSeparator;
                        if (iLength == 19)
                           vDateName.value = vDateValue + " ";
                        return true;
                     }
                     else
                     {
                        // If the value is not in the string return the string minus the last
                        // key entered.
                        undoLast(vDateName, vDateValue, isNav4);
                        return false;
                     }
                  }
                  else
                  {
                     if (iLength == 20)
                     {
                        // only space allowed
                        if (whichCode != 32)
                        {
                           undoLast(vDateName, vDateValue, isNav4);
                           return false;
                        }
                        else
                           return true;
                     }
                     else
                     {
                        if (iLength > 22)
                        {
                           // validate time and exit
                           return true;
                        }
                        else
                        {
                           // last 2 chars should be am or pm
                           if (iLength == 21)
                           {
                              if (strTimeCheck.indexOf(whichCode) != -1)
                              {
                                 return true;
                              }
                              else
                              {
                                 // If the value is not in the string return the string minus the last
                                 // key entered.
                                 undoLast(vDateName, vDateValue, isNav4);
                                 return false;
                              }
                           }
                           else
                           {
                              // last character should be m or M (am/pm)
                              var strMm = '77,109';

                              if (strMm.indexOf(whichCode) != -1)
                              {
                                 // validate time
                                 return true;
                              }
                              else
                              {
                                 // If the value is not in the string return the string minus the last
                                 // key entered.
                                 undoLast(vDateName, vDateValue, isNav4);
                                 return false;
                              }
                           }
                        }
                     }
                  }

               }
            }
         }
      }

      if (vDateType == 4)
      {
         // the first 8 characters is the time
         if (vDateValue.length <= 8)
         {
            if (strCheck.indexOf(whichCode) != -1)
            {
               if ((vDateValue.length == 2) || (vDateValue.length == 5))
                  vDateName.value = vDateValue+strTimeSeparator;
               if (vDateValue.length == 8)
                  vDateName.value = vDateValue+strSpace;

               // if length is 11, validate time
               return true;
            }
            else
            {
               // If the value is not in the string return the string minus the last
               // key entered.
               undoLast(vDateName, vDateValue, isNav4);
               return false;
            }
         }
         else
         {
            // the last 2 characters is the am/pm
            if (vDateValue.length == 9)
            {
               if (whichCode != 32) // 9th char must be space
               {
                  undoLast(vDateName, vDateValue, isNav4);
                  return false;
               }
               else
               {
                  return true;
               }
            }
            else
            {
               if (vDateValue.length > 11)
               {
                  // validate time and exit
                  return true;
               }
               else
               {
                  // last 2 chars should be am or pm
                  if (vDateValue.length == 10)
                  {
                     if (strTimeCheck.indexOf(whichCode) != -1)
                     {
                        return true;
                     }
                     else
                     {
                        // If the value is not in the string return the string minus the last
                        // key entered.
                        undoLast(vDateName, vDateValue, isNav4);
                        return false;
                     }
                  }
                  else
                  {
                     // last character should be m or M (am/pm)
                     var strMm = '77,109';

                     if (strMm.indexOf(whichCode) != -1)
                     {
                        // validate time
                        return true;
                     }
                     else
                     {
                        // If the value is not in the string return the string minus the last
                        // key entered.
                        undoLast(vDateName, vDateValue, isNav4);
                        return false;
                     }
                  }
               }
            }
         }
      }
   }
}

// function CheckDate(vDateName, vDateValue, e, dateCheck, dateType, isNav4)
function CheckDate(vDateName, vDateValue, isNav4)
{
   var strFormattedDate;
   var mDay;
   var mMonth;
   var mYear;
   var strSeparator = "/";

   if (vDateValue.length == 0)
      return true;

   // check for non-valid lengths of string
   if ((vDateValue.length != 6) && (vDateValue.length != 8) && (vDateValue.length != 10))
   {
      alert("Invalid Date\nPlease Re-Enter");
      vDateName.focus();
      return false;
   }

   if (isNav4)
   {
      if (vDateValue.length == 6)
      {
         // add separators and convert 2-digit year to 4 digits
         mDay = vDateValue.substr(2,2);
         mMonth = vDateValue.substr(0,2);
         mYear = vDateValue.substr(4,4)

         //Turn a two digit year into a 4 digit year
         var mToday = new Date();

         //If the year is greater than 30 years from now use 19, otherwise use 20
         var checkYear = mToday.getFullYear() + 30;
         var mCheckYear = '20' + mYear;
         if (mCheckYear >= checkYear)
            mYear = '19' + mYear;
         else
            mYear = '20' + mYear;
      }
      else
      {
         if (vDateValue.length == 8)
         {
            // if string contains separators, convert 2-digit year to 4 digits
            // else add separators (string has 4-digit year)
            if ((vDateValue.indexOf("-") >= 1) || (vDateValue.indexOf("/") >= 1))
            {
               mDay = vDateValue.substr(3,2);
               mMonth = vDateValue.substr(0,2);
               mYear = vDateValue.substr(6,4)

               //Turn a two digit year into a 4 digit year
               var mToday = new Date();

               //If the year is greater than 30 years from now use 19, otherwise use 20
               var checkYear = mToday.getFullYear() + 30;
               var mCheckYear = '20' + mYear;
               if (mCheckYear >= checkYear)
                  mYear = '19' + mYear;
               else
                  mYear = '20' + mYear;
            }
            else
            {
               mDay = vDateValue.substr(2,2);
               mMonth = vDateValue.substr(0,2);
               mYear = vDateValue.substr(4,4)
            }
         }
         else
         {
            // no need to format the date
            mDay = vDateValue.substr(3,2);
            mMonth = vDateValue.substr(0,2);
            mYear = vDateValue.substr(6,4)
         }
      }
   }
   else
   {
      if (vDateValue.length == 6)
      {
         alert("Invalid Date\nPlease Re-Enter");
         vDateName.focus();
         return false;
      }

      mDay = vDateValue.substr(3,2);
      mMonth = vDateValue.substr(0,2);
      mYear = vDateValue.substr(6,4)

      if (vDateValue.length == 8)
      {
         // convert 2-digit year to 4-digit

         //Turn a two digit year into a 4 digit year
         var mToday = new Date();

         //If the year is greater than 30 years from now use 19, otherwise use 20
         var checkYear = mToday.getFullYear() + 30;
         var mCheckYear = '20' + mYear;
         if (mCheckYear >= checkYear)
            mYear = '19' + mYear;
         else
            mYear = '20' + mYear;
      }
   }

   strFormattedDate = mMonth+strSeparator+mDay+strSeparator+mYear;

   // now validate the properly formatted string
   // if valid, reset field to properly formatted string and return true
   // else put up invalid message and return false
   if (!dateValid(strFormattedDate))
   {
      if (isNav4)
      {
         alert("Invalid Date\nPlease Re-Enter");
         vDateName.value = "";
         vDateName.focus();
         vDateName.select();
         return false;
      }
      else
      {
         alert("Invalid Date\nPlease Re-Enter");
         vDateName.value = "";
         vDateName.focus();
         return false;
      }
   }
   else
   {
      strFormattedData = strFormattedDate;
      return true;
   }
}

function CheckTime(vDateName, vDateValue, isNav4)
{
   var bValid;

   bValid = true;

   // check length of string and whether separators are in the right place
   if (vDateValue.length == 0) // blank time is a valid value
      return true;

   if (vDateValue.length == 10) // maybe they're using 1-digit hour (1 instead of 01)
   {
      vDateValue = "0" + vDateValue;
   }

   if (vDateValue.length != 11)
      bValid = false;
   else
   {
      if ((vDateValue.substr(2,1) != ":") || (vDateValue.substr(5,1) != ":") ||
          (vDateValue.substr(8,1) != " "))
         bValid = false;
   }

   // check that last 2 chars are am or pm
   if (bValid)
   {
      var strAm, strPm, strTemp;

      strAm = "AM";
      strPm = "PM";
      strTemp = vDateValue.substr(9,2);

      if ((strAm != strTemp.toUpperCase()) && (strPm != strTemp.toUpperCase()))
         bValid = false;
   }

   if (bValid)
   {
      // now validate the properly formatted string
      // if valid, reset field to properly formatted string and return true
      // else put up invalid message and return false
      if (!timeValid(vDateValue))
         bValid = false;
   }

   if (!bValid)
   {
      if (isNav4)
      {
         alert("Invalid Time\nPlease Re-Enter");
         vDateName.value = "";
         vDateName.focus();
         vDateName.select();
         return true;
      }
      else
      {
         alert("Invalid Time\nPlease Re-Enter");
         vDateName.value = "";
         vDateName.focus();
         return true;
      }
   }
   else
   {
      return true;
   }
}


function undoLast(vDateName, vDateValue, isNav4)
{
   // If the value is not in the string return the string minus the last
   // key entered.
   if (isNav4)
   {
      vDateName.value = "";
      vDateName.focus();
      vDateName.select();
   }
   else
   {
      vDateName.value = vDateName.value.substr(0, (vDateValue.length-1));
   }

   return false;
}

function dateValid(objName)
{
   var strDate;
   var strDateArray;
   var strDay;
   var strMonth;
   var strYear;
   var intday;
   var intMonth;
   var intYear;
   var booFound = false;
   var datefield = objName;
   var strSeparatorArray = new Array("-"," ","/",".");
   var intElementNr;
   // var err = 0;
   var strMonthArray = new Array(12);
   strMonthArray[0] = "Jan";
   strMonthArray[1] = "Feb";
   strMonthArray[2] = "Mar";
   strMonthArray[3] = "Apr";
   strMonthArray[4] = "May";
   strMonthArray[5] = "Jun";
   strMonthArray[6] = "Jul";
   strMonthArray[7] = "Aug";
   strMonthArray[8] = "Sep";
   strMonthArray[9] = "Oct";
   strMonthArray[10] = "Nov";
   strMonthArray[11] = "Dec";
   strDate = objName;
   if (strDate.length < 1)
   {
      return true;
   }
   for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++)
   {
      if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1)
      {
         strDateArray = strDate.split(strSeparatorArray[intElementNr]);
         if (strDateArray.length != 3)
         {
            err = 1;
            return false;
         }
         else
         {
            strDay = strDateArray[0];
            strMonth = strDateArray[1];
            strYear = strDateArray[2];
         }
         booFound = true;
      }
   }
   if (booFound == false)
   {
      if (strDate.length>5)
      {
         strDay = strDate.substr(0, 2);
         strMonth = strDate.substr(2, 2);
         strYear = strDate.substr(4);
      }
   }
   //Adjustment for short years entered
   if (strYear.length == 2)
   {
      strYear = '20' + strYear;
   }
   strTemp = strDay;
   strDay = strMonth;
   strMonth = strTemp;
   intday = parseInt(strDay, 10);
   if (isNaN(intday))
   {
      err = 2;
      return false;
   }
   intMonth = parseInt(strMonth, 10);
   if (isNaN(intMonth))
   {
      for (i = 0;i<12;i++)
      {
         if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase())
         {
            intMonth = i+1;
            strMonth = strMonthArray[i];
            i = 12;
         }
      }
      if (isNaN(intMonth))
      {
         err = 3;
         return false;
      }
   }
   intYear = parseInt(strYear, 10);
   if (isNaN(intYear))
   {
      err = 4;
      return false;
   }
   if (intMonth>12 || intMonth<1)
   {
      err = 5;
      return false;
   }
   if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1))
   {
      err = 6;
      return false;
   }
   if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1))
   {
      err = 7;
      return false;
   }
   if (intMonth == 2)
   {
      if (intday < 1)
      {
         err = 8;
         return false;
      }
      if (LeapYear(intYear) == true)
      {
         if (intday > 29)
         {
            err = 9;
            return false;
         }
      }
      else
      {
         if (intday > 28)
         {
            err = 10;
            return false;
         }
      }
   }
   return true;
}

function LeapYear(intYear)
{
   if (intYear % 100 == 0)
   {
      if (intYear % 400 == 0)
      {
         return true;
      }
   }
   else
   {
      if ((intYear % 4) == 0)
      {
         return true;
      }
   }
   return false;
}

function timeValid(objName)
{
   var strTime;
   var strHour;
   var strMinute;
   var strSecond;
   var strAm;
   var iHour;
   var iMinute;
   var iSecond;

   strTime = objName;
   if (strTime.length < 1)
   {
      return true;
   }

   if (strTime.length == 10) // maybe they're using 1-digit hour (1 instead of 01)
   {
      strTime = "0" + strTime;
   }

   if (strTime.length != 11) // too short or too long
   {
      return false;
   }

   strHour = strTime.substr(0,2);
   strMinute = strTime.substr(3,2);
   strSecond = strTime.substr(6,2);
   strAm = strTime.substr(9,2);

   iHour = parseInt(strHour,10);
   iMinute = parseInt(strMinute,10);
   iSecond = parseInt(strSecond,10);

   if ((iHour < 1) || (iHour > 12))
      return false;

   if ((iSecond < 0) || (iSecond > 59))
      return false;

   if ((iMinute < 0) || (iMinute > 59))
      return false;

   return true;
}


