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

var EmailAssistant = Class.create({
	initialize: function(){
		FirstUseUtil.addDashboardSmall($('target'));
	},
	
	setup: function(){
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));
		
		this.emailAttributes = {
			modelProperty: 'emailAddress',
			multiline: false,
			maxLength: 100, 
			textReplacement: false,
			focusMode: Mojo.Widget.focusSelectMode,
			changeOnKeyPress: true,
		};
		this.emailModel = {
			'emailAddress' : Account.email,
			disabled: false
		};
		
		this.controller.setupWidget('emailAddress', this.emailAttributes, this.emailModel);
		
		$('emailAddress').observe(Mojo.Event.propertyChange, this.emailChanged.bind(this));
		this.emailFocusHandler = this.emailHandler.bind(this);
		Mojo.Event.listenForFocusChanges($('emailAddress'), this.emailFocusHandler);
		
		this.confirmEmailAttributes = {
			modelProperty: 'confirmEmailAddress',
			multiline: false,
			maxLength: 100, 
			textReplacement: false,
			focusMode: Mojo.Widget.focusSelectMode,
			changeOnKeyPress: true,
			requiresEnterKey: true
		};
		this.confirmEmailModel = {
			'confirmEmailAddress' : Account.email,
			disabled: false
		};
		
		this.controller.setupWidget('confirmEmailAddress', this.confirmEmailAttributes, this.confirmEmailModel);
		
		$('confirmEmailAddress').observe(Mojo.Event.propertyChange, this.confirmEmailChanged.bind(this));
		this.confirmEmailFocusHandler = this.confirmEmailHandler.bind(this);
		Mojo.Event.listenForFocusChanges($('confirmEmailAddress'), this.confirmEmailFocusHandler);
		
		this.buttonAttr = {
			disabledProperty: 'disabled'
		};
		
		this.buttonModel = {
			disabled: true,
			buttonLabel : $L('Next'),
			buttonClass: 'palm-button'
		};
		
		this.controller.setupWidget('checkEmail', this.buttonAttr, this.buttonModel);
		this.controller.listen(this.controller.get('checkEmail'), Mojo.Event.tap, this.checkEmail.bindAsEventListener(this));
		
		this.toggleDisabled();
		
		this.keyPressEventHandler =  FirstUseUtil.handleKeyPressEvent.bind(this);
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, this.keyPressEventHandler);
		
		this.usernameField = this.controller.get("emailAddress");
		this.confirmUsernameField = this.controller.get("confirmEmailAddress");
		this.isEmailInFocus = false;
		this.isConfirmEmailInFocus = false;
		this.controller.get("emailAddress").focus();
	},
	
	emailHandler: function (event) {
		this.isEmailInFocus = !this.isEmailInFocus;
		this.toggleListener(this.isEmailInFocus);
	},
	
	confirmEmailHandler: function (event) {
		this.isConfirmEmailInFocus = !this.isConfirmEmailInFocus;
		if(this.isConfirmEmailInFocus) {
			var height = $("firstuse-main").getHeight();
			this.controller.sceneScroller.mojo.scrollTo(0, -height, true);
		}
		this.toggleListener(this.isConfirmEmailInFocus);
	},
	
	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);
		}
	},
		
	hideAllErrors: function() {
		$('emailError').hide();
		$('invalidEmailError').hide();
		$('reenterEmailError').hide();
		$('emailMismatchError').hide();
	},
	
	emailChanged: function(event) {
		this.hideAllErrors();
	 	this.emailModel.emailAddress = event.value;
		this.toggleDisabled();
	},
	
	confirmEmailChanged: function(event) {
		this.hideAllErrors();
	 	this.confirmEmailModel.confirmEmailAddress = event.value;
		this.toggleDisabled();
		if (event && Mojo.Char.isEnterKey(event.originalEvent.keyCode)) {
			if (this.buttonModel.disabled == false) {
				this.confirmUsernameField.blur();
				this.checkEmail();
				Event.stop(event);
			} else {
				this.confirmUsernameField.mojo.focus();
			}
		}
	},
	
	toggleDisabled: function() {
		if((this.emailModel.emailAddress.length > 0) && (this.confirmEmailModel.confirmEmailAddress.length > 0)){
			this.buttonModel.disabled = false;
			this.controller.modelChanged(this.buttonModel);
		} else {
			this.buttonModel.disabled = true;
			this.controller.modelChanged(this.buttonModel);
		}
	},
	
	greyButton: function(){
		this.buttonModel.disabled = true;
		this.controller.modelChanged(this.buttonModel);
	},
			
	checkEmail: function(event){
		Mojo.Log.info("check email");
		
		Account.email = this.emailModel.emailAddress.strip();
		var confirmEmail = this.confirmEmailModel.confirmEmailAddress.strip();
		
		if(Account.email == "" || Account.email == undefined) {
			$('emailError').show();
			this.greyButton();
			this.controller.get('emailAddress').mojo.focus.delay(0);
			return;
		} else if (!(FirstUseUtil.validateEmail(Account.email))) {
			$('invalidEmailError').show();
			this.greyButton();
			this.controller.get('emailAddress').mojo.focus.delay(0);
			return;
		} else if ((confirmEmail == "") || (confirmEmail == undefined)) {
			$('reenterEmailError').show();
			this.greyButton();
			this.controller.get('emailAddress').mojo.focus.delay(0);
			return;
		} else if (!(confirmEmail.toLowerCase() === Account.email.toLowerCase())) {
			$('emailMismatchError').show();
			this.greyButton();
			this.controller.get('emailAddress').mojo.focus.delay(0);
			return;
		}
		if(RoamUtil.enableRoaming() === true) {
			RoamGuardDialog.turnOffRoamguard({
				"sceneController": this.controller,
				"method": this.pushWaitScene.bind(this)
			});
			return;
		}
		this.pushWaitScene();
	},
	
	activate: function () {
		this.toggleDisabled();
	},
	
	pushWaitScene: function () {
		Mojo.Controller.stageController.pushScene('wait', 'validateEmail');
	}
	
});	
