
function isEmail(emailadd) {
/*
desc: this function checks if the given emailadd is valid
pre : emailadd is a string
post: return 0 if emailadd is a valid email address otherwise it returns:
      10  if the string does not contain a @ (is not an email address at all)
      11  if the string starts with an illegal character
      20  if the first @ is to near the beginning or the end of the string
      21  if the last dot is too close to the end of the string
      30  if the string contains multiple @'s
      40  if the part before the @ has a zero length part in it (two dots followed up)
      41  if hyphen detected at illegal position in part before @
      42  if the part before the @ contains illegal characters
      50  if the part after the @ has a zero length part in it (two dots followed up)
      51  if hyphen detected at illegal position in part after @
      52  if the part after the @ contains illegal characters
*/


	var at_pos, length_emailadd
	// search for the (first) @
	at_pos = emailadd.indexOf('@');
	if (at_pos<=0) return(10);
	var allowed_start = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
	if (allowed_start.indexOf(emailadd.charAt(0))==-1) return(11);

	// see if it's position is possible - or die
	length_emailadd = emailadd.length;
	if ((at_pos<2) || (at_pos>length_emailadd-6)) return(20);
	if ((emailadd.lastIndexOf('.')<length_emailadd-5) || (emailadd.lastIndexOf('.')>length_emailadd-3)) return(21);
	// look if this is the only @ - or die
	if (emailadd.indexOf('@', at_pos)<0) return(30);
	


	// now the fast checks are done, check thoroughly
	// split the e-mail address in seperate parts
	var parts = emailadd.split('@');
	var firstpart = (parts[0]).split('.');
	var lastpart = (parts[1]).split('.');

	// each alpha is a string between two dots
	var i, alpha, cursor;
	// set what is allowed is characters between dots
	var allowed_alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_';

	// for the first part (before @) check all alphas
	for (i=0; i<firstpart.length; i++) {
		alpha = firstpart[i];
		if (alpha.length==0) return(40);
		// check all characters of this alpha
		for (cursor=0; cursor<alpha.length; cursor++) {
			// check if this character is allowed
			if (((cursor==0) || (cursor==alpha.length-1)) && (alpha.charAt(cursor)=='-')) return (41);
			if (allowed_alpha.indexOf(alpha.charAt(cursor))==-1) return(42);
		}
	}
	// for the last part (after @) check all alphas
	for (i=0; i<lastpart.length; i++) {
		alpha = lastpart[i];
		if (alpha.length<2) return(50);
		// check all characters of this alpha
		for (cursor=0; cursor<alpha.length; cursor++) {
			// check if this character is allowed
			if (((cursor==0) || (cursor==alpha.length-1)) && (alpha.charAt(cursor)=='-')) return (51);
			if (allowed_alpha.indexOf(alpha.charAt(cursor))==-1) return(52);
		}
	}

	// if everything is ok then the this line is reached, return OK
	return(0);
}
