$.fn.cloneInput=function(name)
{
	var content=this.val();
	var stage=this.hasClass('mid');
	if ((content!='') && (!stage))
	{
		var clone=cloneInput('invite');
		this.next().after('<br/>').after(clone).after('<label>');
		this.addClass('mid');
	}
	else if ((content=='') && (stage))
	{
		if (!this.prev('label').hasClass('first')) this.prev('label').remove();
		this.next('br').remove().end()
			.remove();
	}
};

function cloneInput(name)
{
	var input='<input type="text" name="'+name+'[]"/>';
	input=$(input).keyup(function(){
		$(this).cloneInput();
	});
	return input;
};

$(document).ready(function(){
	$('textarea').attr('name','').hide().after('<br/>').after(cloneInput('invite')).after('<label class="first">Email address');
	$('span.clone').text('just type an email address in the box below. A new box will be created for every address, so you can add as many people as you like.');
});