// The function trims all whitespace from a field
function trimAll(str) {
  var newstr = str.replace(/\s+/g,''); 
  return newstr;
}
// The function to test the validity of an email address
function validml(str) {	 
  if (str.length > 0) 	{
    if (window.RegExp) {	
      var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
      var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
      var reg1 = new RegExp(reg1str);
      var reg2 = new RegExp(reg2str);
      if (!reg1.test(str) && reg2.test(str)) { 
	    return true;
	  }  
	  return false;
    } 
    else { 
      if(str.indexOf("@") >= 0) {
        return true;
	  }
      return false;
    }
	return false;	
  }
}
//
function verify(fm) { 
  var bad_field=false;
  var no_errors=true;
  var msgfield=""; 
  var astr="";
  var msg1="ERROR: The above field can contain a-z and A-Z only";
  var msg2="ERROR: The above field must not be left blank";
  var msg3="ERROR: The field contains an invalid email address";
  for (var i=0; i < fm.length; i++) {
    var a=fm.elements[i];
// check usrname----------------------------------------------------  
    if (a.name=='usrname' ) {	
	  // first remove any spaces from the name 
	  var nospaces=trimAll(a.value);
	  if (nospaces.length==0) {	
	  	msgfield=msg2;
		bad_field=true;
		no_errors=false;
		continue;
	  }
      else {
		// test for alpha value a-z or A-Z 
		var regstr = /^([a-zA-Z]+)$/; 
        if (!regstr.test(nospaces)) { 
		  msgfield=msg1;
          bad_field=true;
		  no_errors=false;
		  continue; 
        }
		else { 
		  msgfield ='';
		}
	  }
	}
//-----------------------------------------------------------------------------------------------------  
	if (a.name=='x_usrname' && bad_field) {  
	  a.value=msgfield;
	  bad_field=false;
	  continue; 
	}	 
	else {
	  if (a.name=='x_usrname') {
	    a.value='';
	  }
	}  	
// check usreml----------------------------------------------------  
    if (a.name=='usreml') {	 
	  // first check to see if input is blank 
	  astr=a.value;
	  if (astr.length==0) {
	    msgfield=msg2;
		bad_field=true;
		no_errors=false;
		continue;
	  }
	  else {
	    if (!validml(a.value)) {
		  msgfield=msg3;
		  bad_field=true;
		  no_errors=false;
		  continue;
		}
		else {
		  msgfield='';
		  continue;
		}    
	  }
	} 
//----------------------------------------------------	  
    if (a.name=='x_usreml' && bad_field) {
	  a.value=msgfield;
	  bad_field=false;
	  continue;
	}
	else {	
	  if (a.name=='x_usreml') {
	    a.value='';
	    continue;
	  }	
	} 
// check recip1ml----------------------------------------------------  
    if (a.name=='recip1ml') {	 
	  // first check to see if input is blank  
	  astr=a.value;
	  if (astr.length==0) {	 
	    msgfield=msg2;
		bad_field=true;
		no_errors=false;
		continue;
	  }
	  else {
	    if (!validml(a.value)) { 
		  msgfield=msg3;
		  bad_field=true;
		  no_errors=false; 
		  continue;
		}
		else {
		  msgfield='';
		  continue;
		}    
	  }
	} 
//----------------------------------------------------	  
    if (a.name=='x_recip1ml' && bad_field) { 
	  a.value=msgfield;
	  bad_field=false;
	  continue;
	}
	else {	
	  if (a.name=='x_recip1ml') {
	    a.value='';
	    continue;
	  }	
	} 
// check recip2ml and recip3ml -----------------------------------------  
    if (a.name=='recip2ml' || a.name=='recip3ml') {	 
	  // first check to see if input not blank, if it is make sure that it is a valid email address 
	  astr=a.value;
	  if (astr.length > 0) {	 
	    if (!validml(a.value)) { 
		  msgfield=msg3;
		  bad_field=true;
		  no_errors=false; 
		  continue;
		}
		else {
		  msgfield='';
		  continue;
		}    
	  }
	} 
//----------------------------------------------------	  
    if (a.name=='x_recip2ml' && bad_field) { 
	  a.value=msgfield;
	  bad_field=false;
	  continue;
	}
	else {	
	  if (a.name=='x_recip1m2') {
	    a.value='';
	    continue;
	  }	
	} 
//----------------------------------------------	  	  		  	  		   
if (a.name=='x_recip3ml' && bad_field) { 
	  a.value=msgfield;
	  bad_field=false;
	  continue;
	}
	else {	
	  if (a.name=='x_recip3ml') {
	    a.value='';
	    continue;
	  }	
	} 
//----------------------------------------------	  	  		  	  		   
  } // end of for loop
  if (no_errors) {
    return true;
    }
    else {
      return false;		  
  }	
}  

