!function(e){var s=new function(){var r=this;r.width=ko.observable(e("#main").outerWidth());var t=function(){var r=this;r.oldHostname="",r.serialize=function(){return{id:r.id(),hostname:r.hostname(),port:r.port(),enableSSL:r.enableSSL()?1:0,enableAuthentication:r.enableAuthentication()?1:0,username:r.username(),password:r.password()}},r.update=function(){if(!s.mailservers.loading)if(r.hostname.isValid())if(r.editing(!1),r.port.isValid()){if(r.enableAuthentication()){if(!r.username.isValid()||!r.password.isValid())return}else r.username().length>0&&(r.usernameSubscription.dispose(),r.username(""),r.usernameSubscription=r.username.subscribe(r.update)),r.password().length>0&&(r.passwordSubscription.dispose(),r.password(""),r.passwordSubscription=r.password.subscribe(r.update));r.id()?e.ajax({url:"../../admin/mailservers/update",data:r.serialize(),type:"POST",timeout:3e4,error:MMONIT.error.json}):(e.ajax({url:"../../admin/mailservers/create",data:r.serialize(),type:"POST",dataType:"json",timeout:3e4,success:function(e,s,t){r.id(e.id)},error:MMONIT.error.json}),s.mailservers.createInProgress(!1)),s.mailservers.expandedRow()&&r.test()}else s.mailservers.expandedRow(r),e("#mailserver"+r.id()+" .mailPort").focus();else r.editing(!0)},r.test=function(){if(r.hostname().length>0&&r.port()>0){r.testInProgress&&4!=r.testInProgress.readyState&&r.testInProgress.abort(),r.showTestResult(!0);var s=e("#mailserver"+r.id()+" .testResult .accessory");s.html("<img src='../../img/spinner_small.gif' height='16' width='16' />"),r.testInProgress=e.ajax({url:"../../admin/mailservers/test",data:r.serialize(),type:"POST",dataType:"json",timeout:3e4,success:function(e,r,t){s.css("color",0==e.status?"red":"green"),s.text(e.description)},error:function(e,r,t){"timeout"===r?(s.css("color","red"),s.text("Test timed out")):"abort"!==r&&MMONIT.error.json(e,r,t)}})}},r.edit=function(e,s){s.stopPropagation(),r.oldHostname=r.hostname(),r.editing(!0)},r.stopEdit=function(){r.hostname().trim().length>0&&(r.oldHostname!==r.hostname()?(r.oldHostname=r.hostname(),r.update()):r.editing(!1))},r.toggleSSL=function(e,s){s.stopPropagation(),r.enableSSL(!r.enableSSL())},r.toggleAuthentication=function(e,s){s.stopPropagation(),r.enableAuthentication(!r.enableAuthentication())},r.id=ko.observable(null),r.hostname=ko.observable("").extend({required:!0,maxLength:255}),r.port=ko.observable().extend({required:!0,number:!0,min:1,max:65535,step:1}),r.enableSSL=ko.observable(!1),r.enableAuthentication=ko.observable(!1),r.username=ko.observable("").extend({required:!0,maxLength:255}),r.password=ko.observable("").extend({required:!0,maxLength:255}),r.editing=ko.observable(!1),r.showTestResult=ko.observable(!1),r.mouseover=ko.observable(ISTABLET),r.portSubscription=r.port.subscribe(r.update),r.enableSSLSubscription=r.enableSSL.subscribe(r.update),r.enableAuthenticationSubscription=r.enableAuthentication.subscribe(r.update),r.usernameSubscription=r.username.subscribe(r.update),r.passwordSubscription=r.password.subscribe(r.update)};r.mailservers={loading:!0,list:ko.observableArray(),mailserver:ko.observable(),expandedRow:ko.observable(),createInProgress:ko.observable(!1),toggleExpand:function(e,s){s.stopPropagation(),r.mailservers.expandedRow(r.mailservers.expandedRow()!==e?e:null),r.mailservers.expandedRow()&&e.test()},isExpanded:function(e){return e===r.mailservers.expandedRow()},enableMouseover:function(e,s){s.stopPropagation(),ISTABLET||e.mouseover(!0)},disableMouseover:function(e,s){s.stopPropagation(),ISTABLET||e.mouseover(!1)},confirmDelete:function(s,t){t.stopPropagation(),r.mailservers.mailserver(s),e("#confirmMailserverDelete").modal("show")},add:function(e,s){if(s.stopPropagation(),!r.mailservers.createInProgress()){r.mailservers.createInProgress(!0),r.mailservers.expandedRow(null);var a=new t;a.editing(!0),r.mailservers.list.push(a)}},remove:function(s,t){t.stopPropagation(),s.id()>0?e.ajax({url:"../../admin/mailservers/delete",data:{id:s.id()},type:"POST",timeout:3e4,success:function(t,a,o){e("#confirmMailserverDelete").modal("hide"),r.mailservers.list.remove(s)},error:function(s,r,t){e("#confirmMailserverDelete").modal("hide"),MMONIT.error.json(s,r,t)}}):(e("#confirmMailserverDelete").modal("hide"),r.mailservers.createInProgress(!1),r.mailservers.list.remove(s))}},e.getJSON("../../admin/mailservers/list",(function(e){for(var s=0;s<e.length;s++){var a=new t;a.id(e[s].id),a.hostname(e[s].hostname),a.port(e[s].port),a.enableSSL(!!e[s].enableSSL),a.enableAuthentication(""!=e[s].username&&""!=e[s].password),a.username(e[s].username),a.password(e[s].password),r.mailservers.list.push(a)}r.mailservers.loading=!1})).fail(MMONIT.error.json)};ko.validation.init({insertMessages:!1,decorateElement:!0,errorElementClass:"error"},!0),ko.applyBindings(s,document.getElementById("mailTab")),e(window).on("resize orientationchange",(function(r){s.width(e("#main").outerWidth())}))}(jQuery);