// Quick and dirty add-links function for output of MySQL queries for old
// review table.
// gerv@mozilla.org, 2007-05-28

var CHECKBOX_INDEX = 0;
var BUG_INDEX = 1;
var ATTACH_INDEX = 2;
var STORAGE_URL = "http://www.gerv.net/cgi-bin/storage";
// var STORAGE_URL = "http://localhost/websites/gerv.net/cgi-bin/storage";
var SET = "oldreviews";

function getInnerText(node, ignorewhitespace)
{
 var text = "";
 // if the node has children, loop through them
 if(node.hasChildNodes())
 {
  var children = node.childNodes;
  for(var i=0; i<children.length; i++)
  {
   // if node is a text node append it
   if(children[i].nodeName == "#text")
   {
    if(ignorewhitespace)
    {
     if(!/^\s+$/.test(children[i].nodeValue))
     {
      text = text.concat(children[i].nodeValue);
     }
    }
    else
    {
     text = text.concat(children[i].nodeValue);
    }
   }
   // if node is a line break append \n
   else if(children[i].nodeName == "BR")
   {
    text = text.concat("\n");
   }
   // otherwise call this function again to get the text
   else
   {
    text = text.concat(getInnerText(children[i]));
   }
  }
 }
 // it has no children, so get the text
 else
 {
  // if node is a text node append it
  if(node.nodeName == "#text")
  {
   text = text.concat(node.nodeValue);
  }
  // if node is a line break append \n
  else if(node.nodeName == "BR")
  {
   text = text.concat("\n");
  }
 }
 return text;
}

function toggleItem(e) {
  if (!e) var e = window.event;
  
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

  var myConn = new XHConn();
  if (!myConn) { return; }

  var bugCell = targ.parentNode.parentNode.cells[BUG_INDEX];
  var bugNumber = getInnerText(bugCell);
  
  if (bugCell.getAttribute("style")) {
    bugCell.removeAttribute("style");  
    myConn.connect(STORAGE_URL, 
                   "GET", 
                   "set=" + SET + "&action=remove&value=" + bugNumber, 
                   null);
  }
  else {
    bugCell.setAttribute("style", "text-decoration: line-through");  
    myConn.connect(STORAGE_URL, 
                   "GET", 
                   "set=" + SET + "&action=add&value=" + bugNumber, 
                   null);
  }
}

function ruleOutDones(xhr) {
  if (!xhr.responseText.match(/^Error/)) {
    var set = eval('(' + xhr.responseText + ')');
    
    var table = document.getElementsByTagName("table")[0];
    for (var i = 0; i < table.tBodies[0].rows.length; i++) {
      var row = table.tBodies[0].rows[i];
      var bugCell = row.cells[BUG_INDEX];
      var bugNumber = getInnerText(bugCell);
    
      if (bugNumber in set) {
        bugCell.setAttribute("style", "text-decoration: line-through");
        row.cells[CHECKBOX_INDEX].firstChild.checked = true;
      }
    }
  }
  else {
    alert(xhr.responseText);
  }
}

function addStuff() {    
  var table = document.getElementsByTagName("table")[0];

  // if this table does not have a tBody, we don't want to know about it
  if (!table.tBodies[0] || 
     !table.tBodies[0].rows || 
     0 == table.tBodies[0].rows.length) 
  {
    return;
  }

  for (var i = 0; i < table.tBodies[0].rows.length; i++) {
    var row = table.tBodies[0].rows[i];    
    var bugCell = row.cells[BUG_INDEX];
    var attachCell = row.cells[ATTACH_INDEX];
      
    // Bug cells
    var bugLinkEl = createElement('a');
    bugLinkEl.href = 'https://bugzilla.mozilla.org/show_bug.cgi?id=' 
                     + bugCell.innerHTML;
    
    // move the current contents of the cell that we're 
    // hyperlinking into the hyperlink
    var innerEls = bugCell.childNodes;
    for (var j = 0; j < innerEls.length; j++) {
      bugLinkEl.appendChild(innerEls[j]);
    }
    
    // and finally add the new link back into the cell
    bugCell.appendChild(bugLinkEl);

    // Attachment cells
    var attachLinkEl = createElement('a');
    attachLinkEl.href = 'https://bugzilla.mozilla.org/attachment.cgi?' +
                        'action=edit&id=' + 
                        attachCell.innerHTML;
    
    // move the current contents of the cell that we're 
    // hyperlinking into the hyperlink
    var innerEls = attachCell.childNodes;
    for (var j = 0; j < innerEls.length; j++) {
      attachLinkEl.appendChild(innerEls[j]);
    }
    
    // and finally add the new link back into the cell
    attachCell.appendChild(attachLinkEl);
    
    // Add checkboxes
    var checkboxEl = createElement('input');
    checkboxEl.type = "checkbox";
    checkboxEl.onclick = toggleItem;
    var checkboxCell = row.cells[CHECKBOX_INDEX];
    checkboxCell.noWrap = true;
    checkboxCell.insertBefore(checkboxEl, checkboxCell.firstChild);
  }
  
  // Kick off phase 2 - loading the "done" info
  var myConn = new XHConn();
  if (!myConn) { return; }
  myConn.connect(STORAGE_URL, 
                 "GET", 
                 "set=" + SET + "&action=list", 
                 ruleOutDones);
}

window.addEventListener("load", addStuff, false);

