function validateEmpty(fld, myText) {
    var error = "";
    var tfld = trim(fld.value);
    fld.style.background = bgColor;
    if (tfld == "") {
        fld.style.background = bgErrorColor;
	    error = myText+"Missing or Empty\n";
    } else {
        fld.style.background = bgColor;
    }
    return error;   
}

function validateUsername(fld,myText) {
    var error = "";
	var illegalChars = /\W/; // allow letters, numbers, and underscores
    fld.style.background = bgColor;
    if (fld.value == "") {
        fld.style.background = bgErrorColor; 
        error = myText+"You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = bgErrorColor; 
        error = myText+"The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = bgErrorColor; 
        error = myText+"The username contains illegal characters.\n";
    } else {
        fld.style.background = bgColor;
    } 
    return error;
}

function validatePassword(fld,myText) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
    fld.style.background = bgColor;
 
    if (fld.value == "") {
        fld.style.background = bgErrorColor;
       error = myText+"Missing or Empty\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 15)) {
        error = myText+"Password should be at least 4 characters.\n";
        error += myText+"---and less than 15 characters\n";
		
        fld.style.background = bgErrorColor;
    } else if (illegalChars.test(fld.value)) {
        error = myText+"The password contains illegal characters.\n";
        error += myText+"---Please use Letters and numbers only.\n";
        fld.style.background = bgErrorColor;
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = myText+"The password must contain at least one numeral.\n";
        fld.style.background = bgErrorColor;
    } else {
        fld.style.background = bgColor;
    }
   return error;
}  

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld,myText) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    fld.style.background = bgColor;
    
    if (fld.value == "") {
        fld.style.background = bgErrorColor;
        error = myText+"Missing or Empty\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = bgErrorColor;
        error = myText+"Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = bgErrorColor;
        error = myText+"The email address contains illegal characters.\n";
    } else {
        fld.style.background = bgColor;
    }
    return error;
}

function validatePhone(fld,myText) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     
    fld.style.background = bgColor;

   if (fld.value == "") {
	    error = myText+"Missing or Empty\n";
        error += myText+"example: nnn-nnn-nnnn\n";		
        fld.style.background = bgErrorColor;
    } else if (isNaN(parseInt(stripped))) {
        error = myText+"The phone number contains illegal characters.\n";
        error += myText+"example: nnn-nnn-nnnn\n";		
        fld.style.background = bgErrorColor;
    } else if (!(stripped.length >= 10 ) && (stripped.length <= 11 ) ) {
        error = myText+"The phone number is the wrong length. Make sure you included an area code.\n";
        error += myText+"example: nnn-nnn-nnnn\n";		
        fld.style.background = bgErrorColor;
    }else {
        fld.style.background = bgColor;
    }
	 
    return error;
}

function validateEmailMatch(fld1, fld2,myText) {
    var error = "";
    fld.style.background = bgColor;
	if (fld1.value == "") {
        fld1.style.background = bgErrorColor;
 		error += myText+"Missing Email Address\n";
		return error;
    } 
	if (fld2.value == "") {
        fld2.style.background = bgErrorColor;
 		error += myText+"Missing Confirmation Email Address\n";
		return error;
    } 
    
	if (fld1.value !=  fld2.value) {
        fld1.style.background = bgErrorColor; 
        fld2.style.background = bgErrorColor; 		
        error += myText+"Address mis-matched.\n"
    } else {
        fld1.style.background = bgColor;
        fld2.style.background = bgColor;		
    }
    return error;   
}

function validateMinimumCB( theForm, myCheckBoxes, minimumNum,myText ) {
  var checkMe;
  var formElement
  var isChecked = 0;
  var error = "";
// Loop through array.. Set isChecked++ if value is checked.  
  for( checkMe in myCheckBoxes ) {
    if ( eval("theForm."+myCheckBoxes[checkMe]+".checked") == true ) {
	  isChecked++;
    }
  }
  
  if ( isChecked < minimumNum  ) {
   if ( minimumNum  == 1 ) {
     error += myText+"Require:"+minimumNum+" item to be selected\n";
   } else {
	 error += myText+"Require:"+minimumNum+" items to be selected\n";
   }
	return error;	
  }
  return error;	 
} 

function validateMinimumCBNE( theForm, myCheckBoxes, minimumNum,myText ) {
  var checkMe;
  var formElement
  var isChecked = 0;
  var error = "";
// Loop through array.. Set isChecked++ if value is checked.  
  for( checkMe in myCheckBoxes ) {
    if ( eval("theForm."+myCheckBoxes[checkMe]+".checked") == true ) {
	  isChecked++;
    }
  }
  
  if ( isChecked < minimumNum  ) {
   if ( minimumNum  == 1 ) {
     error += myText+"\n";
   } else {
	 error += myText+"\n";
   }
	return error;	
  }
  return error;	 
} 

function validateButton(btn,myText) {
    var cnt = -1;
    var error = "";
//    btn.style.background = bgColor;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
//    if (cnt > -1) {
    if (cnt < 0) {
		for (var i=btn.length-1; i > -1; i--) {
		  btn[i].style.background = bgColor; 	
 	      btn[i].style.backgroundColor = bgColor; 
          btn[i].style.background = bgErrorColor;
          btn[i].style.backgroundColor = bgErrorColor;
		}
    	error += myText+"Not Selected.\n"
		
	} 
	return error;
}