/* global exports */
/* global XMLHttpRequest */
/* global module */
/* global process */
"use strict";

exports._ajax = function () {
  var platformSpecific = { };
  if (typeof module !== "undefined" && module.require && !(typeof process !== "undefined" && process.versions["electron"])) {
    // We are on node.js
    platformSpecific.newXHR = function () {
      var XHR = module.require("xhr2");
      return new XHR();
    };

    platformSpecific.fixupUrl = function (url) {
      var urllib = module.require("url");
      var u = urllib.parse(url);
      u.protocol = u.protocol || "http:";
      u.hostname = u.hostname || "localhost";
      return urllib.format(u);
    };

    platformSpecific.getResponse = function (xhr) {
      return xhr.response;
    };
  } else {
    // We are in the browser
    platformSpecific.newXHR = function () {
      return new XMLHttpRequest();
    };

    platformSpecific.fixupUrl = function (url) {
      return url || "/";
    };

    platformSpecific.getResponse = function (xhr) {
      return xhr.response;
    };
  }

  return function (mkHeader, options) {
    return function (errback, callback) {
      var xhr = platformSpecific.newXHR();
      var fixedUrl = platformSpecific.fixupUrl(options.url);
      xhr.open(options.method || "GET", fixedUrl, true, options.username, options.password);
      if (options.headers) {
        try {
          for (var i = 0, header; (header = options.headers[i]) != null; i++) {
            xhr.setRequestHeader(header.field, header.value);
          }
        } catch (e) {
          errback(e);
        }
      }
      xhr.onerror = function () {
        errback(new Error("AJAX request failed: " + options.method + " " + options.url));
      };
      xhr.onload = function () {
        callback({
          status: xhr.status,
          headers: xhr.getAllResponseHeaders().split("\r\n")
            .filter(function (header) {
              return header.length > 0;
            })
            .map(function (header) {
              var i = header.indexOf(":");
              return mkHeader(header.substring(0, i))(header.substring(i + 2));
            }),
          response: platformSpecific.getResponse(xhr)
        });
      };
      xhr.responseType = options.responseType;
      xhr.withCredentials = options.withCredentials;
      xhr.send(options.content);

      return function (/* error */) {
        return function (cancelErrback, cancelCallback) {
          try {
            xhr.abort();
          } catch (e) {
            return cancelErrback(e);
          }
          return cancelCallback();
        };
      };
    };
  };
}();


@@ -2383,48 +2383,15 @@
 on (
-/* 
 error
+,
  
-*/) %7B%0A        return function (
 canc
@@ -2415,26 +2415,24 @@
 Callback) %7B%0A
-  
         try 
@@ -2439,26 +2439,24 @@
 %7B%0A          
-  
 xhr.abort();
@@ -2452,26 +2452,24 @@
 hr.abort();%0A
-  
         %7D ca
@@ -2480,34 +2480,32 @@
 (e) %7B%0A          
-  
 return cancelErr
@@ -2517,30 +2517,26 @@
 e);%0A        
- 
- 
 %7D%0A
-  
         retu
@@ -2560,19 +2560,8 @@
 ();%0A
-        %7D;%0A
     

Return the correctly formed canceller