// Registration/Signup -- sharehub

var RecaptchaOptions = {
   theme : 'blackglass'
};

function showSignupError(message)
{
	$("div#registrationError").text(message);
	$("div#registrationError").fadeIn();
	$('html,body').animate({scrollTop: 0}, 500);  /* make sure they see it */
}

function validateUsername()
{
	var username = $("input#userId").val();
	
	if ( username.match(/[\s\W_]/g) )
	{
		showSignupError("Username contains spaces or other invalid characters.");
		return false;
	}
	else if ( username.length < 3 )
	{
		showSignupError("Username must be at least 3 characters long.");
		return false
	}
	else if ( username.length > 32 )
	{
		showSignupError("Username can be no more than 32 characters.");
		return false;
	}
	else
	{
		return true;
	}
}

function validatePassword()
{
	if ( $("input#password1").val() != $("input#password2").val() ) {
		showSignupError("Password confirmation failed. Please ensure both password fields match.");
		return false;
	} else if ( $("input#password1").val().length < 6 ) {
		showSignupError("Password must be between 6 and 32 characters in length.");
		return false;
	} else if ( $("input#password1").val().length > 32 ) {
		showSignupError("Password must be between 6 and 32 characters in length.");
		return false;
	} else {
		return true;
	}
}

function validateRegistraton()
{
	if ( !validateUsername() )
		return false;
	
	if ( !validatePassword() )	
		return false;

	// quickly loop through the form to make sure that they've filled in everything
	$("form#registrationForm input,select").each(function(e) { 
		$(this).css("border","1px inset #ddd");
		if ( $(this).val() == "" )
		{
			showSignupError("Please fill in all fields.");
			$(this).css("border","1px solid #f00");
			$(this).select();
		}
	});
	
	if ( $("div#registrationError").is(':visible') )
	{
		return false;
	}
	else
		return true;
}


$(document).ready(function() { 
	$("input#userId").bind("change", function(e) {
		$("#userAvailabilityCheck").hide();
		if ( validateUsername() )
		{
			$("#userAvailabilityCheck").removeClass("checkPassed").removeClass("checkFailed");
			$("#userAvailabilityCheck").text("Checking availability...");
			$("#userAvailabilityCheck").fadeIn("fast");
			
			$.post("components/username_check.php", { u: $(this).val()  }, function(data) {
				//console.log(data);
				if ( data.indexOf("UNAVAILABLE") < 0 )
				{
					$("#userAvailabilityCheck").removeClass("checkFailed").addClass("checkPassed");
					$("#userAvailabilityCheck").addClass("checkPassed");
					$("#userAvailabilityCheck").text("Cool. \"" + $("input#userId").val() + "\" is available!");
					$("div#registrationError").hide();
				}
				else
				{
					$("#userAvailabilityCheck").removeClass("checkPassed").addClass("checkFailed");
					$("#userAvailabilityCheck").text("Sorry, but that name is unavailable.");
					$("input#userId").select();
				}
			}, "text");
			
		}
	});
	
	$("form#registrationForm").submit(function(e) {
		$("div#registrationError").hide();
		return validateRegistraton();
	});
});
