HJ.MyResourcesDetail = Class.create();

HJ.MyResourcesDetail.prototype = Object.extend(new Af.HtmlDialog(), {
      
		 initialize: function(title, handler, content) {
		 this._initializeDialog(title, handler, content);
		 this.flag = false;
		 this.category = null;
		 this.comments = null;
		 this.xslStarData = null;
		 this.validateBoolean = true;
		 this.saveCall = false;
		 this.publishCall = false;	
		 this.shareFriends = false;	
		 this.sendEmailsToFriends = false;
		 this.resourceUUID = null;
		 this.obj = new Object();
		 this.publishBtnPressed = false;
		 this.publishResourceUUID = null;
		 this.isCallFromMyLovedOne = false;
		 this.initialPublishedStatus = null;
		 this.resourceInfo = "";
      },
      
      templateLoaded: function() {
		 this.bNamePointer = document.getElementById("businessName");
         var ec = new Af.ElementCollection(this.element);
         this.save = ec.getFirstElementById("save");
         this.armedButton = this.save;
         this.dontSave =  ec.getFirstElementById("dontSave");
         this.save.onclick = this.doSave.bind(this);
         this.dontSave.onclick = this.doDontSave.bind(this);
         this.friendSelector = ec.getFirstElementById("friendSelector");
		 
         this.friendSelector.onclick = this.doFriendSelection.bindAsEventListener(this);
         this.deleteResource = ec.getFirstElementById("deleteResource");
         this.deleteResource.onclick = this.deleteResourceConfirm.bind(this);
         this.publishedClicked = ec.getFirstElementById("published");
         this.publishedClicked.onclick = this.doPublished.bind(this);
		 this.printResource = ec.getFirstElementById("printResource");
		 this.printResource.onclick = this.doPrintResourceDetail.bind(this); 
		 this.friendSelector = ec.getFirstElementById("friendSelector");
		 this.friendSelector.onclick = this.doSelectFriends.bind(this);
         
		 this.bNamePointer.focus();
         var ec = new Af.ElementCollection(this.element);
         
         this.editor = new Af.ObjEditor();
         
         this.bName = this.editor.addTextByEC(ec, "businessName");
		
         this.editor.addTextByEC(ec, "fName");
         this.editor.addTextByEC(ec, "lName");
         this.editor.addTextByEC(ec, "myNotes");
         this.email = this.editor.addTextByEC(ec, "email");
         this.phone = this.editor.addTextByEC(ec, "phone");
         this.website = this.editor.addTextByEC(ec, "website");
         this.address = this.editor.addTextByEC(ec, "address");
         this.city = this.editor.addTextByEC(ec, "city");
         this.state = this.editor.addSelectByEC(ec, "state");
         this.zip = this.editor.addTextByEC(ec, "zip");
		 this.zip.element.onkeypress = this.doValidateZipCode.bindAsEventListener(this);
		         
         if (this.obj != null) {
            this.editor.clearChanges();
            this.editor.setValueFromObj(this.obj);
         }
		 this.showDeleteAndPrintLink();
      },
	
      
      setObj: function(obj, mode) {
         this.isCreateMode = mode;
         this.obj = obj;			
		 if(this.obj["published"] == null || this.obj["published"] == undefined) {
			this.obj["published"] = false;
			this.initialPublishedStatus = false;
		 }
		 		 
         if (this.editor != null) {
            this.editor.clearChanges();
            this.editor.setValueFromObj(this.obj);			
         }
		  if (this.obj["people"] == null) {
            createOneToManyAssoc(this.obj, "people", "Person");
         }
	     if (this.obj["group"] == null) {
            createOneToManyAssoc(this.obj, "group", "Group");
         }
		  this.showDeleteAndPrintLink();
		 /*if(this.bNamePointer != null) {
			 this.bNamePointer.focus();
		 }*/
      },
	showDeleteAndPrintLink: function() {

		 if(this.handler.addNew == true && this.deleteResource != null)  {
			 this.deleteResource.style.display = "none";
			 this.printResource.style.display = "none";
			 this.resourceUUID = null;
			  this.handler.addNew = false;
		 } else {
			 if(this.deleteResource != null && this.printResource != null)	{
				this.deleteResource.style.display = "";
				this.printResource.style.display = "";
			 }
		 }			
		 if(this.publishedClicked != null) {			
			 if(this.obj["published"] != null || this.obj["published"] != undefined) {
				 if(this.obj["published"] == "true") {				
					 this.publishedClicked.value = "Already Published";
					 this.publishedClicked.disabled = true;
				 } else {
					this.publishedClicked.value = "Make Public";
					this.publishedClicked.disabled = false;
				 }	
			 }
		 }
		 if(this.phone != null && this.obj != null) {
			var obj = cloneObject(this.obj);
			app.formatPhone.setListenerOnPhonesId("phone", obj); 
		 }		
	  },
      doSave: function() {
		 this.saveCall = true;		 
		 if(this.validateBoolean) {
			 this.doValidateFields();
			 return false;
		 }
		 this.saveCall = false;
		 this.validateBoolean = true;
		
		 if(this.obj["businessName"] != null){
			 this.obj["businessName"] = this.obj["businessName"];
		 }
		 if(this.obj["address"] != null){
			 this.obj["address"] = this.obj["address"];
		 }
		
		this.editor.addNV("phone", this.obj["phone"]);


	     this.editor.addNV("published", this.obj["published"]);
         var xmlString = this.editor.getChangeXml(this.isCreateMode);	
         
         if (xmlString == null) {
            showMessageDialog("Any changes you have made will be saved.", "Informing You", 300, 100);
            return;
         }		
		
				 this.obj["name"] = getFullName(this.obj);
				 this.obj["bName"] = this.obj["businessName"];
				if (this.isCreateMode) {  
						this.obj["name"] = getFullName(this.obj);
            this.handler.dataTable.addObject(this.obj);
         }	

         var req = new Af.DataRequest(svcURL, this.saveSuccessful.bind(this), this.saveFailed.bind(this), null,
         this.saveTimedOut.bind(this));
         req.addService("WorkspaceService", "saveResource");
         if (this.obj.UUID != null) {
            req.addParameter("UUID", this.obj.UUID);
         }
		 if(this.category != null || this.category == undefined) {
		 	req.addParameter("category", this.category);
		 }
		 if(this.publishBtnPressed) {
			req.addParameter("visibility", "true");
		 }		 
         req.xmlDoc = xmlString;  
		
         ajaxEngine.processRequest(req);
		 this.hide();
		 // this.publishBtnPressed = false;		
		
      },
      
      doDontSave: function() {
		 if(this.initialPublishedStatus == false) {
			this.obj["published"] = false;       
		 }
         this.editor.clearChanges();
         this.hide();
		 if(this.isCallFromMyLovedOne) {
			this.isCallFromMyLovedOne = false;
		    app.myLovedOne.showInfo = 0;
	     }
      },
	doValidateFields: function() {
		 
		  this.resourceInfo = "";
		  this.fObject = app.formatPhone.getFormattedObject();
		  if(this.fObject["phone"] != undefined && this.fObject["phone"] != null){
			this.obj["phone"] = this.fObject["phone"];
		  }
		 
		 if(this.obj["phone"] != null && this.obj["phone"] != undefined) {
			 this.resourceInfo = "<br/>"+this.obj["phone"];			 
		 }
		  
		 if(this.address.getValue() != null && this.address.getValue() != undefined) {
			 this.resourceInfo = this.resourceInfo+ " " + this.address.getValue();
		 }
		 if(this.city.getValue() != null && this.city.getValue() != undefined) {
			 this.resourceInfo = this.resourceInfo + " " + this.city.getValue();
		 } 
		 if(this.state.getValue() != null && this.state.getValue() != undefined) {
			 this.resourceInfo = this.resourceInfo + " " + this.state.getValue();

		 } 
		 if(this.zip.getValue() != null && this.zip.getValue() != undefined) {
			this.resourceInfo = this.resourceInfo + " "+ this.zip.getValue(); 		  
		 }
		 if(this.website.getValue() != null && this.website.getValue() != undefined) {
			this.resourceInfo = this.resourceInfo +" "+'<a href="'+this.website.getValue()+'">'+this.website.getValue()+'</a>';
		 }
		
		 this.businessName = this.bName.getValue();

		 var s ="";
         var ec = new Af.ElementCollection(this.element);
         var bsnsName = trim(ec.getFirstElementById("businessName").value);
         if(bsnsName==""){
            s = "Please enter a business name.<br/>";
         }        
         if(s!=""){
            showMessageDialog(s, "Request", 300, 100);
            return;
         }
		
		 if(this.address.value != undefined && this.address.value.length > 100 ) {
			 showMessageDialog("The address you have entered is too long. Please enter a shorter version.","Request",300,100);
			 return;
		 }			
		  if(this.email.getValue() != "" && this.email.getValue() != undefined) {
			 if(app.emailValidator.validateEmail(this.email.getValue())==false){
					 showMessageDialog("Please enter a valid email address.","Request",300,100);
					return;
			}			
		}
		
		this.email.value = app.emailValidator.extractEmailAddress(this.email.value);

		if(this.zip.value != undefined && this.zip.value != ""){ 
			if(app.validator.validateZip(this.zip.value) == false) {
				showMessageDialog("Please enter a valid zip code.","Request", 300, 100);
				return;	
			}
		 } 		 	
		 if(this.publishCall) {
			 this.publishCall = false;
			 var a0 = trim(this.obj["phone"]);			
			 if(a0 == undefined || a0 == "") {
				showMessageDialog("Please enter a valid phone number to publish resource","Request", 300, 100);
				return;
			 }	
			 this.validateBoolean = false;
			 this.doPublished();
			 return
		 } else {/*
				  var a0 = trim(this.obj["phone"]);
				 if(a0 != undefined && a0 !="") {
					 if(app.formatPhone.digitcheck < 10) {
						showMessageDialog("Please enter a valid phone number","Request", 300, 100);
						return;
					 }
				 }	*/			 
			 }
		 if(this.shareFriends) {
			this.shareFriends = false;
			this.validateBoolean = false;
			this.doSelectFriends();
			return;
		 }
		 
		 if(this.saveCall) {
		   this.saveCall = false;
		   this.validateBoolean = false;
		   this.doSave();
		   return
		 }	
	  },
      
      doFriendSelection: function(ev) {
      },
      
      
      saveSuccessful: function(response) {		
        if (this.obj.UUID == null) {
			this.strArray = (response.responseText).split(",");
            this.obj.UUID = this.strArray[0];
			if(this.strArray[1] != null && this.strArray[1] != undefined) {
				this.publishResourceUUID = this.strArray[1]; 

			}
         } else if(this.publishBtnPressed) {
				this.publishBtnPressed  = false;
				this.publishResourceUUID = response.responseText;
			 }
		 this.publishBtnPressed = false;
         this.editor.clearChanges();        
         
         
         if(this.flag){
            this.flag = false;
            var cbArray = document.getElementsByName("cbMakePublic");
            cbArray[cbArray.length-1].checked = true;
         }
		  if (this.publishedDialog != null) {    			 
			 if(this.publishedDialog.resRublished)  {				 
				 this.publishedDialog.doSave();
			 }
		 } else if (!this.sendEmailsToFriends) {
			showMessageDialog("Your resource has been added or updated successfully.", "Success!", 300, 100);
		 }
		 if(this.isCallFromMyLovedOne) {
			 this.isCallFromMyLovedOne = false;
			 if(app.myLovedOne.createNewObjectOfType != "") {				 
				 app.myLovedOne.obj[app.myLovedOne.createNewObjectOfType][0] = this.obj;			
				 app.myLovedOne.createNewObjectOfType = "";
			 }
			 app.myLovedOne.showContactInformation(this.obj);
		 }else{
			 this.handler.refresh();
		 }
		  if (this.sendEmailsToFriends) {
			 this.sendEmailsToFriends = false;
			 this.resourceUUID = response.responseText;			
			 this.shareToFrndDialog.doSendEmail();			 
		 }
      },
      
      saveFailed: function(dataRequest, msg) {
         showMessageDialog(msg + ".<br/>We are currently unable to save this resource. Please try again.", "Excuse us!", 400, 150);
      },
      
      saveTimedOut: function(dataRequest) {
         showMessageDialog( "We are currently unable to save this resource. Please try again.", "Excuse us!", 400, 150);
      },
      
      setFriendListSelected: function() {
      },
      
      deleteResourceConfirm: function() {
		  if(this.handler.currentRow == -1) {
			   showMessageDialog("There are currently no resources available to delete." , "Informing You", 300, 80);
			   return;
		   }
		   else {
			showConfirmDialog("Are you sure you want to delete this resource?","Are You Sure?",300, 100, this.doDeleteResource.bind(this));
		   }
      },
      
      doDeleteResource: function(){
         if(this.handler.currentRow >= 0) {
            var obj =   this.handler.dataTable.deleteObjectByIndex(this.handler.currentRow);
            this.handler.myResourcesDetailsTable.setDataList(this.handler.dataTable.getDataList());     
            this.handler.currentRow = -1;
            this.deleteResourceFromList(obj);
         }
      },
      
      deleteResourceFromList: function(obj) {
         hideDialogWin();
         var req = new Af.DataRequest(svcURL, this.requestDeleteResourceCompleted.bind(this), this.requestFailedCommon.bind(this), null, this.requestTimedoutCommon.bind(this));
         req.addService("WorkspaceService", "deleteResource");
         req.addParameter("UUID", obj.UUID);
         ajaxEngine.processRequest(req);
      },
      
      requestDeleteResourceCompleted: function(response)	{
         this.hide();
         showMessageDialog("Your resource has been sucessfully deleted." , "Deletion Successful", 300, 80);
      },
      
      requestFailedCommon: function() {
         showMessageDialog("We are currently unable to delete this resource. Please try again.", "Excuse us!", 400, 150);
      },
      
      requestTimedoutCommon: function() {
         showMessageDialog( "We are currently unable to delete this resource. Please try again.", "Excuse us!", 400, 150);
      },
      
      doPublished:function(){  		  
		 this.publishCall = true;		 
		  if(this.validateBoolean) {			
			  this.doValidateFields();
			  return
		  }
		 this.publishCall = false;
		 this.validateBoolean = true;
		// this.obj["published"] = "true";	
		// this.publishBtnPressed = true;
		 this.setPublishedInfo();	
		 
      },

	setPublishedInfo: function(xslData) {			   
		 if (this.publishedDialog == null) {           
            this.publishedDialog = new HJ.PublishResource("Publish A Resource", this, null);
            this.publishedDialog.width = "520px";			
         }
         this.publishedDialog.showHTMLTemplate(basePageURL + "PublishResource.htm", "content", "publishedDialogContent");     		 
		 this.publishedDialog.centerDialog(350, 350);
		 this.publishedDialog.setInfo(this.resourceInfo, this.businessName);
		
	  },
	doPrintResourceDetail: function() {
		var req = new Af.DataRequest(svcURL,this.printCompleted.bind(this), this.printFailed.bind(this), null, this.requestTimedout.bind(this));		   
		req.addService("WorkspaceService", "getPrintHtmlFromXML");
		req.addParameter("templateName", "myResourceDetail");
		req.addParameter("xlstFile","MyResourceDetail.xsl");
		req.addParameter("uuid",this.obj.UUID);
		ajaxEngine.processRequest(req);
		return false;
	  },
	 printCompleted: function(response)	{
		var p = new Af.PrintUtil();
        p.showPrintView(response);
	},

	printFailed: function() {
     showMessageDialog("<br/>Printing is currently unavailable. Please try again.", "Printing Unavailable", 400, 150); 
    },
   
    requestTimedout: function() {
       showMessageDialog( "Printing is currently unavailable. Please try again.", "Printing Unavailable", 400, 150); 
    },

	doSelectFriends: function() {
	 if (this.obj.UUID != null) {
         this.resourceUUID = this.obj.UUID;
     }	
	 this.shareFriends = true;	
	 if(this.validateBoolean) {
		 this.doValidateFields();
		 return
	 }
	this.shareFriends = false;	
	this.validateBoolean = true;
	 if (this.shareToFrndDialog == null) {
         this.shareToFrndDialog = new HJ.ShareComResourcesToFriend("Share a Resource", this, null);        
         this.shareToFrndDialog.width = "370px";
		  this.shareToFrndDialog.centerDialog(350, 350);
      }	  
	  var obj = new Object();
      this.shareToFrndDialog.showHTMLTemplate(basePageURL + "ShareComResourcesToFriend.htm", "content", "shareToFrndContent");
	  this.shareToFrndDialog.centerDialog(350, 350);
      this.shareToFrndDialog.setObj(obj);
    
	},
		
	doValidateZipCode: function(evt) {
		return app.validator.doValidateZipCode(evt);
	}
});

