var template = "biblio.xsl"
var bibFile = "srl_bib_sept2007.xml"
var qname = ""
var qyear = "2007"


function getQ() {

 if (document.URL.indexOf('?') != -1) {
  //NOTE: This does not trap malformed inputs and
  //      expects this to be the last (only) parameter
  //note to self: can use split('?') to break url down
  //into ?-delimited sections

  var query = new Array();
  query = document.URL.split('?');

//  document.write("<p>Your query is: "+query+"</p>");
//  document.write("<p>query.length = "+query.length+"</p>");
//  document.write("<p>query[0] = "+query[0]+"</p>");
//  document.write("<p>query[1] = "+query[1]+"</p>");

//  for (i=0; i<=query.length; i++) {
//      document.write("<p>query[i] for i="+i+" = "+query[i]+"</p>");
//  }


  var nset = false;
  var yset = false;

  for (i=1; i<=query.length-1; i++) {

      var qtype = query[i].substring(0,query[i].indexOf('='));
      if ( qtype.toLowerCase() == "qname" ) {
        qname = query[i].substring(query[i].indexOf('=')+1,query[i].length);
        nset = true;
      } else if ( qtype.toLowerCase() == "qyear" ) {
        qyear = query[i].substring(query[i].indexOf('=')+1,query[i].length);
        yset = true;
      } //else {
        //unknown querry type
        //document.write("<p>Say what?  I don't know what this is: "+qtype+"</p>");
      //}

  }//for loop

  //this forces the display of all years when filtering on a name
  //unless you've specified a name and a year
  //note: I like checking the qname length rather that just seeing if it 
  //is set just in case it is given an empty parameter in the URL
  if (qname.length>0 & yset==false)
    qyear='';

 } //if

}//getqname


function createBib(){

  document.write("<p><font size=\"-2\">(createBib version 1.2.00)</font></p>");

  //see if there is a member name parameter attached to the URL
  getQ();
//  document.write("<p>your qname is "+qname+"</p>");
//  document.write("<p>your qyear is "+qyear+"</p>");

  if ( document.implementation && document.implementation.createDocument ) {
    //document.write("<P>You're running Firefox, Netscape, etc.")
    createBib_Moz()
  } else if ( window.ActiveXObject ) {
    //document.write("<P>You're running IE.")
    createBib_IE()
  }
}//createBib()


//inserts bibliography in div node (id=bibliography)
function createBib_IE () {
  try {
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
    xmlDoc.load(bibFile)
    var xslDoc = new ActiveXObject("MSXML2.FreeThreadedDOMDocument.4.0")
    xslDoc.async = false
    xslDoc.load(template)
    var xslTemplate = new ActiveXObject( "MSXML2.XSLTemplate.4.0" )

    xslTemplate.stylesheet = xslDoc
    var xsltProcessor = xslTemplate.createProcessor( );
    xsltProcessor.input = xmlDoc;

    //Overwrite the xsl:params with runtime selected values:
    xsltProcessor.addParameter( "member_name", qname );
    xsltProcessor.addParameter( "display_year", qyear );

    if (qname.length>0) {
       queryname.innerHTML = "Search results for \""+qname+"\"";
    } else {
       queryname.innerHTML = "Search results for \""+qyear+"\"";
    }

    //Output the XML (as processed by the XSLT) to the div target
    xsltProcessor.transform();
    bibliography.innerHTML = xsltProcessor.output;

  } catch (e) {
    alert("Problems formatting bibliography in IE:" + e)
  }
}//createBib_IE()


//inserts bibliography in div node (id=bibliography)
function createBib_Moz() {
  try {
    var xslStylesheet;
    var xsltProcessor = new XSLTProcessor();

    // load the xslt file
    var myXMLHTTPRequest = new XMLHttpRequest();
    myXMLHTTPRequest.open("GET", template, false);
    myXMLHTTPRequest.send(null);

    // get the XML document and import it
    xslStylesheet = myXMLHTTPRequest.responseXML;
    xsltProcessor.importStylesheet(xslStylesheet);

    // load the xml file
    myXMLHTTPRequest = new XMLHttpRequest();
    myXMLHTTPRequest.open("GET", bibFile, false);
    myXMLHTTPRequest.send(null);

    xsltProcessor.setParameter(null, "member_name", qname);
    xsltProcessor.setParameter(null, "display_year", qyear );

    var xmlSource = myXMLHTTPRequest.responseXML;

    //display query info if there is any
    var frag;
    if (qname.length>0) {
        frag = document.createTextNode("Search results for \""+qname+"\"");
    } else {
        frag = document.createTextNode("Search results for \""+qyear+"\"");
    }
    document.getElementById("queryname").appendChild(frag);


    //affect the transformation and append into document
    frag = xsltProcessor.transformToFragment(xmlSource, document);
    document.getElementById("bibliography").appendChild(frag);

  } catch (e) {
    alert("Problems formatting bibliography in Moz:" +e)
  }
}//createBib_Moz



