// Revision: 2.4
// Last updated: 20th January 2006
function postComment(url,form) {
	var formData = $(form).serialize(true);
	loading();
	new Ajax.Request(url, {
		method: 'post',
		parameters: formData,
		onSuccess: function(transport) {
			var response = transport.responseText || '';
			if(response.length) {
				if(response.indexOf('wronganswer') != -1) {
					Element.hide('loading');
					$('submit').disabled = false;
					$('comment').disabled = false;
					$('errors').innerHTML = 'You have failed the challenge question! Try again.';
					Element.show('errors');
					new Effect.Highlight('errors', { duration: 2.0 } );
				} else if(response.indexOf('noanswer') != -1) {
					Element.hide('loading');
					$('submit').disabled = false;
					$('comment').disabled = false;
					$('errors').innerHTML = 'You must answer the challenge question!';
					Element.show('errors');
					new Effect.Highlight('errors', { duration: 2.0 } );
				} else {
					Element.hide('loading');
					$('submit').disabled = false;
					$('comment').disabled = false;
					new Insertion.Bottom('commentlist', response);
					Element.show('commentform');
					$('comment').value = '';
					if ($('errors')) { Element.hide('errors'); }
					var ourNewComment = $('commentlist').lastChild;
					new Effect.BlindDown(ourNewComment, { duration: 1.0 } );
					$('comments').innerHTML = 
						(Number($('comments').innerHTML.match(/^\d+/)) + 1) + ' ' + $('comments').innerHTML.replace(/^\d+\s/,'');
				}
			}
		},
		onFailure: function(request) {
			failure(request);
		}
	});
}

function commentAdded() {
	$('comment').value = '';
	$('comment').disabled = true;
	$('submit').disabled = true;
	if ($('nocomment')) { Element.remove('nocomment'); }
	if ($('hidelist')) { Element.remove('hidelist'); }
}

function failure(request) {
	Element.hide('loading');
	$('submit').disabled = false;
	$('comment').disabled = false;
	$('errors').innerHTML = request.responseText;
	Element.show('errors');
	new Effect.Highlight('errors', { queue: 'end', duration: 2.0 } );
	if ($('nocomment')) { Element.show('nocomment'); }
}

function loading() {
	if ($('errors')) { Element.hide('errors'); }
	if ($('nocomment')) { Element.hide('nocomment'); }
	$('submit').disabled = true;
	$('comment').disabled = true;  
	Element.show('loading');
}

function complete(request) {
	Element.hide('loading');
	Element.show('commentform');
	$('submit').disabled = false;
	$('comment').disabled = false;  

	if (request.status == 200) {commentAdded()}
	else {failure(request)};
}
