HJ.MyPublicProfile = Class.create();

HJ.MyPublicProfile.prototype = {

   initialize: function() {
		
		  this.url = basePageURL + "MyPublicProfile.htm";
		  this.targetContainerId = "pageBottom";
		  this.tl = null;
		  this.editor = null;
		  this.userObj = new Object();
		  this.sex = "Female";
	},
	
	 loadMyPubProfileTemplate: function () {
		  if (this.tl == null) {
			 this.tl = new Af.TemplateLoader(this.url, this.targetContainerId, "myPublicProfileContent");
			 this.tl.listener = this;
			 this.tl.loadTemplate();
		  } else {
			 this.tl.reAttachElement();
			 this.loadMyPubProfile();	 
			 this.volunteeringCursor.focus();
		  }
		  return false;
     },
     
     templateLoaded: function() {
		 
			var ec = new Af.ElementCollection(this.tl.element);
			this.showMyInfo = ec.getFirstElementById("showMyInfo"); 
			this.showMyInfo.onclick = this.doShowMyInfo.bind(this);
			this.showMyComResources = ec.getFirstElementById("showMyComResources");		 
			this.showMyComResources.onclick = this.doShowMyComResources.bind(this);
			this.showMyMHM = ec.getFirstElementById("showMyMHM");
			this.showMyMHM.onclick = this.doShowMyMHM.bind(this);

			this.savePubProfile = ec.getFirstElementById("saveMyPubProfile"); 
			this.savePubProfile.onclick = this.doSavePubProfile.bind(this);


			this.volunteeringCursor = document.getElementById("volunteering");

			this.editor = new Af.ObjEditor();
			this.editor.clearChanges();		 
			this.editor.setValueFromObj(this.userObj);

			this.editor.addTextByEC(ec, "childrenNames");
			this.editor.addTextByEC(ec, "childrenAges");
			this.editor.addTextByEC(ec, "schoolNames");

			this.editor.addTextByEC(ec, "volunteering");
			this.editor.addTextByEC(ec, "expertIn");

			this.editor.addTextByEC(ec, "believeIn");	      
			this.editor.addTextByEC(ec, "valuesIn");	      
			this.editor.addTextByEC(ec, "likes");	      
			this.editor.addTextByEC(ec, "dream");	      

			//this.editor.addCheckBoxByEC(ec, "mailMe");
			this.editor.addCheckBoxByEC(ec, "workOutSideHome");
			this.gender =  document.getElementsByName("gender");
		  for (gIdx = 0;gIdx < this.gender.length ;gIdx++ )  {
			this.gender[gIdx].onchange = this.doSetGenderSex.bindAsEventListener(this);
		  }
		  

	    this.loadMyPubProfile();
	 },
	 
	 doSavePubProfile: function() {
			this.editor.addNV("gender", this.sex);

			var xmlString = this.editor.getChangeXml();
			
	
		  if (xmlString == null) {
				showMessageDialog("Any changes you have made have been saved. ", "Informing You", 350, 100);
				return;
		  }
		 
		  
		  var req = new Af.DataRequest(svcURL, this.saveSuccessful.bind(this), this.requestFailedCommon.bind(this), null, 
					this.requestTimedoutCommon.bind(this));
					
		  req.addService("AccessControlSvc", "saveMyPubProfile"); 
		  	
		  req.xmlDoc = xmlString;
		  
		  ajaxEngine.processRequest(req);
	 },
	 
	 loadMyPubProfile: function() {
	   var req = new Af.DataRequest(svcURL,
	   this.requestMyProfileLoadCompleted.bind(this), this.requestFailedCommon.bind(this), null, this.requestTimedoutCommon.bind(this));
	   req.addService("AccessControlSvc", "getMyPubProfile");
	   ajaxEngine.processRequest(req);
	   return false;
	 },
	 
	 requestMyProfileLoadCompleted: function(response) {	 
	   var xds = new Af.XMLToDataSet(response.responseXML);
	   this.userObj = xds.data;
	   this.editor.clearChanges();		 
	   this.editor.setValueFromObj(this.userObj);
	   if(this.userObj["gender"] == "Male") {		 
		   this.gender[1].checked = true;
	   }
	   else {		
		  this.gender[0].checked = true;
	   }
	   this.volunteeringCursor.focus();
	},
	
	
	requestFailedCommon: function(dataRequest, msg) {
	    hideDialogWin();
	    showMessageDialog("We are currently unable to display this record. Please try again.", "Excuse us!", 350, 100);
    },

    requestTimedoutCommon: function(dataRequest) {
        hideDialogWin();
        showMessageDialog("We are currently unable to display this record. Please try again.", "Excuse us!", 350, 100);
    },
    
    saveSuccessful: function(response) {
			debugA(response.responseText);
			showMessageDialog("Your profile has been successfully updated.", "Success !", 350, 100);
    },
	
	doShowMyInfo: function() {
		return app.myInfo.loadMyInfoTemplate();
	},

	doShowMyComResources: function() {
		return app.commResProfile.loadCommResTemplate();
	},

	doSetGenderSex: function(evt) {
		var e = evt.target ? evt.target : evt.srcElement;
		if (e != null) {
		this.sex = e.value;
		}	
		
	},

	 doShowMyMHM:function(){
		 return app.myMHM.loadMyMHMTemplate();
	 }

}

