// a regular expression to test for Base64 data
var BASE64_DATA = /^data:.*;base64/i;
// path to the PHP module that will decode the encoded data
var base64Path = window.WP_PLUGIN_URL + "/food-captcha/base64.php";
function fixBase64( img ) {  
	// check the image source  
	if (BASE64_DATA.test(img.src)) {    
		// pass the data to the PHP routine    
		img.src = base64Path + "?" + img.src.slice(5);  
	}
};



// fix images on page load
onload = function() { 
	for ( var i = 0; i < document.images.length; i++ ) {    
		fixBase64( document.images[i] );  
	}
};



function updateCaptcha( postID, prefix ) {
// 	Update the code that displays the anti-spam image
	$( prefix + "captcha-image-" + postID ).src = window.WP_PLUGIN_URL + "/food-captcha/images/transparent.gif";
	new Ajax.Request( window.WP_PLUGIN_URL + "/food-captcha/food-captcha.php?antinew", {
		method: "get",
		onSuccess: function( transport ) {
			results = eval( "(" + transport.responseText + ")" );
			$( prefix + "captcha-matchthis-" + postID ).value = results["id"];
			$( prefix + "captcha-image-" + postID ).src = results["image"];
			afterUpdate( postID, prefix );
		} 
	} );
}



function afterUpdate( postID, prefix ) {
	if( typeof fixBase64 == "function" ) {
		fixBase64( $( prefix + "captcha-image-" + postID ) );
	}
}