/*
 * Validate an email address... very basic. Doesn't check things like control
 * characters or validate the domain, or anything like that. That stuff should
 * be handled in the back end.
 */
function validateEmail(str) {

	/*
	 * Empty string is valid... Other checks should be done for mandatory fields
	 */
	if (str == null || str == "") {
		return true;
	}

	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);

	if (
		lat == -1 ||
		lat == 0 ||
		lat == lstr || /* An @ symbol that is not the first or last character */
		ldot == -1 ||
		ldot == 0 ||
		ldot == lstr || /* A period that is not the first or last character */
		str.indexOf(at, (lat + 1)) != -1 || /* No more than 1 @ symbol */
		str.substring(lat - 1, lat) == dot || /* No period just before the @ */
		str.substring(lat + 1, lat + 2) == dot || /* No period just after the @ */
		str.indexOf(dot, (lat + 2)) == -1 || /* At least 1 dot after the @ */
		str.indexOf(" ") != -1 /* No spaces */
	) {
		return false;
	}

	return true;
}
