var browser;
var version;
var hasJava = navigator.javaEnabled();

if (navigator.userAgent.indexOf("Netscape") != -1) browser = "Netscape";
if (navigator.userAgent.indexOf("Mac")      != -1) browser = "MacIE";

if (browser == "Netscape") version = navigator.userAgent.split("Netscape")[1].split("/")[1];


var h2m2Enabled;
var hacpEnabled;
var scormEnabled;

var aicc_sid  = new String();
var aicc_url  = new String();
var aicc_data = new String();

var jumpPage  = false;

var startTime;
var lessonLocation;
var lessonStatus;


var testLesson = "AC220205";


if (!isUG) scormEnabled = getAPIHandle();


if (!scormEnabled) {
	aicc_sid = querystring("AICC_SID");
	aicc_url = querystring("AICC_URL");

	h2m2Enabled = ((browser == "Netscape" && parseInt(version) < 7) || browser == "MacIE");
}


if (currentPage == 0) {
	if (scormEnabled) {
		doLMSInitialize();

		if (document.location.href.toUpperCase().indexOf(testLesson) != -1) {
			startTime = doLMSGetValue("cmi.suspend_data");

			if (startTime == "" || startTime == null) {
				doLMSSetValue("cmi.suspend_data", new Date().getTime());
			}
		}
		else {
			setCookie("start_time", new Date().getTime());
		}

		if (doLMSGetValue("cmi.core.entry") == "resume") {
			lessonLocation = doLMSGetValue("cmi.core.lesson_location");
			jumpPage = doJumpPage(lessonLocation);
		}
	}
	else if (aicc_sid != null && aicc_url != null) {
		setCookie("aicc_sid", aicc_sid);
		setCookie("aicc_url", aicc_url);
		setCookie("start_time", new Date().getTime());

		if (h2m2Enabled) {
			audioSetting = querystring("audio");

			setCookie("h2m2", "1");
			setCookie("audio", audioSetting);

			lessonStatus   = querystring("lesson_status");
			lessonLocation = querystring("lesson_location");
		}
		else {
			setCookie("aicc", "1");

			hacpEnabled = true;

			lessonStatus   = aiccPost("getparam", "lesson_status");
			lessonLocation = aiccPost("getparam", "lesson_location");
		}

		if (lessonLocation == "" || lessonLocation == null) lessonLocation = "1";
		if (lessonStatus != "c" && lessonStatus != "complete") jumpPage = doJumpPage(lessonLocation);
	}

	if (!jumpPage) navNext();
}
else {
	if (!scormEnabled) {
		if (getCookie("aicc") != null) hacpEnabled = true;
		if (getCookie("h2m2") != null) {
			hacpEnabled = false;
			h2m2Enabled = true;
		}
	}

	if (scormEnabled) {
		if (doLMSGetValue("cmi.student_preference._children").indexOf("audio") != -1) {
			audioSetting = (doLMSGetValue("cmi.student_preference.audio") == -1) ? "0" : "1";
		}
	}
	else if (hacpEnabled) {
		audioSetting = (aiccPost("getparam", "audio") == -1) ? "0" : "1";
	}
	else {
		audioSetting = (getCookie("audio") == "-1") ? "0" : "1";
	}
}


function scoToggleSound(val) {
	val = (val == "1") ? 100 : -1

	if (scormEnabled) {
		if (doLMSGetValue("cmi.student_preference._children").indexOf("audio") == -1) {
			alert("Your LMS does not support this feature.");
		}
		else {
			doLMSSetValue("cmi.student_preference.audio", val);
		}
	}
	else if (hacpEnabled) {
		aiccPost("putparam", "[core]%0D%0Alesson_status=i%0D%0Alesson_location=" + currentPage + "%0D%0Ascore=0%0D%0Atime=00:00:01%0D%0A%0D%0A[student_preferences]%0D%0Aaudio=" + val);
	}
	else {
		setCookie("audio", val);
	}
}


function doSaveInteraction() {
	if (conType != null) {
		var s = "";
		var t = "";
		var e = document.getElementsByTagName("input");

		switch (conType) {
			case 1:
			case 2:
			case 3:
				for (var i=0; i < e.length; i++)
					if (e[i].type == "checkbox" || e[i].type == "radio")
						s += (e[i].checked) ? i + "," : "";

				t = "choice";

				break;
			case 4:
			case 5:
			case 6:
				for (var i=0; i < e.length; i++)
					if (e[i].type == "text")
						s += e[i].value + "||";

				t = "fill-in";

				break;
			case 7:
				s = stepPositions;
				t = "sequencing";
				break;
			case 8:
				s = selections;
				t = "matching";
				break;
			case 9:
				s = document.getElementsByTagName("textarea")[0].value;
				t = "fill-in";
				break;
		}

		if (scormEnabled) {
			var d = new Date();

			doLMSSetValue("cmi.interactions." + currentPage + ".id",objNum + "_" + currentPage);
			doLMSSetValue("cmi.interactions." + currentPage + ".time",zp(d.getHours()) + ":" + zp(d.getMinutes()) + ":" + zp(d.getSeconds()));
			doLMSSetValue("cmi.interactions." + currentPage + ".type",t);
			doLMSSetValue("cmi.interactions." + currentPage + ".student_response",s);
			doLMSCommit();
		}
	}
}


function doLoadInteraction() {
	if (conType != null) {
		if (scormEnabled) {
			var e = document.getElementsByTagName("input");
			var v = doLMSGetValue("cmi.interactions." + currentPage + ".student_response");

			if (v) {
				switch (conType) {
					case 1:
					case 2:
					case 3:
						v = v.split(",");

						for (var i=0; i < e.length; i++)
							for (var j=0; j < v.length; j++)
								if (e[i].type == "checkbox" || e[i].type == "radio")
									if (i == v[j] && v[j] != "")
										e[i].checked = true;

						break;
					case 4:
					case 5:
					case 6:
						v = v.split("||");

						for (var i=0; i < e.length; i++)
							e[i].value = v[i];

						break;
					case 7:
						v = v.split(",");

						for (var i=1; i<6; i++) {
							if (v[i] && !isNaN(parseInt(v[i]))) {
								resetStep();
								stepSelection = v[i];
								stepImage = el("order"+v[i]);
								stepPlace(el("place"+i), i);
							}
						}

						break;
					case 8:
						v = v.split(",");

						for (var i=1; i<6; i++) {
							if (v[i] && !isNaN(parseInt(v[i]))) {
								leftCellSelection = v[i];
								rightCellSelect(i);
							}
						}

						break;
					case 9:
						document.getElementsByTagName("textarea")[0].value = v;
						break;
				}
			}
		}
	}
}


function finishSCO() {
	var convertedTime;
	var currentTime;
	var elapsedTime;
	var lessonScore;

	if (scormEnabled || hacpEnabled || h2m2Enabled) {
		lessonScore = ((currentPage / totalPages) * 100);


		if (scormEnabled) {
			if (document.location.href.toUpperCase().indexOf(testLesson) != -1) {
				startTime = doLMSGetValue("cmi.suspend_data");
			}
			else {
				startTime = getCookie("start_time");
			}
		}

		if (hacpEnabled || h2m2Enabled) startTime = getCookie("start_time");

		if (startTime.length == 0) {
			convertedTime = "00:00:00"
		}
		else {
			currentTime   = new Date().getTime();
			elapsedTime   = ((currentTime - startTime) / 1000);
			convertedTime = aiccTime(elapsedTime);
		}

		if (scormEnabled) {
			doLMSSetValue("cmi.core.session_time", convertedTime);
			doLMSSetValue("cmi.core.lesson_location", currentPage);
			doLMSSetValue("cmi.core.score.raw", lessonScore);

			if (currentPage == totalPages) {
				doLMSSetValue("cmi.core.lesson_status", "completed");
				doLMSSetValue("cmi.core.exit", "");
			}
			else {
				doLMSSetValue("cmi.core.lesson_status", "incomplete");
				doLMSSetValue("cmi.core.exit", "suspend");
			}

			doLMSSetValue("cmi.suspend_data", "");

			exitFlag = false;

			doLMSCommit();
			doLMSFinish();
		}
		else if (hacpEnabled || h2m2Enabled) {
			audioSetting = (audioSetting == "1") ? 100 : -1;
			lessonStatus = (currentPage == totalPages) ? "c" : "i,s";

			exitFlag = false;

			aiccPost("putparam", "[core]%0D%0Alesson_status=" + lessonStatus + "%0D%0Alesson_location=" + currentPage + "%0D%0Ascore=" + lessonScore + "%0D%0Atime=" + convertedTime + "%0D%0A%0D%0A[student_preferences]%0D%0Aaudio=" + audioSetting);
			aiccPost("exitau", "");
		}
	}

	if (top.window.opener) {
		if ((!scormEnabled || unescape(unescape(top.window.location.href)).indexOf("runtime/scorm/frameset.asp") != -1) && unescape(unescape(top.window.location.href)).indexOf("adlesson/player.asp") == -1) top.window.close();
	}
	else
		document.location.href = "../../assets/finish.html";
}


function aiccPost(cmd, param) {
	var xmlhttp;

	aicc_sid  = getCookie("aicc_sid");
	aicc_url  = getCookie("aicc_url");
	aicc_data = "command=" + cmd + "&version=2.2&session_id=" + aicc_sid + "&aicc_data=";

	if (cmd == "putparam") aicc_data = aicc_data + param;

	if (hacpEnabled) {
		var xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

		xmlhttp.open("POST", aicc_url, false);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send(aicc_data);

		var aicc_response = xmlhttp.responseText;

		if (cmd == "getparam" && aicc_response.indexOf(param) > 0)
			return aicc_response.split(param)[1].split("\n")[0].split("=")[1].replace(/^\s+/, '').replace(/\s+$/, '');
		else
			return true;
	}
	else if (h2m2Enabled) {
		var dataEx = new Image();
		dataEx.src = aicc_url + "?" + aicc_data;
	}
}


function aiccTime(sec) {
	if (isNaN(sec)) return sec;

	var hour = Math.floor(sec/3600); sec -= hour * 3600;
	var min  = Math.floor(sec/60);   sec -= min  * 60;

	hour = (hour > 9) ? String(hour) : '0' + hour;
	min  = (min  > 9) ? String(min)  : '0' + min;
	sec  = (sec  > 9) ? String(parseInt(sec))  : '0' + parseInt(sec);

	return hour + ":" + min + ":" + sec;
}