// Globale Variable fuer das Request-Objekt
var req;


function loadXMLDoc(url) {

    if (window.XMLHttpRequest) { // Test, ob der Browser ein eingebautes XMLHttpRequest-Objekt besitzt oder...
        req = new XMLHttpRequest();

        // Event-Handler am Objekt registrieren
        req.onreadystatechange = processReqChange;

        // Request absetzen
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) { // ... ob er das Objekt per ActiveX implementiert

        req = new ActiveXObject("Microsoft.XMLHTTP");
        // Event-Handler am Objekt registrieren
        req.onreadystatechange = processReqChange;

        // Request absetzen
        req.open("GET", url, true);
        req.send();
    }
}


function processReqChange() {
    // falls Status "4" (complete) erreicht ist, geht's los
    if (req.readyState == 4) {
        // aber nur, falls der Server den HTTP-Statuscode 200 gesendet hat
        if (req.status == 200) {

             // Neue Akronym-Liste anzeigen
             showAcroList();

             // Mauszeiger auf Normalzustand zuruecksetzen
             setCursor('auto');
        } else {
            alert("Fehler bei der Kommunikation mit dem Server:\n" +
                req.statusText);
        }
    }
}


function deleteAcroList() {
    document.getElementById("outputB").innerHTML = "";
}


function showAcroList() {

    deleteAcroList();

    var ul, li, anchor, text;

    // Aus der XML-Antwort alle Elemente vom Typ "Acronym" herausholen
    var items = req.responseXML.getElementsByTagName("Acronym");

    // Den gewaehlten Akronymtyp (Organisation) ermitteln
    var acrotyp;
    for (var i = 0; i < document.forms["rootform"].typ.length; i++) {
      if (document.forms["rootform"].typ[i].checked) {
        acrotyp = document.forms["rootform"].typ[i].value;
      }
    }
    //alert(acrotyp);

    // Im Anzeigebereich ein neues UL-Element einfuegen
    var outputArea = document.getElementById("outputB");
    ul = document.createElement("ul");



    // Akronyme als LI's in die Liste einfuegen
    for (var i = 0; i < items.length; i++) {
      var itemType = "";
      // Hier wuerde ich gerne abfragen, ob das Attribut type existiert,
      // doch leider hat MSIE6 die entsprechende Methode aus 
      // DOM Level 2 nicht implementiert.
      //if (items[i].hasAttribute("type")) {
      //  itemType = items[i].getAttributeNode("type").value;
      //}
      itemType = String(items[i].getAttribute("type"));
      if (itemType.indexOf(acrotyp) >= 0 ||acrotyp == "") {
        li = document.createElement("li");
        anchor = document.createElement("a");
        anchor.setAttribute("href","javascript:showDescription(" + i + ")");

        // text = document.createTextNode(items[i].firstChild.nodeValue + ' - ' + items[i].getAttributeNode("expansion").value);
        text = document.createTextNode(items[i].firstChild.nodeValue);
        anchor.appendChild(text);
        li.appendChild(anchor);
        ul.appendChild(li);
      }
    }

    outputArea.appendChild(ul);

}

function loadAcroList(acro) {
   if (acro != "")
   {
     setCursor('wait');
     if (req && (req.readyState == 2 || req.readyState == 3)) {
       req.abort();     // falls ein Request läuft, diesen abbrechen
     }

     loadXMLDoc('getentryWS/?acro='+acro);
   }
}


function setCursor(mode) {
  var docBody = document.getElementsByTagName("body")[0];
  docBody.style.cursor = mode;

  var ids = new Array("input","outputB");
  for (i = 0; i < ids.length; i = i+1) {
     var elem = document.getElementById(ids[i]);
     elem.style.cursor = mode;
  }
}


function showDescription(index) {
    document.getElementById("description").innerHTML = "";

    var text, heading, ul, li, dl, dt, dd;

    // 
    var completeAcronym = req.responseXML.getElementsByTagName("Acronym")[index].parentNode;

    // Im Anzeigebereich neue Elemente einfuegen
    var outputArea = document.getElementById("description");

    // Ueberschrift
    heading = document.createElement("h3");
    text = document.createTextNode(completeAcronym.getElementsByTagName("Acronym")[0].getAttributeNode("expansion").value);
    heading.appendChild(text);
    outputArea.appendChild(heading);


    dl = document.createElement("dl");

    // alternative Formen
    var alternateForms = completeAcronym.getElementsByTagName("AlternateForms");
    if (alternateForms.length != 0) {
       var alternatives = alternateForms[0].getElementsByTagName("AlternateForm");

       dt = document.createElement("dt");
       dt.appendChild(document.createTextNode("Alternative Formen"));
       dd = document.createElement("dd");

       ul = document.createElement("ul");
       for (var i = 0; i < alternatives.length; i++) {
          li   = document.createElement("li");
          text = document.createTextNode(alternatives[i].firstChild.nodeValue);
          li.appendChild(text);
          ul.appendChild(li);
       }
       dd.appendChild(ul);

       dl.appendChild(dt);
       dl.appendChild(dd);
    }


    // Urheber
    // Hier wuerde ich gerne completeAcronym.getElementsByTagName("Acronym")[0].hasAttribute("type")
    // abfragem, doch leider ist das eine "DOM Level 2"-Methode, die der
    // MSIE 6 nicht implementiert hat
    if (completeAcronym.getElementsByTagName("Acronym")[0].getAttribute("type") != "") {

       dt = document.createElement("dt");
       dt.appendChild(document.createTextNode("Entwickelt von"));
       dd = document.createElement("dd");

       // p = document.createElement("p");
       text = document.createTextNode(completeAcronym.getElementsByTagName("Acronym")[0].getAttribute("type"));

       // p.appendChild(text);
       dd.appendChild(text);
       dl.appendChild(dt);
       dl.appendChild(dd);
    }





    // Spezifikationen
    var specs = completeAcronym.getElementsByTagName("SpecLoc");
    if (specs.length != 0) {

       dt = document.createElement("dt");
       dt.appendChild(document.createTextNode("Spezifikationen"));
       dd = document.createElement("dd");

       ul = document.createElement("ul");
       for (var i = 0; i < specs.length; i++) {
          li   = document.createElement("li");
          text = document.createTextNode(specs[i].firstChild.nodeValue);

          anchor = document.createElement("a");
          anchor.setAttribute("href",specs[i].firstChild.nodeValue);

          anchor.appendChild(text);
          li.appendChild(anchor);
          ul.appendChild(li);
       }
       dd.appendChild(ul);

       dl.appendChild(dt);
       dl.appendChild(dd);
    }


    // MoreInfo
    var specs = completeAcronym.getElementsByTagName("MoreInfo");
    if (specs.length != 0) {

       dt = document.createElement("dt");
       dt.appendChild(document.createTextNode("Weitere Informationen"));
       dd = document.createElement("dd");

       ul = document.createElement("ul");
       for (var i = 0; i < specs.length; i++) {
          li   = document.createElement("li");
          text = document.createTextNode(specs[i].firstChild.nodeValue);

          anchor = document.createElement("a");
          anchor.setAttribute("href",specs[i].firstChild.nodeValue);

          anchor.appendChild(text);
          li.appendChild(anchor);
          ul.appendChild(li);
       }
       dd.appendChild(ul);

       dl.appendChild(dt);
       dl.appendChild(dd);
    }

    // Definition
    var def = completeAcronym.getElementsByTagName("Definition");
    if (def.length != 0) {

       dt = document.createElement("dt");
       dt.appendChild(document.createTextNode("Definition"));
       dd = document.createElement("dd");

       p = document.createElement("p");
       text = document.createTextNode(def[0].firstChild.nodeValue);
       p.appendChild(text);
       dd.appendChild(p);

       dl.appendChild(dt);
       dl.appendChild(dd);
    }


    outputArea.appendChild(dl);

}

