function editLanguage() {
  if (window.XMLHttpRequest)
    var doc = new XMLHttpRequest();
  else if (window.ActiveXObject)
    var doc = new ActiveXObject("Microsoft.XMLHTTP")
  doc.onreadystatechange = function() {
    if (doc.readyState == 4) {
      if (doc.status == 200) {
        var content = document.getElementById("languageText").firstChild.nodeValue;
        content = content.strip();
        var language = document.getElementById("language");
        removeAllChildren(language);
        var select = createElement("select");
        language.appendChild(select);
        for (node = doc.responseXML.documentElement.firstChild; node; node = node.nextSibling)
          if (node.nodeType == 1) {
            var option = createElement("option");
            var code;
            var name;
            for (subNode = node.firstChild; subNode; subNode = subNode.nextSibling) {
              switch (subNode.nodeName) {
                case "code":
                  code = subNode.firstChild.nodeValue;
                  option.setAttribute("value", code);
                  setAttribute(option, "http://www.w3.org/XML/1998/namespace", "lang", subNode.firstChild.nodeValue);
                  break;
                case "name":
                  name = subNode.firstChild.nodeValue;
                  if (name == content)
                    option.setAttribute("selected", "selected");
              }
            }
            var text = document.createTextNode(code + " - " + name);
            option.appendChild(text);
            select.appendChild(option);
          }
        var input = createElement("input");
        input.setAttribute("type", "button");
        input.value = "Save";
        language.appendChild(input);
        input.onclick = function() {
          var text = select.value;
          for (node = select.firstChild; node; node = node.nextSibling)
            if (node.getAttribute("value") == text)
              var name = node.firstChild.nodeValue;
          var parms = "id=" + id + "&language=" + text;
          if (window.XMLHttpRequest)
            doc = new XMLHttpRequest();
          else if (window.ActiveXObject)
            doc = new ActiveXObject("Microsoft.XMLHTTP");
          doc.onreadystatechange = function() {
            if (doc.readyState == 4) {
              if (doc.status == 200) {
                removeAllChildren(language);
                language.appendChild(document.createTextNode("Language: "));
                var a = createElement("a");
                a.setAttribute("id", "languageText");
                a.setAttribute("href", "javascript:editLanguage()");
                setAttribute(a, "http://www.w3.org/XML/1998/namespace", "lang",
                             text);
                language.appendChild(a);
                a.appendChild(document.createTextNode(name.split(" - ")[1]));
                setAttribute(document.getElementById("descriptionText"),
                             "http://www.w3.org/XML/1998/namespace", "lang",
                             text);
                setAttribute(document.getElementById("subjectText"),
                             "http://www.w3.org/XML/1998/namespace", "lang",
                             text);
                language.appendChild(document.createTextNode(" "));
                a = createElement("a");
                a.setAttribute("href", "/faq.php#language");
                language.appendChild(a);
                var img = createElement("img");
                img.setAttribute("src", "/help.png");
                img.setAttribute("alt", "Help");
                a.appendChild(img);
                language.appendChild(a);
              } else
                alert("Error saving the language.  Please try again, later.");
            }
          }
          doc.open("POST", "/saveDetails.php", true);
          doc.setRequestHeader("Content-type",
            "application/x-www-form-urlencoded");
          doc.setRequestHeader("Content-length", parms.length);
          doc.setRequestHeader("Connection", "close");
          doc.send(parms);
        }
      }
    }
  }
  doc.open("GET", "/getChoices.php?data=languages", true);
  if (window.XMLHttpRequest)
    doc.send(null);
  else if (window.ActiveXObject)
    doc.send();
}

function editDescription() {
  var content = document.getElementById("descriptionText").firstChild.nodeValue;
  content = content.strip();
  var description = document.getElementById("description");
  removeAllChildren(description);
  var textarea = createElement("textarea");
  textarea.setAttribute("id", "descriptionText");
  textarea.setAttribute("rows", 20);
  textarea.style.width = "100%";
  textarea.appendChild(document.createTextNode(content));
  description.appendChild(textarea);
  var input = createElement("input");
  input.setAttribute("type", "button");
  input.value = "Save";
  description.appendChild(input);
  setupOnclick(input, "description", standardSave);
}

function standardSave(name) {
  var text = document.getElementById(name + "Text").value;
  var parent = document.getElementById(name);
  removeAllChildren(parent);
  var a = createElement("a");
  a.setAttribute("id", name + "Text");
  var tCase = name.substring(0, 1).toUpperCase() + name.substring(1, name.length);
  a.setAttribute("href", "javascript:edit" + tCase + "()");
  parent.appendChild(a);
  a.appendChild(nl2br(text));
}

function nl2br(text) {
  var span = createElement("span");
  var lines = text.split(/[\n\r]/);
  for (var i = 0; i < lines.length; i++) {
    var line = lines[i];
    if (line != null) {
      if (i) {
        span.appendChild(createElement("br"));
      }
      span.appendChild(document.createTextNode(line));
    }
  }
  return span;
}

function setupOnclick(input, name, save) {
  input.onclick = function() {
    var text = document.getElementById(name + "Text").value;
    saveData("/saveDetails.php", name, "id=" + id + "&" + name + "=" + encodeURIComponent(text), save);
  }
}

Event.observe(window, "load", function() {
  var errorHandler = function(transport) {
    if (transport.status == 403) {
      location = "/signin.php";
    } else {
      alert("Error(" + transport.status + ") saving.  Please try again, later.");
    }
  };

  var updateDetails = function(parms) {
    new Ajax.Request("/saveDetails.php", {
      parameters: parms,
      onFailure: errorHandler
    });
  };

  $("adult").observe("change", function() {
    updateDetails({"id": id, adult: $F("adult")});
  });

  var editSubject = function() {
    var content = $("subject").firstChild.nodeValue;
    content = content.strip();
    removeAllChildren($("subject"));
    var input = createElement("input");
    input.setAttribute("id", "subjectText");
    input.setAttribute("type", "textbox");
    input.setAttribute("size", content.length * 1.25);
    input.value = content;
    $("subject").appendChild(input);
    input.activate();
    input.observe("blur", function() {
      var text = $("subjectText").value;
      updateDetails({"id": id, subject: text});
      removeAllChildren($("subject"));
      $("subject").appendChild(document.createTextNode(text));
    });
  }
  $("subject").observe("click", editSubject);

  var editUrl = function() {
    var content;
    if ($("urlText")) {
      content = $("urlText").firstChild.nodeValue.strip();
    } else {
      content = "";
    }
    removeAllChildren($("url"));
    var input = createElement("input");
    Element.extend(input);
    input.setAttribute("id", "urlText");
    input.setAttribute("type", "textbox");
    input.value = content;
    $("url").appendChild(input);
    input.activate();
    input.observe("blur", function() {
      var text = $("urlText").value;
      if (text && !text.startsWith("http")) {
        text = "http://" + text;
      }
      updateDetails({"id": id, url: text});
      $("url").firstDescendant().remove();
      $("url").appendChild(document.createTextNode("URL: "));
      if (text) {
        var a = createElement("a");
        a.setAttribute("id", "urlText");
        a.setAttribute("href", text);
        a.setAttribute("rel", "nofollow");
        a.appendChild(document.createTextNode(text));
        $("url").appendChild(a);
        $("url").appendChild(document.createTextNode(" "));
      } else {
        $("url").appendChild(document.createTextNode("None "));
      }
      var span = createElement("span");
      Element.extend(span);
      span.observe("click", editUrl);
      var img = createElement("img");
      img.setAttribute("src", "/change.png");
      img.setAttribute("alt", "Edit");
      span.appendChild(img);
      $("url").appendChild(span);
    });
  }
  $("editUrl").observe("click", editUrl);
});
