// Note: This page will work only if either /include/validation/client/alertAndRefocus.js
// or /include/banner.asp has been included.

function isValidEmailAddress(str)
{
	var at ="@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);

	if (str == "")
	{
		return false;
	}

	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr - 1)
	{
		return false;
	}

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr - 1)
	{
		return false;
	}

	if (str.indexOf(at, (lat + 1)) != -1)
	{
		return false;
	}

	if (str.substring(lat - 1,lat) == dot || str.substring(lat + 1, lat + 2) == dot)
	{
		return false;
	}

	if (str.indexOf(dot, (lat + 2)) == -1)
	{
		return false;
	}

	if (str.indexOf(" ") != -1)
	{
		return false;
	}

	var rex = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.+-]+\.)+[a-zA-Z]{2,4}$/;
	if (! rex.test(str))
	{
		return false;
	}
	
	if (str.indexOf('..') > lat)
	{
		return false;
	}
	
	return true;
}

// For convenience sake, "&quot;" and "''" in errmsg will be changed to double quotes.
function checkForValidEmailAddress(this_field, errmsg)
{
	if (!isValidEmailAddress(this_field.value))
	{
		alertAndRefocus(this_field, errmsg.replace(/&quot;/ig, '"').replace(/''/ig, '"'));
		return false;
	}
	
	return true;
}

