//--------------------------------------------------------------------------------------------------------- Email address check

function allValidChars(email) {
	var validchars										= "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	for (var i=0; i < email.length; i++) {
		var letter										= email.charAt(i).toLowerCase();
		if (validchars.indexOf(letter) == -1) { return false; }
	}
	return true;
}

function isValidEmail(email) {
	if (email == null) { return false; }
	if (email.length < 9) { return false; }          							// there must be at least 9 characters
	if (!allValidChars(email)) { return false; }								// all characters must be valid
	if (email.indexOf("@") < 1) { return false; }    							// must contain @, and it must not be the first character
			
	// There must not be more than one @		
	var atcount											= 0;
	for (var i=0; i < email.length; i++) {
		var letter										= email.charAt(i).toLowerCase();
		if (letter == "@") { atcount++; }
	}
	if (atcount > 1) { return false; }																	
																					
	if (email.lastIndexOf("@") == email.length-1) { return false; }				// @ must not be the last character
	if (email.indexOf("..") >= 0) { return false; }                             // two dots in a row is not valid
    if (email.lastIndexOf(".") <= email.indexOf("@")) { return false; }         // last dot must be after the @
    if (email.lastIndexOf(".") == email.length-1) { return false; }             // dot must not be the last character
    return true;
}
