<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  void Page_Load()
  {
    if (Request.QueryString["sendData"] != null &&
        Request.QueryString["sendData"] == "ok")
    {
      string xml = "<book title=\"ASP.NET AJAX\" author=\"Christian Wenz\"><chapters><chapter number=\"1\" title=\"Wprowadzenie\" /><chapter number=\"2\" title=\"JavaScript\" /><chapter number=\"3\" title=\"AJAX\" /></chapters></book>";
      Response.ContentType = "text/xml";
      Response.Write(xml);
      Response.End();
    }
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>AJAX w ASP.NET</title>

  <script language="Javascript" type="text/javascript">
function getXMLHTTP() {
  var XMLHTTP = null;
  if (window.ActiveXObject) {
    try {
      XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
      }
    }
  } else if (window.XMLHttpRequest) {
    try {
      XMLHTTP = new XMLHttpRequest();
    } catch (e) {
    }
  }
  return XMLHTTP;
}

var XMLHTTP;
window.onload = function() {
  XMLHTTP = getXMLHTTP();
  if (XMLHTTP != null) {
    XMLHTTP.open("GET", "xmldocument2.aspx?sendData=ok");
    XMLHTTP.onreadystatechange = stateChanged;
    XMLHTTP.send(null);
  }
}

function stateChanged() {
  if (XMLHTTP.readyState == 4 && 
      XMLHTTP.status == 200) {
    var xml = XMLHTTP.responseXML;
    var root = xml.documentElement;

    document.getElementById("output").innerHTML = 
                   root.getAttribute("title") + 
                   " autor: " + 
                   root.getAttribute("author");
                   
    var list = document.getElementById("list");
    var chapters = xml.getElementsByTagName("chapters")[0]; 
    for (var i=0; i<chapters.childNodes.length; i++) {
      if (chapters.childNodes[i].nodeName == "chapter") {
        var listItem = document.createElement("li");
        var listItemText = document.createTextNode(
          chapters.childNodes[i].getAttribute("number") + 
          ": " + 
          chapters.childNodes[i].getAttribute("title"));
        listItem.appendChild(listItemText);
        list.appendChild(listItem);
      }
    }
  }
}
  </script>

</head>
<body>
  <p id="output">Czekaj i patrz...</p>
  <ul id="list"></ul>
</body>
</html>
