var Common={humanFileSize:function(n,t){var r=t?1e3:1024,u,i;if(Math.abs(n)<r)return n+" B";u=t?["kB","MB","GB","TB","PB","EB","ZB","YB"]:["KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"];i=-1;do n/=r,++i;while(Math.abs(n)>=r&&i<u.length-1);return n.toFixed(1)+" "+u[i]},getMegaBytes:function(n){return n/=1e6,n.toFixed(2)+" MB"},camelcaseString:function(n){return n!=undefined&&n!=null&&n.length>0?n[0].toLowerCase()+n.substr(1):""}};Common.Form={toJSON:function(n){function t(n){return n.type==="checkbox"}function i(n){return n.options&&n.multiple}function r(n){return!["checkbox","radio"].includes(n.type)||n.checked}function u(n){return n.name&&n.value}function f(n){return[].reduce.call(n,function(n,t){return t.selected?n.concat(t.value):n},[])}n=$(n);var e=n.serializeArray();return[].reduce.call(e,function(n,e){return u(e)&&r(e)&&(n[e.name]=t(e)?(n[e.name]||[]).concat(e.value):i(e)?f(e):e.value),n},{})}};Common.Utils={objectToFormData:function(n,t,i){var f=t||new FormData,o,u,r,e;if(i=i||"",$.isArray(n)){for(r=0;r<n.length;r++)e=i+"["+r.toString()+"]",this.objectToFormData(n[r],f,e);return f}if(typeof n!="object"||n instanceof File)return typeof n!="undefined"&&n!==null&&n!==""&&f.append(i,n),f;for(u in n)if(n.hasOwnProperty(u))if(o=i===""?u:i+"."+u,$.isArray(n[u]))for(r=0;r<n[u].length;r++)e=o+"["+r.toString()+"]",this.objectToFormData(n[u][r],f,e);else this.objectToFormData(n[u],f,o);return f},saveFormToServer:function(n,t,i,r,u,f){var a,p,w,v,b,s,e,o,h;if(n=$(n),n.valid()!==!1){var c=n.serializeArray(),l,y=n.find("input:[type=file]"),k=y.toArray().some(function(n){var t=$(n).data("files");return typeof t!="undefined"&&t.length>0});if(k){for(a=new FormData,e=0;e<y.length;e++)for(p=y[e],w=$(p).data("files"),v=0;v<w.length;v++)b=w[v],ISeD.Global.objectToFormData(b,a,p.name);for(e=0;e<c.length;e++)o=c[e],ISeD.Global.objectToFormData(ISeD.Global.getInputValue(n,o),a,o.name);l=ISeD.Global.sendFormDataToServer(a,t)}else{for(s={},e=0;e<c.length;e++){o=c[e];h=ISeD.Global.getInputValue(n,o);try{h=JSON.parse(h)}catch(d){}s[o.name]!==undefined?(s[o.name].push||(s[o.name]=[s[o.name]]),s[o.name].push(h)):s[o.name]=h}l=ISeD.Global.sendJsonToServer(s,t)}l.done(function(t){t.Success?(i(t),n[0].reset()):ENCO.WebTools.showMessage(t.Message||r,null,u,f)});l.error(function(){ENCO.WebTools.showMessage(r,null,u,f)})}},sendJsonToServer:function(n,t){var i=new ENCO.AjaxConfig;return i.Url=t,i.Data=JSON.stringify(n),i.ShowLoader=!0,i.ProcessData=!1,i.ContentType="application/json",ENCO.WebTools.ajax(i)},sendFormDataToServer:function(n,t){var i=new ENCO.AjaxConfig;return i.Url=t,i.Data=n,i.ShowLoader=!0,i.ProcessData=!1,i.ContentType=!1,ENCO.WebTools.ajax(i)},updateQueryString:function(n,t,i){var u,r,f;return n||(n=window.location.href),u=new RegExp("([?&])"+t+"=.*?(&|#|$)(.*)","gi"),u.test(n)?typeof i!="undefined"&&i!==null?n.replace(u,"$1"+t+"="+i+"$2$3"):(r=n.split("#"),n=r[0].replace(u,"$1$3").replace(/(&|\?)$/,""),typeof r[1]!="undefined"&&r[1]!==null&&(n+="#"+r[1]),n):typeof i!="undefined"&&i!==null?(f=n.indexOf("?")!==-1?"&":"?",r=n.split("#"),n=r[0]+f+t+"="+i,typeof r[1]!="undefined"&&r[1]!==null&&(n+="#"+r[1]),n):n},insert:function(n,t,i){return typeof i=="undefined"&&(i=0),typeof t=="undefined"&&(t=""),n.slice(0,i)+t+n.slice(i)},guid:function(){function n(){return Math.floor((1+Math.random())*65536).toString(16).substring(1)}return n()+n()+"-"+n()+"-"+n()+"-"+n()+"-"+n()+n()+n()},randomString:function(n){var t=Math.random().toString(36).slice(2)+Math.random().toString(36).slice(2)+Math.random().toString(36).slice(2);return t.substr(0,n)},downloadBlob:function(n,t){var i=URL.createObjectURL(n),r=$("<a />",{href:i,download:t||n.filename||"dokumentum",text:"click"}).hide().appendTo("body")[0].click();setTimeout(function(){URL.revokeObjectURL(i)},1e4)},extractContentDisposition:function(n){var t=/filename[^;=\n]*=(UTF-8(['"]*))?(.*)/.exec(n),i;return t!=null&&t[3]&&(i=t[3].replace(/['"]/g,"")),decodeURI(i)},extend:function(){function n(n,t){return _.isUndefined(n)?t:n}var t=_.partialRight(_.assignWith,n);return t.apply(null,arguments)}};Common.Ajax={get:function(n,t,i,r){return this.sendAjaxRequest(_.assign({type:"GET",url:n,data:t,customErrorHandler:i===!0},r||{}))},put:function(n,t,i,r,u){return this.sendAjaxRequest(_.assign({type:"PUT",url:n,data:t,customErrorHandler:r===!0,form:i},u||{}))},post:function(n,t,i,r,u){return this.sendAjaxRequest(_.assign({type:"POST",url:n,data:t,customErrorHandler:r===!0,form:i},u||{}))},del:function(n,t,i,r){return this.sendAjaxRequest(_.assign({type:"DELETE",url:n,data:t,customErrorHandler:i===!0},r||{}))},file:function(n,t,i,r,u){return this.sendAjaxRequest(_.assign({type:"GET",url:n,data:t,xhrFields:{responseType:"arraybuffer"},converters:{"binary json":function(){var n=String.fromCharCode.apply(null,new Uint8Array(arguments[0]));return n=decodeURIComponent(escape(n)),JSON.parse(n)}},customErrorHandler:r===!0,responseParser:function(n,t,i){var u=i.getResponseHeader("content-type")||"application/octet-stream",r=new Blob([n],{type:u});return r.filename=Common.Utils.extractContentDisposition(i.getResponseHeader("content-disposition")),$.Deferred().resolve(r,t,i)},form:i},u||{}))},sendAjaxRequest:function(n){var t,i;return n.form&&(n.form.validate(),!n.form.valid())?$.Deferred().reject(undefined):(n.data instanceof FormData&&(n.cache=!1,n.contentType=!1,n.processData=!1),t=$.ajax(n).catch(this.parseAjaxError),n.customErrorHandler||(i=this,t=t.fail(function(t){i.handleAjaxError(t,n.form)})),n.responseParser&&(t=t.then(n.responseParser)),t)},parseAjaxError:function(n,t,i){if(n.status===403)try{var r=n.responseJSON||JSON.parse(n.responseText);r&&(n.errorCode=r.code,n.errorMessage=r.message)}catch(u){}return $.Deferred().reject(n,t,i)},handleAjaxError:function(n,t){if(n!==undefined&&n.status!==0)if(n.status===423&&Common.redirect("/",!1),t!==undefined&&n.status===422){var i=Common.Error.handleModelError(n,t);i&&Common.Modal.showError(i)}else Common.Modal.showError(n.errorMessage||n.responseText||n.statusText)}};Common.Modal={show:function(n,t,i){i=$.extend({show:!0,backdrop:!0,padding:!0,footer:undefined},i);var r=i.footer?'<div class="modal-footer">'+i.footer+"<\/div>":"",u=$('<div class="modal fade" role="dialog"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;<\/button><h4 class="modal-title">'+n+'<\/h4><\/div><div class="modal-body'+(i.padding?"":"no-padding")+'">'+t+"<\/div>"+r+"<\/div><\/div><\/div>");return this._showModal(u,i)},showWithCustomContent:function(n,t){t=$.extend({show:!0,backdrop:!0,modalClass:""},t);var i=$('<div class="modal fade '+t.modalClass+'" role="dialog"><div class="modal-dialog"><div class="modal-content">'+n+"<\/div><\/div><\/div>");return this._showModal(i,t)},showError:function(n){var u=$("#modalError"),t=u.find("p[error-text]"),i,r;if(n){if($.isArray(n)){for(i="",r=0;r<n.length;r++)i=i+n[r].toString()+"<br />";t.html(i)}else t.text(n);t.show()}else t.text(""),t.hide();u.modal("hide");u.modal("show")},_showModal:function(n,t){$("body").append(n);n.on("hide.bs.modal",function(){n.find("*").off()});n.on("hidden.bs.modal",function(){n.off();n.remove()});return n.modal(t),n}}