//******************************************************************************/
//*                                                                            */
//*         Internet Computer Software - Java Script library.                  */
//*                                                                            */
//*         Program:                     CheckEmail.JS.                        */
//*         Programmer :                 John  S.  Parry.                      */
//*         Date :                       June 2010.                            */
//*         Language :                   JavaScript.                           */
//*                                                                            */
//*         Copyright (C) 2010,   Internet Computer Software Pty Ltd.          */
//*                               PO Box 279                                   */
//*                               Sandy Bay  7006                              */
//*                               Tasmania  Australia                          */
//*                                                                            */
//******************************************************************************/
//*                                                                            */
//*                                                                            */
//*                                                                            */
//******************************************************************************/

  var ICScheckEmailFlag = null ;

  function FormatEmail ( EmailStr )
    {
      while ( EmailStr.indexOf( ' ' ) >= 0 )
        { EmailStr = EmailStr.replace( ' ', '' ) ; }
      EmailStr = EmailStr.replace( ',', ', ' ) ;
      return ( EmailStr ) ;
    }

  function CheckEmailField ( Field, AllowBlank )
    {
      Field.value = FormatEmail ( Field.value ) ;
      if ( ValidEmail ( Field.value, AllowBlank ))
        { SetNormalBackground ( Field ) ;
          if ( ICScheckEmailFlag == Field )
            { HideErrorCheckBox( 'ICScheckEmail' ) ;
              ICScheckEmailFlag = null ;
            }
        }
      else
        { SetErrorBackground ( Field ) ;
          if ( Field.value != "" )
            { ShowErrorCheckBox( 'ICScheckEmail', Field, "this email address is invalid" ) ; }
          else
            { ShowErrorCheckBox( 'ICScheckEmail', Field, "this field can not be left blank" ) ; }
          ICScheckEmailFlag = Field ;
        }
    }

  function ValidEmail ( EmailStr, AllowBlank )
    {
      var Comma ;

      if (( AllowBlank ) && ( EmailStr == "" ))
        { return ( true ) ; }
      Comma = EmailStr.indexOf ( ',' ) ;
      if ( Comma >= 0 )
        { if ( ValidEmail ( EmailStr.substring ( 0, Comma ), false ))
            { return ( ValidEmail( EmailStr.substring ( Comma + 1 ), false )) ; }
          else
            { return ( false ) ; }
        }
      else
        { AtSymbol = EmailStr.indexOf ( '@' ) ;
          if ( AtSymbol < 1 )
            { return ( false ) ; }
          SemiColon = EmailStr.indexOf( ';' ) ;
          if ( SemiColon >= 0 )
            { return ( false ) ; }
          DotPosition = EmailStr.substring ( AtSymbol + 1 ).indexOf( '.' ) ;
          if ( DotPosition < 1 )
            { return ( false ) ; }
          DotDotPosition = EmailStr.substring ( AtSymbol + 1 ).indexOf( '..' ) ;
          if ( DotDotPosition >= 0 )
            { return ( false ) ; }
          AtSymbol = EmailStr.substring ( AtSymbol + 1 ).indexOf( '@' ) ;
          if ( AtSymbol >= 0 )
            { return ( false ) ; }
          if ( EmailStr.substring ( EmailStr.length ) == '.' )
            { return ( false ) ; }
          return ( true ) ;
        }
    }

  function CheckAllEmails ( 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 ) == "CheckEmailField(this,false)" )
                    { if ( ! ValidEmail ( AllInputs[ loop ].value, false ))
                        { SetErrorBackground ( AllInputs[ loop ] ) ;
                          if ( DisplayAlert )
                            { if ( AllInputs[ loop ].value != "" )
                                { alert ( "Invalid Email - " + AllInputs[ loop ].value ) ;
                                  ShowErrorCheckBox( 'ICScheckEmail', AllInputs[ loop ], "this email address is invalid" ) ;
                                }
                              else
                                { alert ( "A mandatory field is blank" ) ;
                                  ShowErrorCheckBox( 'ICScheckEmail', AllInputs[ loop ], "this field can not be left blank" ) ;
                                }
                              ICScheckEmailFlag = AllInputs[ loop ] ;
                            }
                          ReturnValue = false ;
                        }
                      else
                        { SetNormalBackground ( AllInputs[ loop ] ) ;
                          if ( ICScheckEmailFlag == AllInputs[ loop ] )
                            { HideErrorCheckBox( 'ICScheckEmail' ) ;
                              ICScheckEmailFlag = null ;
                            }
                        }
                    }
                  if ( RemoveSpaces ( AllInputs[ loop ].attributes[ loop2 ].value ) == "CheckEmailField(this,true)" )
                    { if ( ! ValidEmail ( AllInputs[ loop ].value, true ))
                        { SetErrorBackground ( AllInputs[ loop ] ) ;
                          if ( DisplayAlert )
                            { alert ( "Invalid Email - " + AllInputs[ loop ].value ) ;
                              ShowErrorCheckBox( 'ICScheckEmail', AllInputs[ loop ], "this email address is invalid" ) ;
                              ICScheckEmailFlag = AllInputs[ loop ] ;
                            }
                          ReturnValue = false ;
                        }
                      else
                        { SetNormalBackground ( AllInputs[ loop ] ) ;
                          if ( ICScheckEmailFlag == AllInputs[ loop ] )
                            { HideErrorCheckBox( 'ICScheckEmail' ) ;
                              ICScheckEmailFlag = null ;
                            }
                        }
                    }
                }
            }
        }
      return ( ReturnValue ) ;
    }

  function CheckEmailOnSubmit ( SubmitForm )
    {
      if ( CheckAllEmails ( SubmitForm, true ))
        { SubmitForm.submit ( ) ; }
      event.preventDefault ( ) ;
      event.stopPropagation ( ) ;
    }

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

