/*
	Copyright (c) 2004-2016, The JS Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/

//>>built
define("dojo/request/xhr",["../errors/RequestError","./watch","./handlers","./util","../has"],function(t,A,B,m,d){function C(a,b){var c=a.xhr;a.status=a.xhr.status;try{a.text=c.responseText}catch(e){}"xml"===a.options.handleAs&&(a.data=c.responseXML);if(b)this.reject(b);else{try{B(a)}catch(e){var g=e}m.checkStatus(c.status)?g?this.reject(g):this.resolve(a):(b=g?new t("Unable to load "+a.url+" status: "+c.status+" and an error in handleAs: transformation of response",a):new t("Unable to load "+a.url+
" status: "+c.status,a),this.reject(b))}}function D(a){return this.xhr.getResponseHeader(a)}function n(a,b,c){var g=d("native-formdata")&&b&&b.data&&b.data instanceof FormData,e=m.parseArgs(a,m.deepCreate(E,b),g);a=e.url;b=e.options;var u=!b.data&&"POST"!==b.method&&"PUT"!==b.method;10>=d("ie")&&(a=a.split("#")[0]);var q,h=m.deferred(e,v,w,F,C,function(){q&&q()}),f=e.xhr=n._create();if(!f)return h.cancel(new t("XHR was not created")),c?h:h.promise;e.getHeader=D;x&&(q=x(f,h,e,b.uploadProgress));var k=
"undefined"===typeof b.data?null:b.data,l=!b.sync,G=b.method;try{f.open(G,a,l,b.user||y,b.password||y);b.withCredentials&&(f.withCredentials=b.withCredentials);d("native-response-type")&&b.handleAs in z&&(f.responseType=z[b.handleAs]);var p=b.headers;a=g||u?!1:"application/x-www-form-urlencoded";if(p)for(var r in p)"content-type"===r.toLowerCase()?a=p[r]:p[r]&&f.setRequestHeader(r,p[r]);a&&!1!==a&&f.setRequestHeader("Content-Type",a);p&&"X-Requested-With"in p||f.setRequestHeader("X-Requested-With",
"XMLHttpRequest");m.notify&&m.notify.emit("send",e,h.promise.cancel);f.send(k)}catch(H){h.reject(H)}A(h);f=null;return c?h:h.promise}d.add("native-xhr",function(){return"undefined"!==typeof XMLHttpRequest});d.add("dojo-force-activex-xhr",function(){return d("activex")&&"file:"===window.location.protocol});d.add("native-xhr2",function(){if(d("native-xhr")&&!d("dojo-force-activex-xhr")){var a=new XMLHttpRequest;return"undefined"!==typeof a.addEventListener&&("undefined"===typeof opera||"undefined"!==
typeof a.upload)}});d.add("native-formdata",function(){return"undefined"!==typeof FormData});d.add("native-blob",function(){return"undefined"!==typeof Blob});d.add("native-arraybuffer",function(){return"undefined"!==typeof ArrayBuffer});d.add("native-response-type",function(){return d("native-xhr")&&"undefined"!==typeof(new XMLHttpRequest).responseType});d.add("native-xhr2-blob",function(){if(d("native-response-type")){var a=new XMLHttpRequest;a.open("GET","https://dojotoolkit.org/",!0);a.responseType=
"blob";var b=a.responseType;a.abort();return"blob"===b}});var z={blob:d("native-xhr2-blob")?"blob":"arraybuffer",document:"document",arraybuffer:"arraybuffer"};if(d("native-xhr2")){var w=function(a){return!this.isFulfilled()};var v=function(a,b){b.xhr.abort()};var x=function(a,b,c,g){function e(k){b.handleResponse(c)}function u(k){k=new t("Unable to load "+c.url+" status: "+k.target.status,c);b.handleResponse(c,k)}function q(k,l){c.transferType=k;l.lengthComputable?(c.loaded=l.loaded,c.total=l.total,
b.progress(c)):3===c.xhr.readyState&&(c.loaded="loaded"in l?l.loaded:l.position,b.progress(c))}function h(k){return q("download",k)}function f(k){return q("upload",k)}a.addEventListener("load",e,!1);a.addEventListener("error",u,!1);a.addEventListener("progress",h,!1);g&&a.upload&&a.upload.addEventListener("progress",f,!1);return function(){a.removeEventListener("load",e,!1);a.removeEventListener("error",u,!1);a.removeEventListener("progress",h,!1);a.upload.removeEventListener("progress",f,!1);a=null}}}else{w=
function(a){return a.xhr.readyState};var F=function(a){return 4===a.xhr.readyState};v=function(a,b){var c=b.xhr,g=typeof c.abort;"function"!==g&&"object"!==g&&"unknown"!==g||c.abort()}}var y,E={data:null,query:null,sync:!1,method:"GET"};n._create=function(){throw Error("XMLHTTP not available");};if(d("native-xhr")&&!d("dojo-force-activex-xhr"))n._create=function(){return new XMLHttpRequest};else if(d("activex"))try{new ActiveXObject("Msxml2.XMLHTTP"),n._create=function(){return new ActiveXObject("Msxml2.XMLHTTP")}}catch(a){try{new ActiveXObject("Microsoft.XMLHTTP"),
n._create=function(){return new ActiveXObject("Microsoft.XMLHTTP")}}catch(b){}}m.addCommonMethods(n);return n});