/************************************************************************************************
* IsMail checks whether a given string contains only 0-9 or a-z or . or _ or " or <> or - and @ *
* That is, it forms a sound email address (the empty string is a 'good' email)                  *
* No characters or .s after the '@' constitutes a bad email, as does mulitiple @s               *
*************************************************************************************************/
function isMail(mail) {
	var i;
	var atCount;
	i = 0;
	atCount = 0;
	if (mail.length > 0) {
		// If there is no . after the @ then email is invalid
		if (mail.indexOf(".",mail.indexOf("@")) == -1) {
			i = 0;
			return(false);
		}
		while (i < mail.length) {
			if ( (mail.charCodeAt(i) >= 48) && (mail.charCodeAt(i) <= 57) ||	// numbers 0-9
			(mail.charCodeAt(i) >= 65) && (mail.charCodeAt(i) <= 90)  ||		// A-Z
			(mail.charCodeAt(i) >= 97) && (mail.charCodeAt(i) <= 122) ||		// a-z
			(mail.charCodeAt(i) == 46) ||										// . char
			(mail.charCodeAt(i) == 95) ||										// _ char
			(mail.charCodeAt(i) == 45) );										// - char
			else if (mail.charCodeAt(i) == 64) {								// @ char
				// only one @ allowed
				atCount++; 
			} else {
				//alert("error at: "+(i+1));
				i = 0;
				return(false);
			}
			i++;
		}
	}
	// Also check length is >= 5 if len(mail)>0 [min email: a@b.c = 5 char]
	// and only one @ has been parsed
	if ( (i == 0) || ((mail.length > 0) && (mail.length < 5)) || (atCount > 1) || (atCount < 1) ) {
		return(false); //Bad char was found
	} else {
		return(true); //good email address
	}
}
