/* Copyright 2009 Palm, Inc.  All rights reserved. */

var ChangepasswordAssistant = Class.create({
	initialize: function(idToken){
		this.idToken = idToken;
		FirstUseUtil.addDashboardSmall($('target'));
	},
	
	setup: function() {
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));	
				
		this.acctPasswordAttr = {
			modelProperty: 'original',
			focus: true,
			maxLength: FirstUseUtil.MAXPASSWORDLENGTH,
			focusMode: Mojo.Widget.focusSelectMode,
			enterSubmits: true,
			changeOnKeyPress: true
		};
		this.acctPasswordModel = {
			'original' : ''
		};
		this.acctPasswordAttr.charsAllow = FirstUseUtil.filterSpace.bind(this);
		this.controller.setupWidget('acctPassword', this.acctPasswordAttr, this.acctPasswordModel);
		this.passwordFocusHandler = this.passwordHandler.bind(this);
		Mojo.Event.listenForFocusChanges($('acctPassword'), this.passwordFocusHandler);
				
		this.verifyPasswordAttr = {
			modelProperty: 'verify',
			focus: false,
			maxLength: FirstUseUtil.MAXPASSWORDLENGTH,
			focusMode: Mojo.Widget.focusSelectMode,
			className: ' ',
			changeOnKeyPress: true,
			requiresEnterKey: true
		};
		this.verifyPasswordModel = {
			'verify' : ''
		};
		this.verifyPasswordAttr.charsAllow = FirstUseUtil.filterSpace.bind(this);
		this.verifyPassword = "";
		this.controller.setupWidget('verifyPassword', this.verifyPasswordAttr, this.verifyPasswordModel);
		this.verifyPasswordFocusHandler = this.verifyPasswordHandler.bind(this);
		Mojo.Event.listenForFocusChanges($('verifyPassword'), this.verifyPasswordFocusHandler);
		
		this.buttonAttr = {
			disabledProperty: 'disabled'
		};
		
		this.buttonModel = {
			disabled: true,
			buttonLabel : $L('Next'),
			buttonClass: 'palm-button'
		};
		
		this.controller.setupWidget('next', this.buttonAttr, this.buttonModel);
		Mojo.listen(this.controller.get('next'), Mojo.Event.tap, this.changePassword.bindAsEventListener(this));
		
		$('acctPassword').observe(Mojo.Event.propertyChange, this.passwordChanged.bind(this));
		$('verifyPassword').observe(Mojo.Event.propertyChange, this.verifyPasswordChanged.bind(this));
		
		this.passwordField = this.controller.get("acctPassword");
		this.verifyPasswordField = this.controller.get("verifyPassword");
		
		this.keyPressEventHandler =  FirstUseUtil.handleKeyPressEvent.bind(this);
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, this.keyPressEventHandler);
				
		this.isPasswordInFocus = false;
		this.isVerifyPasswordInFocus = false;
		$('acctPassword').focus();		
	},
		
	toggleDisabled: function() {
		
	   this.hideAllDivs();
       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 the password field has focus and Enter is pressed then simulate tapping on "next"
		if (event && Mojo.Char.isEnterKey(event.originalEvent.keyCode)) {
			// If the submit button is enabled then change password
			if (this.buttonModel.disabled == false) {
				this.verifyPasswordField.blur();
				this.changePassword();
				Event.stop(event);
			} else {
				this.verifyPasswordField.mojo.focus();
			}
		}
	},
		
	passwordHandler: function (event) {
		this.isPasswordInFocus = !this.isPasswordInFocus;
		this.toggleListener(this.isPasswordInFocus);
	},
	
	verifyPasswordHandler: function (event) {
		this.isVerifyPasswordInFocus = !this.isVerifyPasswordInFocus;
		this.toggleListener(this.isVerifyPasswordInFocus);
	},
	
	toggleListener: function (flag) {
		if(flag == true) {
			this.controller.stopListening(this.controller.sceneElement, Mojo.Event.keydown, this.keyPressEventHandler);
		} else {
			this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, this.keyPressEventHandler);
		}
	},
	
	handleCommand: function (event) {
		if (event.type == Mojo.Event.back) {
			Mojo.Log.info("------- Blocking back ----------");
			event.preventDefault();
			event.stopPropagation();
		}
	},
	
	hideAllDivs: function() {
		$('passwordError').hide();
		$('verifyPasswordError').hide();
		$('passwordMismatchError').hide();
		$('acctPasswordSpaceError').hide();
	},
	
	changePassword: function() {
		Account.password = this.acctPasswordModel.original;
		this.verifyPassword = this.verifyPasswordModel.verify;
		
		if(Account.password == "" || Account.password == undefined) {
			$('passwordError').show();	
			return;
		} else if(verifyPassword == "" || verifyPassword == undefined) {
			$('verifyPasswordError').show();
			return;
		} else if(!((Account.password.length >= FirstUseUtil.MINPASSWORDLENGTH) && (Account.password.length <=FirstUseUtil.MAXPASSWORDLENGTH))) {
			$('passwordError').show();	
			return;
		} else if (Account.password.indexOf(' ') > -1) {
			$('acctPasswordSpaceError').show();
			return;
		} else if (Account.password != this.verifyPassword) {
			$('passwordMismatchError').show();	
			return;
		}
		Account.idToken = this.idToken;
		if(RoamUtil.enableRoaming() === true) {
			RoamGuardDialog.turnOffRoamguard({
				"sceneController": this.controller,
				"method": this.pushNextScene.bind(this)
			});
			return;
		}
		this.pushNextScene();
	},
	
	activate: function () {
		this.toggleDisabled();
	},
	
	pushNextScene: function () {
		Mojo.Controller.stageController.popScene();
		Mojo.Controller.stageController.pushScene('wait', 'resetPassword');	
	}
	
		
});	
