// email_validate.js  Javascript functions to do initial validation of multiple email addresses
// addr = email address, man = 1 if email address is manatory, db = 1 if displaying error messages is ok

function validateEmail(addr,man,db) {
	if (addr == '' && man) {
		 if (db) alert('email address is mandatory');
		 return false;
	}
	if (addr == '') return true;
	
	addr = addr.replace(/^\s+|\s+$/g, '');		// Trim left and right of spaces
		
	var invalidChars = '\/\'\\";:?!()[]\{\}^|';    // removed blank (space) from illegal chars
	for (i=0; i<invalidChars.length; i++) {
		 if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
				if (db) alert('email address "' + addr + '" contains invalid characters');
				//alert( 'rich' );
					return false;
		 }
	}
	for (i=0; i<addr.length; i++) {
		 if (addr.charCodeAt(i)>127) {
				if (db) alert('email address "' + addr + '" contains non ascii characters.');
					return false;
		 }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
		if (db) alert('email address "' + addr + '" must contain an @');
			return false;
	}
	if (atPos == 0) {
		if (db) alert('email address "' + addr + '" must not start with @');
		 return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
		 if (db) alert('email address "' + addr + '" must contain only one @');
		 return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
		 if (db) alert('email address "' + addr + '" must contain a period in the domain name after the "@"');
		 return false;
	}
	if (addr.indexOf('@.',0) != -1) {
		 if (db) alert('period must not immediately follow @ in email address "' + addr + '"');
		 return false;
	}
	if (addr.indexOf('.@',0) != -1){
		 if (db) alert('period must not immediately precede @ in email address "' + addr + '"');
		 return false;
	}
	if (addr.indexOf('..',0) != -1) {
		 if (db) alert('two periods must not be adjacent in email address "' + addr + '"');
		 return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	suffix = suffix.toLowerCase();
	
	if (suffix.length == 0) {
		 if (db) alert('The email address ' + addr + ' will not work because you need a suffix such as "com" or "net" after the period');
		 return false;
	}
	
	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') {
		 if (db) alert('The email address ' + addr + ' will not work because "' + suffix + '" not a valid domain extension');
		 return false;
	}
	return true;
}

function multiEmail(email_field) 
{
	email_field = email_field.replace(/^\s+|\s+$/g, '');  // Trim left and right of spaces
	var email = email_field.split(',');
	for (var i = 0; i < email.length; i++) 
	{
		if( i > 0 && email[i].length < 1 )
			continue;
		if (!validateEmail(email[i], 1, 1)) 
			return false;
	}
	return true;
}