HJ.MyLovedOne = Class.create();

HJ.MyLovedOne.prototype = {

	initialize: function() {
	   this.targetContainerId = "content";      
	   this.myLovedOneTable = new Af.HtmlTable("myLovedOneTable", ",,fName");
	   this.myLovedOneTable.listener = this;	 
	   this.currentRow = -1;
	  
	   this.myLovedOneTitle = new Af.HtmlTitlePane2("myLovedOneTable", "myLovedOneTitle", "boxLink open", "boxLink closed", this);	   
	   this.myLovedOneTitle.listener = this;
	   this.url = basePageURL + "MyLovedOne.html";
	   this.tl = null;	
	   this.isCreateMode = true;
	   this.editor = new Af.ObjEditor();
	   this.obj = null;
	   this.isTemplateLoaded = false;	   
	   this.showInfo = 0;
	   this.postSaveMode = "edit";
	   this.addNew = false;
	   this.prSelected = false;
	   this.isPreviewClicked = false; 
	   this.createNewObjectOfType = "";	   
	},
	
	cleanup: function() {
	  this.myLovedOneTable.cleanup();
    },
	
	viewSelected: function(invoker) {
		
	 //  HIDE CALENDAR'S HEADER
	  if(calenderTop == null){
		calenderTop = document.getElementById("calenderTop");
	  }
	  calenderTop.style.display = "none";

	  this.postSaveMode = "edit";
	  this.addNew = false;
      if (this.tl == null) {
         var n = this.dataTable.getDataList().length;
         if (n == 0) {
           n = -1;
         } else {
           n = 0;		  
         }
         this.cellSelected(n,0,false);
      } else {													  					
         this.tl.reAttachElement();
		 app.setCurrentView(this);
		 this.doRefreshTemplate();
      }
      
      return false;
    },	 	

	setDataList: function(myLovedOneList) {	
		this.myLovedOneTable.setDataList(myLovedOneList);		
		this.dataTable = new Af.DataTable(myLovedOneList); 		
		this.myLovedOneList = myLovedOneList; 
	},	
	
		
   cellSelected: function(row, col, reselected) {   	  
        //  HIDE CALENDAR'S HEADER
	    if(calenderTop == null){
		  calenderTop = document.getElementById("calenderTop");
	    }
	    calenderTop.style.display = "none";

		refreshWidget();   
	    this.postSaveMode = "edit";
		this.addNew = false;
		if (row >= 0 && row < this.dataTable.getDataList().length) {
		   this.currentRow = row;
		   this.obj = this.dataTable.getDataList()[row];	
	    } else {
		  this.currentRow = 0;
	      this.obj = new Object();
	      this.obj["fName"] = "New";
		  initializeCreatedObject(this.dataTable.getDataList(), this.obj);
		  this.dataTable.getDataList().push(this.obj);
		  this.myLovedOneTable.setDataList(this.dataTable.getDataList());
		}
	    this.myLovedOneTable.selectRow(this.currentRow);
	    this.doInitAssociations();
		if(this.isTemplateLoaded) {
		  this.tl.reAttachElement();	 
		  this.editor.clearChanges();		 
		  this.editor.setValueFromObj(this.obj);	
		  this.doCleanContact();
		  this.TitleName.innerHTML = this.obj.fName;
		  this.setTitleAndValues();
		} else {
		  this.loadLovedTemplate();
		}
   },
     
	doAddMyLoved: function() { 		
	    this.postSaveMode = "new";	   
		this.addNew = true;	   		
		this.doCleanContact();  
		this.obj = new Object();
		this.obj["fName"] = "New";
		this.doInitAssociations();
		this.editor.setValueFromObj(this.obj); 		
		initializeCreatedObject(this.dataTable.getDataList(), this.obj);
		this.TitleName.innerHTML = this.obj.fName;
		this.setTitleAndValues();		
	    return false;
	},

	doSaveMyLovedOne: function() {	
		
		var assocsXml = this.editor.getChangeXmlForObjList(this.obj["contact1"], ["fName","lName","address", "phone1", "phone2", "phone3", "email"], "contact1");
		
		assocsXml += this.editor.getChangeXmlForObjList(this.obj["contact2"], ["fName","lName","address", "phone1", "phone2", "phone3", "email"], "contact2");

		assocsXml += this.editor.getChangeXmlForObjList(this.obj["friendContact1"], ["fName","lName","address","city","state","zip", "phone1", "phone2", "phone3", "email"], "friendContact1");

		assocsXml += this.editor.getChangeXmlForObjList(this.obj["friendContact2"], ["fName","lName","address","city","state","zip", "phone1", "phone2", "phone3", "email"], "friendContact2");

		assocsXml += this.editor.getChangeXmlForObjList(this.obj["doctor1"], ["fName","lName","businessName", "phone", "mobile", "fax", "email","address","city","state","zip"], "doctor1");

		assocsXml += this.editor.getChangeXmlForObjList(this.obj["doctor2"], ["fName","lName","businessName", "phone", "mobile", "fax", "email","address","city","state","zip"], "doctor2");

		assocsXml += this.editor.getChangeXmlForObjList(this.obj["insuranceCarrier"], ["fName","lName","businessName", "phone", "mobile", "fax", "email","address","city","state","zip"], "insuranceCarrier");
		
		this.xmlString = this.editor.getChangeXmlForObj(this.obj, ["fName", "lName", "contact1Relation", "contact2Relation", "fContact1Relation", "fContact2Relation", "type1Doctor", "type2Doctor", "note1", "note2", "policyNumber"], "ObjModify", assocsXml);
		//debugA("xmlString: " + this.xmlString);								
		this.editor.clearChanges();			
		this.myLovedOneTable.setDataList(this.dataTable.getDataList());					
		
		if(this.isPreviewClicked) {
			this.isPreviewClicked = false;
		    this.showPrintPreviw();
			return;
		}
		this.saveMyLovedOne();
		return;
	},

	saveMyLovedOne: function() { 		
		var req = new Af.DataRequest(svcURL, this.saveSuccessful.bind(this), this.saveFailed.bind(this), null,this.saveTimedOut.bind(this));
        req.addService("WorkspaceService", "saveMyLovedOne");    
        if (this.obj.UUID != null) {
            req.addParameter("UUID", this.obj.UUID);
        }
	    req.xmlDoc = this.xmlString;// this.replaceAll(this.xmlString,'&lt;BR/&gt;','\n');
        ajaxEngine.processRequest(req);
	},

	saveSuccessful: function(response) {
	    if (this.obj.UUID == null) {
            this.obj.UUID = response.responseText;			
        }		
		//this.printMyLovedOne();					
		this.addNew = false;
		this.editor.clearChanges();
		
		if (this.postSaveMode == "new") {
		    this.postSaveMode = "edit";			
		    this.dataTable.getDataList().push(this.obj);
			this.TitleName.innerHTML = this.obj.fName;
			this.setTitleAndValues();	
			this.currentRow = this.dataTable.getDataList().length - 1;			
	    } 
	    this.myLovedOneTable.setDataList(this.myLovedOneList);		
	    this.myLovedOneTable.selectRow(this.currentRow);		
		//app.db.loadCalendarTemplate();
		this.viewSelected();
		
		//return false;
	},
	
	saveFailed: function() {
		 showMessageDialog("We are currently unable to save this record.<br/>Please try again." , "Excuse us!", 300, 100);
	},

	 saveTimedOut: function() {
		showMessageDialog("We are currently unable to save this record.<br/>Please try again." , "Request", 300, 100);	
	 },		 
   
   paneStateChanged: function(comp) {      
   },
   loadLovedTemplate: function () {	   
	  this.formatNotesParagraph();
	/*  this.obj["note1"] = this.replaceAll(this.obj["note1"],'<BR/>','\n');
	  this.obj["note2"] = this.replaceAll(this.obj["note2"],'<BR/>','\n');	 */
      if (this.tl == null) {
         this.tl = new Af.TemplateLoader(this.url, this.targetContainerId, "MyLovedContent");
         this.tl.listener = this;
         this.tl.loadTemplate();
      } else {
         this.tl.reAttachElement();	          
      }	  
   },
   
   
   templateLoaded: function() { 
   
	    this.myLoveNameTable = document.getElementById("myLoveNameTable"); 	  					    
	    this.contact1RelationTable = document.getElementById("contact1RelationTable");
		this.contact2RelationTable = document.getElementById("contact2RelationTable");
		this.myLoveFriends1Table = document.getElementById("myLoveFriends1Table");
		this.myLoveFriends2Table = document.getElementById("myLoveFriends2Table");
		this.doctor1Table = document.getElementById("doctor1Table");
		this.doctor2Table = document.getElementById("doctor2Table");
		this.insuranceCarrierTable = document.getElementById("insuranceCarrierTable");
		
		this.notesTable = document.getElementById("notesTable");
		
		
	    this.TitleName = document.getElementById("TitleName");
	    this.includePrContact = document.getElementsByName("includePrmryContact");
		this.include2Contact = document.getElementsByName("include2Contact");
		this.includeFriend1 = document.getElementsByName("includeFriend1");
		this.includeFriend2 = document.getElementsByName("includeFriend2");
		this.include1Doctor = document.getElementsByName("include1Doctor");
		this.include2Doctor = document.getElementsByName("include2Doctor");
		this.includeIncCarrier = document.getElementsByName("includeIncCarrier");
		
		
		this.beforePerSelect = document.getElementById("beforePerSelect");
		this.afterPerSelect = document.getElementById("afterPerSelect");
		this.before2PerSelect = document.getElementById("before2PerSelect");
		this.after2PerSelect = document.getElementById("after2PerSelect");
		this.firend1Before = document.getElementById("firend1Before");
		this.firend1After = document.getElementById("firend1After");
		this.firend2Before = document.getElementById("firend2Before");
		this.firend2After = document.getElementById("firend2After");
		this.doctor1Before = document.getElementById("doctor1Before");
		this.doctor2Before = document.getElementById("doctor2Before");
		this.doctor1After = document.getElementById("doctor1After");
		this.doctor2After = document.getElementById("doctor2After");
		this.incCarrBefore = document.getElementById("incCarrBefore");
		this.incCarrAfter = document.getElementById("incCarrAfter");
		

		this.fullName1 = document.getElementById("fullName");		
		this.cont1Info = document.getElementById("cont1Info");
	  
		this.fullName2 = document.getElementById("fullNameSec");
		this.cont2Info = document.getElementById("cont2Info");
		
		this.frnd1Name = document.getElementById("frnd1Name");
		this.frnd1Info = document.getElementById("frnd1Info");	 	
		this.frnd2Name = document.getElementById("frnd2Name");
		this.frnd2Info = document.getElementById("frnd2Info");

		this.doc1Name = document.getElementById("doc1Name");
		this.doc1Info = document.getElementById("doc1Info");
		this.doc2Name = document.getElementById("doc2Name");
		this.doc2Info = document.getElementById("doc2Info");
		this.incCarrName = document.getElementById("incCarrName");
		this.insCarrInfo = document.getElementById("incCarrInfo");
		
			   
	   
	    var ec = new Af.ElementCollection(this.myLoveNameTable);			  	   
	    var ec1 = new Af.ElementCollection(this.contact1RelationTable);
	    var ec2 = new Af.ElementCollection(this.contact2RelationTable);
		var ecf1 = new Af.ElementCollection(this.myLoveFriends1Table);
		var ecf2 = new Af.ElementCollection(this.myLoveFriends2Table);
		var ecnotes = new Af.ElementCollection(this.notesTable);
		var ecdoc1 = new Af.ElementCollection(this.doctor1Table);
		var ecdoc2 = new Af.ElementCollection(this.doctor2Table);
		var ecIcarr = new Af.ElementCollection(this.insuranceCarrierTable);
		
	 	
	    this.save = document.getElementById("save");
	    this.addMyLoved = document.getElementById("addMyLoved");
	    this.deleteMyLoved = document.getElementById("deleteMyLoved");
		this.printMyLoved = document.getElementById("printMyLoved");
		this.printMyLoved.onclick = 	this.printMyLovedOne.bind(this);
		
		this.myLovedOnePreview = document.getElementById("myLovedOnePreview");
		this.myLovedOnePreview.onclick = 	this.doMyLovedOnePreview.bind(this);

		this.continue1 = document.getElementById("continue1");		
		this.continue2 = document.getElementsByName("continue2");
		this.continue3 = document.getElementsByName("continue3");
		this.continue4 = document.getElementsByName("continue4");


		this.continue1.onclick = this.doContToSecond.bind(this);
		for (ii = 0;ii<this.continue2.length ;ii++ ) {
			this.continue2[ii].onclick = this.doContToThird.bind(this);
		}
		for (i_ = 0;i_<this.continue3.length ;i_++ ) {
			this.continue3[i_].onclick = this.doContToFourth.bind(this);
		}
		for (k = 0;k < this.continue4.length ;k++ ) {
			this.continue4[k].onclick = this.doContToFifth.bind(this);
		}

		this.goBackTo1 = document.getElementsByName("goBackTo1");
		this.goBackTo2 = document.getElementsByName("goBackTo2");
		this.goBackTo3 = document.getElementsByName("goBackTo3");
		this.goBackTo4 = document.getElementsByName("goBackTo4");

		for (g1 = 0;g1< this.goBackTo1.length ;g1++ ) {
			this.goBackTo1[g1].onclick = this.doGoBackTo1.bind(this);
		}
		for (g2 = 0;g2 < this.goBackTo2.length ;g2++ ) {
			this.goBackTo2[g2].onclick = this.doGoBackTo2.bind(this);
		}
		for (g3 = 0;g3 < this.goBackTo3.length ;g3++ ) {
			this.goBackTo3[g3].onclick = this.doGoBackTo3.bind(this);
		}
		for (g4 = 0;g4 < this.goBackTo4.length ;g4++ ) {
			this.goBackTo4[g4].onclick = this.doGoBackTo4.bind(this);
		}

		for( i = 0; i < this.includePrContact.length ; i++)
	       this.includePrContact[i].onclick = this.doIncludePrContact.bind(this);
		for( i2 = 0; i2 < this.include2Contact.length ; i2++)
	       this.include2Contact[i2].onclick = this.doInclude2Contact.bind(this);
		
		  this.save.onclick = this.doSaveMyLovedOne.bind(this);	
		  
		for( i3 = 0; i3 < this.includeFriend1.length ; i3++)
		  this.includeFriend1[i3].onclick = this.doInclFriendOne.bind(this);
		for( i4 = 0; i4 < this.includeFriend2.length ; i4++)
		  this.includeFriend2[i4].onclick = this.doInclFriendTwo.bind(this);
		for( i5 = 0; i5 < this.include1Doctor.length ; i5++)
		  this.include1Doctor[i5].onclick = this.doInclOneDoctor.bind(this);
		for( i6 = 0; i6 < this.include2Doctor.length ; i6++)
		  this.include2Doctor[i6].onclick = this.doInclTwoDoctor.bind(this);
		for( i7 = 0; i7 < this.includeIncCarrier.length ; i7++)
		  this.includeIncCarrier[i7].onclick = this.doIncludeIncCarrier.bind(this);
		  
	 
	    this.addMyLoved.onclick = this.doAddMyLoved.bind(this);
	    this.deleteMyLoved.onclick = this.doDeleteMyLoved.bind(this);

	//	this.createPerson = document.getElementsByName("createPerson");
	//	this.createResource = document.getElementsByName("createResource");
		this.createPerson1 = document.getElementById("createPerson1");		
		this.createPerson2 = document.getElementById("createPerson2");		
		this.createPerson3 = document.getElementById("createPerson3");		
		this.createPerson4 = document.getElementById("createPerson4");		
		this.createPerson1.onclick = this.doShowPersonOne.bind(this);
		this.createPerson2.onclick = this.doShowPersonTwo.bind(this);
		this.createPerson3.onclick = this.doShowPersonThree.bind(this);
		this.createPerson4.onclick = this.doShowPersonFour.bind(this);

		this.createResource1 = document.getElementById("createResource1");		
		this.createResource2 = document.getElementById("createResource2");		
		this.createResource3 = document.getElementById("createResource3");		
		this.createResource1.onclick = this.doShowResourceOne.bind(this);
		this.createResource2.onclick = this.doShowResourceTwo.bind(this);
		this.createResource3.onclick = this.doShowResourceThree.bind(this);


	    this.loveTab = new Af.HtmlTab("tabMenu","myLoveName,myLoveContact,myLoveFriends,myLoveMedical,myLoveNotes",
										"myLoveNameTabContent,myLoveContactTabContent,myLoveFriendsTabContent," +
										 "myLoveMedicalTabContent,myLoveNotesTabContent");
		
		this.loveTab.tabSelectionListener = this;

		this.fName = document.getElementById("fName");
		
		this.fName.onfocus = this.focusOnTextBox.bindAsEventListener(this);
		this.fName.onblur = this.focusLostTextBox.bindAsEventListener(this);
		this.fName.onkeyup = this.doSetTitleName.bind(this);
		this.fName.onclick =  this.focusOnTextBox.bindAsEventListener(this);
		this.defaultName = this.fName.value;


		this.cont1RelFocus = document.getElementById("contact1Relation");		
		this.cont2RelFocus = document.getElementById("contact2Relation");		
		this.fCont1RelFocus = document.getElementById("fContact1Relation");	
		this.fCont2RelFocus = document.getElementById("fContact2Relation");	
		this.doctor1Focus = document.getElementById("type1Doctor");	
		this.doctor2Focus = document.getElementById("type2Doctor");	
		this.policyNoFocus = document.getElementById("policyNumber");	
		this.note1Focus = document.getElementById("note1");	


	    this.editor.addTextByEC(ec, "fName");	
	    this.editor.addTextByEC(ec, "lName");
	    this.cont1Relation = this.editor.addTextByEC(ec1, "contact1Relation"); 	  	 
	    this.cont2Relation = this.editor.addTextByEC(ec2, "contact2Relation"); 	  	 
		this.fCont1Relation = this.editor.addTextByEC(ecf1, "fContact1Relation"); 	  	 
		this.fCont2Relation = this.editor.addTextByEC(ecf2, "fContact2Relation");
		this.doctor1 = this.editor.addTextByEC(ecdoc1, "type1Doctor");
		this.doctor2 = this.editor.addTextByEC(ecdoc2, "type2Doctor");
		this.policyNumber = this.editor.addTextByEC(ecIcarr, "policyNumber");
		this.editor.addTextByEC(ecnotes, "note1");
		this.editor.addTextByEC(ecnotes,"note2");
		
	    this.editor.clearChanges();		 
		this.editor.setValueFromObj(this.obj);		

		this.doCleanContact();		 
		this.isTemplateLoaded = true;
		
		if(this.obj.fName == undefined) {
		   this.TitleName.innerHTML = "";
		} else {
			this.TitleName.innerHTML = this.obj.fName;
	    }
	    
		this.setTitleAndValues();  
			
		
		this.doInitAssociations();
		 
	},

	tabSelected: function(idx, tabName) {		
		if(tabName == "myLoveNotes") {
			this.note1Focus.focus();
		}
	},
	
	doSetTitleName: function() {
		this.TitleName.innerHTML = this.fName.value;		
	},

	doDeleteMyLoved: function() {
		 if(this.currentRow >= 0)
		 { 		   
		   showConfirmDialog("Are you sure you want to delete this Loved One?", "Are You Sure?", 330, 120, this.deleteMyLovedOne.bind(this));		   
		}  else {
           showMessageDialog("Please select a My Loved One to delete. " , "Request", 330, 120);
        }
        return false;
	},

	deleteMyLovedOne: function() {	
		var obj = this.dataTable.deleteObjectByIndex(this.currentRow);
		this.myLovedOneTable.setDataList(this.dataTable.getDataList());
		this.currentRow = -1;		
		 var req = new Af.DataRequest(svcURL, this.requestDeleteCompleted.bind(this), this.requestFailedCommon.bind(this), null, this.requestTimedoutCommon.bind(this));
         req.addService("WorkspaceService", "deleteMyLovedOne");
         req.addParameter("UUID", obj.UUID);
         ajaxEngine.processRequest(req);		
	},

	requestDeleteCompleted: function(response)	{
		showMessageDialog("The record of your My Loved One was sucessfully deleted." , "Deletion Successful", 330, 100);
		this.cellSelected(0, 0, false);
	},

	requestFailedCommon: function() {
		showMessageDialog("We are currently unable to delete this record. <br/> Please try again." , "Excuse us!", 330, 100);
	},

	requestTimedoutCommon: function() {
		showMessageDialog("We are currently unable to delete this record. <br/> Please try again." , "Excuse us!", 300, 80);
	},

	doIncludePrContact: function() {		
		this.showInfo = 1;
		this.doInitAssociations(); 
		var pSelector = getPeopleSingleSelector(this, true);
        pSelector.setPeopleDataList(app.workspace["myPeople"],this.obj["contact1"]);
        return false;

	},
	doInclude2Contact: function() {		
		 this.showInfo = 2;
		 this.doInitAssociations();
		  
		 var pSelector = getPeopleSingleSelector(this, true);
         pSelector.setPeopleDataList(app.workspace["myPeople"],this.obj["contact2"]);
         return false;
	},

	doInclFriendOne: function() {
		 this.showInfo = 3;		
		 this.doInitAssociations();
		 var pSelector = getPeopleSingleSelector(this, true);
         pSelector.setPeopleDataList(app.workspace["myPeople"],this.obj["friendContact1"]);		
         return false; 
	},

	doInclFriendTwo: function() { 		  
		 this.showInfo = 4;		
		 this.doInitAssociations();
		  
		 var pSelector = getPeopleSingleSelector(this, true);
         pSelector.setPeopleDataList(app.workspace["myPeople"],this.obj["friendContact2"]);
         return false;
	},

	doInclOneDoctor: function() {				
		this.showInfo = 5;	
	    this.doInitAssociations();		  
		var rSelector = getResourceSingleSelector(this, true);
        rSelector.setResourceDataList(app.workspace["myResource"],this.obj["doctor1"]);
        return false;			   
	},

	doInclTwoDoctor: function() {	   
	   this.showInfo = 6;	
	   this.doInitAssociations();		  
	   var rSelector = getResourceSingleSelector(this, true);
       rSelector.setResourceDataList(app.workspace["myResource"],this.obj["doctor2"]);
       return false;	
	},
	doIncludeIncCarrier: function() {	   
	   this.showInfo = 7;	
	   this.doInitAssociations();		  
	    var rSelector = getResourceSingleSelector(this, true);
       rSelector.setResourceDataList(app.workspace["myResource"],this.obj["insuranceCarrier"]);		
       return false;
	},

	selectionDone: function(peopleDialog) {				
		if(peopleDialog.selectedPeopleList == null) {
			return
		}
		var contObj = peopleDialog.selectedPeopleList[0];	
		if (contObj == null) {
		   return;
		}	
		this.prSelected = true;
		this.showContactInformation(contObj);		
		
  },
 selectionDoneResource: function(resourceDialog) {			   
	    if(resourceDialog.selectedResourceList == null) {
			return
		}
		var contObj = resourceDialog.selectedResourceList[0];	
		if (contObj == null) {
		   return;
		}	
		this.prSelected = true;
		this.showContactInformation(contObj);		
  },

	doInitAssociations: function() {
	     if (this.obj["contact1"] == null) {
            createOneToManyAssoc(this.obj, "contact1", "Person");
          } 
		 if (this.obj["contact2"] == null) {
            createOneToManyAssoc(this.obj, "contact2", "Person");
          }
		  if (this.obj["friendContact1"] == null) {
            createOneToManyAssoc(this.obj, "friendContact1", "Person");
          }
		  if (this.obj["friendContact2"] == null) {
            createOneToManyAssoc(this.obj, "friendContact2", "Person");
          }
		  
		  if (this.obj["doctor1"] == null) {
            createOneToManyAssoc(this.obj, "doctor1", "Resource");
          }
		  if (this.obj["doctor2"] == null) {
            createOneToManyAssoc(this.obj, "doctor2", "Resource");
          }	
		 if (this.obj["insuranceCarrier"] == null) {
            createOneToManyAssoc(this.obj, "insuranceCarrier", "Resource");
          }	
		  return
	},

	setTitleAndValues: function() {
	    if(this.obj.contact1Relation != undefined) {
		    this.beforePerSelect.style.display = "none";
		    this.afterPerSelect.style.display = "block";			
		  }
		  else {
			this.beforePerSelect.style.display = "block";
		    this.afterPerSelect.style.display = "none";
		  }

		  if(this.obj.contact2Relation != undefined) {
		    this.before2PerSelect.style.display = "none";
		    this.after2PerSelect.style.display = "block";			
		  }
		  else {
			this.before2PerSelect.style.display = "block";
		    this.after2PerSelect.style.display = "none";
		  }

		  if(this.obj.fContact1Relation != undefined) {
		   	 this.firend1After.style.display = "block";
		     this.firend1Before.style.display = "none";			
		  }
		  else {
			  this.firend1After.style.display = "none";
		      this.firend1Before.style.display = "block";			
		  }
		  if(this.obj.fContact2Relation != undefined) {
		   	 this.firend2After.style.display = "block";
		     this.firend2Before.style.display = "none";			
		  }
		  else {
			  this.firend2After.style.display = "none";
		      this.firend2Before.style.display = "block";			
		  }
		  	
		  if(this.obj["contact1"] != undefined) {			  
			  if (this.obj["contact1"][0] != undefined)
			  {	  
			      this.showInfo = 1;
				  var cont1Obj = this.obj["contact1"][0]; 
				  this.showContactInfo(cont1Obj);
			  }			
		  }
		  if (this.obj["contact2"] != undefined)
		  {	
			   if (this.obj["contact2"][0] != undefined) {			     
				 this.showInfo = 2;
				 var cont2Obj = this.obj["contact2"][0]; 
				 this.showContactInfo(cont2Obj);
			   }
		  }
		  if (this.obj["friendContact1"] != undefined)
		  {	
			   if (this.obj["friendContact1"][0] != undefined) {			     
				 this.showInfo = 3;
				 var frnd1Obj = this.obj["friendContact1"][0]; 
				 this.showContactInfo(frnd1Obj);
			   }
		  }
		  if (this.obj["friendContact2"] != undefined)
		  {	
			   if (this.obj["friendContact2"][0] != undefined) {			     			     
				 this.showInfo = 4;
				 var frnd2Obj = this.obj["friendContact2"][0]; 
				 this.showContactInfo(frnd2Obj);
			   }
		  }
		  if (this.obj["doctor1"] != undefined)
		  {	
			   if (this.obj["doctor1"][0] != undefined) {			     			     				 
				 this.showInfo = 5;
				 var frnd2Obj = this.obj["doctor1"][0]; 
				 this.showContactInfo(frnd2Obj);
			   }
		  }
		  if (this.obj["doctor2"] != undefined)
		  {	
			   if (this.obj["doctor2"][0] != undefined) {			     			     				 
				 this.showInfo = 6;
				 var frnd2Obj = this.obj["doctor2"][0]; 
				 this.showContactInfo(frnd2Obj);
			   }
		  }
		  if (this.obj["insuranceCarrier"] != undefined)
		  {	
			   if (this.obj["insuranceCarrier"][0] != undefined) {			     			     				 
				 this.showInfo = 7;
				 var frnd2Obj = this.obj["insuranceCarrier"][0]; 
				 this.showContactInfo(frnd2Obj);
			   }
		  }
	},
	 

	showContactInfo: function(contObj) {				  
	   this.showContactInformation(contObj);
	},

	showContactInformation: function(contObj) {
		 if (contObj == null) {
		    return;
		 }
		 var  firstContactInfo = "";		
			 if(contObj.phone != undefined)
				firstContactInfo  += '<br/>' +'Phone - '+ contObj.phone; 				 
			 if(contObj.phone1 != undefined)
				firstContactInfo = 'Hom - '+contObj.phone1; 
			 if(contObj.phone2 != undefined)
				firstContactInfo  += '<br/>' + 'Off - '+ contObj.phone2; 
			 if(contObj.phone3 != undefined)
				firstContactInfo  += '<br/>' + 'Mob - '+ contObj.phone3;
			 if(contObj.email != undefined)		
				firstContactInfo  += '<br/>' + contObj.email;
			 if(contObj.address != undefined)
				firstContactInfo  +=  '<br/><br/>' + contObj.address ;
			 if(contObj.city != undefined)
				firstContactInfo  +=  '<br/>' + contObj.city ;
			 if(contObj.state != undefined)
				firstContactInfo  +=  '  '+contObj.state ;
			 if(contObj.zip != undefined)
				firstContactInfo  +=  '-'+contObj.zip ;	 		


		 switch(this.showInfo) {

		 case 1: 
			this.beforePerSelect.style.display = "none";
			this.afterPerSelect.style.display = "block";				
		    this.fullName1.innerHTML =  getFullName(contObj);						
			this.cont1Info.innerHTML = firstContactInfo;  			
			if(this.prSelected) {
				this.prSelected = false;
				this.cont1Relation.setValue("");
				this.cont1RelFocus.focus();
			}			
			break;
		case 2:
			this.before2PerSelect.style.display = "none";
			this.after2PerSelect.style.display = "block";			
			this.fullName2.innerHTML =  getFullName(contObj);
			this.cont2Info.innerHTML = firstContactInfo; 			
			if(this.prSelected) {
				this.prSelected = false;
				this.cont2Relation.setValue("");
				this.cont2RelFocus.focus();
			}			
		    break;
		
		case 3:
		    this.firend1Before.style.display = "none";
			this.firend1After.style.display = "block";			
			this.frnd1Name.innerHTML =  getFullName(contObj);		
			this.frnd1Info.innerHTML = firstContactInfo;
			if(this.prSelected) {
				this.prSelected = false;
				this.fCont1Relation.setValue("");
				this.fCont1RelFocus.focus();
			}			
			break;

		case 4:
		    this.firend2Before.style.display = "none";
			this.firend2After.style.display = "block";	
			this.frnd2Name.innerHTML =  getFullName(contObj);			
			this.frnd2Info.innerHTML = firstContactInfo;
			if(this.prSelected) {
				this.prSelected = false;				
				this.fCont2Relation.setValue("");
				this.fCont2RelFocus.focus();
			}			
		    break;

	   case 5:
		   this.doctor1Before.style.display = "none"
	       this.doctor1After.style.display = "block";
	       this.doc1Name.innerHTML =  getBusinessName(contObj);	
		   this.doc1Info.innerHTML = firstContactInfo;
		   if(this.prSelected) {
				this.prSelected = false;				
				this.doctor1.setValue("");
				this.doctor1Focus.focus();
		   }		   
		   break;
	  case 6:
		   this.doctor2Before.style.display = "none"
	       this.doctor2After.style.display = "block";
		   this.doc2Name.innerHTML =  getBusinessName(contObj);	
		   this.doc2Info.innerHTML = firstContactInfo;
		   if(this.prSelected) {			
				this.prSelected = false;				
				this.doctor2.setValue("");
				this.doctor2Focus.focus();
		   }		   
		   break;
	  case 7:	
		   this.incCarrBefore.style.display = "none"
	       this.incCarrAfter.style.display = "block"; 	       
	       this.incCarrName.innerHTML = "<strong>" + getBusinessName(contObj) + "</strong>";
		   this.insCarrInfo.innerHTML = firstContactInfo;
		    if(this.prSelected) {			
				this.prSelected = false;								
				this.policyNumber.setValue("");
				this.policyNoFocus.focus();
		   } 		  
		   break;
		 
		 }			 
	},
	
	doCleanContact: function() {		
		this.fullName1.innerText = "";
		this.fullName2.innerText = "";			
		this.cont1Info.innerText = "";	  		
		this.cont2Info.innerText = "";					
		this.frnd1Info.innerHTML = "";
		this.frnd2Info.innerHTML =""; 
		this.TitleName.innerHTML = "";
		
		this.beforePerSelect.style.display = "block";
		this.afterPerSelect.style.display = "none";
		this.before2PerSelect.style.display = "block";
		this.after2PerSelect.style.display = "none";

		this.firend1After.style.display = "none";
		this.firend1Before.style.display = "block"		
		this.firend2After.style.display = "none";
		this.firend2Before.style.display = "block"
	 
		this.doctor1After.style.display = "none";
		this.doctor1Before.style.display = "block";			

		this.doctor2After.style.display = "none";
		this.doctor2Before.style.display = "block"

		this.incCarrAfter.style.display = "none";
		this.incCarrBefore.style.display = "block"
		
		this.doRefreshTemplate();
		  
	},

	doRefreshTemplate: function() {		
		this.obj = this.dataTable.getDataList()[this.currentRow];		
		this.formatNotesParagraph();
		/*
		this.obj["note1"] = this.replaceAll(this.obj["note1"],'<BR/>','\n');
		this.obj["note2"] = this.replaceAll(this.obj["note2"],'<BR/>','\n');
		*/
		this.editor.setValueFromObj(this.obj); 
		this.TitleName.innerHTML = this.obj.fName;
		this.loveTab._showTab(0);
		this.loveTab._hideTab(1);		
		this.loveTab._hideTab(2);		
		this.loveTab._hideTab(3);		
		this.loveTab._hideTab(4);
	},

	doContToSecond: function() {				
		this.loveTab._hideTab(0);		
		this.loveTab._showTab(1);
		return false;
	},
	doContToThird: function() {	
		var val0 = this.cont1Relation.getValue();		
		var val1 = this.cont2Relation.getValue();		
		if( val0 == "" ||  val1 == "") {
			showMessageDialog("Please enter the relationship of this person to your Loved One. ", "Request", 400, 100);
			return;
		} 		
		this.loveTab._hideTab(1);
		this.loveTab._showTab(2);
		return false;
	},
	doContToFourth: function() {		
		var val0 = this.fCont1Relation.getValue();		
		var val1 = this.fCont2Relation.getValue();		
		if( val0 == "" ||  val1 == "") {
			showMessageDialog("Please enter the relationship of this person to your Loved One. ", "Request", 400, 100);
			return;
		} 
	    this.loveTab._hideTab(2);
		this.loveTab._showTab(3);
		return false;
	},
	doContToFifth: function() {		
		var val0 = this.doctor1.getValue();		
		var val1 = this.doctor2.getValue();		
		if( val0 == "" ||  val1 == "") {
			showMessageDialog("Please enter what kind of doctor this is. ", "Request", 400, 100);
			return;
		} 
	    this.loveTab._hideTab(3);
		this.loveTab._showTab(4);
		return false;
	},
	doGoBackTo1: function() {
	    this.loveTab._hideTab(1);
		this.loveTab._showTab(0);
		return false;
	},
	doGoBackTo2: function() {
	    this.loveTab._hideTab(2);
		this.loveTab._showTab(1);
		return false;
	},
	doGoBackTo3: function() {
	    this.loveTab._hideTab(3);
		this.loveTab._showTab(2);
		return false;
	},
	doGoBackTo4: function() {
	    this.loveTab._hideTab(4);
		this.loveTab._showTab(3);
		return false;
	},
	 doAddPerson: function() {	
	  app.myPeople.IsNewPerson = true;
      if (this.dialog != null) {
    	  delete this.dialog;
      }
	  this.dialog = new HJ.MyPeopleDetail("Person Details", app.myPeople, null);			
	  this.dialog.width = "520px";
      var obj = new Object();
      this.dialog.showHTMLTemplate(basePageURL + "MyPeopleDetail.html", "content", "peopleDetail");
	  this.dialog.centerDialog(520, 280);
      this.dialog.setObj(obj, true);
	  this.dialog.isCallFromMyLovedOne = true;
      return false;
   }
   ,
   
   doAddResource: function() {
	  app.myResources.addNew = true;
      if (this.dialog1 == null) {
         this.dialog1 = new HJ.MyResourcesDetail("My Resources", app.myResources, null);
         this.dialog1.width = "550px";
      }
      var obj = new Object();
      this.dialog1.showHTMLTemplate(basePageURL + "MyResourcesDetail.htm", "content", "resourceDetail");
	  this.dialog1.centerDialog(200, 280);
      this.dialog1.setObj(obj, true);
	  this.dialog1.isCallFromMyLovedOne = true;
      return false;
   },
   
   doMyLovedOnePreview: function() {	
	   this.isPreviewClicked = true;
	   this.doSaveMyLovedOne();		  
	  
   },
	
   showPrintPreviw: function() {
	   showModalMessageDialog("Preparing ... please wait", 240, 40);
	   var req = new Af.DataRequest(svcURL,this.printCompleted.bind(this), this.printFailed.bind(this), null, this.requestTimedout.bind(this));		   
	   req.addService("WorkspaceService", "getPrintPreview");
	   req.addParameter("templateName", "getPrintPreview");
	   req.addParameter("xlstFile","MyLovedOne.xsl");
	   //this.xmlString = this.replaceAll(this.xmlString,'&lt;BR/&gt;','\n');
	   req.xmlDoc = this.xmlString;
	   ajaxEngine.processRequest(req);
	   
	   this.isPreviewClicked = false;	   
   },
	
   printMyLovedOne:function(){
   	   showModalMessageDialog("Preparing ... please wait", 240, 40);
	   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", "allAboutMyLovedOne");
	   req.addParameter("xlstFile","MyLovedOne.xsl");	 
	   if(this.obj.UUID != null) {
		 req.addParameter("UUID",this.obj.UUID);
	   }
	   ajaxEngine.processRequest(req);	   
	   return false;
   },

   printCompleted: function(response)	{
	hideModalMessageDialog();
     var p = new Af.PrintUtil();
     p.listener = this;
     p.showPrintView(response);
     return false;
  },
   
   printFailed: function() {
		showMessageDialog("<br/>Printing is currently unavailable.Please try again.", "Printing Unavailable", 400, 150);
   },
   
   printingDone:function() {	
   		this.viewSelected();
   		return false;
   },
   
   requestTimedout: function() {
		showMessageDialog("<br/>Printing is currently unavailable.Please try again.", "Printing Unavailable", 400, 150);
   },
	
   doShowPersonOne: function() {
	   this.showInfo = 1;
	   this.createNewObjectOfType = "contact1";
	   this.doAddPerson();
   },
	
   doShowPersonTwo: function() {
	   this.showInfo = 2;
	   this.createNewObjectOfType = "contact2";
	   this.doAddPerson();
   },

   doShowPersonThree: function() {
	   this.showInfo = 3;
	   this.createNewObjectOfType = "friendContact1";
	   this.doAddPerson();
   },

   doShowPersonFour: function() {
	   this.showInfo = 4;
	   this.createNewObjectOfType = "friendContact2";
	   this.doAddPerson();
   },
	
   doShowResourceOne: function() {
	   this.showInfo = 5;
	   this.createNewObjectOfType = "doctor1";
	   this.doAddResource();
   },

   doShowResourceTwo: function() {
	   this.showInfo = 6;
	   this.createNewObjectOfType = "doctor2";
	   this.doAddResource();
   },
	
   doShowResourceThree: function() {
	   this.showInfo = 7;
	   this.createNewObjectOfType = "insuranceCarrier";
	   this.doAddResource();
   },

	 focusOnTextBox: function(evt) {			
		  var s = this.fName.value;
		  if (s == null) {
			s = "";
		  } else {
			s = trim(s);
		  }			 
		  if (s == trim(this.defaultName) || s == "") {
			 this.fName.value = "";
			 consumeEvent(evt);
		  }

		 // this.fName.focus();
	   },
   
	focusLostTextBox: function(evt) {    		
		  var s = this.fName.value;
		  if (s == null) {
			s = "";
		  } else {
			s = trim(s);
		  }
		  if (s == trim(this.defaultName) || s == "") {
			 this.fName.value = this.defaultName;
			 consumeEvent(evt);
		  }	         
	 },

	replaceAll:function (argvalue, x, y) {
		if ((x == y) || (parseInt(y.indexOf(x)) > -1)) {
			errmessage = "replace function error: \n";
			errmessage += "Second argument and third argument could be the same ";
			errmessage += "or third argument contains second argument.\n";
			errmessage += "This will create an infinite loop as it's replaced globally.";
			showMessageDialog(errmessage, "Excuse us!", 300, 100);
			return false;
		}
		while (argvalue.indexOf(x) != -1) {
			var leading = argvalue.substring(0, argvalue.indexOf(x));
			var trailing = argvalue.substring(argvalue.indexOf(x) + x.length,argvalue.length);
			argvalue = leading + y + trailing;
		}
		return argvalue;
	},

	formatNotesParagraph: function() {
		if(this.obj["note1"] != null){
			this.obj["note1"] = this.replaceAll(this.obj["note1"],'<BR/>','\n');		
		}
		if(this.obj["note2"] != null){
			this.obj["note2"] = this.replaceAll(this.obj["note2"],'<BR/>','\n');
		}
		return;
	}
}

