function flash(elm, color, delay)
{
	elm.animate({backgroundColor: color}, 100, false, function() {$(this).animate({backgroundColor: '#FFFFFF'}, (!delay)? 3000: delay)});
}


function add_comment()
{
	comment = {name : '', text : '', captcha : '', good_id : ''};
	error = false;
	for(i in comment)
	{
		tmp = $.trim($('#'+i).val());
		if (!tmp)
		{
			flash($('#'+i), '#ff6666');
			error = true;
		}
		else
			comment[i] = tmp;
	}
	if (error)
		return false;
	$.post('/add_comment/', comment, function(data) {
		if ((data.error) && (data.type=='captcha'))
		{
			$('.captcha_refresh').click();
			flash($('#captcha'), '#ff6666');
		}
		else
		{
			$('.captcha_refresh').click();
			comments_node = $('#comments');
			if (comments_node.html()=='')
				comments_node.append('<h3 id="comments_header"><strong>Reviews of '+$('#good_name').val()+':</strong></h3>');
			$('#comments_header').after('<table border="1" width="100%" cellpadding="1" class="user_comments"><tr><th align="left"><strong>By</strong>: '+data.post.name+'</th><th align="right"><i>'+data.post.date+'</i></th></tr><tr><td colspan="2">'+data.post.text+'</td></tr></table>');

			destination = $('#comments_header').offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 500 );
			for(i in comment)
				$('#'+i).val('');
			

		}
	}, 'json');
	
}