// Email Validation Javascript
// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function validateForm(name,email,comment,man,db) {
	
	if (name == '' && man) {
	   if (db) alert('Please enter your name.');
	   return false;
	}
	
	if (email == '' && man) {
	   if (db) alert('Please enter your email address.');
	   return false;
	}
	
	if (email == '') return true;
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (email.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) alert('This email address contains invalid characters.');
		  return false;
	   }
	}
	for (i=0; i<email.length; i++) {
	   if (email.charCodeAt(i)>127) {
		  if (db) alert("This email address contains invalid characters.");
		  return false;
	   }
	}
	
	var atPos = email.indexOf('@',0);
	
	//email address must contain an @
	if (atPos == -1) {
	   if (db) alert('The email address you have entered is invalid.');
	   return false;
	}
	//email address must not start with @
	if (atPos == 0) {
	   if (db) alert('The email address you have entered is invalid.');
	   return false;
	}
	//email address must contain only one @
	if (email.indexOf('@', atPos + 1) > - 1) {
	   if (db) alert('The email address you have entered is invalid.');
	   return false;
	}
	//email address must contain a period in the domain name
	if (email.indexOf('.', atPos) == -1) {
	   if (db) alert('The email address you have entered is invalid.');
	   return false;
	}
	//period must not immediately follow @ in email address
	if (email.indexOf('@.',0) != -1) {
	   if (db) alert('The email address you have entered is invalid.');
	   return false;
	}
	//period must not immediately precede @ in email address
	if (email.indexOf('.@',0) != -1){
	   if (db) alert('The email address you have entered is invalid.');
	   return false;
	}
	//two periods must not be adjacent in email address
	if (email.indexOf('..',0) != -1) {
	   if (db) alert('The email address you have entered is invalid.');
	   return false;
	}
	var suffix = email.substring(email.lastIndexOf('.')+1);
	//invalid primary domain in email address
	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 you have entered is invalid.');
	   return false;
	}
	
	if (comment == '' && man) {
	   if (db) alert('Please enter a coment/question.');
	   return false;
	}
	
	return true;
}
