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

var ActivationUtil = Class.create ({
	
	provisionSuccess: function (response) {
		Mojo.Log.info("provision status = %j ", response);
		if(response.returnValue == false) {
			isActivated = false;
			activationFailed = true;
		}  
	},
	
	readSessionStatus: function (response) {
		Mojo.Log.info("session status = %j ", response);
		if(response.status != undefined) {
			sessionStatus = response.status;
		}
			
		Mojo.Log.info("----------------- Process response ----------------");
		if(response.returnValue) {
			attemptCount = 0;
			this.provisionRequest = AppAssistant.accountService.provisionDevice(ProvisionUtil.provisionSuccess, ProvisionUtil.provisionError);
		} else if(response.status == "Failure") {
				// If the status is Failure, provisionError method will be triggerred as error call back
				
		} else if (response.status == "Success") {
		 		isActivated = true;
				if(ProvisionUtil.sceneAssistant) {
					ProvisionUtil.sceneAssistant.activationDone();
				}
		} else if (response.status == "NetworkRetry") {
				attemptCount++;
				if(ProvisionUtil.sceneAssistant) {
					ProvisionUtil.sceneAssistant.checkActivationStatus();
				}
		} else if ((response.status == "Waiting") || (response.status == "Busy")) {
			if(ProvisionUtil.sceneAssistant) {
				ProvisionUtil.sceneAssistant.checkActivationStatus();
			}
		}		
	},
	
	provisionError: function (response) {
		Mojo.Log.error("provision error = %j", response);
		if(response.status != undefined) {
			sessionStatus = response.status;
		}
		activationFailed = true;
		if(response.trigger && response.errorCode) {
			if (response.trigger == "User") {
				attemptCount++;
				if (attemptCount == 4) {
					activationErrorCode = response.errorCode;
				} 
				if(ProvisionUtil.sceneAssistant) {
					ProvisionUtil.sceneAssistant.cidcFailed();
				}
			} else {
				if(ProvisionUtil.sceneAssistant) {
					ProvisionUtil.sceneAssistant.showHfaFailed();
				}
			}
			Mojo.Log.info("activationErrorCode= %o ", $H(activationErrorCode));	
		}
	},
	
	setScene: function (sceneAssistant) {
		ProvisionUtil.sceneAssistant = sceneAssistant;
		ProvisionUtil.sceneAssistant.provisionDevice();
	},	
	
	resetScene: function() {
		delete ProvisionUtil.sceneAssistant;
	}
			
});

var ProvisionUtil = new ActivationUtil();
