//** Validate a form element Email address.
//** eobj=field,eid=error text
//**
//** Maintenance log
//**
//** Date	  Person  DCR#   Description of change
//** -------- ------- ------ --------------------------------------------------------------
//** 05/15/07 MSG	  102092 various improvements
//**
//** ======================================================================================

function EmailCheckField(eobj, eid)
{
      message = '';

	if (!EmailCheck(eobj, eid))
	{
		alert(message);
		return (false);
	}
	return (true);
}

//*******************
//** Function - EmailCheck
//**
//** Validate an Email address
//**
//*******************

function EmailCheck(eobj, eid)
{


/*   alert("Entering EmailCheck Function");  */

//*******************
//**	Create a variable that contains five invalid characters for an email address:
//**	:blank space, slash, colon, comman, and a semicolon
//*******************
   x=0;
   y=0;
  invalidChars = " /:,;";
  errormsg = '';
//*******************
//**	If the contents of the email is nothing
//*******************
  if (eobj.value.length == 0)
  	{
	return (true);
	}

//*******************
//**	Start a loop that scans throught the invalidChars string. start by inititializing
//**	the counter i to zero, then each time through the loop that i is less than the length
//**	of the string, add 1 to i with the i++ increment operator.
//**	The badChar variable saves the position of the invalid character in the invalidChars
//**	string. For example, if the invalid character was a colon, badChar would contasin a
//**	2, the position of the colon in the five character string (remember that Javascript starts
//**	numbering at 0). indexOf looks for the position of a character in a string. If the result
//**	of the indexOf function is -1, the character isn't in the string, and again you get
//**	a false result.
//*******************


  for (i=0; i<invalidChars.length; i++)
  	{
	badChar = invalidChars.charAt(i);
		if (eobj.value.indexOf(badChar,0) != -1)
			{
	  		if (message == '')
				eobj.focus();
		    message = message + '\n  * ' + eid + ' may not contain a blank space, slash, colon, comma, and semicolon.';
			return (false);
			}
	}

//*******************
//**	Tha atfound variable holds a count of the number of @ signs found. If the result of
//**	the variable is 0, it means that there is no @ sign in the address. Next,
//**	the script is making sure that there is only one @ sign, and rejecting anything more
//**	than one @ by checking the variable atfound variable counter. If the count of the
//**	variable atfound is greater than 1, the string fails the test with more than one @ sing
//**	error.
//*******************
  atfound=0;
  for (j=0; j < eobj.value.length; j++)
  	{
	if 	(eobj.value.substring(j, j+1) == "@")
		{
		atPos = eobj.value.indexOf(eobj.value.substring(j, j+1));
		atfound=atfound+1;
		}
	}

  if (atfound == 0)
  	{
	if (message == '')
		  eobj.focus();
    message = message + '\n  * ' + eid + ' @ sign is required.';
	return (false);
  	}

  if (atfound > 1)
  	{
	if (message == '')
		  eobj.focus();
    message = message + '\n  * ' + eid + ' may not contain more than one (1) @ sign.';
	return (false);
  	}

//*******************
//**	The atPos variable holds the position of the @ sign. The script then checks to see if there
//**	is a period after the @ sign. if not, there is a false result. Then just like the @ sign,
//**	the script is making sure that there is only one period and rejecting anything more
//**	than two periods by checking the variable periodfound variable.
//*******************
  periodfound=0;
  for (j=atPos; j < eobj.value.length; j++)
  	{
	if 	(eobj.value.substring(j, j+1) == ".")
		{
		periodPos = eobj.value.indexOf(eobj.value.substring(j, j+1));
		periodfound=periodfound+1;
		}
	}

  if (periodfound == 0)
  	{
	if (message == '')
		  eobj.focus();
    message = message + '\n  * ' + eid + ' period (.) after the @ sign is required.';
	return (false);
  	}

  if (periodfound > 2)
  	{
	if (message == '')
		  eobj.focus();
    message = message + '\n  * ' + eid + ' may not contain more than two (2) periods (.) after the @ sign';
	return (false);
  	}

//*******************
//**	The script requires that there be at least two characters after the period in the address.
//*******************
  if (periodPos+3 > eobj.value.length)
  	{
    	if (message == '')
			eobj.focus();
    	message = message + '\n  * ' + eid + ' must contain at least two (2) characters after the period.';
    	return (false);
  	}

//*******************
//**	If it made it this far without a false result, the the value of the EmailCheck is true,
//**	meaning you have a good email address.
//*******************
    return (true);
}


