HJ.MyInfo = Class.create();

HJ.MyInfo.prototype = {

   initialize: function() {
		
		  this.url = basePageURL + "MyInfo.html";
		  this.targetContainerId = "pageBottom";
		  this.tl = null;
		  this.editor = null;
		  this.userObj = null;
		  this.originalEmail = null;
		  this.isCancelledEmail = false;
		  
	},
	
	 loadMyInfoTemplate: function () {
		showModalMessageDialog("Processing ... please wait", 300, 40);
		  if (this.tl == null) {
			 this.tl = new Af.TemplateLoader(this.url, this.targetContainerId, "myInfo");
			 this.tl.listener = this;
			 this.tl.loadTemplate();
		  } else {
			 this.tl.reAttachElement();
			  this.loadMyProfile();
			  if(this.fNameCursor != null){
				  this.fNameCursor.focus();
			  }
			   hideModalMessageDialog();
		  }
		  return false;
     },
     
     templateLoaded: function() {

		var ec = new Af.ElementCollection(this.tl.element);

		this.save = ec.getFirstElementById("saveMyProfile");
		/*
		this.showMyPublicProfile = ec.getFirstElementById("showMyPublicProfile");		 
		this.showMyPublicProfile.onclick = this.doShowMyPublicProfile.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.changePwd = ec.getFirstElementById("changePwd");
		this.changePwd.onclick = this.doChangePassword.bind(this);

		this.cancelSubscription = ec.getFirstElementById("cancelSubscription");
		this.cancelSubscription.onclick = this.doCancelSubscription.bind(this);

		this.save.onclick = this.saveClicked.bind(this);
				
		this.editor = new Af.ObjEditor();
		this.fNameCursor = document.getElementById("fName");

		this.fName = this.editor.addTextByEC(ec, "fName");
		this.lName = this.editor.addTextByEC(ec, "lName");
		this.editor.addTextByEC(ec, "phone1");

		this.email = this.editor.addTextByEC(ec, "email");
		this.sendScheduleReminder = this.editor.addCheckBoxByEC(ec, "sendScheduleReminder");

		this.editor.addTextByEC(ec, "zip");

		this.userId = this.editor.addTextByEC(ec, "userId");

		this.loadMyProfile();
		this.fNameCursor.focus();
	 },
	
	 saveClicked: function(){
		 var email = this.userObj.email;
			
		 if(email == null || "" == trim(email) || (app.emailValidator.validateEmail(trim(email))==false)){
			showMessageDialog("Please enter a valid email address.","Request",300, 100, null, true);
			return;
		 }
		 email = app.emailValidator.extractEmailAddress(email);
		 if(email != this.originalEmail){
			 this.emailChangeAlert();
			 return;
		 }
		 this.doSave();
		 email = null;
	 },
	 
	 doSave: function() {

	      var xmlString = this.editor.getChangeXml();		
	  
		  if (xmlString == null) {
				showMessageDialog("Any changes you have made have been saved. ", "Informing You", 350, 100);
				return;
		  }	 
		  
		 // debugA(xmlString);
		  
		  var req = new Af.DataRequest(svcURL, this.saveSuccessful.bind(this), this.requestFailedCommon.bind(this), null, 
					this.requestTimedoutCommon.bind(this));
					
		  req.addService("AccessControlSvc", "saveUser"); 

		  req.xmlDoc = xmlString;
		  
		  ajaxEngine.processRequest(req);
	 },
	 
	 loadMyProfile: function() {
	   var req = new Af.DataRequest(svcURL,
			this.requestMyProfileLoadCompleted.bind(this), this.requestFailedCommon.bind(this), null, this.requestTimedoutCommon.bind(this));
	   req.addService("AccessControlSvc", "getMyProfile");
	   ajaxEngine.processRequest(req);
	   return false;
	 },
	 
	 requestMyProfileLoadCompleted: function(response) {
	   //debugA(response.responseText);
	    this.originalEmail = null;
		var xds = new Af.XMLToDataSet(response.responseXML);
		this.userObj = xds.data;
		if (this.userObj.sendScheduleReminder == null) {
			// default is true
			this.userObj.sendScheduleReminder = true;
		}	 
		this.editor.clearChanges();
		this.editor.setValueFromObj(this.userObj);
		this.fNameCursor.focus();
		this.originalEmail = this.userObj.email;
		hideModalMessageDialog();
	},
	
	
	requestFailedCommon: function(dataRequest, msg) {
	    hideDialogWin();
		this.email.setValue(this.originalEmail);
		this.userObj.email = this.originalEmail;
		this.isCancelledEmail = false;
	    showMessageDialog(msg, "Excuse us!", 350, 130, null, true);
    },

    requestTimedoutCommon: function(dataRequest) {
        hideDialogWin();
		this.isCancelledEmail = false;
        showMessageDialog("We are currently unable to display this record. Please try again.", "Excuse us!", 350, 100);
    },
    
    saveSuccessful: function(response) {
		if(this.isCancelledEmail){
			this.isCancelledEmail = false;
			showMessageDialog("Your personal information has been saved. <br/>"+
				" you had cancelled the change to your email ID", "Success !", 300, 100);
		}else{
			showMessageDialog("Your personal information has been saved. ", "Success !", 300, 100);
		}
		if(this.userId.getValue() != null && this.userId.getValue() != undefined) {
			login.userId = this.userId.getValue();
			var fName = this.fName.getValue();
			var lName = this.lName.getValue();
			if(fName != undefined) {
				login.userFullName = fName ;
			}
			/*if(lName != undefined) {
				login.userFullName += " "+lName;
			}*/
				app.db.changeScreenName();
				this.originalEmail = this.email.getValue();
			}
    },
	
	doShowMyPublicProfile: function() {	
		 return app.publicProfile.loadMyPubProfileTemplate();
	},

	doShowMyComResources: function() {
		return app.commResProfile.loadCommResTemplate();
	},

	 doShowMyMHM:function(){
		 return app.myMHM.loadMyMHMTemplate();
	 },

	doChangePassword: function() {
		if(this.pwdDialog == null) {  
			this.pwdDialog = new HJ.ChangePassword("Password", this, null);				
			this.pwdDialog.width = "370px";	
		}
		this.pwdDialog.showHTMLTemplate(basePageURL + "ChangePassword.htm", this.targetContainerId, "changePwdContent");
		this.pwdDialog.centerDialog(400, 280);
		if(this.pwdDialog.oldPwd != null){
			this.pwdDialog.oldPwd.focus();
		}
		
		return false;		
	},
	
	doCancelSubscription: function() {
		var dlg = showMessageDialog(
				"To cancel either your Free 30 Day Trial or your Subscription, " +
					"<a href='https://payments.amazon.com/sdui/sdui/subscriptions?Type=SUBSCRIBER'>click here</a>. <br /><br />" +
					"If you are past your 30 Day Free Trial period, consider how far you are into your " +
					"year's subscription.  If you have a lot of the year left, consider keeping your " +
					"account open and using it, as we do not give pro-rated returns.  " +
					"Thirty days before your subscription renews, we will send you a notice.",
				"Cancel Free Trial or Subscription", 
				400, 180, 
				null, null, 
				true);
	},
	
	startCancelSubscription: function() {
		window.location = "https://payments.amazon.com/sdui/sdui/subscriptions?Type=SUBSCRIBER";
		return false;

	},

	emailChangeAlert: function(){
		if (this.emailChngDialog != null) {
			delete this.emailChngDialog;
		}
        var handler = {
           templateLoaded: function() {
              this.parent.emailChangeAlertLoaded();
           },
			parent:this
        }
        this.emailChngDialog = new Af.HtmlDialog("Jooners email ID changed", handler, null);
        this.emailChngDialog.width = "400px";
         this.emailChngDialog.showHTMLTemplate(basePageURL + "EmailChangeCrfmDialog.htm", "MainArea", "changeEmailDailog");     
		 this.emailChngDialog.centerDialog(350, 350);
		if(this.messageText != null){
			 this.messageText.innerHTML = "You have changed your email ID for this account to: "+
			"<b>"+ this.userObj.email +"</b>"+"<BR/>"+"Please carefully check to make sure this email ID is correct."+
			"<BR/>"+ "The information stored now in this account will only be accessible with this new email ID.";
		}
	},	

	emailChangeAlertLoaded: function() {				
		this.saveNewEmail = document.getElementById("saveNewEmail");		
		this.cancelNewEmail = document.getElementById("cancelNewEmail");
		this.messageText = document.getElementById("messageText");
		
		this.saveNewEmail.onclick = this.saveEmailNow.bind(this);
		this.cancelNewEmail.onclick = this.cancelEmailNow.bind(this);		
		this.messageText.innerHTML = "You have changed your email ID for this account to: "+
		"<b>"+ this.userObj.email +"</b>"+"<BR/>"+"Please carefully check to make sure this email ID is correct."+
		"<BR/>"+ "The information stored now in this account will only be accessible with this new email ID.";

	},

	saveEmailNow: function(){
		if (this.emailChngDialog != null) {
			this.emailChngDialog.hide();
			this.doSave();
		}		
	},
	
	cancelEmailNow: function(){
		if (this.emailChngDialog != null) {
			this.emailChngDialog.hide();			
			this.email.setValue(this.originalEmail);
			this.userObj.email = this.originalEmail;
			this.isCancelledEmail = true;
			this.doSave();

		}
	}
}

