
function listEntries(json) {

  removeOldResults('data');
  
  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 cellEntries(json) {

  removeOldResults('data');

  var table = document.createElement('table');
  table.setAttribute('id', 'output');
  var tbody = document.createElement('tbody');
  
  var tr;
  var td;
  var rowNum;
  
  rowNum = 0;
  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');
	  td = document.createElement('td');
	  td.setAttribute('class','numbering');
	  td.appendChild(document.createTextNode(rowNum));
	  if(entry.content.$t == "Title" || entry.content.$t == "title") tr.setAttribute('class','firstRow');
	  tr.appendChild(td);
	  rowNum++;
	  
    }
    
    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);

  // Re-enable the ok button.
  //var ok_button = document.getElementById('ok_button');
  //ok_button.removeAttribute('disabled');
  
  

  //update scroller in wrapper
  document.getElementById('scrollSurface_swf').setContentHeight(document.getElementById('scrollSurface').getSize().size.y + 10);
}

function personalCells(json) {

  removeOldResults('personalDetails');

  var table = document.createElement('table');
  table.setAttribute('id', 'perosnalOutput');
  var tbody = document.createElement('tbody');
  var desc = Array();


  for (var i=0; i < json.feed.entry.length; i++) {

		//debug = document.createElement('td');
		//debug.appendChild(document.createTextNode(json.feed.entry[i]));
		//tbody.appendChild(debug);
		
		var entry = json.feed.entry[i];
		if (entry.gs$cell.row == '1')
		{
			desc.push(entry.content.$t);
		}
	
		if (entry.gs$cell.row == personalDetailsWhichLine)
		{
			var tr;
			var td;
			tr = document.createElement('tr');
			td = document.createElement('td');
			td.appendChild(document.createTextNode(desc[Number(entry.gs$cell.col)-1]));
			tr.appendChild(td);
			td = document.createElement('td');
			td.appendChild(document.createTextNode(entry.content.$t));
			tr.appendChild(td);
			tbody.appendChild(tr);
		}
 	
  } 
 

  table.appendChild(tbody);
  document.getElementById('personalDetails').appendChild(table);

  // Re-enable the ok button.
  //var ok_button = document.getElementById('ok_button');
  //ok_button.removeAttribute('disabled');
  
  

  //update scroller in wrapper
  document.getElementById('scrollSurface_swf').setContentHeight(document.getElementById('scrollSurface').getSize().size.y + 10);
}



/**
 * Called when the user clicks the 'OK' button to
 * retrieve a spreadsheet's JSON feed.  Creates a new 
 * script element in the DOM whose source is the JSON feed, 
 * and specifies that the callback function is 
 * 'listEntries' for a list feed and 'cellEntries' for a
 * cells feed (above).
 */
function displayResults(feedType,key,workSheet) {
  removeOldJSONScriptNodes('jsonScript_data');
  removeOldResults('data');

  // Show a "Loading..." indicator.
  var div = document.getElementById('data');
  var p = document.createElement('p');
  p.appendChild(document.createTextNode('Loading...'));
  div.appendChild(p);
  
  // Disable the OK button
  //var ok_button = document.getElementById('ok_button');
  //ok_button.disabled = 'true';

  // Retrieve the JSON feed.
  var script = document.createElement('script');

  if (feedType == 'list') {
    script.setAttribute('src', 'http://spreadsheets.google.com/feeds/'
                         + feedType
                         + '/' + key 
                         + '/' + workSheet + '/public/values' +
                        '?alt=json-in-script&callback=listEntries');
  }

  if (feedType == 'cells') {
    script.setAttribute('src', 'http://spreadsheets.google.com/feeds/'
                         + feedType
                         + '/' + key
                         + '/' + workSheet + '/public/values' +
                        '?alt=json-in-script&callback=cellEntries');
  }
  
  script.setAttribute('id', 'jsonScript_data');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);;
}

function displayPersonalDetails(feedType,key,workSheet) {
  removeOldJSONScriptNodes('jsonScript_personalDetails');
  removeOldResults('personalDetails');

  // Show a "Loading..." indicator.
  var div = document.getElementById('personalDetails');
  var p = document.createElement('p');
  p.appendChild(document.createTextNode('Loading...'));
  div.appendChild(p);
  
  // Disable the OK button
  //var ok_button = document.getElementById('ok_button');
  //ok_button.disabled = 'true';

  // Retrieve the JSON feed.
  var script = document.createElement('script');

  if (feedType == 'list') {
    script.setAttribute('src', 'http://spreadsheets.google.com/feeds/'
                         + feedType
                         + '/' + key 
                         + '/' + workSheet + '/public/values' +
                        '?alt=json-in-script&callback=listEntries');
  }

  if (feedType == 'cells') {
    script.setAttribute('src', 'http://spreadsheets.google.com/feeds/'
                         + feedType
                         + '/' + key
                         + '/' + workSheet + '/public/values' +
                        '?alt=json-in-script&callback=personalCells');
  }
  
  script.setAttribute('id', 'jsonScript_personalDetails');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);;
}

/**
 * Removes the script element from the previous result.
 */
function removeOldJSONScriptNodes(fromWhere) {
  var jsonScript = document.getElementById(fromWhere);
  if (jsonScript) {
    jsonScript.parentNode.removeChild(jsonScript);
  }
}

/**
 * Removes the output generated from the previous result.
 */
function removeOldResults(fromWhere) {
  var div = document.getElementById(fromWhere);
  if (div.firstChild) {
    div.removeChild(div.firstChild);
  }
}

