HJ.GroupDialog = Class.create();

HJ.GroupDialog.prototype = Object.extend(new Af.HtmlDialog(), {
      
      initialize: function(title, handler, content) {
         this._initializeDialog(title, handler, content);
		 this.obj = null;		
		 this.message = "";
		 this.handler = handler;
		  this.isRename = false;		
      },
      
      
      templateLoaded: function() {
         // element collection class limits the look up by id within the scope of the given element (in this case this.element).
         // In case there are other elements with the same id in the "document" scope, this avoids id collision
         var ec = new Af.ElementCollection(this.element);
		 this.groupName = ec.getFirstElementById("groupName");
         this.save = ec.getFirstElementById("save");
         this.armedButton = this.save;
		 this.cancel = ec.getFirstElementById("cancel");
		 this.displayMessage = ec.getFirstElementById("displayMessage");
		 
         this.save.onclick = this.doSave.bind(this);
		 this.cancel.onclick = this.doDontSave.bind(this);
	     this.groupName.focus();	
		 if (this.obj != null) {
		    this.initializeTemplateData();
		 }
      },

	  setDataObject: function(obj, message) {
		  this.obj = obj;		
		  this.message = message;
		  if (this.element != null) {		
	 	     this.initializeTemplateData();
	 	  }
	   },

		initializeTemplateData: function() {
		  if(this.message != null && this.displayMessage != null) {
			  this.displayMessage.innerHTML = this.message; 			 
		  }
		  if(this.groupName != null && this.obj != null) {
			  this.groupName.value = this.obj["name"]; 			  
		  }
		  if(this.groupName != null ) {
			  this.groupName.focus();		  
		  }
		  return;
	  },
      
      doSave: function() {
		 var groupList = this.handler.dataTable.getDataList();
		 for(var i = 0; i < groupList.length ; i++) {
			 var previousGName = trim(groupList[i]["name"]);
			 var newGName = trim(this.groupName.value); 
			 if(previousGName == newGName) {
				 showMessageDialog(newGName + " already exist. Please try other name.", "Excuse us!", 350, 100);
				 return false;
			 }
		 }
		  
		 if (this.groupName.value != "" ) {
			 var obj;

			 if(this.obj != null) {
				 this.obj["name"] = this.groupName.value;
				 this.isRename = true;				 
				 obj =  this.obj;				
			 }
			 else {
				 obj = this.handler.dataTable.insertObject("name", this.groupName.value, 0, true);			
				 createOneToManyAssoc(obj, "people", "Person");
			 }
			 this.saveNewGroup(obj);
	     }
	     
	     return false;
         
      },
      
      saveNewGroup: function(obj)
      {
         obj["name"] = this.groupName.value;		
         var editor = new Af.ObjEditor();
		 var nestedXml = editor.getChangeXmlForObjList(obj["people"], ["name", "fName", "lName"], "people");
		 var xmlString = editor.getChangeXmlForObj(obj, ["name"], "ObjModify", nestedXml);
     
         var req = new Af.DataRequest(svcURL,this.requestSaveGroupCompletedCommon.bind(this), 
			this.requestFailedCommon.bind(this), null, this.requestTimedoutCommon.bind(this));
         req.addService("WorkspaceService", "saveGroupDetails");
         if (obj.UUID != null) {
            req.addParameter("UUID", obj.UUID);
         }
		 if(this.isRename) {
			 req.addParameter("newName", obj["name"]);
		 }
         req.xmlDoc = xmlString;		
         this.savedObj = obj;
         ajaxEngine.processRequest(req);
		 this.isRename = false;
		 return false;
      },
      
      completeRequestSaveGroup: function() {
		 this.handler.rightListTypeChanged();
      },
      
      requestSaveGroupCompletedCommon: function(response) {
		 this.groupName.value = "";
         if (this.savedObj.UUID == null) {
            this.savedObj.UUID = response.responseText;
         }
         this.hide();
		 var dlist = this.handler.dataTable.getDataList();
		 if(dlist != null && dlist.length > 0){
			 dlist.sort(sortGroups);
		 }
         this.handler.myGroupTable.setDataList(dlist);    
		 this.handler.setAllLists();  
		 var indexPos = findObjIndex(dlist, this.savedObj);
		 if(indexPos > -1){			
			 this.handler.groupSelector.setSelectedIndex(indexPos);
			 // Calling rightListTypeChanged after 100 ms
			 // It gives Ajax error in IE 6 otherwise - indicating
			 // a potential race condition. Setting alerts also indicates that
			 // a variable set in setAllLists() is used in rightListTypeChanged().
			 setTimeout(this.completeRequestSaveGroup.bind(this), 100);
		 }
		 return false;
      },
      
      requestFailedCommon: function(dataRequest, msg) {
         hideDialogWin();
         showMessageDialog("We are currently unable to save this record.  Please try again.", "Excuse us!", 350, 100);
         return false;
      },
      
      requestTimedoutCommon: function()	{
         hideDialogWin();
        showMessageDialog("We are currently unable to save this record.  Please try again.", "Excuse us!", 350, 100);
        return false;
      },
      
      doDontSave: function() {
         this.hide();
         return false;
      }
      
});
