
/* Start of validate.js */

(function(jQuery){
	jQuery.fn.validate = function(options) {
		var defaults = {
			// all the input fields we are going to validate
			// we don't have any defaults yet
		};

		var fields = jQuery.extend(defaults, options);

		// obj is the form we are validating
		var obj = jQuery(this);

		obj.submit(function() {

			// form is all good unless we later find a invalid field
			var allGood = true;

			try
			{
				// loop through the list of fields to validate
				for( var i in fields) {
					if( ! validateField(i) ) {
						allGood = false;
					}
				}

				if( allGood ) {
					return true;
				}
				else {
					return false;
				}
			}
			catch(e) {
				alert('An exception has occured: '+e);
				return false;
			}
		});

		// set the method of the form to post
		obj.attr('method','POST');

		validateField = function(i) {
			var el = jQuery('[name="'+i+'"]');
			var vl = el.val();

			// Make sure the name of the element exists in the html document
			if( typeof(el.attr('type')) != 'string' ) {
				alert('\''+i + '\' is an invalid name of an input. Please make sure the input field exists and is spelt correctly');
				return true;
			}

			// now that we got that element, lets see what type of element it is (radio, text, etc.)
			// if this is a radio, we need to rather get the value of the entry which is checked
			if( el.attr('type') == 'radio' ) {
				vl = jQuery('[name="'+i+'"]:checked').val() || '';
			}

			// trim trailing and leading spaces and newlines
			while(	vl.indexOf(' ') == 0 ||  (vl.length > 1 && vl.lastIndexOf(' ') == (vl.length - 1) ) ||
					vl.indexOf('\n') == 0 || (vl.length > 1 && vl.lastIndexOf('\n') == (vl.length - 1) ) ) {

				vl = vl.replace(/(^\s| $|^\n|\n$)/g,'');

			}

			// and put the new value back in the input text box
			//	but only if it is not a radio button
			if( el.attr('type') != 'radio' ) el.val( vl );

			// get the message to display for the invalid field or use a default one if it is not set.
			var ms = (fields[i].msg)?fields[i].msg:(typeof(fields[i])=='string'&&fields[i]!=''?fields[i]:'Field cannot be empty');

			// error message to display after the input field if the field was invalid
			ms = '<span class="'+i+'_errmsg validate_error">'+ms+'</span>';

			// remove the error message if there are any
			jQuery('.'+i+'_errmsg').remove();

			/* required may be a function that returns true or false
				depending on what the value of other fields are */
			if( typeof( fields[i].required ) == 'function' )
			{
				var req = fields[i].required();
			}
			else
			{
				var req = fields[i].required;
			}

			// if the input (text, radio, etc.) is empty and is a required field
			if( vl == '' && req !== false ) {
				// if this element is a radio button, then append the message to the last radio rather than each matched radio
				if( el.attr('type') == 'radio' ) {
					jQuery('[name="'+i+'"]:last').after(ms);
				}
				else {
					el.after(ms);
				}

				return false;
			}
			// else there was no value and it is not required
			else if( vl == '' )
			{
				return true;
			}
			// Else there was a value in that field
			else {
				if( fields[i].type == 'email' ) {
					if( ! validateEmail( vl ) ) {
						el.after(ms);
						return false;
					}
				}
				else if( fields[i].type == 'numbers' ) {
					if( vl != 0 && (vl/vl) != 1 ) {
						el.after(ms);
						return false;
					}
				}

				if( ! isNaN(fields[i].min) ) {
					if( vl.length < fields[i].min ) {
						el.after(ms);
						return false;
					}
				}

				if( ! isNaN(fields[i].max) ) {

					if( vl.length > fields[i].max ) {
						el.after(ms);
						return false;
					}
				}

				/* they have specified the value of this field must match another field */
				if( typeof(fields[i].equals) != 'undefined' && fields[i].equals != '' ) {
					var equals = jQuery('[name="'+fields[i].equals+'"]')
					/* make sure the element we are matching to (i.e. password again) exists */
					if( typeof( equals.attr('type') ) == 'string' ) {

						if( vl != equals.val() ) {
							el.after('<span class="'+i+'_errmsg validate_error">fields don\'t match</span>');
							return false;
						}
					}
					else {
						alert('"' + fields[i].equals + '" is an invalid element');
						return false;
					}
				}

				return true;
			}
		}
	};
})(jQuery);

/* End of validate.js */

