/**
 * XMLHttp request.
 */

var Promise = require('./promise');
var XDomain = window.XDomainRequest;

module.exports = function (_, options) {

    var request = new XMLHttpRequest(), promise;

    if (XDomain && options.crossOrigin) {
        request = new XDomainRequest();
        options.headers = {};
    }

    if (_.isPlainObject(options.xhr)) {
        _.extend(request, options.xhr);
    }


    if (_.isFunction(options.beforeSend)) {

        _.warn('beforeSend has been deprecated in ^0.1.17. ' +
            'Use transformRequest or XHR options instead.'
        );

        options.beforeSend.call(this, request, options);
    }

    promise = new Promise(function (resolve) {

        request.open(options.method, _.url(options), true);
        request.timeout = options.timeout;

        _.each(options.headers, function (value, header) {
            request.setRequestHeader(header, value);
        });

        var handler = function (event) {

            request.ok = event.type === 'load';
            request.headers = request.getAllResponseHeaders();

            if (request.ok && request.status) {
                request.ok = request.status >= 200 && request.status < 300;
            }

            resolve(request);
        };

        request.onload = handler;
        request.onabort = handler;
        request.onerror = handler;
        request.ontimeout = handler;

        request.send(options.data);
    });

    return promise;
};

@@ -226,20 +226,56 @@
 ssOrigin
+ && !('withCredentials' in request)
 ) %7B%0A
+%0A
         
@@ -826,16 +826,65 @@
 meout;%0A%0A
+        if ('setRequestHeader' in request) %7B%0A    
         
@@ -938,32 +938,36 @@
 ) %7B%0A            
+    
 request.setReque
@@ -995,27 +995,41 @@
 e);%0A        
+  
+  
 %7D);
+%0A        %7D
 %0A%0A        va
@@ -1137,16 +1137,53 @@
 eaders =
+ 'getAllResponseHeaders' in request ?
  request
@@ -1206,16 +1206,21 @@
 eaders()
+ : ''
 ;%0A%0A     

use XMLHttpRequest for IE > 9