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

var RoamGuardUtil = Class.create ({
	
	checkNetworkStatus: function () {
		// Initialize isRoaming to false
		RoamUtil.isRoaming = false;
		this.networkStatusRequest = AppAssistant.accountService.getNetworkStatus(this.readNetworkStatus.bind(this), 
																					this.errorNetworkStatus.bind(this));
		
		// Roamguard is enabled by default																			
		RoamUtil.isRoamGaurdEnabled = true;
		this.wanStatusRequest = AppAssistant.accountService.getWanStatus(this.readWanStatus.bind(this), 
																					this.errorWanStatus.bind(this));			
	},
	
	readNetworkStatus: function (response) {
		Mojo.Log.info("--------------- readNetworkStatus -------------%j", response);
		if (response.extended) {
			response = response.extended;
		} else if (response.eventNetwork) {
			response = response.eventNetwork;
		}	
		if(response) {
			RoamUtil.networkState = response.state;
			RoamUtil.dataType = response.dataType;
			if (response.registration == "roaming") {
				RoamUtil.isRoaming = true;
				return;
			}	
		} 
		RoamUtil.isRoaming = false;
		Mojo.Log.info("--------------- RoamUtil.isRoaming -------------%j", RoamUtil.isRoaming);
	},

	errorNetworkStatus: function (response) {
		Mojo.Log.error("--------------------------- errorNetworkStatus ---------------------------%o", $H(response));
		RoamUtil.isRoaming = false;
	},
	
	readWanStatus: function (response) {
		Mojo.Log.info("--------------------------- readWanStatus ---------------------------%j", response);
		if(response.roamGuard) {
			RoamUtil.isRoamGaurdEnabled = (response.roamGuard === "disable") ? false : true;
			return;
		} 
	},
	
	errorWanStatus: function (response) {
		Mojo.Log.error("--------------------------- errorWanStatus ---------------------------%j", response);
	},
	
	enableRoaming: function () {
		if(RoamUtil.isRoaming === true && RoamUtil.isRoamGaurdEnabled === true) {
			return true;
		}
		return false;
	},
	
	processRoamGuardParams: function (params) {
		if(params.method) {
			params.method();
		}
	}
});

var RoamUtil = new RoamGuardUtil();