
	function commentAnon()
	{
		$("#comment_name").attr( 'value' , defaulttext['comment_name'] );
		setInputClass('comment_name');
		
		$("#comment_anon").attr('value','1');
		
		$("#comment_goanon").css('display','none');
		$("#comment_customname").css('display','');
	}
	
	
	
	
	function showCommentLoading()
	{
		$("#commentloading").css("display","block");
		$("#comment_button").attr("disabled","disabled");
	}
	
	function hideCommentLoading()
	{
		$("#commentloading").css("display","none");
		$("#comment_button").attr("disabled","");
	}






	function goComment()
	{
		showCommentLoading();
		preventSpam('comment','cleanComment()');
	}	
		
	
	var comment_name;
	var comment_text;
	
	function cleanComment()
	{	
	
		comment_text = $("#comment_text").val();
		comment_name = $("#comment_name").val();

		//Process comment text
		$.ajax({
			type: "POST",
			url: "processtext.php",
			data: { what: "comment_text" , text: urlencode(comment_text) },
			success: function(resp){

				resp = $.trim(resp);	
				
				//Post it back to the form
				$("#comment_text").attr('value', resp );
				//And set a variable
				comment_text = resp;
				
				//Process comment name
				$.ajax({
					type: "POST",
					url: "processtext.php",
					data: { what: "comment_name" , text: urlencode(comment_name) },
					data: "what=comment_name&text=" + $("#comment_name").attr('value'),
					success: function(resp){
				
						resp = $.trim(resp);
						
						//Post it back to the form
						$("#comment_name").attr('value',resp);
						//Set a variable
						comment_name = resp;
						//And set a cookie
						$.cookie('name', resp, { expires: 9001 , domain: '.amirite.net' });
						
						processComment();
						
					}
				});
						
				
			}
		});
		
	}
	
	function processComment()
	{
		if ( !comment_text || comment_text == "Write a comment"  ) 
		{ 
			$("#comment_captcha_content").css("display","none");
			$("#comment_submit_error").css("display","block");
			centerPopup("#commentPopup");
			loadPopup("#commentPopup");
			hideCommentLoading();
			return false;
		}
		else
		{
					
			$("#comment_submit_error").css("display","none");
			$("#comment_captcha_content").css("display","block");
		
			
			// Captcha disabled, go straight to adding to the database.
			addCommentToDB();
			
				/* if ( $.cookie('passedCaptcha') ) { addCommentToDB(); }
				else 
				{ 
					centerPopup("#commentPopup");
					loadPopup("#commentPopup");
				} */
		}
		
	}
	
	
	
	
	function checkCommentCaptcha()
	{
		//checkCaptcha is in add.js
		checkCaptcha( 'addCommentToDB()' );
	}
	
	
	function waitFor( what, actionOnReady )
	{
		if ( what ) { eval(actionOnReady); }
		else { setTimeout("waitFor('" + what + "', '" + actionOnReady + "');", 100); }
	}
		
	
	
	
	function addCommentToDB()
	{
		
		postID = $("#comment_postID").attr('value');
		userID = $("#userID").attr('value');
		anon = $("#comment_anon").attr('value');
		replyto = $("#comment_replyto").attr('value');
		
		$.ajax({
		type: "POST",
		url: "addcomment.php",
		data: "comment=" + urlencode(comment_text) + "&name=" +  urlencode(comment_name) + "&postID=" + postID + "&userID=" + userID  + "&anon=" + anon + "&replyto=" + replyto ,
		success: function(newCommentID){
			
			newCommentID = $.trim(newCommentID);
			
			getNewComment(newCommentID);
			
			setTimeout("maybeClearCommentForm("+newCommentID+");",1500);
			
			updateInteraction(postID);
			lastComment = newCommentID; 
			
			comment_text = false; comment_name = false;
			closePopup();
		}
		}); 
	}	
	
	function maybeClearCommentForm(newCommentID)
	{
		if ( $("#comment"+newCommentID) ) 
		{ 
			resetCommentForm();
		}
	}
	
	
	function resetCommentForm()
	{
		$('#newcommentform').fadeOut(400);
		$("#comment_reset").css('display','none');
		setTimeout( "$('#comments_section').append( $('#newcommentform') ); animatedcollapse.show('newcommentform'); " , 450 );
		$("#comment_replyto").val('');
		$("#comment_anon").val('');
		
		if ( userID )
		{
			$("#comment_customname").css('display','none');
			$("#comment_goanon").css('display','block');
		}
		else
		{
			$("#comment_goanon").css('display','none');
			$("#comment_customname").css('display','block');
		}

		$("#comment_text").attr('value',"Write a comment");
		$("#comment_text").addClass("defaultinput");
	}
		
		
	
	
	function getNewComment(commentID)
	{		
		$.get("showsinglecomment.php", { ID: commentID },
  			function(resp){
				replyto = resp.substring(0, resp.indexOf(',') );
				if ( replyto == 0 ) { replyto = false; }
				
				comment = resp.substring( resp.indexOf(',') + 1 );
				
				comment = comment + '<div id="c'+commentID+'replies" class="reply"></div>';
				
				if ( replyto && !clinear ) 
				{ 
					$("#c"+replyto+"replies").append(comment); 
				}
				else 
				{ 
					$("#comments").append(comment); 
				}
				
				fadeInComment(commentID);
				//setTimeout( "fadeInComment('"+commentID+"');" , 500);
  		});
	}
	
	
	function fadeInComment(commentID)
	{
		$('#comment'+commentID).fadeIn(1000);
		hideCommentLoading();
	}
	
	
	function quoteComment(commentID,name,replyto)
	{
		
		if ( replyto ) 
		{ 
			$("#comment_reset").css('display','block');
			$('#newcommentform').fadeOut(400);
			setTimeout( "$('#c"+replyto+"replies').append( $('#newcommentform') ); animatedcollapse.show('newcommentform'); " , 450 );
		}
		
		currentText = $("#comment_text").attr('value');
		text = '@' + commentID + ' (' + name + '): ';
		if ( currentText != defaulttext["comment_text"]) { text = currentText + text; }
		
		$("#comment_text").attr('value',text)
		$("#comment_text").removeClass("defaultinput");
		
		$("#comment_replyto").val(replyto);
		
		setTimeout( '$.scrollTo("#newcommentform",600, {offset: {top:-250}} )' , 800 );
	}
		
		

	
	