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

var UnregisteredemailAssistant = Class.create({
	initialize: function(){
		FirstUseUtil.addDashboardSmall($('target'));
	},
	
	setup: function(){
		this.emailAttributes = {
			modelProperty: 'emailAddress',
			multiline: false,
			maxLength: 100, 
			textReplacement: false,
			focusMode: Mojo.Widget.focusInsertMode,
			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);
		
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));
						
		this.passwordAttr = {
			modelProperty: 'original',
			focus: false,
			maxLength: 20,
			className: ' ',
			changeOnKeyPress: true,
			requiresEnterKey: true
		};
		
		this.passwordModel = {
			'original' : Account.password
		};
		this.passwordAttr.charsAllow = FirstUseUtil.filterSpace.bind(this);
		this.controller.setupWidget('password', this.passwordAttr, this.passwordModel);
		this.controller.listen('password', Mojo.Event.propertyChange, this.passwordChanged.bind(this));
		
		this.buttonAttr = {
			disabledProperty: 'disabled'
		};
		
		this.buttonModel = {
			disabled: true,
			buttonLabel : $L('Sign in'),
			buttonClass: 'palm-button'
		};
		
		this.controller.setupWidget('tryAgain', this.buttonAttr, this.buttonModel);
		Mojo.listen(this.controller.get('tryAgain'), Mojo.Event.tap, this.login.bindAsEventListener(this));
		
		$('createAccount').observe(Mojo.Event.tap, this.createAccount.bindAsEventListener(this));
		saveCurrentState("unregisteredemail");
		
		// When user gets to login page from an error screen, as we still populate email
		// address and password, we want to have the button enabled.		
		this.setButtonMode();
		
		this.passwordFocusHandler = this.passwordHandler.bind(this);
		Mojo.Event.listenForFocusChanges($('password'), this.passwordFocusHandler);
		
		//this.passwordField = this.controller.get("password");
		//this.usernameField = this.controller.get("emailAddress");
		
		this.keyPressEventHandler =  FirstUseUtil.handleKeyPressEvent.bind(this);
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, this.keyPressEventHandler);
		
		this.isEmailInFocus = false;
		this.isPasswordInFocus = false;
		$("emailAddress").focus();
		
	},
	
	setButtonMode: function () {
		if((this.emailModel.emailAddress.length > 0) && 
			(this.passwordModel.original.length > 0)) {
			this.buttonModel.disabled = false;
			this.controller.modelChanged(this.buttonModel);		
		} else {
			this.buttonModel.disabled = true;
			this.controller.modelChanged(this.buttonModel);
		}
	},
	
	emailHandler: function (event) {
		this.isEmailInFocus = !this.isEmailInFocus;
		this.toggleListener(this.isEmailInFocus);
	},
	
	passwordHandler: function (event) {
		this.isPasswordInFocus = !this.isPasswordInFocus;
		if(this.isPasswordInFocus) {
			var height = $("firstuse-main").getHeight();
			this.controller.sceneScroller.mojo.scrollTo(0, -height, true);
		}
		this.toggleListener(this.isPasswordInFocus);
	},
	
	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 () {
		Mojo.Log.info("----------------------- toggleDisabled ------------------");
		this.setButtonMode();
	},
	
	handleCommand: function(event) {
		if (event.type == Mojo.Event.back) {
			Mojo.Log.info("------- Blocking back ----------");
			event.preventDefault();
			event.stopPropagation();
		}	
		Mojo.Controller.stageController.popScenesTo('login');
	},
	
	emailChanged: function(event) {
	 	this.emailModel.emailAddress = event.value;
		this.toggleDisabled();
	},
	
	passwordChanged: function(event) {
	 	this.passwordModel.original = event.value;
		this.toggleDisabled();
		if (event && Mojo.Char.isEnterKey(event.originalEvent.keyCode)) {
			if (this.buttonModel.disabled == false) {
				$("password").blur();
				this.login();
				Event.stop(event);
			}
		}
	},
		
	createAccount: function(event){
		this.clearAllErrorMessages();
		Account.email = this.emailModel.emailAddress;
		//Clear password field
		Account.password = '';
		Mojo.Controller.stageController.pushScene('name');
	},
	
	login: function(event){
		this.clearAllErrorMessages();
		Account.email = this.emailModel.emailAddress.strip();
		Account.password = this.passwordModel.original;
				
		if(Account.email == "" || Account.email == undefined) {
			$('emailSyntaxError').show();
			return;
		} else if (!(FirstUseUtil.validateEmail(Account.email))) {
			$('emailSyntaxError').show();
			return;
		}
		
		Account.password = this.passwordModel.original;
		if((Account.password == null) || (!((Account.password.length >= 6) && (Account.password.length <=20)))) {
			$('passwordLengthError').show();
			return;
		} 
		else if (Account.password.indexOf(' ') > -1) {
			$('acctPasswordSpaceError').show();
			return;
		}		
		Mojo.Controller.stageController.pushScene('wait', 'login');
	},
	
	clearAllErrorMessages: function (){
		$('emailNotFound').hide();
		$('emailSyntaxError').hide();
		$('passwordError').hide();
		$('passwordLengthError').hide();
		$('acctPasswordSpaceError').hide();
	}
		
}); 



