function trimmed(mystring)
{
	return mystring.replace(/^\s+|\s+$/g,'');
}

function checkform(theform,alertmessage)
{
	var okform = true;
 	var i;	
	for (i=0;i<theform.elements.length;++i)
	{
		if (theform.elements[i].name && !checkfield(theform.elements[i]))
		{
			okform = false;
		}

	}
	if (okform && theform.form_checked)
	{
		theform.form_checked.value = 'Yes';
	}
	if (theform.return_url)
	{
		theform.return_url.value = document.location;
	}
	if (alertmessage && !okform)
	{
		alert(alertmessage);
	}
	return okform;  
}


function checkfield(formfield)
{
	var ok = true;

	var formid = document.getElementById(formfield.name.replace(/\[\]/,''));

	var required = false;
	var email = false;
	if (formid)
	{
		var classes = formid.className.split(/\W+/);

		for (i=0;i<classes.length;++i)
		{
			if (classes[i].indexOf('required') == 0)
			{
				var class_original = classes[i];
				required = true; 
			}
			if (classes[i] == 'email' && formfield.type == 'text')
			{
				email = true;
			}
		}
		if (required)
		{
			var class_stem = class_original.substr(0,(class_original+'_').indexOf('_'));
			var class_new = class_original;
//alert(formfield.type);


			if (formfield.name != formid.id || formfield.type == 'radio')
			{
// Multiple fields radio/checkbox only 
				ok = false;

// Loop round to make sure one is checked
				for (i=0;i<formfield.form.elements.length;++i)
				{
					if (formfield.form.elements[i].name == formfield.name && formfield.form.elements[i].checked)
					{
						ok = true;
					}
				}

			} else { 
			if (formfield.type == 'text' || formfield.type == 'textarea')
			{
				if (trimmed(formfield.value) == '')
				{
					ok = false;  
				}
			}
			if (formfield.type == 'checkbox')
			{
				if (!formfield.checked)
				{
					ok = false;
				}
			}

			if (formfield.type == 'select-one')
			{
				if (formfield.options[formfield.selectedIndex].value == '')
				{
					ok = false;
				}
			}
			if (formfield.type == 'select-multiple')
			{
				ok = false;
				for (i=0;i<formfield.options.length;++i)
				{
					if (formfield.options[i].selected && formfield.options[i].value != '')
					{
						ok = true;
						break;
					}
				}
			}

			}
			if (ok)
			{
				class_new = class_stem+'_ok';
			} else {
				class_new = class_stem+'_error';
			}
			formid.className = formid.className.replace(class_original,class_new);
		}
	}
	if (email && (required || trimmed(formfield.value) != ''))
	{
		ok = emailCheck(formfield.value);
		if (required)
		{
			if (ok)
			{
				class_new = class_stem+'_ok';
			} else {
				class_new = class_stem+'_error';
			}
			formid.className = formid.className.replace(class_original,class_new);
		}
	}
	return ok;
}


function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or
well-known TLD.  1 means check it, 0 means dont. */

	var checkTLD=1;

/* The following is the list of known TLDs that an e-mail
address must end with. */

	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered
e-mail address fits the user@domain format.  It also is used to separate
the username from the domain. */

	var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching
all special characters.  We dont want to allow special characters in
the address.  These characters include ( ) < > @ , ; : \ " . [ ] */

	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters
allowed in a  username or domainname.  It really states which chars arent
allowed.*/

	var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted
string (in which case, there are no rules about which characters are
allowed and which arent; anything goes).  E.g. "jiminy
cricket"@disney.com is a legal e-mail address. */

	var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP
addresses, rather than symbolic names.  E.g. joe@[123.124.233.4] is a
legal e-mail address. NOTE: The square brackets are required. */

	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

	var atom=validChars + "+";

/* The following string represents one word in the typical
username. For example, in john.doe@somewhere.com, john and doe are
words. Basically, a word is either an atom or quoted string. */

	var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal
symbolic domain, as opposed to ipDomainPat, shown above. */

	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, lets start trying to figure out if the supplied
address is valid. */

/* Begin with the coarse pattern to simply break up
user@domain into different pieces that are easy to analyze. */

	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {

/* Too many/few @s or something; basically, this address
doesnt even fit the general mould of a valid e-mail address. */

		alert("Email address "+emailStr+"  seems incorrect (check @ and .s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Email address "+emailStr+"  seems incorrect (username contains invalid characters).");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Email address "+emailStr+"  seems incorrect (the domain name contains invalid characters).");
			return false;
		}
	}

// See if "user" is valid 

	if (user.match(userPat)==null) {

// user is not valid

		alert("Email address "+emailStr+"  seems incorrect (the username doesn't seem to be valid).");
		return false;
	}

/* if the e-mail address is at an IP address (as opposed to
a symbolic host name) make sure the IP address is valid. */

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {

// this is an IP address

		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Email address "+emailStr+"  seems incorrect (destination IP address is invalid!)");
				return false;
			}
		}
	}
	return true;
}
