function validate_email( email )
{
	apos = email.indexOf( "@" )
	dotpos = email.lastIndexOf( "." )
	
	if( apos <1 || dotpos-apos < 2 )
		return false
		
	else
		return true

} // validate_email

var validation = new Array()

$( document ).ready(

	function()
	{
		// !Check Email, Username
		$( 'input#email, input#username' ).blur(
			function()
			{
				var el = $( this )
				if( $( this ).val() == '' )
					el.removeClass( 'error' ).next( 'span.message' ).text( '' )
				
				else( $( this ).val() != '' )
				{
					//validate first
					if( $( this ).attr( 'id' ) == 'email' )
					{
						if( ! validate_email( $( this ).val() ) )
						{
							$( this )
								.addClass( "error" )
								.next( 'span.message' )
									.text( 'Invalid email address' )
							
							validation['email'] = false
							return false
						} // if
						
						else
						{
							$( this )
								.removeClass( "error" )
								.next( 'span.message' )
									.text( '' )
									
							validation['email'] = true
						}
					} // if
					
					//then check db
					$.post
					(
						"/ajax/user/registration_check/", 
						
						{ 
							type: $( this ).attr( 'id' ),
							value: $( this ).val()
							
						},
						
						function( result )
						{
							if( result == "in use" )
							{
								el
									.addClass( "error" )
									.next( 'span.message' )
										.text( el.attr( 'id' ).charAt(0).toUpperCase() + el.attr( 'id' ).substr(1) + ' is already in use.' )
									
									validation[ el.attr( 'id' ) ] = false
								
							} // if
							
							else
							{
								el
									.removeClass( "error" )
									.next( 'span.message' )
										.text( '' )
									
									validation[ el.attr( 'id' ) ] = true
							} // else
								
								
								
						} // function
					) // post
					
				} // else
					
			} // function
		) // blur
		
		// !Check password
		$( 'input#password1' ).blur(
			function()
			{
				if( $( this ).val().length < 6 && $( this ).val() != '')
				{
					$( this ).addClass( 'error' ).next( 'span.message' ).text( 'Password must be 6 characters or longer.' )
					validation['password1'] = false
				} // if
				
				else
				{
					$( this ).removeClass( 'error' ).next( 'span.message' ).text( '' )
					validation['password1'] = true
				} // else
				
				$( 'input#password2' ).blur()
			} // function
		) // blur
		
		// !Password matching
		$( 'input#password2' ).blur(
			function()
			{
				if( 
					$( this ).val() != '' && 
					$( 'input#password1' ).val() != '' && 
					$( 'input#password1' ).val().length > 5
				)
				{
					if( $( this ).val() != $( 'input#password1' ).val() )
					{
						$( this ).addClass( 'error' ).next( 'span.message' ).text( 'Passwords do not match.' )
						validation['password2'] = false
					} // if
					
					else
					{
						$( this ).removeClass( 'error' ).next( 'span.message' ).text( '' )
						validation['password2'] = true
					} // else
				} // if
			} // function
		) // blur
		
		
		// !Validate on submit
		$( '#registration form' ).submit(
			function()
			{
				var errors = new Array()
				
				// not that anyone would do this, but this line makes sure they didn't do some inline js and screw with the email address
				validation['email'] = validate_email( $( 'input#email' ).val() )
				
				for( i in validation )
				{
					if( ! validation[i] )
						errors[errors.length] = i
				} // for
				
				if( errors.length == 0 )
					return true
				
				else
				{
					for( i in errors )
					{
						var el = $( 'input#' + errors[i] )

						if( ! el.hasClass( 'error' ) )
							el.addClass( 'error' )
						
						if( el.next( 'span.message' ).text() == '' || el.next( 'span.message' ).text().length == 0 )
							el.next( 'span.message' ).text( 'Invalid ' + errors[i]  )
						
					} // for
					
					$( 'input[type=submit]' ).next( 'span.message' ).text( 'Please fix errors before continuing' ).fadeTo( 3000, 1 ).fadeOut()
					
					return false
				} // else

			} // function
		) // submit
	} // function
) // ready
