function listComments(id, scroll, return_path, page, always_focus) {
  var callback = function(xmlhttp) {
    setTimeout("hideSpinnerList()", 700);
    if (xmlhttp.readyState == 4) {
      updateDOM(xmlhttp);
      updateCommentCount();

      if (scroll)
	document.getElementById("comments").lastChild.previousSibling.scrollIntoView(true);
    }
  }
  
  var page_no = "1";
  /*
  if(page)
   page_no = page;
  */
  var focus;
  /*
  if(always_focus){
    if(always_focus == 'focustoplist')
      focus = "toplist";  
    else if(always_focus == 'focus')
      focus = "1";
  }
  */
  
  var comp_id;
  if (id)
    comp_id = id;
  else
    comp_id = component_id; 

  showSpinnerList();
  xmlhttpRequest(action_file + "?action=list-comments&focus=" + focus + "&id=" + escape(comp_id) + 
		 "&__toolbar=0&return=" + return_path + "&page=" + page_no, callback);
   
  return false;
}

function listCommentsReported(page, return_path) {
  /*
  var callback = function(xmlhttp) {
    if (xmlhttp.readyState == 4) {
      updateDOM(xmlhttp);
      setTimeout("hideSpinnerList()", 700);
    }
  }
  
  showSpinnerList();
  var threshold = document.getElementById("comments-threshold").value;
  var days = "";
  if(document.getElementById("comments-days"))
    days = document.getElementById("comments-days").value;
  
  var this_page = 1;
  if(page)
    this_page = page;
  
  xmlhttpRequest(action_file + "?action=list-comments-reported&return=" + return_path +"&threshold=" + 
	   escape(threshold) + "&days=" + escape(days) + "&page=" + escape(this_page) + "&__toolbar=0" ,
		 callback);
  
  return false;
  */
}

function listLatestComment(id, return_path) {
  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) {
  var callback = function(xmlhttp) {
    setTimeout("hideSpinnerWrite()", 700);

    if (xmlhttp.readyState == 4) {
      var comments = getResponseXML(xmlhttp);
      var scratch = document.getElementById("comments-scratch");
      scratch.appendChild(comments);

      if (document.getElementById("comments-user-authentication-failed")) {
	alert(user_authentication_failed);
	return false;
      } else if (document.getElementById("comments-user-captcha-failed")) {
        alert(user_captcha_failed);
        clearCaptcha();
        loadCaptcha();
        return false;
      } else if (document.getElementById("comments-profanity-failed")) {
	var profelem = document.getElementById("comments-profanity-failed");
	alert(user_profanity_failed + profelem.innerHTML);
      } else if (document.getElementById("comments-awaiting-approval")) {
	alert(user_awaiting_approval);
	document.getElementById("comments-text").value = "";
      } else
	document.getElementById("comments-text").value = "";

      // Clean the scratch area:
      while(scratch.firstChild) {
	scratch.removeChild(scratch.firstChild);
      }
      
      comment_list_changed_event.notify(id, 1);
    }
  }

  var captcha_secret = document.getElementById("captcha-secret").value;
  var captcha = document.getElementById("captcha").value;
  var comment = document.getElementById("comments-text").value;
  xmlhttpRequest(action_file + "?" + 
		 "action=write-comment" + "&id=" + escape(id) + 
		 "&path=" + escape(return_path) + 
		 "&__toolbar=0" +
		 "&captcha=" + escape(captcha) + 
		 "&captcha-secret=" + escape(captcha_secret), 
		 callback, 
		 "comment=" + escape(comment));
  showSpinnerWrite();

  return false;
}

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

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

function deleteComment(id, uuid) {
  var callback = function(xmlhttp) {
    if (xmlhttp.readyState == 4) {
      comment_list_changed_event.notify();
      /*
      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 getCaptcha()
{
  var callback = function(xmlhttp) {
    if (xmlhttp.readyState == 4) {
      var captcha = 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(captcha);

      var old_div = document.getElementById("captcha-ajax");
      var new_div = document.getElementById("new-captcha");
  
      old_div.parentNode.replaceChild(new_div, old_div);
      new_div.setAttribute("id", "captcha-ajax");

      // 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);
      }
      setTimeout("hideSpinnerWrite()", 700);
    }
  }
  showSpinnerWrite();
  xmlhttpRequest(action_file + "?action=get-captcha&__toolbar=0", callback);
}

function getRegisterCaptcha()
{
  var callback = function(xmlhttp) {
    if (xmlhttp.readyState == 4) {
      var captcha = 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(captcha);

      var old_div = document.getElementById("captcha-ajax");
      var new_div = document.getElementById("new-captcha");
  
      old_div.parentNode.replaceChild(new_div, old_div);
      new_div.setAttribute("id", "captcha-ajax");
      
      // 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);
      }
      setTimeout("hideSpinnerWrite()", 700);
    }
  }
  showSpinnerWrite();
  xmlhttpRequest(action_file + "?action=get-captcha&__toolbar=0", callback);
}

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);
