function forminit(theFormID){
	/*
	if ( document.getElementById(theFormID + '_control') ) {
		var theSpamControl = document.getElementById(theFormID + '_control');
		theSpamControl.parentNode.removeChild(theSpamControl);
	}*/
	
	if ( document.getElementById && document.getElementById(theFormID) ) {
		theForm = document.getElementById(theFormID);
		theForm.validate = function(){
			return domValidateForm(this,false);
		};
		theForm.oldsubmit = ( typeof theForm.onsubmit == 'function' ) ? theForm.onsubmit : function(){return true;};
		theForm.onsubmit = function() {
			if ( domValidateForm(this,true) ) {
				return this.oldsubmit();
			} else return false;
		}
	}
	
	/*
		Developed by Robert Nyman, http://www.robertnyman.com
		Code/licensing: http://code.google.com/p/getelementsbyclassname/
	*/	
	var getElementsByClassName = function (className, tag, elm){
		if (document.getElementsByClassName) {
			getElementsByClassName = function (className, tag, elm) {
				elm = elm || document;
				var elements = elm.getElementsByClassName(className),
					nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
					returnElements = [],
					current;
				for(var i=0, il=elements.length; i<il; i+=1){
					current = elements[i];
					if(!nodeName || nodeName.test(current.nodeName)) {
						returnElements.push(current);
					}
				}
				return returnElements;
			};
		}
		else if (document.evaluate) {
			getElementsByClassName = function (className, tag, elm) {
				tag = tag || "*";
				elm = elm || document;
				var classes = className.split(" "),
					classesToCheck = "",
					xhtmlNamespace = "http://www.w3.org/1999/xhtml",
					namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
					returnElements = [],
					elements,
					node;
				for(var j=0, jl=classes.length; j<jl; j+=1){
					classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
				}
				try	{
					elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
				}
				catch (e) {
					elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
				}
				while ((node = elements.iterateNext())) {
					returnElements.push(node);
				}
				return returnElements;
			};
		}
		else {
			getElementsByClassName = function (className, tag, elm) {
				tag = tag || "*";
				elm = elm || document;
				var classes = className.split(" "),
					classesToCheck = [],
					elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
					current,
					returnElements = [],
					match;
				for(var k=0, kl=classes.length; k<kl; k+=1){
					classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
				}
				for(var l=0, ll=elements.length; l<ll; l+=1){
					current = elements[l];
					match = false;
					for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
						match = classesToCheck[m].test(current.className);
						if (!match) {
							break;
						}
					}
					if (match) {
						returnElements.push(current);
					}
				}
				return returnElements;
			};
		}
		return getElementsByClassName(className, tag, elm);
	};
	
	function domValidateForm(thisForm,showAlerts) {
		var theReturn = true;
		var thisClass = "";
		var thisName = "";
		var thisValue = "";
		var emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var dateReg = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2,4}$/;
		var phoneReg = /^([0-9+ ()-])+/;
		var intReg = /^\d+$/;
		var handled = {};
		var checked = false;
		for ( var r = 0 ; r < thisForm.elements.length ; r++ ) {
			thisClass = " " + thisForm.elements[r].className + " ";
			thisSpan = thisForm.elements[r].parentNode.getElementsByTagName('span');
			thisName = ( thisSpan.length > 0 ) ? 'The field labeled "' + thisSpan[0].innerHTML + '"' : thisForm.elements[r].name;
			thisAlert = ( thisForm.elements[r].title != '' ) ? thisForm.elements[r].title : '';
			if ( thisClass.indexOf(' required ') > -1 ) {
				if ( ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' || thisForm.elements[r].type == 'textarea' || thisForm.elements[r].type == 'file' ) && thisForm.elements[r].value.length == 0 ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' is required before submitting the form');
					}
					theReturn = false;
				} else if ( thisForm.elements[r].type == 'select-one' && thisForm.elements[r].options[thisForm.elements[r].selectedIndex].value == '' ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' is required before submitting the form');
					}
					theReturn = false;
				} else if ( thisForm.elements[r].type == 'select-multiple' && thisForm.elements[r].selectedIndex == -1 ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' is required before submitting the form');
					}
					theReturn = false;
				} else if ( thisForm.elements[r].type == 'radio' || thisForm.elements[r].type == 'checkbox' ) {
					if ( typeof handled[thisForm.elements[r].name] == 'undefined' ) {
						checked = false;
						if ( typeof thisForm[thisForm.elements[r].name].length == 'undefined' ) {
							checked = ( thisForm.elements[r].checked == true );
						} else {
							for ( var e = 0 ; e < thisForm[thisForm.elements[r].name].length ; e++ ) {
								if ( thisForm[thisForm.elements[r].name][e].checked == true ) {
									checked = true;
									break;
								}
							}
						}
						if ( !checked ) {
							if ( showAlerts ) {
								alert(( thisAlert.length > 0 ) ? thisAlert : thisName+' is required before submitting the form');
							}
							theReturn = false;
						}
						handled[thisForm.elements[r].name] = true;
					}
				}
			}
			if ( thisClass.indexOf(' required-one ') > -1 && !checked ) {
				var theFields = getElementsByClassName("required-one","",thisForm);
				var passed = false;
				for ( var f = 0 ; f < theFields.length ; f++ ) {
					if ( 
							( typeof theFields[f].checked != 'undefined' && theFields[f].checked == true ) ||
							( ( theFields[f].type == 'text' || theFields[f].type == 'password' || theFields[f].type == 'textarea' || theFields[f].type == 'file' ) && theFields[f].value.length > 0 ) ||
							( theFields[f].type == 'select-one' && theFields[f].options[theFields[f].selectedIndex].value != '' ) ||
							( theFields[f].type == 'select-multiple' && theFields[f].selectedIndex != -1 )
					) {
						passed = true;
						break;
					}
				}
				if ( !passed ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' or one of the other options must be chosen');
					}
					theReturn = false;
				}
				checked = true;
			}
			if ( thisClass.indexOf(' integer ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( !intReg.test(thisForm.elements[r].value) ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' must be a proper number');
					}
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' numeric ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( thisForm.elements[r].value.length > 0 && ( parseFloat(thisForm.elements[r].value) != thisForm.elements[r].value-0 ) ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' must be a proper number');
					}
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' email ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( !emailReg.test(thisForm.elements[r].value) ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' must be a valid email address to submit the form');
					}
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' phone ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( !phoneReg.test(thisForm.elements[r].value) ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' must be a valid phone number like "(555) 555-5555"');
					}
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' date ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( !dateReg.exec(thisForm.elements[r].value) ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' must be a valid date to submit the form, like "mm/dd/yy"');
					}
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' confirm ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( r > 0 && thisForm.elements[r].value != thisForm.elements[r-1].value ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' and ' + prevName + ' must have the same values');
					}
					theReturn = false;
				}
			}
			prevName = thisName;
		}
		return theReturn;
	}
}
window.addEvent('domready',function(){
	var theForms = document.getElementsByTagName('form');
	for ( var f = 0 ; f < theForms.length ; f++ ) {
		if ( theForms[f].id ) {
			forminit(theForms[f].id);
		}
	}
	
	var openWin = function ( windowURL, windowName, windowFeatures ) { 
		return window.open( windowURL, windowName, windowFeatures ) ; 
	} 
	
	$$('a[class^=popup_]').addEvent('click',function(e){
		var width = this.className.split('_')[1];
		var height = this.className.split('_')[2];
		newWindow = openWin( this.href+'&method=ajax', 'popup', 'width='+width+',height='+height+',toolbar=0,location=0,directories=0,status=1,menuBar=0,scrollBars=1,resizable=1' ); 
		newWindow.focus();
		e = new Event(e).stop();
	});
});
