var agt = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie4up = (is_ie && (is_major >= 4));

var tID = null;
var jsnav = null;
var courseSelectWindow = null;

var answer = "&nbsp;";
var explanation = "&nbsp;";
var usersAnswerGiven = null;;

function is5thGen() {
	return (!!document.getElementById);
}

function checkAnswer(usersAnswer) {
	var explanationBox = document.getElementById("explanationBox");
	var explanationAnswer = document.getElementById("explanationAnswer");
	var explanationText = document.getElementById("explanationText");
  if (usersAnswer == answer) {
		explanationAnswer.style.color = "green";
		explanationAnswer.innerHTML = "<strong>That is correct!</strong>";
		explanationText.innerHTML = explanation;
	}
	else {
		explanationAnswer.style.color = "red";
		explanationAnswer.innerHTML = "<strong>Sorry, that is incorrect. The correct answer is <u>" + answer + "</u></strong>";
		explanationText.innerHTML = explanation;
	}
  explanationBox.style.display = "block";
	document.qform.submitButton.disabled = false;
	document.qform.answer.value = usersAnswer; 
	for (var i=0; i<document.qform.answerTemp.length; i++) {
		if ((document.qform.answerTemp[i].value == usersAnswer) && (!document.qform.answerTemp[i].checked)) {
			document.qform.answerTemp[i].checked = true;
		}	
		document.qform.answerTemp[i].disabled = true;
	}	
}

function toggle(elmid, tag) {
	var tags = document.getElementsByTagName(tag);
	for (var i=0; i<tags.length; i++) {
		if (tags[i].className == "toggle") {
			if (tags[i].id == elmid) {
				if (tags[i].style.display != "block") {
					tags[i].style.display = "block";
				}
				else {
					tags[i].style.display = "none";
				}	
			}
			else {
				tags[i].style.display = "none";
			}
		}
	}
}

function courseSelect(idx) {
	if (courseSelectWindow && !courseSelectWindow.closed) {
		courseSelectWindow.focus();
	}
	else {
		courseSelectWindow = window.open("/forms/licenserenewal/courselist.cfm?idx=" + idx, "courselist", "height=150,width=550,location=no,toolbar=no,menubar=no,resizable=no,scrollbars=no,status=no");
	}
}

function resetTimer() {
	clearTimeout(tID);
	tID = setTimeout("executeTimer()",500);
}

function executeTimer() {
	hideMenu();
}

function getAbs(elm, pos) {
	if (!elm) {return;}
	var nPos = null;
	if (pos == "x")
		nPos = elm.offsetLeft
	else
		nPos = elm.offsetTop;
	var pElm = elm.offsetParent;
	while (pElm) {
		if (pos == "x")
			nPos += pElm.offsetLeft
		else
			nPos += pElm.offsetTop;
		pElm = pElm.offsetParent;
	}
	return nPos;
}

function findSubMenu(subMenu) {
	var item = null;
	var menu = document.getElementById(subMenu);
	if (!menu) {return;}
	menu.onmouseout = resetTimer;
	menu.style.visibility = "hidden";
	for (var i=0; i<menu.childNodes.length; i++) {
		item = menu.childNodes[i];
		if (item.nodeType == 3) continue;
		if (item.getAttribute("menu")) findSubMenu(item.getAttribute("menu"));
		if (item.getAttribute("cmd")) item.onclick = new Function("doCmd(\""+item.id+"\")");
		item.onmouseover = new Function("highlight(\""+item.id+"\")");
	}  
} 

function showMenu(obj) {
	clearTimeout(tID);
	hideMenu(jsnav);
	if (obj != null && obj.length) {
		obj = document.getElementById(obj);
		if (obj.getAttribute("cmd")) window.status = obj.getAttribute("cmd");
		var menu = document.getElementById(obj.getAttribute("menu"));
		var bar = document.getElementById(obj.id);
		if (bar.getAttribute("cmd")) {
			if (bar.getAttribute("cmd").substr(0,1) == "/") {
				window.status = window.location.protocol + "//" + window.location.host + bar.getAttribute("cmd");
			}
			else {
				window.status = bar.getAttribute("cmd");
			}
		}
		else {
			window.status = "";
		}
		bar.className="o";
		if (menu) {
			menu.style.visibility = "visible";
			menu.style.top = (getAbs(obj, "y") + obj.offsetHeight - 1) + "px";
			menu.style.left = (getAbs(obj, "x")) + "px";
			if (!!document.all) {
				var ienavfix = document.getElementById(menu.id + "fix");
				if (!!ienavfix) {
					ienavfix.style.display = "block";			
				}
				else if (!!document.all && !!document.createElement && !!document.appendChild && !!is_ie4up && !!jsnav) {
					var ienavfix = document.createElement("iframe");
					ienavfix.id = menu.id + "fix";
					ienavfix.style.position = "absolute";
					ienavfix.style.zIndex = "1";
					ienavfix.style.display = "block";
					ienavfix.style.top = menu.style.top;
					ienavfix.style.left = menu.style.left;
					ienavfix.style.width = menu.offsetWidth + "px";
					ienavfix.style.height = menu.offsetHeight + "px";
					jsnav.appendChild(ienavfix);
				}		
			}
		}
	}  
}
   
function highlight(oid) {
	clearTimeout(tID);
	var obj = document.getElementById(oid);
	var pElm = document.getElementById(obj.parentNode.id);
	for (var i=0; i<pElm.childNodes.length; i++) {
		var elm = pElm.childNodes[i];
		elm.className = "";
	}
	obj.className="o";
	if (obj.getAttribute("cmd")) {
		if (obj.getAttribute("cmd").substr(0,1) == "/") {
			window.status = window.location.protocol + "//" + window.location.host + obj.getAttribute("cmd");
		}
		else {
			window.status = obj.getAttribute("cmd");
		}
	}
	else {
		window.status = "";
	}
	showSubMenu(obj);
}
   
function doCmd(oid) {
	var cmd = document.getElementById(oid).getAttribute("cmd");
	window.location = cmd;
}
   
function hideMenu(obj) {
	if (!obj || obj.tagName != "DIV") obj = jsnav;
	for (var i=0;i<obj.childNodes.length;i++) {
		var child = obj.childNodes[i];
		if (child.nodeType == 3) continue;
		if (child.className == "o") document.getElementById(child.id).className = "";
		var childMenu = document.getElementById(child.getAttribute("menu"));
		if (childMenu) {
			if (childMenu.hasChildNodes()) {
				for (var j=0; j<childMenu.childNodes.length; j++) {
					var tElm = childMenu.childNodes[j];
					if (tElm.nodeType == 3) continue;
					if (tElm.className == "o") tElm.className = "";
				}
				hideMenu(childMenu);
				childMenu.style.visibility = "hidden";
				if (!!document.all && !!document.createElement && !!document.appendChild && !!is_ie4up) {
					var ienavfix = document.getElementById(childMenu.id + "fix");
					if (!!ienavfix) {ienavfix.style.display = "none";}
				}	
				
			}
		}	
	}
	window.status = "";
}

function showSubMenu(obj) {
	var pMenu = obj.parentNode;
	//hideMenu(pMenu);
	var menu = document.getElementById(obj.getAttribute("menu"));
	if (menu) {
		menu.style.visibility = "visible";
		menu.style.top = (obj.offsetTop + pMenu.offsetTop) + "px";
		menu.style.left = (pMenu.offsetLeft + obj.offsetWidth + 3) + "px";
	}
}   

function initMenu() {
	if (!document.getElementById && !document.childNodes) {return;}
	document.body.onclick = hideMenu;
	jsnav = document.getElementById("jsnav");
	var bar,menu,item,menuItem = null;
	for (var i=0; i<jsnav.childNodes.length; i++) {
		bar = jsnav.childNodes[i];
		if (bar.nodeType == 3) continue;
		bar.onmouseout = resetTimer;
		bar.onmouseover = new Function("showMenu(\""+bar.id+"\")");
		if (bar.getAttribute("cmd")) bar.onclick = new Function("doCmd(\""+bar.id+"\")");
		menu = document.getElementById(bar.getAttribute("menu"));
		if (menu) {
			menu.onmouseout = resetTimer;
			menu.style.visibility = "hidden";
			for (var j=0; j<menu.childNodes.length; j++) {
				item = menu.childNodes[j];
				if (item.nodeType == 3) continue;
				menuItem = document.getElementById(item.id);
				if (menuItem.getAttribute("menu")) findSubMenu(menuItem.getAttribute("menu"));
				if (menuItem.getAttribute("cmd")) menuItem.onclick = new Function("doCmd(\""+menuItem.id+"\")");
				menuItem.onmouseover = new Function("highlight(\""+item.id+"\")");
			}
		}	
	}
	var exp = document.getElementById("explanationBox");
	if ((!!exp) && (usersAnswerGiven != null)) {
		checkAnswer(usersAnswerGiven);
	}
}  	

window.onload = initMenu;
