/**
 * Builds an unordered list from the JSON feed entries.
 * This function is the call-back function for the JSON scripts which 
 * executes a Google Base query.
 *
 * @param {JSON} j is the JSON object pulled from the Google Base service.
 */
lb = function () { return document.createElement("br"); }

function qcallback(j) {

	// ALL results
	var JSONstring = JSON.stringify(j.feed);
	
	// PHP doesn't like pound signs and ampersands in GET variables...
	JSONstring = JSONstring.replace(/#/g,"%23");
//	qsendJSON(JSONstring.replace(/&/g,"%26"));
	printTable(j);
}

/**
 * Adds a JSON script element which queries Google Base and calls the 
 * call-back function.
 *
 * @param {DOM object} query The form element containing the input parameters
 *     "bq" and "feed".
 */
function qsearch(query) {
  // Delete any previous Google Base JSON queries.
  qremoveOldJSONScriptNodes();
  // Clear any old data to prepare to display the Loading... message.
  qremoveOldResults()

  // Show a "Loading..." indicator.
  var div = document.getElementById('qdata');
  var p = document.createElement('p');
  p.appendChild(document.createTextNode('Searching...'));
  div.appendChild(p);

  // Form query for what we are searching by.
  var searchby = query.searchby.value;
  var qr = new Array(query.bq.value);
  if(searchby=="protein") {
	//by type (protein)
	qr.push("[type:protein|cds]");
	//protein formulation
  }
  if(searchby=="operator") {
	//by type (operator)
	qr.push("[type:binding]");
	//by 
	//operator formulation
  }

  // Crowd out redundant results
  qr.push("&crowdby=title:1");

  // Get max possible results
  qr.push("&max-results=250");

  // Add a script element with the src as the user's Google Base query. 
  // JSON output is specified by including the alt=json-in-script argument
  // and the callback funtion is also specified as a URI argument.
  var scriptElement = document.createElement("script");
  scriptElement.setAttribute("id", "searchquery");
  var qurl = "http://www.google.com/base/feeds/snippets?bq="
  qrlen = qr.length;
  for(i=0;i<qrlen;i++) {
	qurl += qr[i];
  }
  qurl += 
//[authorid:5751624]" +
      "&alt=json-in-script&callback=qcallback";
  scriptElement.setAttribute("src", qurl);
  scriptElement.setAttribute("type", "text/javascript");
  
  document.documentElement.firstChild.appendChild(scriptElement);
}

/**
 * Deletes any old script elements which have been created by previous calls
 * to search().
 */
function qremoveOldJSONScriptNodes() {
  var searchquery = document.getElementById("searchquery");
  if (searchquery) {
    searchquery.parentNode.removeChild(searchquery);
  }
}

/**
 * Deletes pre-existing children of the data span from the page. The data span 
 * may contain a "Loading..." message, or the results of a previous query. 
 * This old data should be removed before displaying new data.
 */
function qremoveOldResults() {
  var span = document.getElementById("qdata");
  while(span.firstChild) {
    span.removeChild(span.firstChild);
  }
}