	// 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,#email_login_row').show();
		else $('#email_row,#email_login_row').hide();
		if(usn) $('#username_row,#username_login_row').show();
		else $('#username_row,#username_login_row').hide();
		if(pass1) $('#password_row,#password_login_row').show();
		else $('#password_row,#password_login_row').hide();
		if(pass2) $('#password_row_two,#password_login_row_two').show();
		else $('#password_row_two,#password_login_row_two').hide();
		if(forgot) $('#forgot_link,#forgot_login_link').show();
		else $('#forgot_link,#forgot_login_link').hide();
		if(newacct) $('#newacct_link,#newacct_login_link').show();
		else $('#newacct_link,#newacct_login_link').hide();
		if(existingacct) $('#existingacct_link,#existingacct_login_link').show();
		else $('#existingacct_link,#existingacct_login_link').hide();
	}
	
	// Submit our review, handle the login/register at the same time.
	function comment_form_submit(rev_id) {
		$('#comment_submit_button').attr('disabled', true);
		
		var review = $('#review').attr('value');
		
		if(review.length > 5) {	
			hadasuccess('Submitting review...');
			pageTracker._trackEvent("Comment System", "Submit", "Product Family "+rev_id);
			
			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'),
					revid: rev_id,
					family: rev_id,
					rating: $('#rating').attr('value'),
					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", "Product Family "+rev_id);
						$('#comment_form').slideUp('slow');
					//	$('#comments_text').load('/assets/comments-reviewed-comments.php?family='+rev_id+'&commentsonly=1&ts='+new Date().getTime());
					}
					updateLogin();
				}
			);
		}
		else if(review.length == 0) {
			hadanerror("You must put in a review and log in. The system will not log you in without a review.<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 review 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) {
		if($('#logout_text').length > 0)
			$('#logout_text').fadeOut().html('');
		$.get(
			'/assets/ajax/logout.php?logout='+tstyle,
			function(response) {
				if($('#logout_text').length > 0)
					$('#logout_text').html('You are now logged out of the review system.').fadeIn();
				comment_filter_elements(0,1,1,0,1,1,0);
				updateLogin();
			}
		);
	}
	
	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 hadaloginerror(text) {
		if($('#login_status').length > 0 && text.length > 0)
			$('#login_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:100%'>"+text+'</div>';
			$('#comment_status').hide().css('color','#000').css('fontWeight','normal').html(text).fadeIn('slow');
		}
	}

	// Send login or signup request
	function login() {
		var username = $('#lusername').attr('value').replace("&","!");
		var password = $('#lpassword').attr('value');
		var source = $('#source').attr('value');
		var email = $('#lemail').attr('value');
		var password2 = $('#lpassword_two').attr('value');
		
		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');
					hadaloginerror(response[1]);
				}
				else{
					// good login
					$('#status').html(response[1]);
					comment_filter_elements(0,0,0,0,0,0,0);
				}
			}
		);
	}
	
	function updateLogin() {
		$('#status').load('/assets/ajax/logstatus.php');
		init_page();
	}
	
	function showLogin() {
		$('#login_top').toggle();
	}
	
	function getSubCats(catid) {
		$('#levelb').load('/assets/ajax/subcats.php?catid='+catid);
	}
	
	function getProducts(catid) {
		$('#product').load('/assets/ajax/products.php?catid='+catid);
	}
	
	function myratings() {
		window.location='/my-ratings.php?'+new Date().getTime();
	}
	
	function init_page() {
		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);
		}
	}
	
	$(function() {
		init_page();
	});
