HJ.AddMyListDialog = Class.create();

HJ.AddMyListDialog.prototype = Object.extend(new Af.HtmlDialog(), {
      
      initialize: function(title, handler, content, zIndex) {
         this.zIndex = zIndex;
         this._initializeDialog(title, handler, content);
		 this.obj = null;		
		 this.message = "";
      },
      
      templateLoaded:function() {
         if (this.zIndex != null) {
			this.element.style.zIndex = this.zIndex;
         }
         this.ec = new Af.ElementCollection(this.element);
         this.save = this.ec.getFirstElementById("save");	
         this.armedButton = this.save;	 
		 this.displayMessage = this.ec.getFirstElementById("displayMessage");
		 this.myListName = this.ec.getFirstElementById("myListName");		 
		 this.cancel = this.ec.getFirstElementById("cancel");

		 this.cancel.onclick = this.doCancel.bind(this);
         this.save.onclick = this.doSave.bind(this);

		
		 this.initializeTemplateData();
      },
	  setDataObject: function(obj, message) {
		  this.obj = obj;		
		  this.message = message;
		  this.initializeTemplateData();
	  },
	  initializeTemplateData: function() {
		  if(this.message != null && this.displayMessage != null) {
			  this.displayMessage.innerHTML = this.message; 			 
		  }
		  if(this.myListName != null && this.obj != null) {
			  this.myListName.value = this.obj["name"]; 			  
		  }
		  if(this.myListName != null) {
			  this.myListName.focus();
		  }
		   
		  return;
	  },
      
      doSave: function() {
         
         if(this.myListName.value == null || trim(this.myListName.value) == "") {
            this.hide();
            return;
         }
         var obj;
			 if(this.obj != null) {
				 this.obj["name"] = this.myListName.value;
				 obj = this.obj;	
				 app.myLists.refreshListDetailView(obj);
			 }
			 else {
				 obj = app.myLists.dataTable.insertObject("name", this.myListName.value, 0, true);
			 }	 
         
         this.hide();
         this.saveMyList(obj);
      },
      
      saveMyList: function(obj)
      {
		  var s = "<message>";
		  s += "<myListName>" + xmlEncode(this.myListName.value) + "</myListName>";
		  s += "<templateType>" + "default" + "</templateType>";
		  s += "<startDT>" + " " + "</startDT>";
		  s += "<endDT>" + " " + "</endDT>";
		  s += "<repeatType>" + " " + "</repeatType>";
		  s += "<organizerName>" + login.userFullName + "</organizerName>";
		  s += "<phone>" + " " + "</phone>";
		  s += "<notes>" + " " + "</notes>";
		   s += "</message>";
         var req = new Af.DataRequest(svcURL,this.requestSaveMyListCompletedCommon.bind(this), this.requestFailedCommon.bind(this), null, this.requestTimedoutCommon.bind(this));
         req.addService("WorkspaceService", "saveMyList");
         if (obj.UUID != null) {
            req.addParameter("UUID", obj.UUID);
         }
         //req.addParameter("myListName", this.myListName.value);
         this.saveObj = obj;
		 req.xmlDoc = s;
         ajaxEngine.processRequest(req);
      },
      
      requestSaveMyListCompletedCommon: function(response) {
		 this.myListName.value = "";
         if (this.saveObj.UUID == null) {
            this.saveObj.UUID = response.responseText;
         }
		 app.myLists.myListsTable.setDataList(app.myLists.dataTable.getDataList());
         if (this.handler.myListCreated != null) {
            this.handler.myListCreated(this.saveObj.UUID);
         }
      },
      
      requestFailedCommon: function() {
         showMessageDialog("We are currently unable to save this list. Please try again.", "Excuse us!", 350, 100);
      },
      
      requestTimedoutCommon: function() {
         showMessageDialog("We are currently unable to save this list. Please try again.", "Excuse us!", 350, 100);
      },
	
	doCancel: function() {
		this.hide();
	}
      
});
