var logged_in = false;	// set in /support/index.html and used in login.html and other sub-pages
var TraceBuffer=""
var debug=false;

var HM_DOM = (document.getElementById) ? true : false;
var HM_NS4 = (document.layers) ? true : false;
var HM_IE = (document.all) ? true : false;
var HM_IE4 = HM_IE && !HM_DOM;
var HM_Mac = (navigator.appVersion.indexOf("Mac") != -1);
var HM_IE4M = HM_IE4 && HM_Mac;
var HM_IsMenu = (HM_DOM || HM_NS4 || (HM_IE4 && !HM_IE4M));
var HM_NS6 = (navigator.vendor == ("Netscape6") || navigator.product == ("Gecko"));
var HM_PG_NSFontOver = true;
var HM_BrowserString = HM_NS4 ? "NS4" : HM_DOM ? "DOM" : "IE4";

function Trace(tmsg) {
	if (debug) {
		TraceBuffer=TraceBuffer+tmsg+" ";	
	}
}

// example: <A HREF="javascript:ShowTrace()">Show Debug Trace</A>
function ShowTrace() {
	if (TraceBuffer=="") TraceBuffer="no trace data";
	alert(TraceBuffer);
}
// example: <A HREF="javascript:ClearTrace()">Clear Debug Trace</A>
function ClearTrace() {
	TraceBuffer="";
}
// example: <A HREF="javascript:CopyToClipboard()">Copy Debug Trace to Clipboard</A>
function CopyToClipboard() {
	if (window.clipboardData) window.clipboardData.setData("Text", TraceBuffer);
}

function returnParameter(lsearch, param) {
	result = lsearch.indexOf("?" + param, 0)
	if (result>-1) {
		result2 = lsearch.indexOf("?", result + 1);	// params separated by '?'
		if (result2==-1) result2 = lsearch.indexOf("&", result + 1);	// params separated by '&'
		if (result2==-1) result2 = lsearch.length;
	} else {
		result = lsearch.indexOf("&" + param, 0);
		if (result>-1) {
			result2 = lsearch.indexOf("&", result + 1);
			if (result2==-1) result2 = lsearch.length;
		}
	}
	if (result>-1&&result2>-1) {
		return unescape(lsearch.substring(result+param.length+2,result2))
	} else {
		return ""
	}
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)	break;
	}
	return null;
}

function SetCookie(name, value) {
	expire=new Date("July 1, 2100");
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	path = "/";
	document.cookie = name + "=" + escape (value) +
										((expire == null) ? "" : ("; expires=" + expire.toGMTString())) +
										((path == null) ? "" : ("; path=" + path)) +
										((domain == null) ? "" : ("; domain=" + domain)) +
										((secure == true) ? "; secure" : "");
}

function LaunchLesson(actPath, actNumber, actName, actLessonMode) {
	if (g_sStudentName=="" || g_sStudentName==null) {
		alert("You must login first!");
	} else {
		g_sLessonName = actName;	// for APIadaptor.js	
		g_sLessonMode = actLessonMode;	
		ClearFormData();
		InitFormData();		
// need to fix APIadaptor to search for the frmLMS frame before releasing full screen mode:
//		if (HM_IE && !HM_Mac && (g_sCoursePath.indexOf("pcskills", 0)==-1) && (g_sCoursePath.indexOf("windows", 0)==-1) ){
//			CBT = window.open(g_sCoursePath + actPath + "/" + actNumber + "fs.html", "idActWnd", "status=no,fullscreen=yes, resizable=no,toolbar=no,scrollbars=no");
//		} else {
			NewWnd = window.open(g_sCoursePath + actPath + "/" + actNumber + ".html", "idActWnd", "status=no,fullscreen=no, resizable=no,toolbar=no,scrollbars=no,width=640,height=512");
			if (NewWnd) {
				try {NewWnd.moveTo((screen.width-640)/2,(screen.height-512)/4)} catch (e) {}
//			}
		}
		if (NewWnd) {
			try {NewWnd.opener = this} catch (e) {}
			try {NewWnd.focus()} catch (e) {}
		}
	}
}

function launchWnd(wndPath, wndWidth, wndHeight) {
	if (g_sStudentName=="" || g_sStudentName==null) {
		alert("You must login first!");
	} else {
		NewWnd = window.open(wndPath, "idlWnd", "scrollbars=yes,width="+wndWidth+", height="+wndHeight);
		if (NewWnd) {
			try {NewWnd.opener = this} catch (e) {}	// try/catch fixes "Class doesn't support Automation" IE 6.0.2 with service pack 1 bug
			try {NewWnd.moveTo((screen.width-wndWidth)/2,(screen.height-wndHeight)/4)} catch (e) {}
			try {NewWnd.focus()} catch (e) {}
		}
	}
}

function NewSubWindow(wndPath, wndWidth, wndHeight, wndProps) {
	NewWnd = window.open(wndPath, "subWnd", wndProps + ", width="+wndWidth+", height="+wndHeight);
	if (NewWnd) {
		try {NewWnd.opener = this} catch (e) {}
		try {NewWnd.moveTo((screen.width-wndWidth)/2,(screen.height-wndHeight)/4)} catch (e) {}
		try {NewWnd.focus()} catch (e) {}
	}
}

function setFrames(masterFrame) {
	if ((top.frames.length == 0) || (top.frames.location.pathname != masterFrame)) {
		if (masterFrame.indexOf('/') != -1) { // otherwise keeps refreshing if the path wasn't correctly set in the call to setFrames
			SetCookie('newURL', document.URL);
			location.replace(masterFrame);	
		}
	}
}

function ci(evt){
	tk=0;
	if (document.all){
		tk=window.event.keyCode
	}
	if (document.layers){
		tk=evt.which
		evt.which=0
	}
	if (tk==13){
		window.event.keyCode=0
		submitCheck()
	}
}

function NewBrowserWnd(wndPath, wndWidth, wndHeight, wndFeatures) {
	if (typeof(wndFeatures)!="undefined") {
		NewWnd = window.open(wndPath, "NewBrsrWnd", wndFeatures + " width="+wndWidth+", height="+wndHeight);
	} else {
		NewWnd = window.open(wndPath, "NewBrsrWnd", "location=1, directories=1, toolbar=1, status=1, menubar=1, scrollbars=1, resizable=1,width="+wndWidth+", height="+wndHeight);
	}
	if (NewWnd) {
    NewWnd.opener = this;
		try {
			if ((NewWnd.width > screen.width)||(wndHeight > screen.height)) {
				NewWnd.moveTo(0, 0);
			}	else {
				NewWnd.moveTo((screen.width-wndWidth)/2, (screen.height-wndHeight)/4);
			}
			NewWnd.focus();
		}
		catch(e) {}		
  }
}

function SetCurrentCourse(CoursePath) {
	if (CoursePath.indexOf("/learn") > -1) {
		CurrentCourse = CoursePath.substr(10);
		CurrentCourse = CurrentCourse.substring(0, CurrentCourse.indexOf("/"));
		SetCookie("CurrentCourse", CurrentCourse);	// for search engine
	}
}

function d2h(d) {
	var hD="0123456789ABCDEF";
	var h = hD.substr(d&15,1);
	while(d>15) {
		d>>=4;
		h = hD.substr(d&15,1)+h;
	}
	return h;
}

function h2d(h) {
	return parseInt(h,16);
}

function ZeroFill(iNum, iDigits) {
	var sNum = "" + iNum;	// convert to string
	
	while (sNum.length < iDigits)
		sNum = "0" + sNum;
	return sNum;
}