/**
 * 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.
 */

function listEntries(j) {

	// Check for results
	if(j.feed.entry == undefined) {
		var div = document.getElementById('load');
		div.innerHTML = "<p style='color:red'>No results. Please check your search terms, or the \"Help\" tab above.</p>";
		return;
	}

	// FIRST entry only
	JSONstring = JSON.stringify(j.feed.entry[0]);

	// PHP doesn't like pound signs and ampersands in GET variables...
	// We're doing POST now, do we still need this?
	JSONstring = JSONstring.replace(/#/g,"%23");
	JSONstring = JSONstring.replace(/&/g,"%26");
	sendJSON(JSONstring,0);
}

/**
 * Builds an unordered list from the JSON feed entries.
 * This function is the call-back function for the second JSON script, which 
 * executes a Google Base query of features.
 *
 * @param {JSON} j is the JSON object pulled from the Google Base service.
 */

function listEntries2(j) {

	if(j.feed.entry) {
		// ALL entries
		var JSONstring = JSON.stringify(j.feed.entry);
	}
	else {
		var JSONstring = " ";
	}
	// PHP doesn't like pound signs and ampersands in GET variables...
	JSONstring = JSONstring.replace(/#/g,"%23");
	sendJSON(JSONstring.replace(/&/g,"%26"),1);

}

/**
 * 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 search(query) {
  // Delete any previous Google Base JSON queries.
  removeOldJSONScriptNodes();
  // Clear any old data to prepare to display the Loading... message.
//  removeOldResults()

  var qid
  if(typeof(query)=="string")
	 qid = query;
  else
	qid = escape(query.bq.value);

  // Show a "Loading..." indicator.
  var div = document.getElementById('load');
  var p = document.createElement('p');
  if(!div) {
	div = document.getElementById('content');
	p.appendChild(document.createTextNode('Loading Composite Brick...'));
  }
  else
	p.appendChild(document.createTextNode('Loading...'));
  div.appendChild(p);

  // Add "BBa_" prefix if user has not already:
  if("BBA_" != (qid.substr(0,4)).toUpperCase()) {
	qid = "BBa_".concat(qid);
  }

  // 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", "jsonScript");
  scriptElement.setAttribute("src", "http://www.google.com/base/feeds/snippets" + 
      "?bq=[title:" + qid + "]" +
//[authorid:5751624]" +
      "&alt=json-in-script&callback=listEntries");
  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 removeOldJSONScriptNodes() {
  var jsonScript = document.getElementById("jsonScript");
  if (jsonScript) {
    jsonScript.parentNode.removeChild(jsonScript);
  }
}

/**
 * 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 removeOldResults() {
  var span = document.getElementById("data");
  if (span.firstChild) {
    span.removeChild(span.firstChild);
  }
}
