
function validateCaptcha()
{
	challengeField = $("input#recaptcha_challenge_field").val();
	responseField = $("input#recaptcha_response_field").val();
	console.log(challengeField);
	console.log(responseField);
	//return false;
	var html = $.ajax({
		type: "POST",
		url: "includes/ajax.recaptcha.php",
		data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
		async: false
		}).responseText;
	
	if(html.replace(/^\s+|\s+$/, '') == "success") {
		//Add the Action to the Form
		$("form").attr("action", "verify.php");
		//Indicate a Successful Captcha
		$("#captchaStatus").html("Captcha entered successfully");
		return true;
	} else {
		$("#captchaStatus").html("The security code you entered did not match. Please try again.");
		Recaptcha.reload();
		return false;
	}
}	


$(document).ready(function() {

		//the min chars for username
		var min_chars = 3;

		//result texts
		var characters_error = 'Minimum amount of chars is 3';
		var checking_html = 'Checking...';
		//validateCaptcha();
		//when button is clicked
		$('#username').focusout(function(){
			//run the character number check
			if($('#username').val().length < min_chars){
				//if it's bellow the minimum show characters_error text '
				$('#username_availability_result').html(characters_error);
			}else{
				//else show the cheking_text and run the function to check
				$('#username_availability_result').html(checking_html);
				check_availability();
			}
		});

  });

//function to check username availability
function check_availability(){

		//get the username
		var username = $('#username').val();

		//use ajax to run the check
		$.post("includes/check_username.php", { username: username },
			function(result){
				//if the result is 1
				if(result == 1){
					//show that the username is available
					$('#username_availability_result').html(username + ' is Available');
					$('#username_availability_result').removeClass('username_unavailable').addClass('username_available');  
					return true;
				}else{
					//show that the username is NOT available
					$('#username_availability_result').html(username + ' is not Available');
					$('#username_availability_result').removeClass('username_available').addClass('username_unavailable');  
					return false;
				}
		});
}

