IWP_Module_EmailToFriend = function (uniqueId, captchaImageAction) {
	var self = this;
	
	this.uniqueId = uniqueId;
	this.captchaImageAction = captchaImageAction;
	
	this.initialised = false;
	this.form = null;
	
	this.initialise = function () {
		//	initalise
		
		self.form = $('#EmailToFriendForm_' + self.uniqueId);
		if (!self.form) {
			return;
		}
		
		$('.requestNewCaptchaLink', self.form).click(self.requestNewCaptchaClick);
		
		//@TODO Change to using interspireAjaxForm, this was written before the jquery plugin
		self.form.ajaxForm({
			beforeSubmit: self.beforeSubmit,
			success: self.response
		});
		
		self.initialised = true;
	};
	
	this.requestNewCaptchaClick = function () {
		$(this).blur();
		$.post(self.captchaImageAction, self.requestNewCaptchaResponse);
		return false;
	};
	
	this.requestNewCaptchaResponse = function (xml, result) {
		if (result == 'success') {
			var html = $('captchaimg', xml).text();
			if (html) {
				$('.captchaImageContainer', self.form).html(html);	//	replace the img html
				$(':input[name=captcha]', self.form).val('');		//	clear the captcha text input
			}
		}
	};
	
	this.clearMessages = function () {
		//	removes highlights / messages from the form which are otherwise not removed by a browser's form.reset() function
		
		$(':input', self.form).each(function(){
			//	remove highlights from individual elements
			$(this).parents('dd').eq(0).removeClass('Highlight');
			$('.HighlightMessage_' + $(this).attr('name'), self.form).css('display', 'none');
		});
		
		//	remove general messages
		$('.FormMessage, .FormError', self.form).css('display', 'none');
	};
	
	this.disabledElements = [];
	
	this._disableForm = function () {
		self.disabledElements = [];
		$(':input', self.form).each(function(){
			if (!this.disabled) {
				this.disabled = true;
				self.disabledElements[self.disabledElements.length] = this;
			}
		});
	}
	
	this.disableForm = function () {
		window.setTimeout(self._disableForm, 1);
	};
	
	this.enableForm = function () {
		$(self.disabledElements).each(function(){
			this.disabled = false;
		});
	}
	
	this.beforeSubmit = function () {
		self.clearMessages();
		self.disableForm();
	};
	
	this.response = function (xml, result) {
		self.enableForm();
		
		if (result == 'success') {
			$('message', xml).each(function(){
				var message = $(this).text();
				if (message) {
					$('.FormMessage', self.form).css('display', '').text(message);
				}
			});
			
			$('error', xml).each(function(){
				var message = $(this).text();
				if (message) {
					$('.FormError', self.form).css('display', '').text(message);
				}
			});
			
			$('field', xml).each(function(){
				var fieldResponse = $(this);
				var highlight = (fieldResponse.attr('highlight') == 'true');	//	xml, this comes through as a string, convert it
				var name = fieldResponse.attr('name');
				var message = fieldResponse.attr('message');
				
				if (highlight) {
					$(':input[name=' + name + ']', self.form).parents('dd').eq(0).addClass('Highlight');
				}
				
				if (message) {
					$('.HighlightMessage_' + name, self.form).css('display', '').text(message);
				}
			});
			
			if ($('status', xml).text() == 1) {
				//	clear the friend fields
				$(':input[name=friendname], :input[name=friendemail]').val('');
			}
			
			//	expect a new captcha image, the response callback will handle the rest
			self.requestNewCaptchaResponse(xml, result);
		}
	};
};
