/**
  * Given a table ID, export the contents of the table to CSV
  */
function exportTable(tableId) {
  var table = document.getElementById(tableId);
  if (!table) {
    return;
  }
  var walker = document.createTreeWalker(table, NodeFilter.SHOW_ELEMENT, null,
                                         false);
  lines = [];
  thisLine = [];
  while (walker.nextNode()) {
    node = walker.currentNode;
    if (node.nodeName == 'TD' || node.nodeName == 'TH') {
      if (node.childNodes[0]) {
        thisLine.push(node.childNodes[0].data);
      } else  {
        thisLine.push('');
      }
    }
    if (node.nodeName == 'TR') {
      if (thisLine.length > 0) {
        lines.push(thisLine);
        thisLine = [];
      }
    }
  }
  csvString = '';
  for (l in lines) {
    rows = lines[l];
    for (r in rows) {
      cols = rows[r];
        csvString += '"'+cols+'"';
        csvString += ',';
    }
    csvString += "\n";
  }
  var win = window.open(
    'data:text/csv,' + encodeURIComponent(csvString)
  );
}
