document.getElementsByClassName = function (cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) {
			retnode.push(elem[i]);
		}
	}
	return retnode;
};

/* This bit lets us use Array.push() in browsers that lack it, like IE5. */
if (typeof(Array.prototype.push)!="function") {
	Array.prototype.push = function () {
		var A_p = 0;
		for (A_p = 0; A_p < arguments.length; A_p++) {
			this[this.length] = arguments[A_p];
		}
		return this.length;
	};
}
/* (Swiped from: http://www.samspublishing.com/articles/article.asp?p=30111&seqNum=3&rl=1) */

function focusAlertStop(field, message) {
	alert(message);
	field.focus();
	return false;
}


// Get the last form on the page (in case any search forms, etc, get added to the template)
var theForm = document.forms[document.forms.length - 1];
theForm.onsubmit = function () {
	var fieldsToTest = theForm.getElementsByClassName('requiredField');
	var fieldName;
	for (var a=0; a<fieldsToTest.length; a++) {
		if (fieldsToTest[a].tagName=='TEXTAREA' || (fieldsToTest[a].tagName=='INPUT' && fieldsToTest[a].type=='text')) {
			if (fieldsToTest[a].value=='') {
				fieldName = fieldsToTest[a].name.replace(/_Primary$/g, '').replace(/_/g, ' ');
				return focusAlertStop(fieldsToTest[a], 'Please fill in this required field:\n'+fieldName);
			}
		} else if (fieldsToTest[a].tagName=='INPUT' && fieldsToTest[a].type=='radio') {
			var radioGroup = theForm[fieldsToTest[a].name];
			var noneChecked = true;
			for (var b=0; b<radioGroup.length; b++) {
				if (radioGroup[b].checked) {
					noneChecked = false;
				}
			}
			if (noneChecked) {
				fieldName = fieldsToTest[a].name.replace(/_Primary$/g, '').replace(/_/g, ' ');
				return focusAlertStop(radioGroup[0], 'Please select one of these options:\n'+fieldName);
			}
		}
	}
	return true;
};