//******************************************************************************/
//*                                                                            */
//*         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                          */
//*                                                                            */
//******************************************************************************/
//*                                                                            */
//*                                                                            */
//******************************************************************************/

  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 ))
        { if ( Field.style.setAttribute )
            { Field.style.setAttribute ( "backgroundColor", "White" ) ; }
          else
            { if ( Field.style.setProperty )
                { Field.style.setProperty ( "background-color", "White", null ) ; }
            }
        }
      else
        { if ( Field.style.setAttribute )
            { Field.style.setAttribute ( "backgroundColor", "Red" ) ; }
          else
            { if ( Field.style.setProperty )
                { Field.style.setProperty ( "background-color", "Red", null ) ; }
            }
        }
    }

  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 )
    {
      var AllInputs ;
      var loop ;

      AllInputs = SubmitForm.getElementsByTagName ( "Input" ) ;
      if ( ! AllInputs )
        { return ( 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 ( AllInputs[ loop ].attributes[ loop2 ].value == "CheckEmailField( this, false )" )
                    { if ( ! ValidEmail ( AllInputs[ loop ].value, true ))
                        { alert ( "Invalid Email - " + AllInputs[ loop ].value ) ;
                          return ( false ) ;
                        }
                    }
                  if ( AllInputs[ loop ].attributes[ loop2 ].value == "CheckEmailField( this, true )" )
                    { if ( ! ValidEmail ( AllInputs[ loop ].value, true ))
                        { alert ( "Invalid Email - " + AllInputs[ loop ].value ) ;
                          return ( false ) ;
                        }
                    }
                }
            }
        }
      return ( true ) ;
    }

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