/**
 * javascript file for ssr
 */
function getXMLHttpRequest(){
  var req = false;
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch(e) { /** ignore */ }
  } else if (window.ActiveXObject) {
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (err1) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (err2) { /** ignore */  }
    }
  }
  return req;
}

function getReadyStateHandler(req, responseXmlHandler){
  return function(){
    if(req.readyState==4){
      if(req.status==200){
        responseXmlHandler(req.responseText);
      } /** fail without care */
    }
  }
}

function strip(node){
  try {
    if(node.hasChildNodes() && node.childNodes) {
      while (node.firstChild) {
        node.removeChild(node.firstChild);
      }
    }
  } catch(e){
    //alert("failed to strip " + e.message);
  }
}

function addText(node, text){
  if(text!=null && node!=null) {
    node.appendChild(document.createTextNode(text));
  }
}

function addTextHtml(node, text){
  if(text!=null && node!=null) {
    node.innerHTML = text;
  }
}

function getNode(name){
  var node = document.getElementById(name);
  return node;
}

function rate(commentid, rating)
{
  if (rating>1){ rating=1;}
  if (rating<-1){ rating=-1;}

  var req = getXMLHttpRequest();
  var handlerFunction = getReadyStateHandler(req, updateRate);
  req.onreadystatechange = handlerFunction;

  var url = "/rate/?rating=" + rating + "&commentid=" + commentid;    

  req.open("Post",url);
  req.setRequestHeader("ContentType","application/x--www-form-urlencoded");
  req.send("");
}

function updateRate(s)
{
  var resp = eval('(' + s + ')');
  var success = resp.success;
  var commentid = resp.commentid;
  var node = getNode("rate" + commentid);
  if (success == true) {
    var average = resp.average;
    var divisor = resp.divisor;
    var buttons = getNode("buttons" + commentid);
    strip(buttons);
    var divnode = getNode("divisor" + commentid);
    strip(divnode);
    addText(divnode, divisor);
    var avnode = getNode("average" + commentid);
    strip(avnode);
    addText(avnode, average);
    var lowavnode = getNode("lowaverage" + commentid);
    strip(lowavnode);
    addText(lowavnode, average)
    strip(node);
    addText(node, "comment rated"); 
  }
  else {
    strip(node);
    addText(node, "Failed");
  }    
}

function commentPreview()
{
  var req = getXMLHttpRequest();
  var handlerFunction = getReadyStateHandler(req, updateCommentPreview);
  req.onreadystatechange = handlerFunction;
  var goop_body = '';
  if (document.getElementById('commentbody')!=null) {
    goop_body = document.getElementById('commentbody').value;
    goop_body = goop_body.replace('\n','<p>')
  }  
  var url = "/comment_preview/?goopbody=" + goop_body;    
  /**alert(url);*/
  req.open("Post",url);
  req.setRequestHeader("ContentType","application/x--www-form-urlencoded");
  req.send("");
}

function updateCommentPreview(s)
{
  var resp = eval('(' + s + ')');
  var success = resp.success;
  var comment = resp.comment;
  var node = getNode("preview");
  if (success == true) {
    strip(node);
    addTextHtml(node, comment);
  }
  else {
    strip(node);
    addText(node, "No preview");
  }    
}
