function updateRating( updateValue, commentId )
{
	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'rating' );
	myParams.set( 'rating', updateValue );
	myParams.set( 'commentid', commentId );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			var selectedComment;
			var selectedElement;
			
			selectedComment = $( 'comment_' + commentId );
			selectedElement = selectedComment.select( '[class="rating"]' );
			selectedElement[0].update( 'rating:&nbsp;' + transport.responseText );
			//selectedElement[0].addClassName( 'success' );
			
			// update rating dongus
			var tempRating = "";
			switch ( updateValue ) {
			
				case 1:
					tempRating += "&nbsp;(&nbsp;+&nbsp;/&nbsp;<a  href=\"javascript:updateRating(-1," + commentId + ")\" title=\"Decrease the rating of this comment\">-</a>&nbsp;)";
					break;
					
				case -1:
					tempRating += "&nbsp;(&nbsp;<a href=\"javascript:updateRating(1," + commentId + ")\" title=\"Increase the rating of this comment\">+</a>&nbsp;/&nbsp;-&nbsp;)";
					break;
					
			}			
			
			selectedElement = selectedComment.select( '[class="doRating"]' );			
			selectedElement[0].update( tempRating );

		}
	});	
				
}

function replyToComment( commentId ) {

	var parentElement;
	var parentText;
	var parentPostedBy;
	var replySection;	
	var selectedElement;
	var replyForm;
	
	// let's get some stuff from the parent comment
	parentElement = $( 'comment_' + commentId );
	//alert( parentElement );
	parentText = parentElement.select( '[class="bodyText"]' );
	parentPostedBy = parentElement.select( '[class="postedBy"]' );
	
	//alert( parentPostedBy );
	
	parentPostedBy = parentPostedBy[0].innerHTML;
	parentText = parentText[0].innerHTML;

	// now alter some fields in the form
	replySection = $( 'composeReply' );
	
	selectedElement = replySection.select( '[class="replyHead"]' );
	selectedElement[0].update( 'Reply to comment #' + commentId );
	
	selectedElement = replySection.select( '[class="instructions"]' );
	
	// IEEEEEEEEEEEEEEEEE!
	try {
		selectedElement[0].update( '<p>You are replying directly to a comment on this page. For your convenience, the text of comment #' + commentId + ' is duplicated below.</p>\n<div class="parentComment"><div class="postedBy">' + parentPostedBy + '</div><div class="bodyText">' + parentText +  '</div></div>\n<p>Please enter your reply in the following space and click the "Submit comment" button when complete. If you decide you don\'t want to reply to this comment, please <a href=\"javascript:resetEntryForm()\">click here</a> to reset this form.</p>' );	
	}
	catch (e) { };

	// set the parentid field in the form to match the parent comment id
	replyForm = $( 'replyForm' );
	replyForm['parentid'].value = commentId;
	replyForm['message'].select();

	
	window.location.href = "#postReply";
	
}

function resetEntryForm( ) {

	var replySection;
	var selectedElement;
	var replyForm
	
	// first, let's mess with some of the content in the post reply section to
	// make it more inviting and such
	replySection = $( 'composeReply' );
	
	selectedElement = replySection.select( '[class="replyHead"]' );
	selectedElement[0].update( 'Reply to Article' );
	
	selectedElement = replySection.select( '[class="instructions"]' );
	selectedElement[0].update( 'Type your comment into the field below. Click the button labeled \'Submit comment\' when finished.' );	
	
	// set the parentid field in the form to reflect stark nothingness
	// trying to reference the form field breaks IE

	replyForm = $( 'replyForm' );
	replyForm['parentid'].value = "";
	replyForm['message'].select();		

	window.location.href = "#postReply";
	
}

var submitted = 0;

function submitComment( ) 
{

	var forwardUrl;
	var replyForm;
	var dummyParentId;

	// silly locking mechanism to keep comments from being added twice
	if ( submitted == 0 ) {
		
		// set lock
		submitted = 1;

		// grab the form
		$( 'replyForm' ).request({
			onComplete: function( transport ) {
			
				// check if we return an error
				if ( transport.responseText.indexOf( "rror" ) > 0 ) {
					alert( transport.responseText );
					
					// release the lock so that they can try again maybe
					submitted = 0;
				}
				else {
					
					// clear out text area set it does not persist
					replyForm = $( 'replyForm' );
					replyForm['message'].value = "";
					
					// try to fool the whackness that is IE by navigating it pretty close to the new message location
					// before reload
					dummyParentId = replyForm['parentid'];
					replyForm['parentid'].value = "";	

					// those case won't scroll anywhere useful, so substitute the end of the page.
					// this will probably be close enough.
					if ( dummyParentId == "" ) {
						dummyParentId = "postReply";
					}

				
					// add the returned comment id to the url and redirect to load it
					forwardUrl = window.location.href.substring( 0, window.location.href.indexOf( '#' ) );
					
					// here is the IE silliness
					//window.location.href = "#" + dummyParentId;
					window.location.href = forwardUrl + "#" + transport.responseText;
					document.location.reload();
				}
			
			}
		})
	}

}


function updateFeatured( value, commentId )
{

	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'updatefeatured' );
	myParams.set( 'value', value );	
	myParams.set( 'commentid', commentId );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			alert( transport.responseText );

		}
	});	

}

function requestBlock( commentId )
{

	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'requestblock' );
	myParams.set( 'commentid', commentId );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			var selectedComment;
			var selectedElement;
			
			selectedComment = $( 'comment_' + commentId );
			selectedElement = selectedComment.select( '[class="reportThis"]' );
			selectedElement[0].update( transport.responseText );
			selectedElement[0].addClassName( 'success' );

		}
	});	

}


function resetHiddenComments( articleId, articleType )
{

	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'resetviewed' );
	myParams.set( 'articleid', articleId );
	myParams.set( 'articletype', articleType );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			document.location.reload();

		}
	});	

}



// The user wants to read this blocked comment. Make it so, with extreme prejudice.
function removeBlock( commentId, userLoggedIn ) {

	// don't do all this ajax jibba jabba if the user isn't logged in
	if ( userLoggedIn != -1 ) {
		var url = '/ajax/comments_handler.php';	
		
		var now = new Date();
		var x = now.getTime();
		
		// create a hash with the AJAX parameters
		var myParams = new Hash();
		myParams.set( 'type', 'view' );
		myParams.set( 'commentid', commentId );
		myParams.set( 'x', x );
		
		// send the dong
		// reflect sending field with updated data
		new Ajax.Request( url, {
			method: 'get',
			parameters: myParams,
			onComplete: function( transport ) {
				// alert ( ">" + userId + "< " + transport.responseText );
				var selectedElement;
				var blockedClasses;
				
				blockedClasses = new Array( 'neg', 'blocked' );
				
				// find the comment with an id matching the input and remove the classes that are
				// hiding it.
				selectedElement = $( 'comment_' + commentId );
				
				blockedClasses.each( function( item ) {	
					if ( selectedElement.hasClassName( item ) ) {
						selectedElement.removeClassName( item );
					}
				} )
			}
		});	
	}
	else {
	
		var selectedElement;
		var blockedClasses;
		
		blockedClasses = new Array( 'neg', 'blocked' );
		
		// find the comment with an id matching the input and remove the classes that are
		// hiding it.
		selectedElement = $( 'comment_' + commentId );
		
		blockedClasses.each( function( item ) {	
			if ( selectedElement.hasClassName( item ) ) {
				selectedElement.removeClassName( item );
			}
		} )	
	
	
	}
	
}

function blockComment( commentId, action )
{

	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'blockcomment' );
	myParams.set( 'commentid', commentId );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
		
			var selectedComment;
						
			if ( transport.responseText.indexOf( "rror" ) > 0 ) {
				alert ( transport.responseText );	
			}
			else {
				switch (action) {
					case 'hide':
						selectedComment = $( 'comment_' + commentId );
						selectedComment.hide();
						break;
					
					case 'reload':
						document.location.reload();
						break;
				}

			}
			
		}
	});	

}

function approveComment( commentId, action )
{

	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'approvecomment' );
	myParams.set( 'commentid', commentId );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			
			var selectedComment;
						
			if ( transport.responseText.indexOf( "rror" ) > 0 ) {
				alert ( transport.responseText );	
			}
			else {
				switch (action) {
					case 'hide':
						selectedComment = $( 'comment_' + commentId );
						selectedComment.hide();
						break;
					
					case 'reload':
						document.location.reload();
						break;
				}
			}

		}
	});	

}

function requestViewComments( articleId, articleType )
{


	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'requestviewcomments' );
	myParams.set( 'articleid', articleId );
	myParams.set( 'articletype', articleType );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			
			//Effect.BlindDown('commentHider', { duration: 0.5 });
			$('commentHider').show();
			window.location.href = "#commentMessage";
		}
	});	


}

function requestHideComments( articleId, articleType )
{


	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'requesthidecomments' );
	myParams.set( 'articleid', articleId );
	myParams.set( 'articletype', articleType );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			
				//Effect.BlindUp('commentHider', { duration: 0.5 });
				//alert ( transport.responseText );
				$('commentHider').hide();

		}
	});	


}

function commentViewerHider( articleId, articleType, pageMode )
{
	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();


	if ( pageMode == 'print' ) {

		if ( $('commentHider').getStyle('display') == 'block' ) {
		
			$('commentHider').hide();
			$('commentInstruction').update('(Click to view comments)');					
		
		} 
		else {
		

			$('commentHider').show();
			window.location.href = "#commentMessage";
			$('commentInstruction').update('(Click to hide comments)');

		
		}


	
	}
	else {

		if ( $('commentHider').getStyle('display') == 'block' ) {
		
			
			// create a hash with the AJAX parameters
			var myParams = new Hash();
			myParams.set( 'type', 'requesthidecomments' );
			myParams.set( 'articleid', articleId );
			myParams.set( 'articletype', articleType );
			myParams.set( 'x', x );
			
			// send the dong
			// reflect sending field with updated data
			new Ajax.Request( url, {
				method: 'get',
				parameters: myParams,
				onComplete: function( transport ) {
					
					//Effect.BlindUp('commentHider', { duration: 0.5 });
					//alert ( transport.responseText );
					$('commentHider').hide();
					$('commentInstruction').update('(Click to view comments)');					
		
				}
			});	
		
		
		} 
		else {
		
				// create a hash with the AJAX parameters
			var myParams = new Hash();
			myParams.set( 'type', 'requestviewcomments' );
			myParams.set( 'articleid', articleId );
			myParams.set( 'articletype', articleType );
			myParams.set( 'x', x );
			
			// send the dong
			// reflect sending field with updated data
			new Ajax.Request( url, {
				method: 'get',
				parameters: myParams,
				onComplete: function( transport ) {
					
					//Effect.BlindDown('commentHider', { duration: 0.5 });
					$('commentHider').show();
					window.location.href = "#commentMessage";
					$('commentInstruction').update('(Click to hide comments)');
				}
			});	
		
	
	
		
		}

	}
}