// --------------------------------------------------------------------------
/*
	Filename	:	ck_emailaddress.asp	version 1.0
	
	Author		:	Rusty Swayne
	Copyright	:	Mindfly, Inc. 2000
	
	Updated		:	July 12, 2000	Created
	
	Purpose		:	Test email addresses submitted through HTML	forms to identify
					obviously incorrect emails
	
*/
// --------------------------------------------------------------------------

function isValidEmail(email)
// Purpose:			Attempt to filter out obviously wrong email address
//					so they are not stored in a database
// Precondition:	none
// Postcondition:	Returns 1 if format passed check 0 if failed
{
	var valid = 1;
		
	if(email.length < 5)
	{
		valid = 0;
	}
	else
	{
		var i			= 0;
		var atCount		= 0;
		var atIndex		= 0;
		var dotIndex	= 0;
		var invalidChars = "\"*^#!$%&()+=\n\t/?|,<>';:{}[]~`";
			
		while(i < email.length  && valid == 1)
		{
			for(j=0; j<= invalidChars.length; j++)
			{
				if(email.charAt(i) == invalidChars.charAt(j)) valid = 0;
			}	// end for
			if(valid==1)
			{
				switch(email.charAt(i))
				{
					case '@':
						atCount++;
						atIndex = i;
						if(i < 2 || atCount > 1) // check for rusty@some@something.com
							valid = 0;
							
						if(dotIndex != 0)		// check for .@
							if(i-dotIndex <= 1) valid = 0;
					break;
					case ' ':
						valid = 0;
						
					break;
					case '.':
						if(i==0 || i-1 == dotIndex)		// check .rusty@ and for rusty..rusty@
							valid = 0;
						dotIndex = i;
					break;
				} // end switch
			} // end if
			
			i++;
		} // end while
		
		if((email.length - (dotIndex + 1)) < 2 ||  (email.length - (dotIndex + 1)) > 3) valid = 0;	
	} // end else

	return valid;
		
} // end function


