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