var name_error_string = "You must supply a name.<br>";
var email_error_string = "You must supply a valid email.<br>";
var phone_error_string = "You must supply a phone number.<br>";
var password_error_string = "You must supply a password<br>longer than 3 characters.<br>";
var confirm_error_string = "Your passwords do not match.<br>";

$(function()
{
	$( ".menu_button" ).mouseover( function(){ $( this ).addClass(    "over" ) } );
	$( ".menu_button" ).mouseout(  function(){ $( this ).removeClass( "over" ) } );
	
	$( "#signup_error" ).hide( );
	
	$( "#signup_form" ).submit( function( )
	{
		check_name(             "#signup_name"     );
		check_email(            "#signup_email"    );
		check_phone(            "#signup_phone"    );
		check_password(         "#signup_password" );
		check_confirm_password( "#signup_confirm"  );
		if( name_error_string.length     > 0 ||
				email_error_string.length    > 0 ||
				phone_error_string .length   > 0 ||
				password_error_string.length > 0 ||
				confirm_error_string.length  > 0 )
		{
			$( "#" + $( "#signup_name" ).attr( "id" ) + "_error" ).html( name_error_string );
			$( "#" + $( "#signup_email" ).attr( "id" ) + "_error" ).html( email_error_string );
			$( "#" + $( "#signup_phone" ).attr( "id" ) + "_error" ).html( phone_error_string );
			$( "#" + $( "#signup_password" ).attr( "id" ) + "_error" ).html( password_error_string );
			$( "#" + $( "#signup_confirm" ).attr( "id" ) + "_error" ).html( confirm_error_string );
			$( "#signup_error" ).show( "slow" );
			return false;
		}
		else
		{
			return true;
		}
	});
	
	$( "#signup_name" ).blur(     function( ){ check_name(             "#signup_name"     ); } );
	$( "#signup_email" ).blur(    function( ){ check_email(            "#signup_email"    ); } );
	$( "#signup_phone" ).blur(    function( ){ check_phone(            "#signup_phone"    ); } );
	$( "#signup_password" ).blur( function( ){ check_password(         "#signup_password" ); } );
	$( "#signup_confirm" ).blur(  function( ){ check_confirm_password( "#signup_confirm"  ); } );
});

function check_name( self )
{
	if( $( self ).val( ).length > 0 )
	{
		$( self ).removeClass("error");
		$( self ).addClass("success");
		name_error_string = "";
	}
	else
	{
		$( self ).removeClass( "success" );
		$( self ).addClass( "error" );
		name_error_string = "You must supply a name.<br>";
	}
	$( "#" + $( self ).attr( "id" ) + "_error" ).html( name_error_string );
}

function check_email( self )
{
	/* var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; */
	var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if( re.test( $( self ).val( ) ) )
	{
		$( self ).removeClass("error");
		$( self ).addClass("success");
		email_error_string = "";
	}
	else
	{
		$( self ).removeClass( "success" );
		$( self ).addClass( "error" );
		email_error_string = "You must supply a valid email.<br>";
	}
	$( "#" + $( self ).attr( "id" ) + "_error" ).html( email_error_string );
}

function check_phone( self )
{
	if( $( self ).val( ).length > 9 )
	{
		$( self ).removeClass("error");
		$( self ).addClass("success");
		phone_error_string = "";
	}
	else
	{
		$( self ).removeClass( "success" );
		$( self ).addClass( "error" );
		phone_error_string = "You must supply a phone number.<br>";
	}
	$( "#" + $( self ).attr( "id" ) + "_error" ).html( phone_error_string );
}

function check_password( self )
{
	if( $( self ).val( ).length > 3 )
	{
		$( self ).removeClass( "error" );
		$( self ).addClass( "success" );
		password_error_string = "";
	}
	else
	{
		$( self ).removeClass( "success" );
		$( self ).addClass( "error" );
		password_error_string = "You must supply a password<br>longer than 3 characters.<br>";
	}
	$( "#" + $( self ).attr( "id" ) + "_error" ).html( password_error_string );
}

function check_confirm_password( self )
{
	if( $(self).val( ) == $( "#signup_password" ).val( ) )
	{
		$( self ).removeClass( "error" );
		$( self ).addClass( "success" );
		confirm_error_string = "";
	}
	else
	{
		$( self  ).removeClass( "success" );
		$( self  ).addClass( "error" );
		confirm_error_string = "Your passwords do not match.<br>";
	}
	$( "#" + $( self ).attr( "id" ) + "_error" ).html( confirm_error_string );
}
