function postComment(){
   createRequest();
   var comment = document.getElementById("comment").value;
   var reply_to = document.getElementById("reply_to").value;
   var entity_id = document.getElementById("entity_id").value;
   var entity_type = document.getElementById("entity_type").value;
   var url = "/comments/add_comment.php";
   request.open("POST", url, true);
   request.setRequestHeader("Content-Type",
			    "application/x-www-form-urlencoded");
   request.onreadystatechange = updatePage;
   request.send("comment=" + escape(comment)+
		"&reply_to=" + escape(reply_to)+
		"&entity_type=" + escape(entity_type)+
		"&entity_id="+ escape(entity_id));
}
function rateDown(comment){
   createRequest();
   var entity_id = document.getElementById("entity_id").value;
   var entity_type = document.getElementById("entity_type").value;
   var url = "/comments/rate_comment.php?down=1&comment="+escape(comment)+
      "&entity_id="+escape(entity_id)+"&entity_type="+escape(entity_type)+"&rand="+Date();
   request.open("GET", url, true);
   request.onreadystatechange = updatePage;
   request.send(null);
}
function rateUp(comment){
   createRequest();
   var entity_id = document.getElementById("entity_id").value;
   var entity_type = document.getElementById("entity_type").value;
   var url = "/comments/rate_comment.php?up=1&comment="+comment+
            "&entity_id="+escape(entity_id)+"&entity_type="+escape(entity_type)+"&rand="+Date();
   request.open("GET", url, true);
   request.onreadystatechange = updatePage;
   request.send(null);
}
function reportComment(comment){
   var answer = confirm("Are you sure you want to report this comment?");
   if( answer  ){
      createRequest();
	    var entity_id = document.getElementById("entity_id").value;
   	    var entity_type = document.getElementById("entity_type").value;
            var url = "/comments/report_comment.php?comment="+comment+
	       "&entity_id="+escape(entity_id)+"&entity_type="+escape(entity_type)+"&rand="+Date();
	    request.open("GET", url, true);
	    //request.onreadystatechange = updatePage;
	    request.send(null);
   }
}
function replyTo(comment){
   createRequest();
   var url = "/comments/display_comment_reply.php?comment="+comment+
            "&rand="+Date();
   request.open("GET", url, true);
   request.onreadystatechange = addReply;
   request.send(null);
}
function changePreferences(){
   createRequest();
   var display_by = document.getElementById("display_by").value;
   var display_rating = document.getElementById("display_rating").value;
   var display_per = document.getElementById("display_per").value;
   var url = "/comments/change_preferences.php?display_by="+
      escape(display_by)+"&display_rating="+
      escape(display_rating)+"&display_per="+
      escape(display_per)+
            "&rand="+escape(Date());
   request.open("GET", url, true);
   request.onreadystatechange = newPreferences;
   request.send(null);
}
function displayComments(){
   createRequest();
   var entity_id = document.getElementById("entity_id").value;
   var entity_type = document.getElementById("entity_type").value;
   var url = "/comments/display_comments.php?entity_id="+escape(entity_id)+
	"&entity_type="+escape(entity_type)+"&rand="+Date();
   request.open("GET", url, true);
   request.onreadystatechange = updateComments;
   request.send(null);
}
function updateComments(){
   if (request.readyState == 4) {
      var currentComments = document.getElementById('display-comments');
      currentComments.innerHTML = request.responseText;
   }
}
function updatePage(){
   if (request.readyState == 4) {
      document.getElementById('form_comment').reset();
      cancelReply();
      var currentComments = document.getElementById('display-comments');
      currentComments.innerHTML = request.responseText;
      if( window.location.hash == '#reply' ){
	window.location.hash= "#comments";
      } 
   }
}
function addReply(){
   if(request.readyState == 4){
      var displayReply = document.getElementById('display-reply');
      displayReply.innerHTML = request.responseText;
   }
}
function newPreferences(){
   if(request.readyState == 4){
      var displayPreferences = document.getElementById('display-preferences');
      displayPreferences.innerHTML = request.responseText;
      displayComments();
   }
}
function cancelReply(){
   var clearReply = document.getElementById('display-reply');
   clearReply.innerHTML = '<input type="hidden" id="reply_to" name="reply_to" value="0">';
}
function loggedIn(){
   alert( 'You must be logged in to change a comments ratings');
}

