// legal ch: 1234567890_.-@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
function check_email(cell) 
{
	var txt=cell.value;
	
	if (txt.indexOf("@")<1) {
		cell.focus();
		return false;		
	}
	
	if (txt.indexOf(".") < 1) {
		cell.focus();
		return false;	
	}
	
	// last index of . must not be the end 
	if (txt.lastIndexOf(".") < txt.indexOf("@")) {
		cell.focus();
		return false;
	}
	
	// can't end with "." or ".x"
	if (txt.lastIndexOf(".") >= txt.length - 2) {
		cell.focus();
		return false;
	}
	
	// remove space
	var email = "";
	var legalCh = "1234567890_.-@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	for(var k = 0; k < txt.length; k++){
		ch = txt.charAt(k);
		if (ch != " ") {
		  	email = email + ch;
			
			if (legalCh.indexOf(ch) < 0) {
				alert("Email address contains a bad char:" + ch + ". Please correct and try again.");
				return false;
			}
		}
	}
	cell.value = email;
		
	return true;
}
