/**
 * Framework for validating forms
 * @author Luke Sneeringer
 * @author Copyright 2008, Live Oak 360, Inc.
 */
Event.observe(document, 'dom:loaded', function() {
	// set up validators
	validators = $H({
		'.v-numeric': {
			regex: /^[0-9]+$/,
			message: 'Please provide a number.'
		},
		'.v-alpha': {
			regex: /^[a-zA-Z]+$/,
			message: 'Please provide only alphabetic characters.'
		},
		'.v-alphanumeric': {
			regex: /^[A-Za-z0-9]+$/,
			message: 'Please provide only alphanumeric characters.'
		},
		'.v-zip': {
			regex: /^[0-9]{5}$/,
			message: 'Please provide a valid ZIP code.'
		},
		'.v-ip': {
			regex: /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/,
			message: 'Please provide a valid IP address.'
		},
		'.v-email': {
			regex: /^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/,
			message: 'Please provide a valid e-mail address.'
		},
		'.v-phone': {
			regex: /^[\D]{0,1}[\d]{3}[\D]{0,2}[\d]{3}[\D]{0,1}[\d]{2}[\D]{0,1}[\d]{2}$/,
			message: 'Please provide a valid phone number.'
		},
		'.v-domain_name': {
			regex: /^[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/,
			message: 'Please provide a valid domain name.'
		},
		'.v-length': {
			regex: /^.+$/,
			message: 'Required field; cannot be empty.'
		},
		'.v-on': {
			regex: /^on$/,
			message: 'You must acknowledge the %s.'
		}
	});
	
	// iterate through forms and cause my function to take effect
	// when those forms are submitted
	$$('form').each(function(form_element) {
		form_element.observe('submit', function(ev) {
			ev.stop(); // stop submission until this completes
			ok = true;
			
			// remove any current error messages from the DOM
			form_element.select('.v-error').each(function(error_elem) {
				error_elem.remove();
			});
			
			// deflag any fields that are flagged as being errant
			form_element.select('.v-errant').each(function(error_elem) {
				error_elem.removeClassName('v-errant');
			});
			
			// validate all form elements that are flagged for validation
			validators.each(function(validator) {
				form_element.select(validator.key).each(function(validated_elem) {
					if (!validated_elem.value.match(validator.value.regex)) {
						ok = false;
						err = validator.value.message.replace('%s', validated_elem.name.replace('_', ' '));
						
						// create element for the error message
						div = new Element('div', {
							'class': 'v-error'
						}).update(err);
						
						// set an error class to the element itself
						validated_elem.addClassName('v-errant');
												
						// insert the errata element
						new Insertion.After(validated_elem, div); // deprecated as of Prototype 1.6.0
						
						// theoretically, this is the Prototype 1.6 syntax...but it doesn't seem to actually work, and the
						// Prototype documentation doesn't give me any useful example or tell me what I am doing wrong...
						// div.insert(validated_elem, {
						// 	position: 'after'
						// });
					}
				});
			});
						
			// submit the form if everything went okay
			if (ok) {
				form_element.submit();
			}
		});
	});
});