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

var WaitAssistant = Class.create({
	
	spinner: 'activity-spinner',
	
	spinning: false,
	
	spinnerAttrs: {
		spinnerSize: Mojo.Widget.spinnerLarge
	},
	
	initialize: function(action) {
		this.action = action;	
		this.packageURLArray = [];
		Mojo.Log.info("------- Initial value of this.packageURLArray ----------", this.packageURLArray);
		this.locale = Account.language+"_"+Account.country;
		Mojo.Log.error("------- wait assistant -------------------", Account.action);
		if(Account.action === "update") {
			this.action = "update";
		} else if (Account.action === "restore") {
			this.action = "restore";
		}
		FirstUseUtil.addDashboardSmall($('target'));
	},
	
	handleCommand: function(event) {
		if (event.type == Mojo.Event.back) {
			Mojo.Log.info("------- Blocking back ----------");
			event.preventDefault();
			event.stopPropagation();
		}	
	},	
	
	setup: function(){
		$('connection-error').hide();
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));
				
		this.startEmailCheck = new Date().getTime( );
		Mojo.Log.info("------------ START - Check if email available ---------------------", new Date());
		
		this.doAction();
		Mojo.Log.info("start spinner now ");		
		this.controller.setupWidget(this.spinner, this.spinnerAttrs, {});	
		
		// Listen to ## key presses
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
	},
	
	doAction: function () {
		if (this.setApn && this.setApn === true) {
			this.setApn = false;
		}	
		if(this.action === "update" || this.action === "restore") {
			this.isDataAvailable();
			return;
		}
		if (this.action !== "createAccount") {
			this.emailCheckRequest = AppAssistant.accountService.isEmailAvailable(Account.email, 
										this.checkEmailAvailability.bind(this), this.checkEmailError.bind(this), this.controller);
		}			
		if(this.action == "createAccount") {
			this.controller.get('createAccount').show();
			this.startCreateAccount = new Date().getTime( );
			Mojo.Log.info("------------ START - Create account ---------------------%o", new Date());
			this.createAccountRequest = AppAssistant.accountService.createAccount(Account, this.showAccountStatus.bind(this), this.showAccountError.bind(this), this.controller);
		} else if(this.action == "login") {
			this.controller.get('login').show();
		} else {
			this.controller.get('validateEmail').show();
		}
	},

	activate: function() {
		this.spinning = true;
		$(this.spinner).mojo.start();
		
		Mojo.Log.info("---------------- this.setApn --------------- ", this.setApn);		
		if(this.setApn && this.setApn === true) {
			// Continue when connection comes back up
			this.statusRequest = AppAssistant.accountService.getConnectionStatusForScene(this.readConnectionStatus.bind(this), 
							this.errorConnectionStatus.bind(this), this.controller);
			return;
		}
		//Time out spinner after 2 minutes
		this.timeOutMethod = setTimeout(this.timeOutSpinner.bind(this), 2*60*1000);
	},
	
	readConnectionStatus: function (response) {
		if(response && response.isInternetConnectionAvailable == true) {
			if(this.statusRequest) {
				this.statusRequest.cancel();
			}
			this.doAction();
		}
	},
	
	errorConnectionStatus: function (response) {
		Mojo.Log.error("-------------------errorConnectionStatus -----------------------%j ", response);
		if(this.statusRequest) {
			this.statusRequest.cancel();
		}
		// Show error screen right here
		this.doAction();
	},
	
	deactivate: function () {
		this.stopSpinner();	 
	},
	
	showAccountCreationError: function () {
		if (isConnected == false) {
			Account.action = "account-creation-connection-error";
		}
		else {
			Account.action = "account-creation-error";
		}
		Mojo.Controller.stageController.pushScene('status');
	},
	
	showSignInError: function () {
		if (isConnected == false) {
			Account.action = "login-connection-error";
		}
		else {
			Account.action = "login-error";
		}
		Mojo.Controller.stageController.pushScene('status');
	},
	
	showInvaidTokenError: function () {
		Account.action = "invalid-token-error";
		Mojo.Controller.stageController.pushScene('status');
	},
		
	timeOutSpinner: function() {
		Mojo.Log.info("Timeout spinner called");
		Mojo.Log.info("---------------------- Timeout Spinner --------------------------------", new Date());
		
		if (this.action == "createAccount") {
			this.showAccountCreationError();
		} else {
			if (this.action == "invalid-token") {
				this.showInvaidTokenError();
			}
			else {
				this.showSignInError();
			}	
		}
	},
	
	checkEmailError: function(response) {
		Mojo.Log.error("------------ END - Check if email available ---------------------%o", $H(response));
		Mojo.Log.info("------------ Time taken - Check if email available ---------------------%o", (new Date().getTime( )-this.startEmailCheck));
		Mojo.Log.info("Email check error response ========= %o", $H(response));
		Mojo.Log.info("---------------- this.setApn --------------- ", this.setApn);		
		if(this.setApn && this.setApn === true) {
			// Don't handle error as still user could be setting values
			return;
		}
		// Check if failed because of bad APN for GSM
		if (GsmUtil.isApnNeeded() === true) {
			this.setApn = true;
			var params = {
				"sceneController": this.controller,
				"method": this.doAction.bind(this)
			};
			GsmUtil.showApnIntro(params);
			return;
		}	
		if (response) {
			if (response.errorCode == "CONNECTION_ERROR") {
				$('connection-error').show();
				return;
			}
		}
	
		if (this.action == "createAccount") {
			this.showAccountCreationError();
		}
		else {
			if (this.action == "invalid-token") {
				this.showInvaidTokenError();
			} else {
				this.showSignInError();
			}
		}	
	},
		
	getAllSecurityQuestions: function () {
		if (isConnected == false) {
			Account.action = "account-creation-connection-error";
			Mojo.Controller.stageController.swapScene('status');
			return;
		}
		Mojo.Controller.stageController.swapScene('security-question');
	},
	
	checkEmailAvailability: function (response) {
		Mojo.Log.info("------------ END - Check if email available ---------------------%o", new Date());
		Mojo.Log.info("------------ Time taken - Check if email available ---------------------%o", (new Date().getTime( )-this.startEmailCheck));
		if(this.action == "resetPassword") {
			this.changePasswordRequest = AppAssistant.accountService.changePassword(Account.password, Account.idToken, true, 
				this.successChangePassword.bind(this), this.errorChangePassword.bind(this), this.controller);
			return;	
		}
		if (response.isEmailAvailable != undefined) {
			if (this.action === "validateEmail") {
				if (response.isEmailAvailable == true) {
					this.getAllSecurityQuestions();
					return;
				}
				else if (response.isEmailAvailable == false) {	
					Mojo.Controller.stageController.pushScene('registeredemail')
				}
			} else {
				Mojo.Log.info("------------ response.isEmailAvailable ---------------------%o", response.isEmailAvailable);
				if (response.isEmailAvailable == true) {		
					Mojo.Controller.stageController.pushScene('unregisteredemail');
				}
				else if (response.isEmailAvailable == false) {
					Mojo.Log.info("------------ Account.showDeviceInUse ---------------------%o", Account.showDeviceInUse);
					// Check if user has already signed in with the same account on a different device
					if (Account.showDeviceInUse == true) {
						if (Account.deviceId == "") {
							Mojo.Log.info("fetch Account device id =======");
							this.deviceIdRequest = AppAssistant.accountService.getDeviceId(this.readDeviceId.bind(this), 
														this.deviceIdRequestFailed.bind(this), this.controller);
						}
						else {
							//Added for simulator
							if (FirstUseUtil.isSimulator()) {
								this.loginUser();
								return;
							}	
							Mojo.Log.info("------------ getDeviceModel ---------------------");
							this.modelRequest = AppAssistant.accountService.getDeviceModel( 
													this.readDeviceModel.bind(this), this.errorDeviceModel.bind(this), 
													this.controller);
						}
					} else {
						this.loginUser();
					}	
				}
			}
		}
		else if (response.errorCode == "CONNECTION_ERROR") {
			Mojo.Log.info("Connection error");
			$('connection-error').show();
		}
		else if (response.errorCode == "SYSTEM_ERROR") {
			Mojo.Log.info("System error - Please try again");
			$('connection-error').show();
		}
	},
	
	readDeviceId: function (response) {
		if(response.deviceId) {
			Account.deviceId = response.deviceId;
			this.modelRequest = AppAssistant.accountService.getDeviceModel( 
											this.readDeviceModel.bind(this), this.errorDeviceModel.bind(this), 
											this.controller);
		}
	}, 
	
	deviceIdRequestFailed:function (response) {
		if (response.errorCode == -1) {
			// Try again
			this.deviceIdRequest = AppAssistant.accountService.getDeviceId(this.readDeviceId.bind(this), 
							this.deviceIdRequestFailed.bind(this), this.controller);
			return;				
		}	
		Account.action = 'login-error';		
		Mojo.Controller.stageController.pushScene('status');
	},
	
	readDeviceModel: function (response) {
		Mojo.Log.info("------------------------ Read device model ------------------------%o", $H(response));
		response = $H(response);
		if(response.values()) {
			var model = response.values().first();
			if(model) {
				this.deviceInUseRequest = AppAssistant.accountService.isDeviceInUse(Account.email, 
						Account.deviceId, model, this.readDeviceInUseResponse.bind(this), this.errorDeviceInUseResponse.bind(this), 
						this.controller);
				return;		
			}
		} // If model value can't be get, show error
		//Account.action = 'login-error';		
		//Mojo.Controller.stageController.pushScene('status');
		// Ideally we should error out here but EVT1 devices don't have the value
		//Account.action = 'login-error';		
		//Mojo.Controller.stageController.pushScene('status');
		this.loginUser();
		
	},
	
	errorDeviceModel: function (response) {
		Mojo.Log.error("------------------------ errorDeviceModel ------------------------%o", $H(response));
		if (FirstUseUtil.isSimulator()) {
			this.loginUser();
			return;
		}	
		// the service is down, try again
		if (response.errorCode == "-1") {
			this.modelRequest = AppAssistant.accountService.getDeviceModel(this.readDeviceModel.bind(this), 
							this.errorDeviceModel.bind(this), this.controller);
			return;				
		}			
		// Ideally we should error out here but EVT1 devices don't have the value
		//Account.action = 'login-error';		
		//Mojo.Controller.stageController.pushScene('status');
		this.loginUser();
	},
	
	readDeviceInUseResponse: function (response) {
		Mojo.Log.error("----------------- readDeviceInUseResponse --------------------------%o", $H(response));
		if (response.isDeviceInUse && response.isDeviceInUse == true) {
			Mojo.Controller.stageController.popScene();
			Mojo.Controller.stageController.pushScene('login', "DEVICE_IN_USE");
		}
		else {
			this.loginUser();
		}	
	},
	
	errorDeviceInUseResponse: function (response) {
		Mojo.Log.error("----------------- errorDeviceInUseResponse --------------------------%o", $H(response));
		this.showSignInError();
	},
	
	loginUser: function () {
		this.startLoginAccount = new Date().getTime( );
		Mojo.Log.info("------------ START - Authenticate account ---------------------%o", new Date());
		this.loginRequest = AppAssistant.accountService.authenticateAccount(Account, 
											this.checkLoginStatus.bind(this), this.loginError.bind(this), this.controller);
	},
	
	successChangePassword: function(response) {
		Mojo.Log.info("success in changing password = %o", $H(response));
		this.autheticateRequest = AppAssistant.accountService.authenticateAccount(Account, 
									this.checkLoginStatus.bind(this), this.loginError.bind(this), this.controller);
	},
	
	errorChangePassword: function(response) {
		Mojo.Log.info("error in changing password = "+response);
	},
	
	stopSpinner: function() {
		clearTimeout(this.timeOutMethod);
		if (this.spinning) {
			$(this.spinner).mojo.stop();
			this.spinning = false;
		}
	},
	
	showAccountStatus: function(response) {
		Mojo.Log.info("------------ END - Create account ---------------------%o", new Date());
		Mojo.Log.info("------------ Time taken - Create account ---------------------%o", (((new Date().getTime()) - this.startCreateAccount)));
		Mojo.Log.info("Account creation status = %o", $H(response));
		$('connection-error').hide();
		
		if(response.returnValue == true) {
			Account.action = 'create';
			Mojo.Controller.stageController.pushScene('status');
		}
		
	},
	
	showAccountError: function(response) {
		Mojo.Log.info("------------ END - Create account ---------------------%o", new Date());
		Mojo.Log.info("------------ Time taken - Create account ---------------------%o", (((new Date().getTime()) - this.startCreateAccount)));
		if(response) {
			Mojo.Log.error("Account creation error = %o", $H(response));
			if (response.errorCode == "CONNECTION_ERROR") {
				$('connection-error').show();
			} else {
				this.showAccountCreationError();
			}
		} else {
			this.showAccountCreationError();
		}
		
	},
	
	checkLoginStatus: function(response) {
		Mojo.Log.info("------------ END - Authenticate account ---------------------%o", new Date());
		Mojo.Log.info("------------ Time taken - Authenticate account ---------------------%o", (new Date().getTime( ) - this.startLoginAccount));
		Mojo.Log.info("Login response = %o", $H(response));
		if (response.returnValue == true) {	
			if (this.action == "resetPassword") {
				Mojo.Controller.stageController.popScene();
				Mojo.Controller.stageController.pushScene('changepasswordsuccess');
			}
			else {
				this.startDateCheck = new Date().getTime( );
				if (Account.dbErrorType != "TokenCorrupted") {
					this.isDataAvailable();
				} else {
					this.decideAction();
				}	
			}											
		} 						
	},
	
	isDataAvailable: function () {
		Mojo.Log.info("------------ START - Check if data is available to restore ---------------------");
		this.urlRequest = AppAssistant.appCatalogService.getServerUrl();
		AppAssistant.accountService.isDataAvailable(this.checkIfDataAvailable.bind(this), this.getDevicesFailure.bind(this), this.controller);
	},
	
	loginError: function(response) {
		Mojo.Log.error("login error = %o", $H(response));
		if (response.errorCode) {
			this.loginErrorCode = response.errorCode;
			if ((response.errorCode == "PAMS1100") || (response.errorCode == "PAMS1101")) {
				Account.loginAttemptCount++;
				this.getSecurityQuestion();
				return;
			}else if (response.errorCode == "PAMS1106") {	
				Mojo.Controller.stageController.pushScene('unregisteredemail');
				return;
			} else if (response.errorCode == "PAMS9999") {
				if (this.action == "invalid-token") {
					this.showInvaidTokenError();
				}
				else {
					this.showSignInError();
				}
				return;
			} else if (response.errorCode == "DEVICE_PROFILE_ERROR") {
				//TODO Show diff error message
				if (this.action == "invalid-token") {
					this.showInvaidTokenError();
					return;
				}
				else {
					// In this case, WAN could be down. We shouldn't interpret this as connection failure.
					// So don't use showSignInError() method
					Account.action = 'login-error';
				}
				Mojo.Controller.stageController.pushScene('status');
				return;
			} else  if (response.errorCode == "CONNECTION_ERROR") {
				$('connection-error').show();
			} else {
				if (this.action == "invalid-token") {
					this.showInvaidTokenError();
				}
				else {
					this.showSignInError();
				}
			}
		}
	},
	
	getSecurityQuestion: function () {
		Mojo.Log.info("------------------------ get security question ----------------------");
		if (Account.questionId == "-1") {
			this.securityRequest = AppAssistant.accountService.getAccountSecurityQuestion(Account.email, this.locale, this.checkIfQuestionAvailable.bind(this), this.getQuestionFailure.bind(this), this.controller);
			return;
		}
		
		this.handleError();
														
	},
	
	handleError: function() {
		Mojo.Log.info("------------------------ handleError ----------------------");
		if (this.loginErrorCode == "PAMS1101") {
			this.handleLockedAccount();
			return;
		} else if (this.loginErrorCode == "PAMS1100") {
			//TODO Remove this if we never get to this
			if (Account.loginAttemptCount == 3) {
					this.resetRequest = AppAssistant.accountService.requestPasswordResetEmail(Account.email, 
						this.resetEmailSuccess.bind(this), this.resetEmailFailure.bind(this), this.controller);	
					return;
			}
		}
		Mojo.Controller.stageController.popScene();
		if(Account.isInvalidToken == true) {
			Mojo.Controller.stageController.pushScene('invalid-token', this.loginErrorCode);
			return;
		}
		Mojo.Controller.stageController.pushScene('login', this.loginErrorCode);
		
	}, 
	
	handleLockedAccount: function() {
		Mojo.Log.info("-------------- Account.loginAttemptCount ------------------", Account.loginAttemptCount);
		
		// When PAMS1101 occurs the first time, the loginAttemptCount is 3. We need to either send an email or ask security question
		// based on availability of security question for the account.
		// If the loginAttemptCount is greater than 3, always send the recover-password scene
		if (Account.loginAttemptCount > 3) {
			Mojo.Controller.stageController.popScene();
			Mojo.Controller.stageController.pushScene('recover-password');
			return;
		}
		if (Account.questionId == "-1") {
			this.resetRequest = AppAssistant.accountService.requestPasswordResetEmail(Account.email, this.sendEmailSuccess.bind(this), this.resetEmailFailure.bind(this), this.controller);
		}
		else {
			Mojo.Controller.stageController.swapScene('forgotpassword', Account.questionId, Account.questionText);
		}	
	},
	
	sendEmailSuccess:function(response) {
		Mojo.Controller.stageController.popScene();
		Mojo.Controller.stageController.pushScene('send-email');
	},
	
	resetEmailSuccess: function(response) {
		Mojo.Controller.stageController.popScene();
		Mojo.Controller.stageController.pushScene('resetemail');
	},
	
	resetEmailFailure: function(response) {
		Mojo.Log.error("reset email failure = %o", $H(response));
	},
	
	checkIfQuestionAvailable: function (response) {
		Mojo.Log.info("Response: %o", $H(response));
		if(response.id) {
			Account.questionId = response.id;
			Account.questionText = response.question;
		} else {
			Account.questionId = "-1";
		}	
		
		Mojo.Log.error("this.loginErrorCode == %s", this.loginErrorCode);
		this.handleError();	
	},
	
	getQuestionFailure: function (response) {
		Mojo.Log.error("Response: %o", $H(response));
		
		Mojo.Controller.stageController.popScene();
		if(Account.isInvalidToken == true) {
			Mojo.Controller.stageController.pushScene('invalid-token', this.loginErrorCode);
			return;
		}
		Mojo.Controller.stageController.pushScene('login', this.loginErrorCode, -1, "");
	},
	
	checkIfDataAvailable: function (response) {
		var deviceId = RestoreUtil.getDeviceIdWithData(response);
		Mojo.Log.info("deviceId ====== %s",deviceId);
		if(deviceId === -1) {
			if(this.action === "update") {
				// This means that revalidation has failed
				Mojo.Controller.stageController.swapScene('update-error');
				return;
			}
			// Updates should happen
			RestoreUtil.showVersionError();
			return;
		}
		this.backedUpDeviceId = deviceId;
		if (Account.dbErrorType == "Corrupted") {
			this.decideAction();
			return;
		}	
		this.checkIfAppsAreAvailable();	
	},
	
	getDevicesFailure: function(response) {
		Mojo.Log.error("Why did getDevices fail? %o", $H(response));
		
		Mojo.Controller.stageController.swapScene('restore', this.backedUpDeviceId, 'backupError', this.packageURLArray, this.accountToken);
	},
	
	decideAction: function() {
		Mojo.Log.info("decideAction ======= this.backedUpDeviceId", this.backedUpDeviceId, "this.packageURLArray.size() = ",this.packageURLArray.size());
		if((this.backedUpDeviceId == undefined || this.backedUpDeviceId == "") && this.packageURLArray.size() == 0) {
			Account.action = 'login';
			Mojo.Controller.stageController.swapScene('status');
			return;
		}
		
		Mojo.Log.info("Account device id ======= %s", Account.deviceId);
		if (!(Account.deviceId)) {
			Mojo.Log.info("fetch Account device id =======");
			//TODO Check if deviceProfileFailure is what you should use
			this.deviceIdRequest = AppAssistant.accountService.getDeviceId(this.restoreData.bind(this), this.deviceProfileFailure.bind(this), this.controller);
		} else {
			this.startRestoreData(Account.deviceId);
		}		
	},
		
	deviceProfileFailure: function(response) {
		Mojo.Log.error("could not read device profile: %j", response);
		
		if(response.deviceInfo && response.deviceInfo.deviceId) {
			Account.deviceId = response.deviceInfo.deviceId;
			deviceCarrier = response.deviceInfo.carrier;
			this.getAccountToken();
			return;
		} 
		Mojo.Controller.stageController.swapScene('restore', this.backedUpDeviceId, 'backupError', this.packageURLArray, this.accountToken);
	},
	
	startRestoreData: function(devId) {
		Mojo.Log.info("this device id ======= %s", devId);
		var action = "migrate";	
		if(devId == this.backedUpDeviceId) {
			action = "restore";
		}
		Mojo.Log.info("What is the action == %s", action);
		
		if(this.backedUpDeviceId == undefined || this.backedUpDeviceId == "") {
			action = 'installApps';	
		} 
		Mojo.Controller.stageController.swapScene('restore', this.backedUpDeviceId, action, this.packageURLArray, this.accountToken);
	},
	
	restoreData: function(response) {	
		Account.deviceId = response.deviceId;
		this.startRestoreData(response.deviceId);
	},
	
	checkIfAppsAreAvailable: function() {
		if((Account.deviceId == "") || (deviceCarrier == undefined)) {
			this.deviceProfileRequest = AppAssistant.accountService.getDeviceProfile (this.readDeviceProfile.bind(this), this.deviceProfileFailure.bind(this), this.controller);
		} else {
			this.getAccountToken();	
		}
	},
	
	getAccountToken: function() {
		this.tokenRequest = AppAssistant.accountService.getAccountToken (this.readAccountToken.bind(this), this.accountTokenFailure.bind(this), this.controller);
	},
	
	readAccountToken: function(response) {
		this.appsCount = 0;
		this.pURLs = new Array();
		this.currentApp = 0;
		
		this.accountToken = response.token;
		if (!AppCatalogService.findappsServerURL) {
			this.urlRequest = AppAssistant.appCatalogService.getServerUrlForScene(this.readUrl.bind(this), this.errorUrl.bind(this), this.controller);
			return;
		}	
		var params = this.getParamsForAppListRequest(Account.email, this.accountToken, Account.deviceId, deviceCarrier);
		this.appsListRequest = AppAssistant.appCatalogService.getInstalledAppsList(params, this.readApps.bind(this), this.appsError.bind(this));
	},
	
	readUrl: function(response) {
		Mojo.Log.info(" OnSuccess.readUrl = %o " , $H(response));
		if (response.serverUrl) {
			AppCatalogService.findappsServerURL = response.serverUrl;
		}
		else  {
			Mojo.Log.error(" onFailure.readServerUrl = %o " , $H(response));
		}
		Mojo.Log.info(" this.findappsServerURL = %o ", AppCatalogService.findappsServerURL);
		var params = this.getParamsForAppListRequest(Account.email, this.accountToken, Account.deviceId, deviceCarrier);
		this.appsListRequest = AppAssistant.appCatalogService.getInstalledAppsList(params, this.readApps.bind(this), this.appsError.bind(this));
	}, 
	
	getParamsForAppListRequest: function (email, token, deviceId, carrier) {
		var params = {
			"InGetUserAppsV2": {
				"accountTokenInfo": {
					"email": email,
					"token": token,
					"deviceId": deviceId,
					"carrier": carrier
				},
				"locale": this.getLocale()
			}
		};		
		return params;
	},
	
	getLocale: function () {
		var locale = Account.language+"_"+Account.country;
		return locale;
	},
	
	errorUrl: function(response) {
		Mojo.Log.error("OnFailureCallback.errorUrl response=%o", $H(response));
		this.decideAction();
	},
	
	readApps: function (response) {
		Mojo.Log.info("OnSuccessCallback.getInstalledAppsList %j", response);
		
		if (response && response.responseText) {
			var jResponse = response.responseText.evalJSON();
			if(jResponse.OutGetUserInstalledAppsV2 && jResponse.OutGetUserInstalledAppsV2.userApps) {
				var appsList = jResponse.OutGetUserInstalledAppsV2.userApps;
				if (Object.isArray(appsList) == true) {
					this.packageURLArray = appsList;
				} else {
					this.packageURLArray[0] = appsList;
				}
			}	
		}
		Mojo.Log.info("OnSuccessCallback.this.packageURLArray=", this.packageURLArray);
		Mojo.Log.info("decide action");
		this.decideAction();
	},
	
	appsError: function (response) {
		Mojo.Log.error("OnFailureCallback.getInstalledAppsList response=%j", response);
		this.decideAction();
	},
	
	accountTokenFailure: function(response) {
		Mojo.Log.error("Could not get account token == %o", $H(response));
		Mojo.Controller.stageController.swapScene('restore', this.backedUpDeviceId, 'backupError', this.packageURLArray, this.accountToken);
		//Account.action = 'login';
		//Mojo.Controller.stageController.swapScene('status');
	},
	
	readDeviceProfile: function(response) {
		Account.deviceId = response.deviceInfo.deviceId;
		deviceCarrier = response.deviceInfo.carrier;
		this.getAccountToken();
	}
	
});	

