HJ.ShareNotificationEmail = Class.create();

HJ.ShareNotificationEmail.prototype = Object.extend(new Af.HtmlDialog(), {
	
	initialize: function(title, handler, content) {
		 this._initializeDialog(title, handler, content);
		 this.obj = null;		
		 this.sendNotifyMailTo = "new";
		 this.pgSelector = null;
		 this.newSelectedPList = new Array();
		 this.newSelectedGList = new Array();
		 this.send = null;
	},
	
	templateLoaded: function() {
  	
		 var ec = new Af.ElementCollection(this.element);

		 this.sendToNew = ec.getFirstElementById("sendToNew");
		 this.sendToNew.onclick = this.doSendToSelected.bind(this);
		 this.sendToEveryone = ec.getFirstElementById("sendToEveryone");
		 this.sendToEveryone.onclick = this.doSendToAll.bind(this);
		
		 this.send = ec.getFirstElementById("send");
		 this.armedButton = this.send;
		 this.dontSend =  ec.getFirstElementById("dontSend");
		 this.send.onclick = this.doSend.bind(this);
		 this.dontSend.onclick = this.doDontSend.bind(this);
		 
		 this.message = ec.getFirstElementById("message");
		 this.shareCalName = ec.getFirstElementById("shareCalName");
		 this.sendBy = ec.getFirstElementById("sendBy");
		 
		 this.selectionTrigger = ec.getFirstElementById("selectionTrigger");
		 this.selectionTrigger.onclick = this.doSelectPeople.bind(this);
		 
		 this.emails = ec.getFirstElementById("emails");
		 this.emails.onfocus = this.focusOnTextBox.bindAsEventListener(this);
		 this.emails.onblur = this.focusLostTextBox.bindAsEventListener(this);
		 
		 this.selectionList = ec.getFirstElementById("selectionList");
		 this.selGroupList = ec.getFirstElementById("selGroupList");
		 
		 this.defaultEmailMessage = this.emails.value;
		 
		 this.defaultMessage = this.message.value;
		
 
		 if (this.obj != null) {
		    this.setObj(this.obj, this.allDetails);
		 }
	},
	
	doSelectPeople: function() {     
      if (this.pgSelector == null) {
		  this.pgSelector = new HJ.PeopleGroupSelector("Choose Groups &amp; People", this, null);
		   this.pgSelector.centerDialog(400, 300);
	   }
	  
	   this.pgSelector.showHTMLTemplate(basePageURL + "PeopleGroupSelector.htm", "MainArea", "peopleSelector");
	    this.pgSelector.centerDialog(400, 300);
	 
       this.pgSelector.setDataList(app.workspace["myGroup"],
         app.workspace["myPeople"], this.gl, this.pl, true);
      
      
       return false;
    },
   
   
	doSendToSelected: function() {
		this.sendNotifyMailTo = "new";
	},
	
	doSendToAll: function() {
		this.sendNotifyMailTo = "all";
	},
	
   doSend: function() {
       this.hide();
       this.obj["notifMessage"] = this.message.value;
       var t1 = this.emails.value;
       if (t1 == null || t1 == this.defaultEmailMessage) {
          t1 = "";
       }
	   else {
		   t1 = "";
		   var s = trim(this.emails.value);
			var sl = s.split(",");
		    if (sl.length == 1) {
		    	sl = sl[0].split(';');
		    }
		    for (var i=0; i<sl.length; i++) {
				 s = trim(sl[i]);
				 if(app.emailValidator.validateEmail(s)==false){
					 if (s != "" ) {
						if(t1 == "") {
								t1 += s;
							}else {
								if(t1.indexOf(s) == -1) {
								t1 += "," + s;
							}
						}
					}
					continue;
				 }
				 s = app.emailValidator.extractEmailAddress(s);
				 if (s != "") {
				  if (t1 == "") {
					t1 += s;
				  } else {
						 if(t1.indexOf(s) < 0) {
							t1 += "," + s;
						 }
					}
				 }
		  }
	   }
       this.originalGL.length = 0;
       for (var i=0; i<this.gl.length; i++) {
          this.originalGL.push(this.gl[i]);
       }
       this.originalPL.length = 0;
       for (var i=0; i<this.pl.length; i++) {
          this.originalPL.push(this.pl[i]);
       }
	   this.handler.save("notify", t1, this.sendNotifyMailTo, this.allDetails);
	   this.cleanTemplateData();
   },
   
   doDontSend: function() {
       this.hide();
	   this.cleanTemplateData();
   },
   
   setObj: function(obj, allDetails) {
       this.cleanTemplateData();
       this.obj = obj;
		   
       this.allDetails = allDetails;
       if (this.send) {
          this.mySetObj();
       }
	   return
   },
   
   mySetObj: function() {
       
       this.emails.value = this.defaultEmailMessage;
       this.message.value = this.defaultMessage;
       if (this.allDetails) {
		  this.originalGL = this.obj["group2"];
		  this.originalPL = this.obj["people2"];
	   } else {
		  this.originalGL = this.obj["group"];
		  this.originalPL = this.obj["people"];
	   }
	   this.pl = this.originalPL.slice(0, this.originalPL.length);
	   this.gl = this.originalGL.slice(0, this.originalGL.length);

	   
	   this.showPeopleGroups();
	   this.shareCalName.innerHTML = '<b><strong>'+this.obj.name +'</b></strong>';
	   this.sendBy.innerHTML = login.userFullName;
	   return
   },
   
   selectionDone: function(child) {
	  this.newSelectedPList = child.selectedPeopleList;
	  this.newSelectedGList = child.selectedGroupList;
      this.showPeopleGroups();
   },
   
   showPeopleGroups: function() {
   /*   var s = "";
     
	  if(this.originalPL.length > 0 || this.gl.originalGL > 0) {
	     s += "<br/> <strong> Already sent mail to : </strong> ";
	  }
	  var l = this.originalPL;
      for (var i=0; i<l.length; i++) {
         s += "<br/>" + getFullName(l[i]);
      }	 
	  
      l = this.originalGL;
      for (var i=0; i<l.length; i++) {
         s += "<br/>" + l[i].name;
      }
     
      
      var s2 = "";
	 
	  for(Id = 0;Id<this.newSelectedPList.length;Id++) {
	      var t = this.newSelectedPList[Id];
	      if (findObjById(this.originalPL, t.UUID) == null) {
	          if (s2 == "") {
				 s2 += "<br/> <strong> New additions : </strong> ";
			  }
			  s2 += "<br/>" + getFullName(t);
	      }
	     
	  }
	  for(Id1 = 0;Id1<this.newSelectedGList.length;Id1++) {
		 var t = this.newSelectedGList[Id1];
	      if (findObjById(this.originalGL, t.UUID) == null){
	          if (s2 == "") {
				 s2 += "<br/> <strong> New additions : </strong> ";
			  }
			  s2 += "<br/>" + t.name;
	      }
	  }
	  s += s2; */
      this.selectionList.innerHTML = getNewPeopleNames(this.newSelectedPList, this.originalPL);
	  this.selGroupList.innerHTML = getNewGroupsPeople(this.newSelectedGList, this.originalGL);
	 
   },

   cleanTemplateData: function() {
	   if(this.selectionList != null){
		   this.selectionList.innerHTML = "";
	   }
		if(this.selGroupList != null){
			this.selGroupList.innerHTML;
		}
	   this.newSelectedPList = new Array();
	   this.newSelectedGList = new Array();
   },

	focusOnTextBox: function(evt) {				
		  var s = this.emails.value;
		  if (s == null) {
			s = "";
		  } else {
			s = trim(s);
		  }		
		  if (s == trim(this.defaultEmailMessage) || s == "") {
			 this.emails.value = "";
			 consumeEvent(evt);
		  }

		  this.emails.focus();
	   },
   
	focusLostTextBox: function(evt) {    		
		  var s = this.emails.value;
		  if (s == null) {
			s = "";
		  } else {
			s = trim(s);
		  }
		  if (s == trim(this.defaultEmailMessage) || s == "") {
			 this.emails.value = this.defaultEmailMessage;
			 consumeEvent(evt);
		  }	         
	 }
});



