// Adapted from http://snipplr.com/view/15563/jquery-validating-recaptcha-with-ajax/

// Validate the Recaptcha' Before continuing with POST ACTION
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: "/ajax/recaptcha.php",
		data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
		async: false
	}).responseText;

	//console.log( html );
	if(html == "success") {
		//Add the Action to the Form
		//$("form").attr("action", "http://action/to/the/form_handler");

		//Indicate a Successful Captcha
		//$("#captchaStatus").html("Success!");

		// Uncomment the following line in your application
		return true;
	} else {
		//$("#captchaStatus").html("The security code you entered did not match. Please try again.");
		Recaptcha.reload();
		return false;
	}
}

function ValidateCaptchaMsg(msg)
{
	// if there are other errors then do not test the catcha out yet, 
	// as the user would be forced to type in a new captcha when they try to fix the other errors on the form
	if (msg != '')
		return "";

	var captcha_msg = "";
	var is_valid_recaptcha = ValidateCaptcha();
	if (!is_valid_recaptcha)
		captcha_msg += "Please enter the correct security code.\n\n";
	return captcha_msg;
}

