var ChangePasswordAssistant = Class.create({
	
	initialize : function(sceneAssistant, myProfile) {
		this.myProfile = myProfile;
		this.sceneAssistant = sceneAssistant;
		this.controller = sceneAssistant.controller;
		
	}, 
	
	handleCommand: function (event) {
		if (event.type == Mojo.Event.back) { 
			Mojo.Log.info("back pressed");
			event.preventDefault();
			this.widget.mojo.close();       
			event.stop(); 
		}
	},
	
	activate: function(){
		this.controller.get("newPassword").mojo.focus();
	},
	
	setup : function (widget){
		this.widget = widget; 
		this.clearErrorMessages(); 
		
		this.acctPasswordAttr = {
			hintText: ProfileUtil.label_usrpwd_password_hintText,
			modelProperty: 'original',
			focus: true,
			focusMode: Mojo.Widget.focusSelectMode,
			maxLength: ProfileUtil.MAXPASSWORDLENGTH,
			changeOnKeyPress: true
		};                                             
		
		this.acctPasswordModel = {                     
			'original' : ''                            
		};
		
		this.acctPasswordAttr.charsAllow = ProfileUtil.filterSpace.bind(this);
		this.controller.setupWidget('newPassword', this.acctPasswordAttr, this.acctPasswordModel);
		
		this.verifyPasswordAttr = {
			hintText: ProfileUtil.label_usrpwd_confirm_password_hintText,
			modelProperty: 'verify',
			maxLength: ProfileUtil.MAXPASSWORDLENGTH,
			focusMode: Mojo.Widget.focusSelectMode,
			className: ' ',
			changeOnKeyPress: true, 
			requiresEnterKey: true,
			focus: false
		};
		
		this.verifyPasswordModel = { //for verifying password satisfies criteria
			'verify' : ''            //for storing result of verification
		};
		
		this.verifyPasswordAttr.charsAllow = ProfileUtil.filterSpace.bind(this);
		this.controller.setupWidget('confirmPassword', this.verifyPasswordAttr, this.verifyPasswordModel); //set up the widget with new model (ie state and/or attributes)
		
		this.controller.get('newPassword').observe(Mojo.Event.propertyChange, this.passwordChanged.bind(this)); //assumption for future: passwordChanged will ungray the submitChangePassword button?
		this.controller.get('confirmPassword').observe(Mojo.Event.propertyChange, this.verifyPasswordChanged.bind(this));
		
		this.buttonAttr = {
			disabledProperty: 'disabled'
		};
		
		this.buttonModel = {
			disabled: true,
			buttonLabel : $L('Done'),
			buttonClass: 'palm-button'
		}; 
		
		this.controller.setupWidget('submitChangePassword', this.buttonAttr, this.buttonModel); //the button that submits the password is initially set up to be grayed out, according to model
		Mojo.listen(this.controller.get('submitChangePassword'), Mojo.Event.tap, this.changePassword.bindAsEventListener(this)); //listen to tap on submitChangePassword button
	},
	
	toggleDisabled: function(){
			if ((this.acctPasswordModel.original.length > 0) &&
				(this.verifyPasswordModel.verify.length > 0)) {
					this.buttonModel.disabled = false;
					this.controller.modelChanged(this.buttonModel);
			}else{
					this.buttonModel.disabled = true;
					this.controller.modelChanged(this.buttonModel);
			}
	},
	
	passwordChanged: function (event) {
		this.toggleDisabled();
	},
	
	verifyPasswordChanged: function (event) {
		this.toggleDisabled();
		if (event && Mojo.Char.isEnterKey(event.originalEvent.keyCode))
		{
			if (this.buttonModel.disabled == false)
			{
				this.changePassword();
				Event.stop(event);
			}
		}
	},

	clearFields : function()
	{
		this.acctPasswordModel.original = '';
		this.verifyPasswordModel.verify = '';
		this.controller.modelChanged(this.acctPasswordModel);
		this.controller.modelChanged(this.verifyPasswordModel);
	},
	
	clearErrorMessages: function() {
		this.controller.get("mismatchMessage").hide();
		this.controller.get("noPasswordMessage").hide();
		this.controller.get("noConfirmMessage").hide();
		this.controller.get("systemErrorMessage").hide();
		this.controller.get("passwordLengthError").hide();
	},
	
	changePassword: function() {
		this.newPassword = this.acctPasswordModel.original;
		this.confirmPassword = this.verifyPasswordModel.verify;
		this.clearErrorMessages();
		if(this.newPassword == "" || this.newPassword == undefined) {
			this.controller.get("noPasswordMessage").show();
			return;
		}
		else if ((this.newPassword.length < ProfileUtil.MINPASSWORDLENGTH) 
			|| (this.confirmPassword.length < ProfileUtil.MINPASSWORDLENGTH)
			|| (this.newPassword.length > ProfileUtil.MAXPASSWORDLENGTH)
			|| (this.confirmPassword.length > ProfileUtil.MAXPASSWORDLENGTH)) {
			this.controller.get("passwordLengthError").show();
			
				this.greyButton();
				this.controller.get("newPassword").mojo.focus.delay(0);
			
			return;
		}
		else if (this.confirmPassword == "" || this.confirmPassword == undefined) { 
			this.controller.get("noConfirmMessage").show();
			return;
		} 
		else if (this.newPassword != this.confirmPassword) {
			this.controller.get("mismatchMessage").show();
			this.greyButton();
			this.controller.get("newPassword").mojo.focus.delay(0);
			return;
		} 
			
		if (isConnectionAvailable === false)
		{
			Mojo.Log.info("show connection error -------");
			ProfileUtil.showConnectionError(this.controller);
			return;
		}
		
		this.greyButton();
		
		AppAssistant.accountService.changeAccountPassword(
			this.myProfile.password,
			this.newPassword,
			this.changePasswordSuccess.bind(this),
			this.changePasswordError.bind(this),
			this.controller);
	},
	
	changePasswordSuccess: function (response) {
		Mojo.Log.info("ChangePassword successful :%o", $H(response));
		if (response.returnValue == true)
		{
			this.myProfile.password = this.newPassword;
			this.sceneAssistant.acctPassword = this.newPassword;
			this.widget.mojo.close();
			var bannerParams = {
        		icon: "images/tap-info.png",
        		messageText: ProfileUtil.label_usrpwd_confirm_message
   			};
			this.controller.showBanner(bannerParams, {}, 'pubsubbanner');
		}
	},
	
	greyButton : function()
	{
		this.buttonModel.disabled = true;
		this.controller.modelChanged(this.buttonModel);
	},
	
	changePasswordError: function(response){
		Mojo.Log.error("changePassword error = %o", $H(response));
		if ((isConnectionAvailable === false) || (reponse.errorText && response.errorText === "No response")) {
			Mojo.Log.info("show conncetion error ----------");
			this.widget.mojo.close();
			ProfileUtil.showConnectionError(this.controller);
			return;
		}
		
		this.controller.get("systemErrorMessage").show();		
	}
})




