function listComments(id, scroll, return_path, page, commentsperpage, always_focus) {
  
  var callback = function(xmlhttp) {
    if (xmlhttp.readyState == 4) {
      updateDOM(xmlhttp);

      if(existUserCookie() && getUserFromCookie()) {
	var username_span = getElement("login_info_ok_user");
	var login_info_ok = getElement("login_ok_wrapper");
	username_span.innerText = username_span.textContent = " " + getUserFromCookie();
	login_info_ok.style.display = "block";
	
	if (scroll) {
	  getElement("comment_rows").lastChild.scrollIntoView(true);

	  var comments_text = document.getElementById("comments-text");
	  if(comments_text)
	    comments_text.focus();
	}
	
	var at_checkbox = getElement("accepts_terms");
	if(at_checkbox)
	  at_checkbox.checked = true;
      } else {
	var login_info_failed = getElement("login_failed_wrapper");
	login_info_failed.style.display = "block";
      }
      if(document.getElementById("set-focus")){
	var comments_text = document.getElementById("comments-text");
	if(comments_text){
	  comments_text.focus();
	}
      }

      setTimeout("hideSpinner()", 400);
    }
  }
  
  if(!page)
   var page = page_no;
   
  if(!commentsperpage)
   var commentsperpage = comments;

  var focus;
  if(always_focus){
    focus = "1";
  }
  
  var from = (page-1)*commentsperpage;  
  showSpinner();
  xmlhttpRequest(action_file + "?action=list-comments&focus=" + focus + "&id=" + escape(id) + "&__toolbar=0&return=" + return_path + "&page=" + page + "&com=" + commentsperpage + "&from=" + from,
  callback);
   
  return false;
}

function listCommentsReported(return_path) {
  //alert("listCommentsReported()");
  var callback = function(xmlhttp) {
    if (xmlhttp.readyState == 4) {
      updateDOM(xmlhttp);
      setTimeout("hideSpinner()", 400);
    }
  }
  
  showSpinner();
  var threshold = document.getElementById("comments-threshold").value;
  xmlhttpRequest(action_file + "?action=list-comments-reported&return=" + return_path +"&threshold=" + 
		 escape(threshold) + "&__toolbar=0",
		 callback);
  
  return false;
}

function listLatestComment(id, return_path) {
  //alert("listLatestComment(), id: " + id);
  var callback = function(xmlhttp) {
    if (xmlhttp.readyState == 4) {
      updateDOM(xmlhttp);
    }
  }

  xmlhttpRequest(action_file + "?action=list-comments&id=" + escape(id) + 
		 "&latest=1&__toolbar=0&return=" + return_path,
		 callback);
  return false;
}

function writeComment(id, return_path) {
  //alert("writeComment(), id: " + id + ", return path: " + return_path);
  var callback = function(xmlhttp) {
    if (xmlhttp.readyState == 4) {
      var comments = getResponseXML(xmlhttp);
      
      // Add the response XML to the scratch area, so that we can use
      // getElementById to find the component nodes:
      var scratch = document.getElementById("comments-scratch");
      scratch.appendChild(comments);

      if (document.getElementById("comments-user-max-comments")) {
	alert(user_max_comments_msg);
	return false;
      }      
      
      if (document.getElementById("comments-user-auth-failed")) {
	location.href = user_auth_login_page + "?return=" + return_path + "&comment=1";
	return false;
      }
      
      // Clean the scratch area:
      while(scratch.firstChild) {
	scratch.removeChild(scratch.firstChild);
      }
      
      comment_list_changed_event.notify(id, 1);
    }
  }
  
  var comment = document.getElementById("comments-text").value;
  xmlhttpRequest(action_file + "?action=write-comment&id=" + escape(id) + 
		 "&path=" + escape(return_path) + "&__toolbar=0",
	         callback, "comment=" + escape(comment));
  
  document.getElementById("comments-text").value = "";
  
  return false;
}

function reportComment(uuid) {
  //alert("reportComment(), uuid: " + uuid);
  var callback = function(xmlhttp) {
    if (xmlhttp.readyState == 4) {
      setCookie("reported-" + uuid, "", 14, "/");
      alert(msg_reported);
    }
  }

  if (document.cookie.match("reported-" + uuid)) {
    alert(msg_already_reported);
  } else {
    xmlhttpRequest(action_file + "?action=report-comment&uuid=" + escape(uuid) + "&__toolbar=0",
		   callback);
  }  
  
  return false;
}

function deleteComment(id, uuid) {
  //alert("deleteComment(), id: " + id + ", uuid: " + uuid);
  var callback = function(xmlhttp) {
    if (xmlhttp.readyState == 4) {
      if (id) {
      
        
	comment_list_changed_event.notify(id);
      } else {
	listCommentsReported();
      }
    }
  }
 
  if (uuid)
    xmlhttpRequest(action_file + "?action=delete-comment&uuid=" + escape(uuid) + "&__toolbar=0",
		   callback);
  else {
    var inputs = document.getElementById("comments").getElementsByTagName("input");
    var to_delete = "";
    for (var i = 0; i < inputs.length; i++) {
      if (inputs[i].checked) {
	if (i > 0 && to_delete.length > 0) {
	  to_delete += ",";
	}
	to_delete += inputs[i].id.substr(9);
      }
    }

    xmlhttpRequest(action_file + "?action=delete-comment&__toolbar=0",
		   callback, "uuid=" + escape(to_delete));
  }
  
  return false;
}

function listCommentsEditor(id, uuid) {
  
  var callback = function(xmlhttp) {
    if (xmlhttp.readyState == 4) {
      var comments = getResponseXML(xmlhttp);

      // Add the response XML to the scratch area, so that we can use
      // getElementById to find the component nodes:
      var scratch = document.getElementById("comments-scratch");
      scratch.appendChild(comments);
      
      var old_div = document.getElementById("comments");
      var new_div = document.getElementById("new-comments")
      old_div.parentNode.replaceChild(new_div, old_div);
      new_div.setAttribute("id", "comments");
      
      // IE workaround to make it recognize its attributes:
      if (isIE) {
	new_div.innerHTML = new_div.innerHTML;
      }
      
      // Fix hidden store input name
      var hidden_storage = document.getElementById("comments-store-uuid");
      hidden_storage.name = "comments-store-uuid";

      // Fix radiobutton names, text and set selected
      var inputs = new_div.getElementsByTagName("input");
      for(var i=0; i < inputs.length; i++) {
      if(inputs[i].type == "radio") {
        inputs[i].name = "comments-radio-featured-comment";
        if(inputs[i].id == "radio-"+uuid)
          inputs[i].checked = "checked";
      	}
      }
      // Clean the scratch area:
      while(scratch.firstChild) {
	scratch.removeChild(scratch.firstChild);
      }
    }
  }
  
  xmlhttpRequest(action_file + "?action=list-comments-editor&id=" + escape(id) + "&__toolbar=0",
		 callback);
  
  return false;
}

function updateDOM(xmlhttp) {
  var comments = getResponseXML(xmlhttp);
  
  // Add the response XML to the scratch area, so that we can use
  // getElementById to find the component nodes:
  var scratch = document.getElementById("comments-scratch");
  scratch.appendChild(comments);

  var old_div = document.getElementById("comments");
  var new_div = document.getElementById("new-comments");
  
  old_div.parentNode.replaceChild(new_div, old_div);
  new_div.setAttribute("id", "comments");
  
  // IE workaround to make it recognize its attributes:
  if (isIE) {
    new_div.innerHTML = new_div.innerHTML;
  }
  
  // Clean the scratch area:
  while(scratch.firstChild) {
    scratch.removeChild(scratch.firstChild);
  }
}

comment_list_changed_event.registerCb(listComments);
