(function(){var i=YAHOO.util.Dom,a=YAHOO.util.Selector,c=YAHOO.util.KeyListener;Alfresco.module.SimpleDialog=function(m,n){n=YAHOO.lang.isArray(n)?n:[];return Alfresco.module.SimpleDialog.superclass.constructor.call(this,"Alfresco.module.SimpleDialog",m,["button","container","connection","json","selector"].concat(n))};YAHOO.extend(Alfresco.module.SimpleDialog,Alfresco.component.Base,{dialog:null,form:null,options:{templateUrl:null,actionUrl:null,firstFocus:null,onSuccessCallback:{fn:null,obj:null,scope:window},onSuccessMessage:"",onFailureCallback:{fn:null,obj:null,scope:window},onFailureMessage:"",doBeforeDialogShow:{fn:null,obj:null,scope:null},doSetupFormsValidation:{fn:null,obj:null,scope:null},doBeforeFormSubmit:{fn:null,obj:null,scope:window},doBeforeAjaxRequest:{fn:null,obj:null,scope:window},width:"30em",clearForm:false},onComponentsLoaded:function j(){if(this.id==="null"){return}},show:function b(){if(this.dialog){this._showDialog()}else{var m={htmlid:this.id};if(this.options.templateRequestParams){m=YAHOO.lang.merge(this.options.templateRequestParams,m)}Alfresco.util.Ajax.request({url:this.options.templateUrl,dataObj:m,successCallback:{fn:this.onTemplateLoaded,scope:this},failureMessage:"Could not load dialog template from '"+this.options.templateUrl+"'.",scope:this,execScripts:true})}return this},_showDialog:function d(){var m=i.get(this.id+"-form");var w=this.options.doSetupFormsValidation;if(typeof w.fn=="function"){w.fn.call(w.scope||this,this.form,w.obj)}var o=this.options.doBeforeFormSubmit;if(typeof o.fn=="function"){this.form.doBeforeFormSubmit=o}else{this.form.doBeforeFormSubmit={fn:function n(){this.widgets.okButton.set("disabled",true);this.widgets.cancelButton.set("disabled",true)},scope:this}}var q=this.options.doBeforeAjaxRequest;if(typeof q.fn=="function"){this.form.doBeforeAjaxRequest=q}if(this.options.actionUrl!==null){m.attributes.action.nodeValue=this.options.actionUrl}if(this.options.clearForm){var s=a.query("input",m),u;s=s.concat(a.query("textarea",m));for(var r=0,p=s.length;r<p;r++){u=s[r];if(u.getAttribute("type")!="radio"&&u.getAttribute("type")!="checkbox"){u.value=""}}}var v=this.options.doBeforeDialogShow;if(v&&typeof v.fn=="function"){v.fn.call(v.scope||this,this.form,this,v.obj)}this.widgets.okButton.set("disabled",false);this.widgets.cancelButton.set("disabled",false);this.form.updateSubmitElements();this.dialog.show();Alfresco.util.caretFix(m);this.form.applyTabFix();var t=new c(document,{keys:c.KEY.ESCAPE},{fn:function(y,x){this._hideDialog()},scope:this,correctScope:true});t.enable();if(this.options.firstFocus!==null){i.get(this.options.firstFocus).focus()}},hide:function k(){this._hideDialog()},_hideDialog:function f(){var m=i.get(this.id+"-form");Alfresco.util.undoCaretFix(m);this.dialog.hide()},onTemplateLoaded:function e(m){var o=document.createElement("div");o.innerHTML=m.serverResponse.responseText;var n=i.getFirstChild(o);while(n&&n.tagName.toLowerCase()!="div"){n=i.getNextSibling(n)}this.dialog=Alfresco.util.createYUIPanel(n,{width:this.options.width});if(i.get(this.id+"-form-submit")){this.widgets.okButton=Alfresco.util.createYUIButton(this,"form-submit",null);if(i.get(this.id+"-form-cancel")){this.widgets.cancelButton=Alfresco.util.createYUIButton(this,"form-cancel",this.onCancel)}}else{this.widgets.okButton=Alfresco.util.createYUIButton(this,"ok",null,{type:"submit"});this.widgets.cancelButton=Alfresco.util.createYUIButton(this,"cancel",this.onCancel)}this.form=new Alfresco.forms.Form(this.id+"-form");this.form.setSubmitElements(this.widgets.okButton);this.form.setAJAXSubmit(true,{successCallback:{fn:this.onSuccess,scope:this},failureCallback:{fn:this.onFailure,scope:this}});this.form.setSubmitAsJSON(true);this.form.init();this._showDialog()},onCancel:function g(m,n){this._hideDialog()},onSuccess:function h(m){this._hideDialog();if(!m){if(this.options.onFailure&&typeof this.options.onFailure.fn=="function"){this.options.onFailure.fn.call(this.options.onFailure.scope,this.options.onFailure.obj)}else{Alfresco.util.PopupManager.displayMessage({text:"Operation failed."})}}else{if(this.options.onSuccess&&typeof this.options.onSuccess.fn=="function"){this.options.onSuccess.fn.call(this.options.onSuccess.scope,m,this.options.onSuccess.obj)}else{Alfresco.util.PopupManager.displayMessage({text:"Operation succeeded."})}}},onFailure:function l(m){this.widgets.okButton.set("disabled",false);this.widgets.cancelButton.set("disabled",false);this.form.updateSubmitElements();if(typeof this.options.onFailureCallback.fn=="function"){this.options.onFailureCallback.fn.call(this.options.onFailureCallback.scope,this.options.onFailureCallback.obj)}else{if(m.json&&m.json.message&&m.json.status.name){Alfresco.util.PopupManager.displayPrompt({title:m.json.status.name,text:m.json.message})}else{Alfresco.util.PopupManager.displayPrompt({text:this.msg("message.failure")})}}}})})();new Alfresco.module.SimpleDialog("null");
