	// we have to change these a lot, so a function to handle it all for us
	// the post-comment-form input fields and links, in order
	function comment_filter_elements(email, usn, pass1, pass2, forgot, newacct, existingacct) {
		if(email) $('#email_row').show();
		else $('#email_row').hide();
		if(usn) $('#username_row').show();
		else $('#username_row').hide();
		if(pass1) $('#password_row').show();
		else $('#password_row').hide();
		if(pass2) $('#password_row_two').show();
		else $('#password_row_two').hide();
		if(forgot) $('#forgot_link').show();
		else $('#forgot_link').hide();
		if(newacct) $('#newacct_link').show();
		else $('#newacct_link').hide();
		if(existingacct) $('#existingacct_link').show();
		else $('#existingacct_link').hide();
	}

	function comment_form_init() {
		if($('#logged_in').length > 0) { // we are logged in
			comment_filter_elements(0,0,0,0,0,0,0);
		}
		else { // we are not logged in; show login stuff
			comment_filter_elements(0,1,1,0,1,1,0);
		}
		
		if($('#subscribed').length > 0) { // we are subscribed; hide the box
			$('#notify_row').hide();
		}
		
		$('#comment_body').slideDown('normal', function() {
			if(document.location.hash.length > 0)
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: $(document.location.hash).offset().top }, 400);
		});
	}
	
	// Submit our review, handle the login/register at the same time.
	function comment_form_submit(revid) {
		$('#comment_submit_button').attr('disabled', true);
		
		var review = $('#review').attr('value');
		
		if(review.length > 5) {	
			hadasuccess('Submitting comment...');
			pageTracker._trackEvent("Comment System", "Submit", "Article Family "+revid);
			
			if($('#email').attr('value').length > 1)
				pageTracker._trackEvent("Comment System", "Registering");
			
			$.post(
				'/assets/ajax/post.php',
				{
					username: $('#username').attr('value'),
					password: $('#password').attr('value'),
					password2: $('#password_two').attr('value'),
					email: $('#email').attr('value'),
					notify: $('#notify').attr('checked'),
					family: revid,
					post: encodeURIComponent(review),
					ts: new Date().getTime()
				},
				function(response) {
					var response = response.split('|');
					if(response.length == 1) {
						response[1] = response[0];
						response[0] = 'Error';
					}
					
					if(response[0] == 'Error') {
						// got an error!
						hadanerror(response[1]);
						pageTracker._trackEvent("Comment System", "Response - Error", response[1]);
						$('#comment_submit_button').removeAttr('disabled');
					}
					else{
						// good post
						hadasuccess(response[1]);
						pageTracker._trackEvent("Comment System", "Response - Success", "Article Family "+revid);
						$('#comment_form').slideUp('slow');
						$('#comments_text').load('/assets/comments-reviewed-comments.php?family='+revid+'&commentsonly=1&ts='+new Date().getTime());
					}
					// handle other login things if needed: corner login [not cci/dci/pri/wli]
				}
			);
		}
		else if(review.length == 0) {
			hadanerror("You must put in a comment and log in. The system will not log you in without a comment.<br /><br />If you are posting for the first time, you must also submit a post when you register. Our editors will review your first post for quality. We do this to reduce SPAM and improve the quality of the discussion.");
			$('#comment_submit_button').removeAttr('disabled');
		}
		else {
			hadanerror('Your comment is too short; please say something useful!');
			$('#comment_submit_button').removeAttr('disabled');
		}
	}
	
	// they clicked the "New Account" link
	function new_acct() {
		comment_filter_elements(1,1,1,1,1,0,1);
	}
	
	// they clicked the "existing account" link after they clicked the "new account" link.
	function existing_acct() {
		$('#email').attr('value', '');
		$('#password_two').attr('value', '');
		comment_filter_elements(0,1,1,0,1,1,0);
	}

	function logout(tstyle) {
		$('#logout_text').fadeOut().html('');
		$.get(
			'/assets/ajax/logout.php?logout='+tstyle,
			function(response) {
				$('#logout_text').html('You are now logged out of the commenting system.').fadeIn();
				if($('#subscribed_text').length > 0) $('#subscribed_text').hide();
				$('#notify_row').show();
				comment_filter_elements(0,1,1,0,1,1,0);
			}
		);
	}
	
	function flagPost(comid) {
		$.get(
			'/assets/comments-reviewed-flagpost.php?id='+comid+'&ts='+new Date().getTime(),
			function(response) {
				alert('Thank you for flagging this post; our editors have been alerted and will review it soon.');
			}
		);
	}
	
	function hadanerror(text) {
		if($('#comment_status').length > 0 && text.length > 0)
			$('#comment_status').hide().css('color','#f00').css('fontWeight','bold').html(text).fadeIn('slow');
	}
	
	function hadasuccess(text) {
		if($('#comment_status').length > 0 && text.length > 0) {
			text = "<div class='comment_header_left' style='width:748px;border:1px solid #ccc'>"+text+'</div>';
			$('#comment_status').hide().css('color','#000').css('fontWeight','normal').html(text).fadeIn('slow');
		}
	}

	// Send login or signup request
/* This is not in use
	function login() {
		var username = $('#username').attr('value');
		var password = $('#password').attr('value');
		var source = $('#source').attr('value');
		var email = $('#email').attr('value');
		var password2 = $('#password_two').attr('value');
		
		$('#lbutton').attr('disabled', true);
		
		if($('#islogin').attr('value') != 'signup') {
			email = '';
			password2 = '';
		}			

		$.get(
			'/assets/ajax/login.php?username='+username+'&password='+password+'&password2='+password2+'&email='+email+'&source='+source,
			function (response) {
				// either a login, OR a new user
				var response = response.split('|');
				if(response[0] == 'Error' || response.length == 1) {
					// got an error!
					$('#lbutton').removeAttr('disabled');
					hadanerror(response[1]);
				}
				else{
					// good login
					hadasuccess('Login successful. Loading "My Comments"...');
					window.location.reload();
				}
			}
		);
	}*/
