function ValidateForm( root )
{
	var success = true;

	root.find( ".required" ).each( function( i )
	{
		if( ! IsValid( this ) )
		{
			MakeRequired( this, true );

			success = false;
		}
	});

	if( ! success )
	{
		InitForm();
	}

	return( success );
}

function IsValid( obj )
{
	var valid = true;

	if( $( obj ).hasClass( "email" ) )
	{
		if( ! IsValidEmail( obj.value ) )
		{
			valid = false;
		}
	}

	if( $( obj ).is( "[type='radio']" ) )
	{
		valid = ( $( "[name='" + $( obj ).attr( "name" ) + "']:checked" ).length > 0 );
	}
	else
	{
		if( obj.value == "" || obj.selectedIndex == 0 )
		{
			valid = false;
		}
	}

	return( valid );
}

function Validate( obj, valid )
{
	if( valid )
	{
		ClearRequired( obj );
	}
	else
	{
		MakeRequired( obj, false );
	}
}

function MakeRequired( obj, isOnSubmit )
{
	if( isOnSubmit )
	{
		FlashyClass( $( obj ), 50, 5 );
		FlashyClass( $( "label[for='" + $( obj ).attr( "id" ) + "']" ), 50, 5 );
	}
	else
	{
		$( obj ).addClass( "required_on" );
		$( "label[for='" + $( obj ).attr( "id" ) + "']" ).addClass( "required_on" );
	}
}

function ClearRequired( obj )
{
	$( obj ).removeClass( "required_on" );
	$( "label[for='" + $( obj ).attr( "id" ) + "']" ).removeClass( "required_on" );

	ResetFlashyClass();
}

function IsValidEmail( value )
{
	var ereg = new RegExp( "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$" );

	return( ereg.test( value ) );
}

function InitForm()
{
	$( "input.required, textarea.required" ).each( function( i )
	{
		$( this ).keyup( function()
		{
			Validate( this, IsValid( this ) );
		});
	});

	$( "input[type='radio'].required" ).each( function( i )
	{
		$( this ).click( function()
		{
			$( "input[name='" + $( this ).attr( "name" ) + "']" ).each( function()
			{
				Validate( this, IsValid( this ) );
			});
		});
	});

	$( "select.required" ).each( function( i )
	{
		$( this ).change( function()
		{
			Validate( this, IsValid( this ) );
		});
	});
}

var animatedObj = new Array();

function FlashyClass( obj, speed, frequency )
{
	var index = animatedObj.length;

	animatedObj[ index ] = obj;
	animatedObj[ index ].addClass( "required_on" );

	for( var t = 1; t <= ( frequency * 2 ); t += 2 )
	{
		setTimeout( "animatedObj[ " + index + " ].removeClass( 'required_on' );", t * speed );
		setTimeout( "animatedObj[ " + index + " ].addClass( 'required_on' );", ( t + 1 ) * speed );
	}
}

function ResetFlashyClass()
{
	animatedObj = new Array();
}