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

var ForgotpasswordAssistant = Class.create({
	initialize: function(){
		Mojo.Log.info("init ForgotpasswordAssistant");
		this.question = Account.questionText;
		this.questionId = Account.questionId;
		this.loginAttempt=0;
		FirstUseUtil.addDashboardSmall($('target'));
		saveCurrentState("forgotpassword");
	},
	
	setup: function() {
		
		this.responseAttributes = {
			modelProperty: 'response',
			multiline: false,
			maxLength: 50, 
			textReplacement: false,
			focusMode: Mojo.Widget.focusSelectMode,
			changeOnKeyPress: true,
			requiresEnterKey: true
		};
		this.responseModel = {
			'response' : Account.response,
			disabled: false
		};

		this.controller.setupWidget('response', this.responseAttributes, this.responseModel);
		this.controller.listen('response', Mojo.Event.propertyChange, this.responseChanged.bind(this));	
		
		this.responseFocusHandler = this.responseHandler.bind(this);
		Mojo.Event.listenForFocusChanges($('response'), this.responseFocusHandler);
				
		$('showQuestion').textContent = this.question;
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));
		
		
		this.buttonAttr = {
			disabledProperty: 'disabled'
		};
		
		this.buttonModel = {
			disabled: true,
			buttonLabel : $L('Next'),
			buttonClass: 'palm-button'
		};
		
		this.controller.setupWidget('login', this.buttonAttr, this.buttonModel);
		Mojo.listen(this.controller.get('login'), Mojo.Event.tap, this.login.bindAsEventListener(this));
						
		this.keyPressEventHandler =  FirstUseUtil.handleKeyPressEvent.bind(this);
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, this.keyPressEventHandler);
		
		this.isResponseInFocus = false;
		this.controller.get("response").focus();
	},
	
	responseHandler: function (event) {
		this.isResponseInFocus = !this.isResponseInFocus;
		this.toggleListener(this.isResponseInFocus);
	},
	
	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 ----------");
			$('responseError').hide();
			$('emptyResponseError').hide();
		}	
	},
	
	responseChanged: function(event) {
		$('responseError').hide();
		$('emptyResponseError').hide();
		this.responseModel.response = event.value;
		this.toggleDisabled();
		if (event && Mojo.Char.isEnterKey(event.originalEvent.keyCode)) {
			if (this.buttonModel.disabled == false) {
				this.controller.get("response").blur();
				this.login();
				Event.stop(event);
			} else {
				this.controller.get("response").mojo.focus.delay(0);
			}
		}
	},
	
	toggleDisabled: function() {
		if(this.responseModel.response.length > 0) {
			this.buttonModel.disabled = false;
			this.controller.modelChanged(this.buttonModel);
		} else {
			this.buttonModel.disabled = true;
			this.controller.modelChanged(this.buttonModel);
		}
	},
		
	login: function() {
		
		this.response = this.responseModel.response;
		$('responseError').hide();
		$('emptyResponseError').hide();
		if(this.response == "" || this.response == undefined) {
			$('emptyResponseError').show();
			this.controller.get("response").mojo.focus.delay(0);
			return;
		}
		if(RoamUtil.enableRoaming() === true) {
			RoamGuardDialog.turnOffRoamguard({
				"sceneController": this.controller,
				"method": this.authenticateFromSecurityQuestion.bind(this)
			});
			return;
		}
		this.buttonModel.disabled = true;
		this.controller.modelChanged(this.buttonModel);
		
		this.authenticateFromSecurityQuestion();
	},
	
	authenticateFromSecurityQuestion: function () {
		AppAssistant.accountService.authenticateAccountFromSecurityQuestion(Account.email, 
					this.questionId, this.response, this.checkLoginStatus.bind(this), this.loginError.bind(this), this.controller);
	},
	
	checkLoginStatus: function(response) {
		Mojo.Log.info("Login response = %o", $H(response));
		if (response.returnValue == true) {		
			Mojo.Controller.stageController.popScene();
			Mojo.Controller.stageController.pushScene('changepassword', response.idToken);
		} else if(response.returnValue == false) {
			this.loginError("");
		}					
	},
	
	loginError: function(response) {
		Mojo.Log.error("login error = %o", $H(response));
		if(response.errorCode && response.errorCode === "CONNECTION_ERROR") {
			Account.action = "login-connection-error";
			Mojo.Controller.stageController.pushScene('status');
			return;
		}
		else if(response.errorCode && response.errorCode == "-1") {
			Account.action == "login-error";
			Mojo.Controller.stageController.pushScene('status');
			return;
		}
		if (++this.loginAttempt < 3) {
			$('responseError').show();
			this.controller.get("response").mojo.focus.delay(0);

		} else {
			AppAssistant.accountService.requestPasswordResetEmail(Account.email, 
						this.resetEmailSuccess.bind(this), this.resetEmailFailure.bind(this), this.controller);	
		}	
	},
	
	resetEmailSuccess: function(response) {
		Mojo.Controller.stageController.pushScene('resetemail');
	},
	
	resetEmailFailure: function(response) {
		Mojo.Log.error("reset email failure = %o", $H(response));
	}
	
});	
