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

var PasswordAssistant = Class.create({
	initialize: function(){
		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.acctPasswordAttr.charsAllow = FirstUseUtil.filterSpace.bind(this);
		this.acctPasswordModel = {
			'original' : ''
		};

		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.clearPasswordErrors();
		
		$('acctPassword').observe(Mojo.Event.propertyChange, this.passwordChanged.bind(this));
		$('verifyPassword').observe(Mojo.Event.propertyChange, this.verifyPasswordChanged.bind(this));
		
		this.buttonAttr = {
			disabledProperty: 'disabled'
		};
		
		this.buttonModel = {
			disabled: true,
			buttonLabel : $L('Next'),
			buttonClass: 'palm-button'
		};
		
		this.controller.setupWidget('submitPassword', this.buttonAttr, this.buttonModel);
		Mojo.listen(this.controller.get('submitPassword'), Mojo.Event.tap, this.savePassword.bindAsEventListener(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();
	},
	
	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 create the account
			if (this.buttonModel.disabled == false) {
				this.passwordField.blur();
				this.savePassword();
				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);
		}
	},
	
	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);
	   }
    },
	
		
	clearPasswordErrors: function() {
		$('acctPasswordError').hide();
		$('verifyPasswordError').hide();
		$('acctPasswordLengthError').hide();
		$('passwordMisMatchError').hide();
		$('acctPasswordSpaceError').hide();
	},
		
	savePassword: function(event){
		Account.password = this.acctPasswordModel.original;
		this.verifyPassword = this.verifyPasswordModel.verify;
		
		if(Account.password == "" || Account.password == undefined) {
			this.clearPasswordErrors();
			$('acctPasswordError').show();
			this.controller.get("acctPassword").mojo.focus.delay(0);
			return;
		} else if(!((Account.password.length >= FirstUseUtil.MINPASSWORDLENGTH) && (Account.password.length <=FirstUseUtil.MAXPASSWORDLENGTH))) {
			this.clearPasswordErrors();
			$('acctPasswordLengthError').show();
			this.controller.get("acctPassword").mojo.focus.delay(0);
			return;
		} else if(this.verifyPassword == "" || this.verifyPassword == undefined) {
			this.clearPasswordErrors();
			$('verifyPasswordError').show();
			this.controller.get("acctPassword").mojo.focus.delay(0);
			return;
		} else if (Account.password.indexOf(' ') > -1) {
			this.clearPasswordErrors();
			$('acctPasswordSpaceError').show();
			this.controller.get("acctPassword").mojo.focus.delay(0);
			return;
		} else if (Account.password != this.verifyPassword) {
			this.clearPasswordErrors();
			$('passwordMisMatchError').show();
			this.controller.get("acctPassword").mojo.focus.delay(0);
			return;
		}
		this.clearPasswordErrors();
		this.getEmail();
	},
	
	getEmail: function(){		
		Mojo.Controller.stageController.pushScene('email');
	}
	
});	
