// JavaScript Document

//function checkEmail(email){
//var str=email
//var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
//if (filter.test(str))
//testresults=true
//else{
//alert("Please input a valid email address!")
//testresults=false
//}
//return (testresults)
//}

function checkEmail(emailAddr) {
	// this function checks for a well-formed e-mail address
	// in the format:
	// user@domain.com
	
	var i;

	
	// check for @
	i = emailAddr.indexOf("@");
	if (i == -1) {
		return false;
	}

	// check for .
	dotSpot = emailAddr.indexOf(".");
	if (dotSpot ==-1 || dotSpot-i==1 ) {
		return false;
	}

	
	// separate the user name and domain
	var username = emailAddr.substring(0, i);
	var domain = emailAddr.substring(i + 1, emailAddr.length)

	// look for spaces at the beginning of the username
	i = 0;
	while ((username.substring(i, i + 1) == " ") && (i < username.length)) {
		i++;
	}
	// remove any found
	if (i > 0) {
		username = username.substring(i, username.length);
	}

	// look for spaces at the end of the domain
	i = domain.length - 1;
	while ((domain.substring(i, i + 1) == " ") && (i >= 0)) {
		i--;
	}
	// remove any found
	if (i < (domain.length - 1)) {
		domain = domain.substring(0, i + 1);
	}

	// make sure neither the username nor domain is blank
	if ((username == "") || (domain == "")) {
		return false;
	}
	
	// check for bad characters in the username

	var ch;
	for (i = 0; i < username.length; i++) {
		ch = (username.substring(i, i + 1)).toLowerCase();
		if (!(((ch >= "a") && (ch <= "z")) || 
			((ch >= "0") && (ch <= "9")) ||
			(ch == "_") || (ch == "-") || (ch == "."))  && ch!="'") {
				return false;
		}
	}

	// check for bad characters in the domain
	for (i = 0; i < domain.length; i++) {
		ch = (domain.substring(i, i + 1)).toLowerCase();
		if (!(((ch >= "a") && (ch <= "z")) || 
			((ch >= "0") && (ch <= "9")) ||
			(ch == "_") || (ch == "-") || (ch == "."))) {
				return false;
		}
	}

	var j;
	j = emailAddr.indexOf(".")
	
	if (j== -1){
		return false;
	}

	
	var suffix = emailAddr.substring(j + 1, emailAddr.length)
	if (suffix.length<2){
		return false;
	}

    //check for period as last character in username
	var dotLast = (username.substring(username.length, username.length-1))

	if (dotLast=="."){
		return false;
	}
	
// we would have exited if we'd found a good suffix, so return false
	return true;
}


