
/**************************************************************
 Description: Email address must be of form a@b.c i.e.,
	there must be at least one character before the @
	there must be at least one character before and after the .
	the characters @ and . are both required
 Dependencies: alltrim, isEmpty, occurs of string.js
**************************************************************/

function validEmail(objFld) {
	var strInvalid = "*?#&^~`'\\[]<>;/:\" ";
	var bAliasedEmail = false;
	var  strAliasedEmail = "";

	var i = 0;
    var  bValid;
	bValid = true;

	objFld.value = alltrim(objFld.value);
	if (isEmpty(objFld.value))
		return true;

	var strEmail = new String(objFld.value);

	// Assumption: if strEmail contains < and >, Email is between < and >
	if (strEmail.indexOf('<') < strEmail.indexOf('>') && strEmail.indexOf('<') >= 0)
	{
		bAliasedEmail = true;
		strAliasedEmail = strEmail;
		strEmail = strEmail.substring(strEmail.indexOf('<')+1,strEmail.indexOf('>'));
	}

	if (strEmail.length < 5)	// check the length
		bValid = false;
	else if (strEmail.lastIndexOf("@") <= 0 || (strEmail.lastIndexOf(".") - strEmail.lastIndexOf("@") <= 1))

// check positions of @ and .

		bValid = false;
	else if (occurs('@', strEmail) > 1)	// check if @ occurs more than once
		bValid = false
	else	// check if any invalid characters present
	{
		for (i = 0; i < strEmail.length; i++)
			if (strInvalid.indexOf(strEmail.charAt(i)) >= 0)
			{
				bValid = false;
				break;
			}
	}

	if (!bValid) {
		alert("Please enter a valid email address");
		objFld.value = "";
		objFld.focus();
		return bValid;
	}

	if (bAliasedEmail)
	 this.value = strAliasedEmail;
	else
	 this.value = strEmail;

	return bValid;
}


