//  TrimAll.cfm - Trim leading and trailing blanks from an input field

   function TrimAll(obj) {

      var startpos = 0, endpos = obj.value.length - 1;

      if (endpos == -1) { //  null string
        return;
      }

      while (startpos <= obj.value.length && obj.value.substring(startpos, startpos + 1) == " ") {
 	      startpos++; // find start position of string 
      }

      while (endpos >= 0 && obj.value.substring(endpos, endpos + 1) == " ") {
	      endpos--; // find end position of string 
      }

      if (startpos > endpos) { //- trim the object 
         obj.value = "";
      } else {
         obj.value = obj.value.substring(startpos, endpos + 1);
      }
   }

//   BadText.js - Check for the presence of any illegal character

   function BadText(obj, objname) {
      TrimAll(obj);
      c = '|';
      if (obj.value.indexOf(c) != -1) {
         alert("'" + objname + "' contains an illegal '" + c + "' character");
         return true;
      }
      return false;
   }

 //  IsAbsent.js - check for the presence of any character besides a blank

   function IsAbsent(obj, objname) {
      var retval = true;
      for (var i = 0; i < obj.value.length; i++) {
         if (obj.value.substring(i, i + 1) != " ") {
            retval = false;
            break;
         }
      }
      if (retval && (objname != "")) {
         alert("'" + objname + "' may not be left empty");
      }
      return retval;
   }
   
//  BadEmail.js - Check for illegally formatted email address

   function BadEmail(obj, objname)
   {
     TrimAll(obj);
     x=obj;
     L =x.value.length;
 	 if (x.value.charAt(L-1)=="."){alert("'" + objname + "' may not have a period as it last character");x.focus();return true}
	 At=x.value.indexOf("@");
	 if (At==-1){alert("'" + objname + "' is missing an @ sign");x.focus();return true}
	 if (At==0){alert("'" + objname + "' may not have @-sign as first character");x.focus();return true}
	 if (At==(L-1)){alert("'" + objname + "' may not have @-sign as last character");x.focus();return true}
	 A=x.value.indexOf("@",At+1);	 
	 if (A !=-1){alert("'" + objname + "' may not have more than one @-sign");x.focus();return true}
	 P =x.value.indexOf(".");
	 if (P==-1){alert("'" + objname + "' must have at least one period");x.focus();return true}
	 if (P==0){alert("'" + objname + "' may not have period as first character");x.focus();return true}
	 P =x.value.indexOf(".",At+1);
 	 if (P==-1){alert("'" + objname + "' must have at least one period after @-sign");x.focus();return true}
	 if (At+1==P){alert("'" + objname + "' must have characters between @sign and period");x.focus();return true}
	 y=x.value.substring(L-3,L); // last three characters
	 z=x.value.substring(L-2,L); // last two characters
	 z=z.toLowerCase();
	 if (y.charAt(0)=="." && z=="us") {y=x.value.substring(L-6,L-3)}
 	 if (y.charAt(0)!=".")
	     {
    	 y=y.toLowerCase();
	     // if (y!="com" && y!="edu" && y!="net" && y!="org" && y!="gov" && y!="mil" && y!="int"){alert("'" + objname + "' must be .com, .edu, .net, .org, .gov, .mil, or .int ");x.focus();return true};
		 }
	// if (P==(L-3)){alert("'" + objname + "' may not have only one period and end in just two letters");x.focus();return true}
	 if (x.value.indexOf(",")!=-1){alert("'" + objname + "' may not contain a comma");x.focus();return true}
	 if (x.value.indexOf(" ")!=-1){alert("'" + objname + "' may not contain a space");x.focus();return true}
	 if (x.value.indexOf("*")!=-1){alert("'" + objname + "' may not contain a asterisk");x.focus();return true}
	 if (x.value.indexOf(")")!=-1){alert("'" + objname + "' may not contain a close parenthesis");x.focus();return true}
	 if (x.value.indexOf("(")!=-1){alert("'" + objname + "' may not contain a open parenthesis");x.focus();return true}
	 if (x.value.indexOf(">")!=-1){alert("'" + objname + "' may not contain a greater than sign");x.focus();return true}
	 if (x.value.indexOf("<")!=-1){alert("'" + objname + "' may not contain a less than sign");x.focus();return true}
	 if (x.value.indexOf(":")!=-1){alert("'" + objname + "' may not contain a colon");x.focus();return true}
	 if (x.value.indexOf(";")!=-1){alert("'" + objname + "' may not contain a semi-colon");x.focus();return true}
	 if (x.value.indexOf('"')!=-1){alert("'" + objname + "' may not contain a quotation mark");x.focus();return true}
	 return false;
	}

function validateNewsletterSignup(aForm) {
	// email address must exist and be valid syntatically
	if (BadText(aForm.email,"Your E-mail Address")) { aForm.email.focus();return false;}
	if (IsAbsent(aForm.email,"Your E-mail Address")) { aForm.email.focus();return false;}
	if (BadEmail(aForm.email,"Your E-mail Address"))  { aForm.email.focus();return false;}

}

