init();

function init() {
   XDescription = new Array();
   RequireText = 0;
   RequireSelect = 0;
   RequireRadio = 0;
   RequireCheckbox = 0;
   RequireRegex = 0;
}

function ValidateForm(theForm) {
   if (ProcessTexts(theForm,RequireText) &&
   ProcessSelects(theForm,RequireSelect) &&
   ProcessRadios(theForm,RequireRadio) &&
   ProcessCheckboxes(theForm,RequireCheckbox) &&
   ProcessRegexes(theForm,RequireRegex)
   ) {
      return true;
   } else {
      return false;
   }
}

function ProcessTexts(formname,fields) {
   if(typeof fields != "object") {return true;} //don't bother if the form doesn't require any
   if (fields.length == 0) { return true; } //short-circuit, legacy.
   for( var i = 0; i<fields.length; i++) {
      if(formname.elements[fields[i]].value.length < 1) {
         formname.elements[fields[i]].focus();
         if (XDescription[fields[i]]) {
            alert(XDescription[fields[i]]);
         } else {
            alert("Please enter your " + fields[i] + ".");
         }
         return false;
      }
   }
   return true;
}
function ProcessSelects(formname,fields) {
   if(typeof fields != "object") {return true;} //don't bother if the form doesn't require any
   if (fields.length == 0) { return true; } //short-circuit, legacy.
   for( var i = 0; i<fields.length; i++) {
      if(formname.elements[fields[i]].selectedIndex < 1) {
         formname.elements[fields[i]].focus();
         if (XDescription[fields[i]]) {
            alert(XDescription[fields[i]]);
         } else {
            alert("Please select your " + fields[i] + ".");
         }
         return false;
      }
   }
   return true;
}
function ProcessRadios(formname,groups) {
   if(typeof groups != "object") {return true;} //don't bother if the form doesn't require any
   if (groups.length == 0) { return true; } //short-circuit, legacy.
   for (var j = 0; j < groups.length; j++) {
      var field = groups[j];
      var hits = 0;
      for( var i = 0; i< formname.elements[field].length; i++) { // start one field past the desc.
         if(formname.elements[field][i].checked) { //the user better not have specified nonexistent fields...
            hits++; //increment hit counter
         }
      }
      if (hits < 1) {
         if (XDescription[field]) {
            alert(XDescription[field]);
         } else {
            alert("Please select an option from the " + field + " group.");
         }
         formname.elements[field][0].focus();
         return false;
      }
   }
   return true;
}
function ProcessCheckboxes(formname,groups) {
   if(typeof groups != "object") {return true;} //don't bother if the form doesn't require any
   if (groups.length == 0) { return true; } //short-circuit, legacy.
   for (var j = 0; j < groups.length; j++) {
      var fields = groups[j];
      var groupdesc = fields[0];  //grab that description
      var hits = 0;
      for( var i = 1; i<fields.length; i++) { // start one field past the desc.
         if(formname.elements[fields[i]].checked) { //the user better not have specified nonexistent fields...
            hits++; //increment hit counter
         }
      }
      if (hits < 1) {
         if (XDescription[groupdesc]) {
            alert(XDescription[groupdesc]);
         } else {
            alert("Please select an option from the " + groupdesc + " group.");
         }
         formname.elements[fields[1]].focus();
         return false;
      }
   }
   return true;
}
function ProcessRegexes(formname,pairs) {
   if(typeof pairs != "object") {return true;} //don't bother if the form doesn't require any
   if (pairs.length == 0) { return true; } //short-circuit, legacy.
   for (var j = 0; j < pairs.length; j++) {
      var thispair = pairs[j];
      var hits = 0;
      var field = thispair[0];
      var regex = thispair[1];

      if(!regex.exec(formname.elements[field].value)) {
         if (XDescription["reg" + field]) {
            alert(XDescription["reg" + field].replace(/\$val/, formname.elements[field].value));
         } else {
            alert("The text you entered in the " + field + " field is improperly formatted. Please check your entry and try again.");
         }
         formname.elements[field].focus();
         return false;
      }
   }
   return true;
}


// end form validation service. ;)

/*
DOCUMENTATION: (holy crap, eric wrote dox!)

Paste the following code into the head of your HTML page.  
Start at the begin arrow and copy up to the end arrow.

You MUST MUST MUST make sure that all field elements that are 
mentioned in the following code actually exist on the form.
If they don't, the user will get Javascript errors and then the 
form will be submitted whether or not it passes validation.

You have been warned.

BEGIN COPYING AT THE END OF THIS LINE ---->

<script src="includes/scripts/formvalidater.js" language="JavaScript"></script>
<script language="JavaScript">
<!--

var Description = new Array();

RequireText = new Array("Name","Company","Phone","Fax","FooField");
Description["Name"] = "Please enter your Name.";
Description["Company"] = "Please enter your Company.";
Description["Fax"] = "Please enter your Fax Number.";
Description["Phone"] = "Please enter your Phone Number.";
		// since we didn't enter a description for the FooField field,
		// the script will say "Please enter a value for the FooField field." when it fails validation.

RequireSelect = new Array("SomeSelect","SomeOtherSelectBox");
Description["SomeSelect"] = "Please select a flying frobozz disc type.";

RequireRadio = new Array("radio1","radio2");
Description["radio1"] = "Please select an option from the Bar Group.";
Description["radio2"] = "Please select an option from the Foo Group.";

		// Checkbox groups are complicated.
		// Enter the names of the boxes in the group you wish to require.
		// Then push their array names onto the RequireCheckbox array.
		// Plus, checkboxes need to have a symbolic group name as the 
		// first element on the array.  That's how we determine the 
		// error message in the Description array.
		// Here's a sample.
		// LetterCheckboxes = new Array("GroupSymbolicName","a","b","c","d","e");
		// Description["GroupSymbolicName"] = "Please choose a box from the letters group.";
		// RequireCheckbox = new Array(LetterCheckboxes);
		// of course, this is a sample.  Don't uncomment it.
		// if you don't have any checkboxes to validate,
		// just uncomment the following line:
		// RequireCheckbox = new Array();

Regex1 = new Array("fieldname",/regex/);
		// to make error messages for regex fields, just stick 'reg' 
		// on the front of the field name.
Description["regfieldname"] = "you entered invalidly formatted text on the field called fieldname.  Moron."; 
Regex2 = new Array("otherfieldname",/regex/);
Description["regotherfieldname"] = "you entered invalidly formatted text on the other field"; 
Regex3 = new Array("fieldname",/regex/);
RequireRegex = new Array(Regex1,Regex2,Regex3);

//-->
</script>

<---- END COPYING AT THE BEGINNING OF THIS LINE

Then, in the <form> tag on your page, add the onSubmit tag as follows: 
<form onSubmit="return ValidateForm(this);">

And users will now be validated. :)

END DOCUMENTATION
*/


