/*=============================================================================

			 	 TITLE:		NetMediaOne - Simple Form Validation
		  MODIFIED:		2008.01.17
		 AUTHOR(S): 	Graham Wheeler - NetMediaOne - www.netmediaone.com
		  REQUIRES:		NetMediaOne Core 1.2
									jQuery 1.2

=============================================================================*/

NMO.Validator = {
	
	// 
	//	Set to "alert" to have errors displayed in a javascript alert window, or
	//	"inline" to have them appear next to the referenced form fields
	//
	notificationMethod: "inline",
	
	// 
	//	Set to "before" or "after" to set the position of inline error messages
	//	relative to the referenced form field
	//
	inlineNotificationPosition: "after",
	
	isValid: function(formElement) {
		
		var errors = [];
		
		//
		//	Verify that the input is not empty
		//
		$("input.Required", formElement).each( function() {
			if ( this.value == "" ) {
				var el = this;
				errors.push( { 
					field: el,
					alertMessage: el.name + " must be provided",
					inlineMessage: "this field must be provided"
				} );
			}
		} );

		
		//
		//	Verify that the input is in the correct format for an email address
		//
		$("input.Email", formElement).each( function() {
			if ( !/^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/.test(this.value) ) {
				var el = this;
				errors.push( { 
					field: el,
					alertMessage: el.name + " must contain an email address in a valid format",
					inlineMessage: "this field must contain an email address in a valid format"
				} );
			}
		} );


		//
		//	Verify that the input value is a positive number
		//
		$("input.IsPositiveNumber", formElement).each( function() {
			var num = parseInt( this.value, 10 );
			if ( isNaN(num) || num < 1 ) {
				var el = this;
				errors.push( { 
					field: el,
					alertMessage: el.name + " must be a number greater than zero",
					inlineMessage: "this field must be a number greater than zero"
				} );
			}
		} );
		
		
		//
		//	Verify that the selected value of a select box is NOT an invalid option
		//
		$("select.CheckForValidSelection", formElement).each( function() {
			var opt = this.options[this.selectedIndex];
			if ( $(opt).hasClass("InvalidSelection") ) {
				var el = this;
				errors.push( { 
					field: el,
					alertMessage: "'" + opt.value + "' is not a valid option for " + el.name,
					inlineMessage: "'" + opt.value + "' is not a valid option for this field"
				} );
			}
		} );
		
		
		if ( errors.length > 0 ) {
			
			if ( NMO.Validator.notificationMethod == "alert" ) {
				
				var eList = "Please correct the following errors:\n\n";
				
				for ( var i = 0; i < errors.length; i++ ) {
					eList += errors[i].alertMessage + "\n";
				}
				alert( eList);
				
			} else if ( NMO.Validator.notificationMethod == "inline" ) {
				
				// remove previous error messages if any exist
				$(".ValidationErrorMessage").remove();
				
				for ( var i = 0; i < errors.length; i++ ) {
					
					var field = $( errors[i].field );
					var message = '<span class="ValidationErrorMessage">'+errors[i].inlineMessage+'</span>';
					if ( NMO.Validator.inlineNotificationPosition == "before" ) { field.before(message); }
					else if ( NMO.Validator.inlineNotificationPosition == "after" ) { field.after(message); }

				}
				
			}
			
			return false;
			
		} else {
		
			return true;
			
		}

	},
	
	init: function() {
		
		$("form.Validated").submit( function() {

			return NMO.Validator.isValid(this);

		} );
		
	}
	
};

jQuery( function() { NMO.Validator.init(); } );
