/**
 * This function is the call-back function for the JSON scripts which 
 * executes a SeeAlso response.
 *
 * @param {JSON} value is the JSON object pulled from the SeeAlso service.
 */
function seealsoEntries(value) {

  var mainDiv = document.createElement("div");

  var isbn;
  var label;
  var desc;
  var uri;

  if (typeof value == "object") {

    if (typeof value[0] == "string") 
      isbn = value[0];
      //alert(isbn);

    if (typeof value[1] == "object") {

      // Clear any old data to prepare to display the Loading... message.
      var identifier = "seealso-" + isbn;
      var div = document.getElementById(identifier);
      //if (div.firstChild) div.removeChild(div.firstChild);

      label = typeof value[1] == "object" ? value[1] : "";
      desc = typeof value[2] == "object" ? value[2] : "";
      uri = typeof value[3] == "object" ? value[3] : "";
      if (typeof value[3] != "object") value[3] = [];

      for (var i=0; i<value[1].length; i++) {

        //alert(label[i]);
        //alert(desc[i]); 
        //alert(uri[i]); 

        // Create a DIV for each info
        var iconDiv = document.createElement("div");
        iconDiv.className = "icon";

        // Add a link to each info's informtaion page
        var a = document.createElement("a");
        a.href = "/cgi-bin/redirect.pl?url=" + encodeURI(uri[i]);
        a.target = "_blank";
        a.innerHTML = label[i];

        // Display an icon
        var img = document.createElement("img");
        img.src = "http://kug.ub.uni-koeln.de/images/openbib/wikipedia.png";
        img.style.border = "1px solid #999";
        img.title = label[i];
        //a.appendChild(img);

        iconDiv.appendChild(img);
        iconDiv.appendChild(a);
        mainDiv.appendChild(iconDiv);

      }

      if (div) div.appendChild(mainDiv);

    }

  }

}

/**
 *
 * @param {DOM object} query The form element containing the
 *                     input parameters "isbns"
 */
function searchseealso(isbn) {
  // Delete any previous SeeAlso JSON queries.
  var jsonScript = document.getElementById("jsonScript");
  if (jsonScript) {
    jsonScript.parentNode.removeChild(jsonScript);
  }
      
  // Add a script element with the src as the user's SeeAlso 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://kug5.ub.uni-koeln.de/portal/connector/seealso/isbn2wikipedia?id=" + escape(isbn) + "&format=seealso&callback=seealsoEntries");
  scriptElement.setAttribute("type", "text/javascript");
  // make the request to the SeeAlso service
  document.documentElement.firstChild.appendChild(scriptElement);
}

