//******************************************************************************/
//*                                                                            */
//*         Internet Computer Software - Java Script library.                  */
//*                                                                            */
//*         Program:                     CheckDate.JS.                         */
//*         Programmer :                 John  S.  Parry.                      */
//*         Date :                       November 2005.                        */
//*         Language :                   JavaScript.                           */
//*                                                                            */
//*         Copyright (C) 2005,   Internet Computer Software Pty Ltd.          */
//*                               PO Box 279                                   */
//*                               Sandy Bay  7006                              */
//*                               Tasmania  Australia                          */
//*                                                                            */
//******************************************************************************/
//*                                                                            */
//* This javascript routine ensures that all date fields only contain valid    */
//* dates in the form dd/mm/yyyy as used in Australia.                         */
//*                                                                            */
//*                                                                            */
//*                                                                            */
//******************************************************************************/

  var ICScheckDateFlag = null ;
  var DaysInMonth = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] ;

  function ValidDateChar ( DateStr )
    {
      var Loop ;

      for ( Loop = 0; Loop < DateStr.length; Loop++ )
        { Char = DateStr.substring ( Loop, Loop + 1 ) ;
          if (( Char < '0' ) || ( Char > '9' ))
            { if (( Char != '/' ) && ( Char != '.' ) && ( Char != '-' ))
                { return ( false ) ; }
            }
        }
      return ( true ) ;
    }

  function FormatDate ( DateStr )
    {
      var Day   = 0 ;
      var Month = 0 ;
      var Year  = 0 ;
      var Today = new Date ( ) ;
      var ThisYear = Today.getYear ( ) ;
      var ThisMonth = Today.getMonth ( ) + 1 ;
      var GetPos ;

      if ( ThisYear < 200 )
        { ThisYear = ThisYear + 1900 ; }
      if ( DateStr != '' )
        { GetPos = 0 ;
          while (( GetPos < DateStr.length ) && ( DateStr.charAt ( GetPos ) != '/' ) &&
                ( DateStr.charAt ( GetPos ) != '.' ) && ( DateStr.charAt ( GetPos ) != '-' ))
            { Day = Day * 10 + parseInt ( DateStr.charAt ( GetPos++ ), 10 ) ; }
          if ( GetPos < DateStr.length )
            { GetPos++ ; }
          while (( GetPos < DateStr.length ) && ( DateStr.charAt ( GetPos ) != '/' ) &&
                 ( DateStr.charAt ( GetPos ) != '.' ) && ( DateStr.charAt ( GetPos ) != '-' ))
            { Month = Month * 10 + parseInt ( DateStr.charAt ( GetPos++ ), 10 ) ; }
          if ( GetPos < DateStr.length )
            { GetPos++ ; }
          if ( GetPos < DateStr.length )
            { while (( GetPos < DateStr.length ) && ( DateStr.charAt ( GetPos ) != '/' ) &&
                     ( DateStr.charAt ( GetPos ) != '.' ) && ( DateStr.charAt ( GetPos ) != '-' ))
                { Year = Year * 10 + parseInt ( DateStr.charAt ( GetPos++ ), 10 ) ; }
              if ( Year < 100 )
                { LastDigits = ThisYear % 100 ;
                  if (( Year + 50 ) <= LastDigits )
                    { Year = Year + 100 ; }
                  if (( Year - 50 ) > LastDigits )
                    { Year = Year - 100 ; }
                  Year  = Year + ThisYear - LastDigits ;
                }
            }
          else
            { Year = ThisYear ;
              if (( Month + 6 ) <= ThisMonth )
                { Year++ ; }
              if (( Month - 6 ) > ThisMonth )
                { Year-- ; }
            }
          Day   = '' + Day ;
          Month = '' + Month ;
          Year  = '' + Year ;
          while ( Day.length < 2 )
            { Day   = '0' + Day ; }
          while ( Month.length < 2 )
            { Month = '0' + Month ; }
          while ( Year.length < 4 )
            { Year  = '0' + Year ; }
          return ( Day + '/' + Month + '/' + Year ) ;
        }
      else
        { return ( DateStr ) ; }
    }

  function CheckDateField ( Field, AllowBlank )
    {
      var ValidDateFlag ;

      ValidDateFlag = false ;
      if ( ValidDateChar ( Field.value ))
        { Field.value = FormatDate ( Field.value ) ;
          if ( ValidDate ( Field.value, AllowBlank ))
            { ValidDateFlag = true ; }
        }
      if ( ValidDateFlag )
        { SetNormalBackground ( Field ) ;
          if ( ICScheckDateFlag == Field )
            { HideErrorCheckBox( 'ICScheckDate' ) ;
              ICScheckDateFlag = null ;
            }
        }
      else
        { SetErrorBackground ( Field ) ;
          if ( Field.value != "" )
            { ShowErrorCheckBox( 'ICScheckDate', Field, "this date is invalid" ) ; }
          else
            { ShowErrorCheckBox( 'ICScheckDate', Field, "this field can not be left blank" ) ; }
          ICScheckDateFlag = Field ;
        }
    }

  function ValidDate ( DateStr, AllowBlank )
    {
      if ( DateStr != '' )
        { if ( DateStr.length != 10 )
            { return ( false ) ; }
          if (( DateStr.substring( 2, 3 ) != '/' ) || ( DateStr.substring( 5, 6 ) != '/' ))
            { return ( false ) ; }
          Day   = parseInt ( DateStr.substring( 0, 2 ), 10 ) ;
          Month = parseInt ( DateStr.substring( 3, 5 ), 10 ) ;
          Year  = parseInt ( DateStr.substring( 6, 10 ), 10 ) ;
          if (( Month < 1 ) || ( Month > 12 ))
            { return ( false ) ; }
          if (( Day < 1 ) || ( Day > DaysInMonth[ Month - 1 ] ))
            { return ( false ) ; }
          return ( true ) ;
        }
      else
        { return ( AllowBlank ) ; }
    }

  function IncrementDate ( DateStr )
    {
      var DateField = new Date ( ) ; 
      DateStr = FormatDate ( DateStr ) ;
      DateField.setYear ( Date.parse ( DateStr )) ;
      DateField.setDate ( DateField.getDate ( ) + 1 ) ;
      return ( FormatDate ( DateField.getDate ( ) + '/' + ( DateField.getMonth ( ) + 1 ) + '/' + ( DateField.getYear ( ) + 1900 ))) ;
    }

  function CheckAllDates ( SubmitForm,
                           DisplayAlert )
    {
      var AllInputs ;
      var ReturnValue ;
      var loop ;

      AllInputs = SubmitForm.getElementsByTagName ( "Input" ) ;
      if ( ! AllInputs )
        { return ( true ) ; }
      ReturnValue = true ;
      for ( loop = 0; loop < AllInputs.length; loop++ )
        { for ( loop2 = 0; loop2 < AllInputs[ loop ].attributes.length; loop2++ )
            { if ( AllInputs[ loop ].attributes[ loop2 ].name == "onblur" )
                { if ( RemoveSpaces ( AllInputs[ loop ].attributes[ loop2 ].value ) == "CheckDateField(this,false)" )
                    { AllInputs[ loop ].value = FormatDate ( AllInputs[ loop ].value ) ;
                      if ( ! ValidDate ( AllInputs[ loop ].value, false ))
                        { SetErrorBackground ( AllInputs[ loop ] ) ;
                          if ( DisplayAlert )
                            { if ( AllInputs[ loop ].value != "" )
                                { alert ( "Invalid Date - " + AllInputs[ loop ].value ) ;
                                  ShowErrorCheckBox( 'ICScheckDate', AllInputs[ loop ], "this date is invalid" ) ;
                                }
                              else
                                { alert ( "A mandatory field is blank" ) ;
                                  ShowErrorCheckBox( 'ICScheckDate', AllInputs[ loop ], "this field can not be left blank" ) ;
                                }
                              ICScheckDateFlag = AllInputs[ loop ] ;
                            }
                          ReturnValue = false ;
                        }
                      else
                        { SetNormalBackground ( AllInputs[ loop ] ) ;
                          if ( ICScheckDateFlag == AllInputs[ loop ] )
                            { HideErrorCheckBox( 'ICScheckDate' ) ;
                              ICScheckDateFlag = null ;
                            }
                        }
                    }
                  if ( RemoveSpaces ( AllInputs[ loop ].attributes[ loop2 ].value ) == "CheckDateField(this,true)" )
                    { AllInputs[ loop ].value = FormatDate ( AllInputs[ loop ].value ) ;
                      if ( ! ValidDate ( AllInputs[ loop ].value, true ))
                        { SetErrorBackground ( AllInputs[ loop ] ) ;
                          if ( DisplayAlert )
                            { alert ( "Invalid Date - " + AllInputs[ loop ].value ) ;
                              ShowErrorCheckBox( 'ICScheckDate', AllInputs[ loop ], "this date is invalid" ) ;
                              ICScheckDateFlag = AllInputs[ loop ] ;
                            }
                          ReturnValue = false ;
                        }
                      else
                        { SetNormalBackground ( AllInputs[ loop ] ) ;
                          if ( ICScheckDateFlag == AllInputs[ loop ] )
                            { HideErrorCheckBox( 'ICScheckDate' ) ;
                              ICScheckDateFlag = null ;
                            }
                        }
                    }
                }
            }
        }
      return ( ReturnValue ) ;
    }

  function CheckDateOnSubmit ( SubmitForm )
    {
      if ( CheckAllDates ( SubmitForm, true ))
        { SubmitForm.submit ( ) ; }
      event.preventDefault ( ) ;
      event.stopPropagation ( ) ;
    }

  document.write( "<Div Id='ICScheckDate' Class='ErrorCheckBox'>" ) ;
  document.write( "</Div>" );
  document.write( "<Img Id='ICScheckDateArrow' src='" + ErrorCheckArrow + "'>" ) ;
  HideErrorCheckBox( 'ICScheckDate' ) ;

