/**
* Adds onclick event to the save search prefs buttons
*
* @param	string	The ID of the button that fires the search prefs
*/
function AJAX_Captcha_Init()
{
	if (AJAX_Compatible && (typeof disable_ajax == 'undefined' || disable_ajax < 2) && fetch_object('captcha'))
	{
		fetch_object('captcha').onclick = AJAX_Captcha.prototype.image_click;
		fetch_object('captcha').style.cursor = pointer_cursor;
		fetch_object('captchatxt').style.display = '';
	}
};

/**
* Class to handle saveing search prefs
*
* @param	object	The form object containing the search options
*/
function AJAX_Captcha()
{
	// AJAX handler
	this.xml_sender = null;

	// Imagehach
	this.hash = '';

	// Closure
	var me = this;

	/**
	* OnReadyStateChange callback. Uses a closure to keep state.
	* Remember to use me instead of this inside this function!
	*/
	this.handle_ajax_response = function()
	{
		if (me.xml_sender.handler.readyState == 4 && me.xml_sender.handler.status == 200)
		{
			fetch_object('progress_captcha').style.display = 'none';
			if (me.xml_sender.handler.responseXML)
			{
				var hash = me.xml_sender.fetch_data(fetch_tags(me.xml_sender.handler.responseXML, 'hash')[0]);
				if (hash)
				{
					fetch_object('hash').value = hash;
					fetch_object('captcha').src = '../image.php?hash=' + hash;
				}
			}

			if (is_ie)
			{
				me.xml_sender.handler.abort();
			}
		}
	}
};

/**
* Submits the form via Ajax
*/
AJAX_Captcha.prototype.fetch_image = function()
{
	fetch_object('progress_captcha').style.display = '';
	this.xml_sender = new AJAX_Handler(true);
	this.xml_sender.onreadystatechange(this.handle_ajax_response);
	this.xml_sender.send('../ajax.php?do=captcha&hash=' + this.hash, 'do=captcha&hash=' + this.hash);
};

/**
* Handles the form 'submit' action
*/
AJAX_Captcha.prototype.image_click = function()
{
	var Captcha = new AJAX_Captcha();
	Captcha.hash = fetch_object('hash').value;
	Captcha.fetch_image();
	return false;
};
