exports.cleanup = function() {
	try {
		if (mSess != null) {
			mSess.close();
		}
		if (mBgSess == null) {
			mBgSess = opensync.syncagent.createBGSession();
		}
		if (mBgSess.getFatalError() != null) {
			platform.exit();
		}
	} catch (e) {
		platform.exit();
	}
	
	mSess = null;
	mBGSess = null;
	mSyncProgress = null;
	mUserText = null;
	mPwdText = null;
	mSvPwdChkBx = null;
	mUrlText = null;
	mStatusText = null;
	mStatusStr = null;
};
exports.initialize = function() {
	mStatusStr = "";
	mStatusIsLog = false;

	// Create the OpenSync session
	mSess = opensync.ose.createOSESession();
	if (!mSess.isOpen()) {
		mSess = null;
	}

	// If a previous session was restored, then start the
	// sync agent if desired.
	if (mSess != null) {
		try {
			mBgSess = opensync.syncagent.createBGSession();
			if (mBgSess) {
				mBgSess.start();
			}
		} catch (e) {
			mBgSess = null;
		}
	}
};
function doSyncAgent() {
	try {
		if (mBgSess == null) {
			mBgSess = opensync.syncagent.createBGSession();
			if (!mBgSess.isOpen()) {
				mBgSess = null;
				return;
			}
		}

		if ((mSess == null) || (mBgSess.getAgentStatusCode() != opensync.syncagent.BGAgentStatus.STOPPED)) {
			mBgSess.showUI();
		} else {
			mStatusStr = mStatusText.value;
			setStatus('Starting sync Agent...');
			mBgSess.start();
			// Wait until the sync agent is running, then display the UI
			mBgSess.waitForStatus({
				status: opensync.syncagent.BGAgentStatus.RUNNING,
				timeout: 5000,
				success: function() {
					mBgSess.messageHandler = function(e) {
						Ti.API.info("msgHandler: " + JSON.stringify(e));
					};
					mBgSess.showUI();
					setStatus(mStatusStr);
				},
				error: function(e) {
					showError(e);
				}
			});
		}
	} catch (e) {
		showError(e);
	}
}