
document.$recaptcha_valid = false;

function check_recaptcha(){
	
	if(document.$recaptcha_valid){
		validate_form();
		return true;
	}
	
	var $url = 'includes/recaptcha.php';
	
	var $challenge = $F('recaptcha_challenge_field');
	var $response = $F('recaptcha_response_field');
	
	
	var params = 	'recaptcha_challenge_field=' + $challenge +
					'&amp;recaptcha_response_field=' + $response;
	
	new Ajax.Request($url,
	{
		method:'post',
		parameters: {
				recaptcha_challenge_field: $challenge,
				recaptcha_response_field:  $response
		}, 
	    onSuccess: function(transport){
	      
	      var $response = transport.responseText || "";
	      
	      if($response == 'true'){
	      	// continue validating form
	      	document.$recaptcha_valid = true;
	      	
	      	$('recaptcha').innerHTML = '<h2>CAPTCHA Successfully Entered.</h2>';
	      	//$('recaptcha_response_field').disable();
	      	
	      	validate_form();
			
	      }else{
	      	// continue validating form
	      	recaptcha_error();
	      }
	      
	    },
	    onFailure: function(){ alert('Something went wrong...') }
	});
}

function recaptcha_error(){
	alert('CAPTCHA does not match. Please Try again');
	try{
			$publickey = "6LcCjwcAAAAAAJ4CqvEu7sJleSAT18o82FG_Q_LF";
			Recaptcha.create($publickey,
			   "recaptcha", 
			   {  
			     theme: "clean",
			     callback: Recaptcha.focus_response_field
			   }
			 );
		}catch($e){
			alert('Error: ' + $e.message);
		}
}

function loadEvent(){
		try{
			$publickey = "6LcCjwcAAAAAAJ4CqvEu7sJleSAT18o82FG_Q_LF";
			Recaptcha.create($publickey,
			   "recaptcha", 
			   {  
			     theme: "clean",
			     callback: function(){
			     	// do nothing.
			     }
			   }
			 );
		}catch($e){
			alert('Error: ' + $e.message);
		}
}	