// simple e-mailcheck
function isValidEmail(email) { 
	if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		return true; 
	}
	else {
		alert ("This does not appear to be a valid e-mail address.\nPlease enter your e-mail address in the form: you@you.com");
		return false; 
	} 
}
// advanced e-mailcheck
function emailCheck (emailStr,field) {
	var alertmsg = true;
	if (emailCheck.arguments.length == 2 ) alertmsg = emailCheck.arguments[1];
	if(emailStr.length > 0){
		var emailPat = /^(.+)@(.+)$/;
		var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
		var validChars = "\[^\\W" + specialChars + "\]";
		var quotedUser = "(\"[^\"]*\")";
		var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom = validChars + '+';
		var word = "(" + atom + "|" + quotedUser + ")";
		var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
		var matchArray = emailStr.match(emailPat);
		if (matchArray == null) {
			if (alertmsg) {
				alert("The current e-mail address seems to be incorrect. Please check the @ and the '.'");
				document.getElementById(field).focus();
				return false;
			}
		}
		var user = matchArray[1];
		var domain = matchArray[2];
		if (user.match(userPat) == null) {
		    if (alertmsg) {
				alert("The current e-mail address seems to be incorrect. Please check the username.");
				document.getElementById(field).focus();
			    return false;
			}
		}
		var IPArray = domain.match(ipDomainPat);
		if (IPArray != null) {
		    // this is an IP address
			  for (var i = 1; i <= 4; i++) {
			    if (IPArray[i] > 255) {
			        if (alertmsg) {
						alert("The current e-mail address seems to be incorrect. Please check the IP-adres.");
						document.getElementById(field).focus();
						return false;
					}
			    }
		    }
		    return true;
		}
		var domainArray = domain.match(domainPat)
		if (domainArray == null) {
			if (alertmsg) {
				alert("The current e-mail address seems to be incorrect. Please check the domain.");
				document.getElementById(field).focus();
			    return false;
			}
		}
		var atomPat = new RegExp(atom,"g");
		var domArr = domain.match(atomPat);
		var len = domArr.length;
		if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 3) {
			// the address must end in a two letter or three letter word.
			if (alertmsg) {
				alert("The current e-mail address seems to be incorrect. An e-mail address usually ends on with two or three letters.");
				document.getElementById(field).focus();
				return false;
			}
		}
		if (len < 2) {
			var errStr = "The current e-mail address seems to be incorrect. Please check the domain.";
			if (alertmsg) {
				alert(errStr);
				document.getElementById(field).focus();
				return false;
			}
		}
	}
	return true;
}
/*	removes leading and trailing spaces and replaces double spaces with a single space;
	if not a string is given, the input is returned unchanged */
function trim(inputString) {
	if (typeof inputString != "string") { return inputString; }
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") {
		// check leading spaces
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") {
		// check trailing spaces
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) {
		// find multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
	}
	// return the string
	return retValue;
}