/*
		formTest() v1.0 
		Generic form checking routines
	 (c) 2005, Bill Tomczak Consulting - tech@btomczak.com

		This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
		*************************************************************************** 

		Simple generic form testing function

		1)	Include a hidden "required" field to indicate which fields, separated by commas, are required. 
		2)	Automatic messaging uses the field name to indicate problems. _ and [] are translated into spaces.
		3)	Customizable message text
		4) 	If a required field contains the word email anywhere in its name, it will be tested for proper format for an email address
		5)	Checks boxes with the same name are treated as an array group. At least
			  one needs to be checked. Note that specifying a single check box (individually
		  	named) makes no sense.
		
		To use:
			<form onSubmit="return formTest(this)">

			Don't forget to include the link to this .js file in the page header!
*/

// Set the following messages as desired
var strRequired = "required"; // name of the hidden field naming required fields
var strMsgHead = "Please correct the following problems";
var strMsgBlank = "Blank";
var strMsgRadio = "Unselected";
var strMsgEmail = "Email address isn't properly formatted";
var strMsgCheck = "Please check at least one";
var strMsgNofield = "No such field";
//**********************************************************

function formTest(oForm) {
	var strAlert = "";
	var i;
	var oField;
	var aRequired;
	var re = /_|\[\]/g;
	var oRequired = getFormField(oForm, strRequired);
	
	if (oRequired && oRequired.type == "hidden") {
		aRequired = oRequired.value.split(",");
		for (i = 0; i < aRequired.length; i++) {
			oField = getFormField(oForm, aRequired[i])
			if (oField == null) {
				strAlert += "\n***" + strMsgNofield + ": " + aRequired[i] + "***";
			} else {
				switch (oField.type) {
					case "checkbox":
						if (!oneChecked(oField)) {
							strAlert += "\n" + strMsgCheck + " " + oField.name.replace(re, " ");
						}
						break;

					case "radio":
						if (getRadioValue(oField).length == 0) {
							strAlert += "\n" + strMsgRadio + " " + oField.name.replace(re, " ");
						}
						break;

					case "text":
						if (!oField.name.toLowerCase().indexOf("email") && oField.value.length > 0) {
							if (!emailCheck(oField)) {
									strAlert += "\n" + strMsgEmail;
									break;
							}
						}

					default:
						if (oField.value.length == 0) {
							strAlert += "\n" + strMsgBlank + " " + oField.name.replace(re, " ");
						}
				}
			}
		}
	}

	if (strAlert != "") {
		alert(strMsgHead + ":\n" + strAlert);
		return false;
	}
	return true;
}

//
// Test for proper email format
function emailCheck(oField) {
  var strAlert = "";
  var re1 = new RegExp(".+\@.+\..+");
  var re2 = new RegExp("^[a-zA-Z0-9_@.-]+$");
  var strEmail = oField.value;

  if (!re1.exec(strEmail) || !re2.exec(strEmail)) {
    return false;
	}
  return true;
}

//
// Return object reference to a field
// Ignores case and leading/trailing spaces and [] in the case of possible arrays
function getFormField(oForm, strName) {
	var i;
	var reStrip = /^ +| +$/g;
	var re = /\[\]/;

	for (i = 0; i < oForm.elements.length; i++) {
		if (oForm.elements[i].name.toLowerCase().replace(re, "") == strName.toLowerCase().replace(reStrip, "").replace(re, "")) {
			return oForm.elements[i];
		}
	}
	return null;
}

//
// Look for other checkboxes with the same name and see if at least one is checked
function oneChecked(oField) {
	var i;
	var aFields = oField.form.elements;

	for (i = 0; i < aFields.length; i++) {
		if (aFields[i].type == "checkbox" && aFields[i].name == oField.name && aFields[i].checked) {
			return true;
		}
	}
	return false;
}

//
// Return the checked value of a radio group
function getRadioValue(oField) {
	var i;
	var aFields = oField.form.elements;

	for (i = 0; i < aFields.length; i++) {
		if (aFields[i].type == "radio" && aFields[i].name == oField.name && aFields[i].checked) {
			return aFields[i].value;
		}
	}
	return "";
}
