/**
 * A thing I knocked together to make client-side form validation quicker and
 * easier to set up.
 *
 * Simply include this script on a page (and the jQuery lib, of course), and
 * then assign classes to form controls as appropriate:
 *
 * validate-required
 * validate-email
 * validate-zip
 * validate-zip4 (tolerates optional Zip+4)
 * validate-url
 * validate-phone (xxx-xxx-xxxx)
 * validate-posint (positive integers)
 * etc.
 *
 */
$( function() {

	var emailRegEx = new RegExp( '^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$', 'i' );
	var urlRegEx = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
	var zipRegEx = new RegExp( '^[0-9]{5}$' );
	var zip4RegEx = new RegExp( '^[0-9]{5}(-[0-9]{4})?$' );
	var phoneRegEx = new RegExp( '^[2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4}$' );
	var posintRegEx = new RegExp( '^[1-9][0-9]*$' );

	$("form").submit( function( e ) {
	
		var i = null;
		var $controls = $( "input,select,textarea", $(this) );
		var $thisControl = null;
		var thisValue = null; 

		for ( i = 0; i < $controls.length; i++ ) {

			$thisControl = $( $controls.get(i) );
			thisValue = $thisControl.val();

			if ( $thisControl.hasClass("validate-required") ) {
				if ( thisValue === "" ) {	
					$thisControl.focus();
					alert( "This field is required." );
					return false;
				}
			}
			
			if ( $thisControl.hasClass("validate-email") ) {
				if ( thisValue !== "" && !emailRegEx.test( thisValue ) ) {
					$thisControl.focus();
					alert( "This is not a valid email address." );
					return false;
				}
			}
			
			if ( $thisControl.hasClass("validate-zip") ) {
				if ( thisValue !== "" && !zipRegEx.test( thisValue ) ) {
					$thisControl.focus();
					alert( "This is not a valid ZIP code." );
					return false;
				}
			}

			if ( $thisControl.hasClass("validate-zip4") ) {
				if ( thisValue !== "" && !zip4RegEx.test( thisValue ) ) {
					$thisControl.focus();
					alert( "This is not a valid ZIP code." );
					return false;
				}
			}
			
			if ( $thisControl.hasClass("validate-url") ) {
				if ( thisValue !== "" && !urlRegEx.test( thisValue ) ) {
					$thisControl.focus();
					alert( "This is not a valid URL." );
					return false;
				}
			}
			
			if ( $thisControl.hasClass("validate-phone") ) {
				if ( thisValue !== "" && !phoneRegEx.test( thisValue ) ) {
					$thisControl.focus();
					alert( "This is not a valid telephone number. Please use the format xxx-xxx-xxx." );
					return false;
				}
			}
			
			if ( $thisControl.hasClass("validate-posint") ) {
				if ( thisValue !== "" && !posintRegEx.test( thisValue ) ) {
					alert( "This field must contain a positive whole number." );
					return false;
				}
			}
		
		}
		
		return true;
	
	} );

} );

