
function ValidateEmail (cEmail, nAllowBlank, cFieldName)
{

// Checks for valid e-mail. Allows a blank address if 'nAllowBlank' == 1
// (Mostly) Mam'selle Jacqui Grant
// AST (Cape Town)
// 15/11/2000

   var invalidChars = "/:,;" ;
   var nError       = 0 ;
   var i ;
   var badChar ;
	
   if ( cEmail.length > 0 )
   {   
      for ( i=0 ; i < invalidChars.length ; i++ )
      {
	     badChar = invalidChars.charAt(i)
	     if ( cEmail.indexOf(badChar,0) > -1 )
	     {
	        nError = 1 ;
	        break ;
         }
      }
	
      atPos     = cEmail.indexOf ( "@", 1 )
      periodPos = cEmail.indexOf ( ".", atPos )

      if ( atPos < 0 )
         nError = 2 ;
	
      else if ( cEmail.indexOf ( ".", atPos + 1 ) == -1 )
	     nError = 3 ;

      else if ( periodPos < 0 )
         nError = 4 ;

      else if ( periodPos + 3 > cEmail.length )
	     nError = 5 ;
   }	     
   else if ( nAllowBlank != 1 )
      nError = 6 ;
      
   if ( nError == 0 )
      return true ;
      
   alert ("The " + cFieldName + " is not valid" ) ;
   return false ; 
}
 
