/**
 * Lists the entries from the specified JSON feed
 * by creating a new 'dl' element in the DOM.
 * Each 'dt' is the title of the row, and each 'dd' 
 * is the content of the row. 
 */
function listEntries(json) {

  removeOldResults();
  
  var dl = document.createElement('dl');
  dl.setAttribute('id', 'output');

  for (var i = 0; i < json.feed.entry.length; i++) {
    
    var entry = json.feed.entry[i];
    
    var dt = document.createElement('dt');
    var title = document.createTextNode(entry.title.$t);
    dt.appendChild(title);
    
    var dd = document.createElement('dd');
    var content = document.createTextNode(entry.content.$t);
    dd.appendChild(content);
  
    dl.appendChild(dt);
    dl.appendChild(dd);
  }

  document.getElementById('data').appendChild(dl);

  // Re-enable the ok button.
  //var ok_button = document.getElementById('ok_button');
  //ok_button.removeAttribute('disabled');
}

/**
 * Lists the entries from the specified JSON feed
 * by inserting the cells into a new 'table' 
 * element in the DOM.  Each 'tr' represents a 
 * row in the spreadsheet, and each 'td' is a cell
 * within that row.
 */
function cellsEntries(json) {

  removeOldResults();
  var what = document.createElement('h4');
	what.appendChild(document.createTextNode(json.feed.title.$t));
	document.getElementById('data').appendChild(what);
	
  var table = document.createElement('table');
  table.setAttribute('id', 'output');
  var tbody = document.createElement('tbody');
  
  var tr;
  
  for (var i=0; i < json.feed.entry.length; i++) {

    var entry = json.feed.entry[i];
    if (entry.gs$cell.col == '1') {
      if (tr != null) {
        tbody.appendChild(tr);
      }

      tr = document.createElement('tr');
    }
    
    var td = document.createElement('td');
    td.appendChild(document.createTextNode(entry.content.$t));
    tr.appendChild(td);
  } 
 
  tbody.appendChild(tr);
  table.appendChild(tbody);
  document.getElementById('data').appendChild(table);
  tabize();

  // Re-enable the ok button.
  //var ok_button = document.getElementById('ok_button');
  //ok_button.removeAttribute('disabled');
}

/**
 * Removes the script element from the previous result.
 */
function removeOldJSONScriptNodes() {
  var jsonScript = document.getElementById('jsonScript');
  if (jsonScript) {
    jsonScript.parentNode.removeChild(jsonScript);
  }
}

/**
 * Removes the output generated from the previous result.
 */
function removeOldResults() {
  var div = document.getElementById('data');
  if (div.firstChild) {
    div.removeChild(div.firstChild);
  }
}
function makeAScript(key, type){
	removeOldJSONScriptNodes();
	removeOldResults();
	
	var script = document.createElement('script');
	script.setAttribute('src', 'http://spreadsheets.google.com/feeds/' + type + '/' + key + '/public/values?alt=json-in-script&callback='+type+'Entries');
	script.setAttribute('id', 'jsonScript');
	script.setAttribute('type', 'text/javascript');
	document.documentElement.firstChild.appendChild(script);
}
