HJ.TellAFriendDialog = Class.create();

HJ.TellAFriendDialog.prototype = Object.extend(new HJ.Dialog(), {
      
	initialize: function(toEmailList) {
		this.HJ_Dialog_initialize("Invite Friends", null, null);
		this.obj = null;
		this.url = basePageURL + "TellAFriendDialog.htm";
		this.toEmailList = toEmailList;
		this.emailValidator = new HJ.EmailValidator();
	},
	
	showTemplate: function() {
        this.showHTMLTemplate(this.url, "MainArea", "tellAFriendFormContent");
        this.centerDialog(520, 250);

		this.emails = "";		
		ajaxEngine.commFailureHandler = commFailure;
		ajaxEngine.timoutHandler = commTimedout;
		
	},
	
	templateLoaded: function() {
		hideModalMessageDialog();
		  
		var ec = new Af.ElementCollection(this.element);
		
		this.sendButtonElem = ec.getFirstElementById("send");
		this.armedButton = this.sendButtonElem;
		this.sendButtonElem.onclick = this.doSendEmail.bind(this);
		this.toEmailListErrorMsgElem = ec.getFirstElementById("toEmailListErrorMsg");
		
		this.toEmailListElem = ec.getFirstElementById("toEmailList");
		this.personalNote = ec.getFirstElementById("personalNote");
		
		this.toEmailListElem.onfocus = this.focusOnToEmailListElem.bindAsEventListener(this);
		this.toEmailListElem.onblur = this.focusLostToEmailListElem.bindAsEventListener(this);
		this.emailsDefaultValue = this.toEmailListElem.value;
	 	 	  
		if(this.toEmailList!=null){
			this.toEmailListElem.value = this.toEmailList;
		}
	},
   	
   
	requestTellAFriendCompleted: function(response)	{
		if(this.invalidEmails!=""){	
			alert("We are unable to send email to the following email addresses:<BR/> "+this.invalidEmails);
			return false;
		}
		alert("Email sent to your friends ", "Email Sent", 300, 100);
		
		// track help request with google analytics
		// Use logical name instead of name of the destination page
		GATrackAction(GA_ACTION_SUCCESSFUL_TELL_A_FRIEND);
		this.close();
		return false;
	},
	   
	requestFailedCommon: function(dataRequest,msg) {
		 alert("We are currently unable to process this tell a friend request. <br/> Please try again.");
	},
	
	requestTimedoutCommon: function(dataRequest) {
		 alert("We are currently unable to process this tell a friend request. <br/> Please try again.");
	},
   
   doDontSend: function() {
	 if (app != null) {
		app.db.displayCurrentTab();
	 }
	 else {
	 	window.location = basePageURL;
	 }
	 return false;
   },  

	focusOnToEmailListElem: function(evt) {	 			 
		  var s = this.toEmailListElem.value;
		  if (s == null) {
			s = "";
		  } else {
			s = trim(s);
		  }		
		  if (s == trim(this.emailsDefaultValue) || s == "") {
			 this.toEmailListElem.value = "";
			 consumeEvent(evt);
		  }

		  this.toEmailListElem.focus();
	   },
   
	 validateToEmailListElem: function() {
      this.emails = "";
	  this.invalidEmails = "";
      
      if ((this.toEmailListElem.value != this.emailsDefaultValue)) {
            var s = trim(this.toEmailListElem.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(this.emailValidator.validateEmail(s)==false){
				if (s != "" ) {
					if(this.invalidEmails == "") {
							this.invalidEmails += s;
						}else {
							if(this.invalidEmails.indexOf(s) == -1) {
								this.invalidEmails += "," + s;
							}
					}
				}
			   continue;
			  }
			  
			  s = this.emailValidator.extractEmailAddress(s);
               if (s != "") {
                 if (this.emails == "") {
					this.emails += s;
				 } else {
					 if(this.emails.indexOf(s) < 0) {
						this.emails += "," + s;
					 }
				 }
               }
            }
       }
	          
      if (this.emails == "" || this.invalidEmails != "") {
      	this.toEmailListErrorMsgElem.style.display = "block";
        return false;
      }
	  this.toEmailListErrorMsgElem.style.display = "none";
      return true;
	 },

	focusLostToEmailListElem: function(evt) {
		  var s = this.toEmailListElem.value;
		  if (s == null) {
			s = "";
		  } else {
			s = trim(s);
		  }
		  if (s == trim(this.emailsDefaultValue) || s == "") {
			this.toEmailListElem.value = this.emailsDefaultValue;
		  }
		  if (!this.validateToEmailListElem()) {
		  	this.toEmailListElem.focus();
		  	return false;
		  }
		  this.toEmailListErrorMsgElem.style.display = "none";
		  return true;
	 },
	 
   doSendEmail: function(evt) {
	   	// invoke focusLost<element name>Elem method to validate all elements
	   	var validateFields = this.validateToEmailListElem(evt);
		if (!validateFields)
		{
			alert("Please correct the errors shown");
			consumeEvent(evt);
			return false;
		}
	    var req = new Af.DataRequest(svcURL,this.requestTellAFriendCompleted.bind(this), this.requestFailedCommon.bind(this), null, this.requestTimedoutCommon.bind(this));
		req.addService("WorkspaceService", "tellAFriend");		   
		req.addParameter("emails", xmlEncode(this.emails));
		var s = "<messages>";
		s += "<personalNote>" + xmlEncode(this.personalNote.value)+ "</personalNote>";
		s += "</messages>";
		req.xmlDoc = s;
		ajaxEngine.processRequest(req);
		return false;
   } 
});

