// _isValidEmail.js
// Email Validation Javascript
// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd

/***************************************************************
	NOTE:	changed RETURN
			returns:	message (TRUE)		if error found
						FALSE				email address is OK
*****************************************************************/

function validateEmail(addr) {

	if (addr == '') {
		return 'Email address is mandatory';
	}

	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
		if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
			return 'Email address contains invalid characters';
		}
	}
	for (i=0; i<addr.length; i++) {
		if (addr.charCodeAt(i)>127) {
			alert("email address contains non ascii characters.");
			return false;
		}
	}

	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
		return 'Email address must contain an @';
	}
	if (atPos == 0) {
		return 'Email address must not start with @';
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
		return 'Email address must contain only one @';
	}
	if (addr.indexOf('.', atPos) == -1) {
		return 'Email address must contain a period in the domain name';
	}
	if (addr.indexOf('@.',0) != -1) {
		return 'Period must not immediately follow @ in email address';
	}
	if (addr.indexOf('.@',0) != -1){
		return 'Period must not immediately precede @ in email address';
	}
	if (addr.indexOf('..',0) != -1) {
		return 'Two periods must not be adjacent in email address';
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
		return 'Invalid primary domain in email address';
	}
	return false;
}

