var SUPRESS_ALERTS = true;

var API = getAPI();

function getAPI() {
	var theAPI = findAPI(window);
	if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
		theAPI = findAPI(window.opener);
	}
	if (theAPI == null) {
		if (!SUPRESS_ALERTS) {
			alert("LMS not found. Running in offline mode.");
		}
	}
	return theAPI;
}

function findAPI(win) {
	findAPITries = 0;
	while ((win.API == null) && (win.parent != null) && (win.parent != win)){
		findAPITries++;

		if (findAPITries > 500) {
			if (!SUPRESS_ALERTS) {
				alert("Error finding API -- too deeply nested.");
			}
			return null;
		}
			win = win.parent;
	}

	return win.API;
}

function doLMSInitialize() {
	if (!API) return "";

	value = API.LMSInitialize("");
	if (doLMSGetLastError() != "0") {
		if (!SUPRESS_ALERTS) {
			alert(doLMSGetErrorString(doLMSGetLastError()));
		}
	}
	return value;
}

function doLMSTerminate() {
	if (!API) return "";

	value = API.LMSFinish("");
	if (doLMSGetLastError() != "0") {
		if (!SUPRESS_ALERTS) {
			alert(doLMSGetErrorString(doLMSGetLastError()));
		}
	}
	return value;
}

function doLMSGetValue(name) {
	if (!API) return "";

	return API.LMSGetValue(name);
}

function doLMSSetValue(name, value) {
	if (!API) return "";

	return API.LMSSetValue(name, value);
}

function doLMSCommit() {
	if (!API) return "";

	return API.LMSCommit("");
}

function doLMSGetLastError() {
	if (!API) return "";

	return API.LMSGetLastError("");
}

function doLMSGetErrorString(errorCode) {
	if (!API) return "";

	return API.LMSGetErrorString(errorCode);
}

function doLMSGetDiagnostic() {
	if (!API) return "";

	return API.LMSGetDiagnostic("");
}