<!--  HJRS_JS_ValDatesTimes.js, (c) 2009 HJR SOFTWARE 704-825-7440.  Not to be modified without HJR SOFTWARE's prior written permission.
// Validation Tools for Date and Time
// Requires: HJRS_JS_ValStrs.js

function ValTime( oTime ) {
  var x, y, z = "", sample = "0123456789: MmAaPp", sAP = 0, h,m;
  
  if ( oTime.value.length > 0 ) {
    oTime.value = trim(oTime.value);
    x = oTime.value.substr(0,8);
    for( y = 0; y < oTime.value.length; y++ ) {
      if ( sample.indexOf(x.charAt(y)) > -1 ) {
        if ( ( sample.indexOf(x.charAt(y)) < 11 ) && ( sAP < 6 ) ) {
          if ( x.charAt(y) == ":" ) {
            if ( z.indexOf(":") == -1 ) {
              sAP = 0;
            } else {
              sAP = -1;
            }
          }
          if ( z.length > 4 )
            sAP = 5;
          sAP ++;
        }
        if ( sAP > 0 )
          z += "" + x.charAt(y);
      }
    }

	z = trim(z);
    if ( hjrsMidStr(z,-1,-1).toLowerCase() == "m" )
      z = trim(z.substr(0,z.length - 1));
    if ( hjrsMidStr(z,-1,-1).toLowerCase() == "a" ) {
      sAP = " AM";
      z = trim(z.substr(0,z.length - 1));      
    } else if ( hjrsMidStr(z,-1,-1).toLowerCase() == "p" ) {
      sAP = " PM";
      z = trim(z.substr(0,z.length - 1));      
    } else {
      sAP = " AM";
    }
      
      
    y = z.indexOf(":");
    if ( y == -1 ) {
      z = hjrsMidStr("0000" + z, -1, -4);	// append zeros to front of number
      z = hjrsMidStr(z,0,2) + ":" + hjrsMidStr(z,-1,-2);
    } else {
      x = hjrsMidStr("000" + z.substr(0, y), -1, -2 );
      z = x + z.substring(y,z.length);
    }
    y = z.indexOf(":");


    if ( ( y == 2 ) && ( z.substr(0,1) == "0" ) ) {
      h = parseInt(z.substr(1,1));
    } else {
      h = parseInt(z.substr(0,2));
    }
    if ( isNaN(h) )
      h = 0;

    x = hjrsMidStr(z,y+1,2);
    if ( x.substr(0,1) == "0" ) {
      m = parseInt(x.substr(1,1));
    } else {
      m = parseInt(x);
    }
    if ( isNaN(m) )
      m = 0;
    
    if (m > 59)
      m = 59;
    if (h > 24)
      h -= parseInt( (h/24).toString() ) * 24;
    if (h > 12) {
      sAP = " PM";
      h -= 12;
    }
    z = hjrsMidStr("00" + h.toString(),-1,-2) + ":" + hjrsMidStr("00" + m.toString(),-1,-2) + sAP;
   
    oTime.value = z;
  }
  return true;
}

function ValDate( oDate ) {
  var t, x, y, z = 0, d = "", s = 0, sample = "0123456789";
  var m30 = "02,04,06,09,11";
  var sMonth = Array("+","jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");

  if ( oDate.value.length > 0 ) {
    oDate.value = trim(oDate.value.toLocaleLowerCase());
    x = oDate.value;
    for( y = 0; y < 13; y ++ ) {
      z = x.indexOf(sMonth[y]);
      if (z > -1) {
        x = hjrsMidStr(x,0,z-1) + hjrsMidStr("0" + y.toString(),-1,-2) + hjrsMidStr(x,z+3,x.length-z-3);
        break;
      }
    }
    z = 0;
    for( y = 0; y < x.length; y++ ) {
      if ( sample.indexOf(x.charAt(y)) > -1 ) {
        if ( ( z + 1 > 2 ) && ( s < 2 ) ) {
          s ++;
          d += "/";
          z = 0;
        }
        d += x.charAt(y);
        z ++
      } else {
        // convert non-digits to slashes
        if ( ( y > 0 ) && ( sample.indexOf(x.charAt(y-1)) == -1 ) ) {
          // 2 non-digit characters back to back, so skip all after the first one
        } else {
          if ( ((s<2) && (z<2)) || ((s==2) && (z<4)) ) {   // < 2 digits between slashes 1 & 2 and 4 digits between 3rd non-digit. 
            t = "2" + "000".substr(0,3-z) + hjrsMidStr(d,-1,-z);
            z = (s==2) ? -4 : -2;
            q = d.lastIndexOf("/");
            q = (q<0) ? 0 : q + 1;
            d = d.substr(0,q) + hjrsMidStr(t,-1,z);
            z = 0;
          }
          if ( s + 1 < 3 ) {	// insert slash
            d += "/";
            s ++;
            z = 0;
          } else {  // more than 2 non-digit areas found - date is done. ignore remaining chars
            break;
          }
        }
      }
      if ( ( z >= 4 ) || ( d.length >= 10 ) )
        break;
    }
    d = d.substr(0,10);
    t = d.split("/");
    d = new Date();
    if ( !( t[2] ) )
      t[2] = d.getFullYear().toString();
    if ( !( t[1] ) )
      t[1] = hjrsMidStr("00" + (d.getUTCDate() + 1).toString(),-1,-2);
    if ( !( t[0] ) )
      t[0] = hjrsMidStr("00" + (d.getMonth() + 1).toString(),-1,-2);
    if ( parseInt(t[0]) > 12 )
      t[0] = "12";
    if ( t[0] == "00" )
      t[0] = "01";
    if ( t[1] == "00" )
      t[1] = "01";
    if ( parseInt(t[2]) < 100 )
      t[2] = "20" + hjrsMidStr("00" + t[2],-1,-2);
    if ( parseInt(t[2]) < d.getFullYear() )
      t[2] = d.getFullYear().toString();
    if ( parseInt(t[2]) > 2020 )
      t[2] = "2020";
    if ( m30.indexOf(t[0]) >  0 ) {
      if ( t[0] == "02" ) {
        if ( parseInt(t[2])/4 == parseInt(parseInt(t[2])/4) ) {
          if ( parseInt(t[1]) > 29 )
            t[1] = "29";
        }
      } else {
        if ( parseInt(t[1]) > 30 )
          t[1] = "30";
      }
    } else {
      if ( parseInt(t[1]) > 31 )
        t[1] = "31";
    }
    x = d;
    d = t[0] + "/" + t[1] + "/" + t[2];
    y = new Date(d);
    if ( y - x  < 86400000 ) {
      t[2] = (parseInt(t[2]) + 1).toString();
      d = t[0] + "/" + t[1] + "/" + t[2];
    }
  
    if ( IsFieldRequired(oDate) ) {
      x = GetPromptObj(oDate);
      if ( x ) {
        RR(d == oDate.value,oDate,x,""); 
      }
    }
    oDate.value = d;
  }
        
  return true;
}
// -->
