// validate directly from form: onblur="valEmail('fieldID','nopass');   nopass fails on blank field
function browserCompatible() 
 {
  // checkW3C DOM, then MSIE 4, then NN 4.
 
  if (document.getElementById) return true;
  if (document.all) return true;  
  if (document.layers) return true;
  return false;
 }
 
function valEmail(emailIn,BlankOK) 
 {  
   if (!browserCompatible()) return true;
    var str=document.getElementById(emailIn).value;
    if (str=='')
     {
      if (BlankOK=='nopass')
       {
	    alert('Email must be filled in');
        setTimeout(function(){document.getElementById(emailIn).focus();document.getElementById(emailIn).select();},10);
	    return false;
  	   }
      else return true;
     }	
	var ve=true;
	var at="@";
	var dot=".";
	var invchars=" ()/;:,<>"+'"';
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);

	if (str=='') return true;
	if (lat==-1 || lat==0 || lat==lstr-1) ve=false; //@ not there, @ at start, @ at end
	if (ldot==-1) ve=false;  // no .
	if (str.indexOf(at,(lat+1))!=-1)  ve=false; // @ after @
 	if (str.charAt(0)==dot || str.charAt(lat-1)==dot || str.charAt(lat+1)==dot || str.charAt(lstr-1)==dot) ve=false;  // . at start, end, or right before or after @
    for(i=0;(i<invchars.length && ve==true);i++)	
	   if (str.indexOf(invchars.charAt(i))!=-1)  ve=false; // invalid character in email address
	for(i=0;(i<lstr && ve==true);i++) 
	   if (str.charCodeAt(i)>127) ve=false;  // check each char for extended value 
    if (ve==false)
     {
      alert('Incorrect email format');
      setTimeout(function(){document.getElementById(emailIn).focus();document.getElementById(emailIn).select();},10);
      return false;
     }
    else {
	  return true;	// email passes muster
	  }		
 }
