// Check a date for valid format CCYY-MM-DD
// Author. John D. Coryat 07/2005...
// Copyright 1997-2005 John Coryat Systems. All rights reserved.

function datecheck(date,id,mid)
{

 var dtCh = "-" ; var minYear = 1900 ; var maxYear = 2100 ;
 var dtStr = date.value ;
 if ( dtStr.search(/\//) )
 {
  dtStr = dtStr.replace( /\//g, "-" ) ;		// Fix slashes to dashes
 }
 if ( dtStr.search(/-(\d)-/ ) )
 {
  dtStr = dtStr.replace( /-(\d)-/, "-0$1-" ) ;	// Add Leading Zeros to month
 }
 if ( dtStr.search(/-(\d)\b/) )
 { 
  dtStr = dtStr.replace( /-(\d)\b/, "-0$1" ) ;	// Add Leading Zeros to month
 }
 if ( dtStr.search(/\s/) )
 {
  dtStr = dtStr.replace( /\s/g, "" ) ;		// remove spaces
 }
 date.value = dtStr ;

 var daysInMonth = DaysArray(12) ; var pos1 = dtStr.indexOf(dtCh) ;  var pos2 = dtStr.indexOf(dtCh,pos1+1) ;

 var strYear = dtStr.substring(0,pos1) ;
 var strMonth = dtStr.substring(pos1+1,pos2) ; var strDay = dtStr.substring(pos2+1) ;
 var mid = document.getElementById(mid) ;

 strYr = strYear ; if ( strDay.charAt(0) == "0" && strDay.length > 1 )
 {
  strDay=strDay.substring(1) ;
 } if (strMonth.charAt(0)=="0" && strMonth.length>1)
 {
  strMonth=strMonth.substring(1) ;
 } for (var i = 1; i <= 3; i++) 
 {  if (strYr.charAt(0)=="0" && strYr.length>1)
  {
   strYr=strYr.substring(1) ;
  } } month = parseInt(strMonth) ; day = parseInt(strDay) ; year = parseInt(strYr) ; if (pos1==-1 || pos2==-1)
 {
  dateerror( mid, date, "Invalid Date (" + date.value + ") - Must be YYYY-MM-DD format" ) ;
  return(false) ;
 } if (strMonth.length<1 || month<1 || month>12)
 {
  dateerror( mid, date, "Invalid Month (" + date.value + ") - Must be YYYY-MM-DD format" ) ;
  return(false) ;
 } if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
 {
  dateerror( mid, date, "Invalid Day (" + date.value + ") - Must be YYYY-MM-DD format" ) ;
  return(false) ;
 } if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
 {
  dateerror( mid, date, "Invalid Year (" + date.value + ") - Must be YYYY-MM-DD format" ) ;
  return(false) ;
 } if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripChars(dtStr, dtCh))==false)
 {
  dateerror( mid, date, "Invalid Date (" + date.value + ") - Must be YYYY-MM-DD format" ) ;
  return(false) ;
 }
 mid.innerHTML = "" ;
 mid.style.display = "none" ;
 return true ;

// Additional Functions...

 function dateerror( mid, date, msg )
 {
  mid.style.display = "block" ;
  mid.innerHTML = "<br>" + msg ;
  setfocus(date) ;
 }

 function isInteger(s)
 {  var i;  for (i = 0; i < s.length; i++)
  {      // Check that current character is number.   var c = s.charAt(i) ;   if ( (c < "0") || (c > "9") )
   {
    return false;
   }  }  // All characters are numbers.  return true ; }
 function stripChars(s, bag)
 {  var i ;  var returnString = "" ;  for (i = 0; i < s.length; i++)
  {      var c = s.charAt(i) ;   if (bag.indexOf(c) == -1) 
   {
    returnString += c ;
   }  }  return returnString; } 

 function daysInFebruary (year)
 {  return ( ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 ) ; }

 function DaysArray(n) 
 {  for (var i = 1; i <= n; i++) 
  {   this[i] = 31 ;   if (i==4 || i==6 || i==9 || i==11) 
   {
    this[i] = 30 ;
   }   if (i==2) 
   {
    this[i] = 29 ;
   }  }   return this ; }
 function setfocus(xfield) {  b_xfield = xfield ;  setTimeout( 'FocusDelay()', 100 ) ;
 }
}

function FocusDelay(xfield){  b_xfield.focus() ;}
