var commentSort = 'asc';
function addComment(entity_id)
{
	var bodyText = document.getElementById('myReaction').value;
	if(bodyText!='' && bodyText!='Please enter something before submitting.' && bodyText!='Enter your reaction here.')
	{
		$.get("/PC_load_letter/addReaction",{ entity_id: entity_id, reactText: bodyText },
			function (data)
			{
				if(data == "OK")
				{
					$.get("/PC_load_letter/getReactions",{ entity_id: entity_id, page: "1", sort_order: commentSort }, 
						function(data){
							document.getElementById('reactionsContainer').innerHTML = data;
							document.getElementById('addReaction').innerHTML = "Your reaction has been added. Thank you for your contribution.";
						}
					);
				}
				else
				{
					alert("Unable to post reaction at this time.");
				}
			}
		);
	}
	else
	{
		document.getElementById('myReaction').innerHTML = "Please enter something before submitting.";
	}
}

function checkReaction(reaction)
{
	if(reaction=='Enter your reaction here.' || reaction=='Please enter something before submitting.' || reaction=='')
		document.getElementById('myReaction').innerHTML='Enter your reaction here.';
}

function clearReaction(reaction)
{
	if(reaction=='Enter your reaction here.' || reaction=='Please enter something before submitting.')
		document.getElementById('myReaction').innerHTML='';
}

function sendRating(rating,entity_id)
{
	$.get("/PC_load_letter/sendRating",{ entity_id: entity_id, rating: rating }, 
		  function(data)
		  {
			  if(data != "false")
			  {
			  	document.getElementById('rateThis').innerHTML = data;
			  }
			  else
			  {
			  	alert("Unable to send rating at this time.");	
			  }
		  }
	);																						
}

function commentVote(i, comment_id, type)
{
	$.get("/PC_load_letter/voteReaction",{ comment_id: comment_id, i: i, vote: type }, 
		  function(data)
		  {
			  if(data != "false")
			  {
			  	document.getElementById('notice' + i).innerHTML = data;
			  }
			  else
			  {
			  	alert("Could not apply opinion at this time.");	
			  }
		  }
	);
}

function commentFlag(i, comment_id)
{
	$.get("/PC_load_letter/flagReaction",{ comment_id: comment_id, i: i }, 
		  function(data)
		  {
			  if(data != "false")
			  {
			  	document.getElementById('notice' + i).innerHTML = data;
			  }
			  else
			  {
			  	alert("Could not apply flag at this time.");	
			  }
		  }
	);	
}

function applyFlag(i, comment_id, vote)
{
	$.get("/PC_load_letter/applyCommentFlag",{ comment_id: comment_id, i: i, reason: vote }, 
		  function(data)
		  {
			  if(data != "false")
			  {
			  	document.getElementById('notice' + i).innerHTML = data;
			  }
			  else
			  {
			  	alert("Could not apply flag at this time.");	
			  }
		  }
	);
}

function editorJournalCreate(entity_id, category_id)
{
	$.get("/paper_jam/makeEditorJournal",{ entity_id: entity_id, category_id: category_id }, 
		  function(data)
		  {
			  if(data != "false")
			  {
				document.getElementById('eJournalBox').innerHTML = data;
			  }
		  }
	);	
}

function sortComments(entity,order)
{
	commentSort = order;
	$.get("/PC_load_letter/getReactions",{ entity_id: entity, sort_order: order, page: 1 }, 
		  function(data)
		  {
			  if(data != "false")
			  {
				document.getElementById('reactionsContainer').innerHTML = data;
			  }
		  }
	);	
}