// el is the form element, as returned by document.getElementById()
// set allowblank to allow empty submission
// nm is the name to use in error strings. defaults to 'email address'
function check_email_format(el, allowblank, nm)
{
	// basic prep
	var em = el.value;
	em = em.replace(/\s+/g, '');  // remove all spaces
	em = em.replace(/\.+/g, '.');  // fix double .
	el.value = em;
	
	if (allowblank && em == '')
		return true;

	if (!nm)
		nm = 'email address';

	/* errors */
	
	// check for blank email
	if (em == '') {
		alert('Error: The ' + nm + ' must be provided.');
		return false;
	}

	// more than one @?
	var tst = em.replace(/[^@]+/g, '');
	if (tst.length == 0) {
		alert('Error: The ' + nm + ' must contain an @ symbol.');
		return false;
	}
	if (tst.length > 1) {
		alert('Error: The ' + nm + ' must contain only one @ symbol.');
		return false;
	}
	
	// nothing before @?
	if (em.indexOf('@') == 0) {
		alert('Error: The ' + nm + ' has nothing before the @ symbol.');
		return false;
	}
	
	// dot required after @
	var afterat = em.substring(em.indexOf('@'));
	if (afterat.indexOf('.') == -1) {
		alert('Error: The ' + nm + ' is missing a . after the @ symbol.');
		return false;
	}

	// letters, numbers, dashes, dots only after @
	if (afterat.match(/[^a-z0-9@\.\-]/i)) {
		alert('Error: The ' + nm + ' must contain only letters, numbers, -, and . after the @ symbol.');
		return false;
	}

	// must end in a letter
	if (em.match(/[^a-z]$/i)) {
		alert('Error: The ' + nm + ' does not end in a letter.');
		return false;
	}
	
	// domain cannot start with -
	if (afterat.indexOf('@-') != -1) {
		alert('Error: The ' + nm + ' cannot have a - following the @ symbol.');
		return false;
	}

	// domain cannot end in -. before tld
	if (afterat.match(/\-\.[a-z0-9]+$/)) {
		alert('Error: The ' + nm + ' cannot have a - immediately before the last . symbol.');
		return false;
	}

	/* warnings */

	// commas in address?
	tst = em.replace(/[^,]+/g, '');
	if (tst.length > 1) {
		if (!confirm('The ' + nm + ' contains commas (,), which is unusual. Is that what you meant?'))
			return false;
	}
	if (tst.length > 0) {
		if (!confirm('The ' + nm + ' contains a comma (,), which is unusual. Is that what you meant?'))
			return false;
	}

	// check for unusual symbols
	if (em.match(/[^a-z0-9@\._\-]/i)) {
		if (!confirm('The ' + nm + ' "' + em + '" contains unusual symbols. Did you enter it correctly?'))
			return false;
	}

	// check for .con
	if (em.match(/\.con$/i)) {
		if (!confirm('The ' + nm + ' ends in ".con", a common typo. Is that what you meant?'))
			return false;
	}

	// check for .cpm
	if (em.match(/\.cpm$/i)) {
		if (!confirm('The ' + nm + ' ends in ".cpm", a common typo. Is that what you meant?'))
			return false;
	}

	// check for .ocm
	if (em.match(/\.ocm$/i)) {
		if (!confirm('The ' + nm + ' ends in ".ocm", a common typo. Is that what you meant?'))
			return false;
	}

	// check for c.om
	if (em.match(/c\.om$/i)) {
		if (!confirm('The ' + nm + ' ends in "c.om", a common typo. Is that what you meant?'))
			return false;
	}

	// check for .om
	if (em.match(/\.om$/i)) {
		if (!confirm('The ' + nm + ' ends in ".om", a common typo. Is that what you meant?'))
			return false;
	}

	// should start with a letter or number
	if (em.match(/^[^a-z0-9]/i)) {
		if (!confirm('The ' + nm + ' does not start with a letter or number, a possible typo. Is that what you meant?'))
			return false;
	}

	// gmail typos
	if (em.match(/@gmai\.com$/i) ||
	em.match(/@gmal\.com$/i) ||
	em.match(/@gmali\.com$/i) ||
	em.match(/@gmil\.com$/i) ||
	em.match(/@gmial\.com$/i) ||
	em.match(/@gmaik\.com$/i) ||
	em.match(/@gmai;\.com$/i) ||
	em.match(/@gmaul\.com$/i)
	) {
		if (!confirm('The ' + nm + ' "' + em + '" contains a common misspelling of "gmail.com". Did you enter it correctly?'))
			return false;
	}

	// hotmail typos
	if (em.match(/@hotmai\.com$/i) ||
	em.match(/@hotmal\.com$/i) ||
	em.match(/@hotmil\.com$/i) ||
	em.match(/@hotmaul\.com$/i) ||
	em.match(/@hotmaill\.com$/i) ||
	em.match(/@hotmauil\.com$/i)
	) {
		if (!confirm('The ' + nm + ' "' + em + '" contains a common misspelling of "hotmail.com". Did you enter it correctly?'))
			return false;
	}

	// yahoo typos
	if (em.match(/@yaho\.com$/i) ||
	em.match(/@yahooo\.com$/i)
	) {
		if (!confirm('The ' + nm + ' "' + em + '" contains a common misspelling of "yahoo.com". Did you enter it correctly?'))
			return false;
	}

	return true;
}
