jQuery.fn.field = function( fieldname )
{
	return $( this ).find( ':input[name=' + fieldname + ']' );
};

jQuery.fn.validator = function( fn )
{
	if( fn ) $( this ).data( 'validator', fn );
	else return $( this ).data( 'validator' );
};

$( document ).ready( function()
{
	$( 'form' ).bind( 'submit', function()
	{
		$( this ).find( 'ul.errors li' ).remove();
		
		var errors = new Array();
		
		$( this ).find( ':input' ).each( function()
		{
			var validator = $( this ).validator();
			
			if( validator )
			{
				var fielderrors = validator( $( this ) );
				if( fielderrors ) errors = errors.concat( fielderrors );
			}
		} );
		
		if( errors.length == 0 )
			return true;
		else
		{
			if( $( this ).find( 'ul.errors' ).length == 0 )
				$( this ).prepend( '<ul class="errors"></ul>' );
			
			for( var i in errors )
				$( this ).find( 'ul.errors' ).append( '<li>' + errors[i] + '</li>' );
			
			$( this ).find( 'ul.errors' ).hide().slideDown();
			return false;
		}
	} );
} );
