$(
	function () {
		
		$('#comment-form').hide();
		
		$.ajax ({
			success: usernameAvailable,
			dataType: 'json',
			url: '/user.json',
			type: 'GET',
			error: aError
		});
		
		$('#comment-form').bind('submit', function() {
			//alert (this.action);
			//return false;
			$.ajax ({
				success: responseAvailable,
				dataType: 'json',
				data: $(this).serialize(),
				url: this.action,
				type: 'GET',
				error: aError
			});
			return false; // <-- important!
		});
		$('#comment-submit').removeAttr("disabled");
	}
);

function aError (XMLHttpRequest, textStatus, errorThrown) {
	//console.log (DumpFields(XMLHttpRequest));
}

function usernameAvailable (data, statusText) {
	$('#comment-username').val (data.forum_user.membername);
	$('#comment-form').show();
	$('#comment-submit').removeAttr("disabled");
}

function DumpFields (d) {
	var a = [];
	
	if (d instanceof Array) {
		var l = d.length;
		for (i = 0; i < l; i++) {
			a.push (d[k]);
		}
		
		return '[' + a.join (",\n") + ']';
	} else if (d instanceof Object) {
		for (var k in d) {
			if (typeof d[k] != 'function')
				a.push(k + ': "' + d[k] + '"');
		}
		
		return '{' + a.join (",\n") + '}';
	}
}

function responseAvailable (data, statusText)  {
	
	$('#comment-message').val('');
	
	window.location = window.location;
	
	//alert('status: ' + statusText + '\n\nresponseText: \n' + data + 
	//	'\n\nThe output div should have already been updated with the responseText.'); 
}
