(function(){"use strict";var e={};u8.Base.Files=function(t){$.extend(this,t);this.id="files";this.supportsFileAPI=true;this._httpUploadTransport=e.xhr;this._url="~/crm/FileUpload.aspx"};$.extend(u8.Base.Files.prototype,{upload:function(e,t){if(!e){return}var r=new u8.Base.FileUploadCommand(e);r.execute(t)},uploadForm:function(e,t){if(!e.url){e.url=this._url}var r=this;return this._httpUploadTransport(e,function(e){if(e.error){var a=u8.tools.createError(e.error);u8.log.exception("Exception: ",a)}return t&&t(r,e)})},getFileInfo:function(e){if(!e){return undefined}var t;if("lastModifiedDate"in e){t=e.lastModifiedDate.clone()}else if("lastModified"in e){t=new Date(e.lastModified)}return{type:e.type,size:e.size,name:e.name,lastModifiedDate:t}},equalsFileInfo:function(e,t){if(!e&&!t){return true}if(e&&t){return e.lastModifiedDate.equals(t.lastModifiedDate)&&e.type==t.type&&e.size==t.size&&e.name==t.name}return false}});u8.Base.Files.makeClass({name:"Files"});e.xhr=function(){var e=1;return function(r,a){var n=r.form||$$(),i=r.file;var o=n.length?n.window():window;var s=o.page;var u=new o.FormData(n[0]);var l=r.progress||u8._.nop;if(!n.length&&i){u.append("filesData",i)}for(var d in r.params){u.append(d,r.params[d])}var f=new o.XMLHttpRequest;f.addEventListener("error",m,false);f.addEventListener("load",m,false);var p={id:"xhrupload"+e++,percentComplete:0,indeterminate:!("upload"in f),options:r,abort:function(){if(this.done){return}this.aborted=true;f.abort();m()}};l(p);if(!p.indeterminate){f.upload.addEventListener("progress",c,false)}f.open("POST",s.resolveUrl(r.url),true);f.setRequestHeader("Accept","application/json");if(u8._csrfToken){f.setRequestHeader("x-csrf-token",u8._csrfToken)}f.send(u);if(r.onUploadStart){r.onUploadStart(p,{})}function c(e){p.total=e.total;p.loaded=e.loaded;p.percentComplete=p.total==0?100:p.loaded*100/p.total;l(p)}function m(){if(p.done){return}var e=f.readyState>=2;var r=e&&f.getResponseHeader("content-type")||"";var n=f.responseText;var i=e&&f.status;var o=e&&f.statusText;var s=i>=200&&i<=299;p.done=true;p.contentType=r;p.responseText=n;if(r.indexOf("application/json")>=0){try{$.extend(p,n&&JSON.parse(n))}catch(e){}if(!s){p.error=$.extend(p.error,{status:i,message:o})}}else{p.error=t(n);if(!p.error&&!s){p.error={status:i,message:o}}}if(p.aborted){p.error={status:"Aborted",message:"The request has been aborted."}}if(s){p.percentComplete=100}l(p);if(a){a(p)}}return p}}();$.extend(u8.Base.Files,{fileListToArray:function(e){var t=[];if(!e){return t}for(var r=0;r<e.length;r++){t.push(e[r])}return t}});function t(e){var t=e.match(/HTTP Error ([0-9\.]+)[ \.\t-]*([^\n<]+)/i);var r=t&&t[1]||500;var a=$.trim(t&&t[2]);if(!a){t=e.match(/<title>([^<]+)<\/title>/i);if(t&&t[1]){a=$.trim(t[1])}}if(!a){a="Unknown error"}t=e.match(/<h3>([^<]+)<\/h3>/i);if(t&&t[1]){a+=" ("+$.trim(t[1])+")"}return{status:r,message:a}}u8.services.add({name:"files",scope:"Application",ctor:u8.Base.Files})})();