var xhReqForAsync;

window.onload = function() {

  andThen(function() {
    var xhReq = createXMLHttpRequest();
    xhReq.open("GET", "sumGet.phtml?figure1=5&figure2=1", false);
    xhReq.send(null);
    var serverResponse = xhReq.responseText;
    $("response").innerHTML = serverResponse;
  });

  andThen(function() {
    xhReqForAsync = createXMLHttpRequest();
    xhReqForAsync.open("GET", "sumGet.phtml?figure1=5&figure2=2", true);
    xhReqForAsync.onreadystatechange = onSumResponse;
    xhReqForAsync.send(null);
  });

  andThen(function() {
    var xhReq = createXMLHttpRequest();
    xhReq.open("GET", "sumGet.phtml?figure1=5&figure2=3", true);
    xhReq.onreadystatechange = function() {
      if (xhReq.readyState != 4) { return; }
      $("response").innerHTML = xhReq.responseText;
    };
    xhReq.send(null);
  });

  andThen(function() {
    var xhReq = createXMLHttpRequest();
    xhReq.open("GET", "sumGet.phtml?figure1=5&figure2=4", true);
    xhReq.onreadystatechange = function() {
      if (xhReq.readyState != 4)  { return; }
      if (xhReq.status != 200)  {
        $("response").innerHTML =
          "Can't deal with status "+xhReq.status+" '" + xhReq.statusText + "'.";
      }
      $("response").innerHTML = xhReq.responseText;
    };
    xhReq.send(null);
  });

  andThen(function() {
    var xhReq = createXMLHttpRequest();
    xhReq.open("GET", "nonExistent.phtml?figure1=5&figure2=5", true);
    xhReq.onreadystatechange = function() {
      if (xhReq.readyState != 4)  { return; }
      if (xhReq.status != 200)  {
        $("response").innerHTML =
          "Can't deal with status "+xhReq.status+" '" + xhReq.statusText + "'.";
        return;
      }
      $("response").innerHTML = xhReq.responseText;
    };
    xhReq.send(null);
  });

  andThen(function() {
    var xhReq = createXMLHttpRequest();
    xhReq.open("POST", "sumPostGeneric.phtml", true);
    xhReq.onreadystatechange = function() {
      if (xhReq.readyState != 4) { return; }
      $("response").innerHTML = xhReq.responseText;
    };
    xhReq.send("Calculate this sum: 5+6");
  });

  andThen(function() {
    var xhReq = createXMLHttpRequest();
    xhReq.open("POST", "sumPostForm.phtml", true);
    xhReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhReq.onreadystatechange = function() {
      if (xhReq.readyState != 4) { return; }
      $("response").innerHTML = xhReq.responseText;
    };
    xhReq.send("figure1=5&figure2=7");
  });


  andThen(function() {
    var xhReq = createXMLHttpRequest();
    xhReq.open("GET", "sumXML.phtml?figure1=5&figure2=8", true);
    xhReq.onreadystatechange = function() {
      if (xhReq.readyState != 4) { return; }
      xml = xhReq.responseXML;
      var figure1 = xml.getElementsByTagName("figure")[0].firstChild.nodeValue;
      var figure2 = xml.getElementsByTagName("figure")[1].firstChild.nodeValue;
      var sum = xml.getElementsByTagName("outputs")[0].firstChild.nodeValue;
      $("response").innerHTML = sum + " (From " +figure1+ " + " + figure2 +")";
    };
    xhReq.send(null);
  });

  // Same as above, but this time, should timeout.
  andThen(function() {
    $("response").innerHTML = "Sending at " + new Date();
    var xhReq = createXMLHttpRequest();
    xhReq.open("GET", "infiniteLoop.phtml", true);
    var requestTimer = window.setTimeout(function() {
      xhReq.abort();
      $("response").innerHTML += ". *** Timed out at " + new Date();
    }, 5000);
    xhReq.onreadystatechange = function() {
      if (xhReq.readyState != 4)  { return; }
      window.clearTimeout(requestTimer);
      if (xhReq.status != 200)  {
        $("response").innerHTML =
          "Can't deal with status "+xhReq.status+" '" + xhReq.statusText + "'.";
      }
      $("response").innerHTML += "Got back '" + xhReq.responseText + "'"
        + " at " + new Date();
    };
    xhReq.send(null);
  });

}

function onSumResponse() {
  if (xhReqForAsync.readyState < 4) { return; }
  $("response").innerHTML = xhReqForAsync.responseText;
}

function createXMLHttpRequest() {
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
  try { return new XMLHttpRequest(); } catch(e) {}
  alert("XMLHttpRequest not supported");
  return null;
}
