var CT_Course = 1;
var CT_Tools = 2;
var NT_Home = 1;
var NT_Topics = 2;
var NT_InstantMentoring = 3;
var NT_CourseTools = 4;
var NT_Search = 5;
var NT_Help = 6;

function MlPage()
{
	this.getCacheElem = getCacheElem;
	this.isClientOnlyDataEnabled = isClientOnlyDataEnabled;
	this.setClientOnlyData = setClientOnlyData;
	this.getClientOnlyData = getClientOnlyData;
	this.getCookie = getCookie;
	this.setCookie = setCookie;
	this.writeLmsStylesheetHtml = writeLmsStylesheetHtml;
	this.getLmsStylesheetHtml = getLmsStylesheetHtml;
	this.writeCourseStylesheetHtml = writeCourseStylesheetHtml;
	this.getCourseStylesheetHtml = getCourseStylesheetHtml;
	this.getLmsObject = getLmsObject;
	this.floatWin = floatWin;
	this.getPassedParm = getPassedParm;
	this.replaceText = replaceText;
	this.replaceSpecialCharacters = replaceSpecialCharacters;
	this.FindAPI = FindAPI;
	this.findWnd = findWnd;
	this.isReferenceWindow = isReferenceWindow;
	this.getScreenWidth = getScreenWidth;
	this.getScreenHeight = getScreenHeight;
	this.scrollElementIntoView = scrollElementIntoView;
	this.is3rdPartyLms = is3rdPartyLms;
	this.isGettingStarted = isGettingStarted;
	this.hasActiveX = hasActiveX;

	this.lmsObject = "";

	var agt = navigator.userAgent.toLowerCase();
	this.browserVersion = 0;
	this.isIE = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	this.isNav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
	                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
	                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
	this.isNavMac = false;

	if (this.isIE)
	{
		var re = /msie ([^;]*);/;
		var matches = re.exec(agt);
		if (matches != null)
			this.browserVersion = parseFloat(matches[1]);
	}
	else
		this.browserVersion = parseFloat(navigator.appVersion);

	if (this.isNav && this.browserVersion >= 5)
	{
		var re = /netscape6\/([\d\.]*)/;
		var matches = re.exec(agt);
		if (matches != null)
			this.browserVersion = parseFloat(matches[1]);
		else
			this.browserVersion = 6;
	}

	if (this.isNav && navigator.platform.substring(0,1) == "M") 
	{
		this.isNav = false;
		this.isNavMac = true;
	}

	this.isWin = (navigator.appVersion.indexOf("Windows") != -1);
	this.userID = this.getCookie("id");
	this.evalPrompt = this.getCookie("evalprmpt");

	this.crsMode = this.getPassedParm("mode");

	if (this.crsMode == "")
	{
		if (isMindLeaders && this.isIE && this.browserVersion >= 5 && !this.is3rdPartyLms())
			this.crsMode = "mlie";
		else if (isMindLeaders)
			this.crsMode = "ml";
		else
			this.crsMode = "cd";
	}

	function hasActiveX()
	{
		var hasActiveX = false;
		if (this.isIE)
		{
			hasActiveX = true;
			eval("try {var xmlHttp = new ActiveXObject(\"Microsoft.XMLHTTP\");} catch(e){ hasActiveX = false; }");
		}
		return hasActiveX;
	}
		
	function getCookie(name)
	{
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1)
		{
			begin = dc.indexOf(prefix);
			if (begin != 0)
				return null;
		}
		else
			begin += 2;
		var end = document.cookie.indexOf(";", begin);
		if (end == -1)
			end = dc.length;
		return unescape(dc.substring(begin + prefix.length, end));
	}

	function setCookie(name, value, seconds)
	{
		var today = new Date();
		var expires = null;
		if (typeof(seconds) != "undefined")
		{
			expires = new Date();
			expires.setTime(today.getTime() + (seconds * 1000));
		}
		thisCookie = name + "=" + escape(value) + "; path=/"
			+ ((expires == null) ? "" : ("; expires=" + expires.toGMTString()));
		document.cookie = thisCookie;
	}

	function getCacheElem(cacheElem)
	{
		if (typeof(cacheElem) == "undefined")
		{
			if (this.getLmsObject() == null)
			{
				alert("No cache element was supplied.  getClientOnlyData() cannot be invoked.");
				return null;
			}
			else
				return this.getLmsObject().wndMain.document.getElementById("MainFrameset");
		}
		return cacheElem;
	}	

	function isClientOnlyDataEnabled(cacheElem)
	{
		var isEnabled = true;

		cacheElem = this.getCacheElem(cacheElem);
		if (cacheElem == null)
			return false;

		eval("try { cacheElem.load(\"MlClientOnlyData\"); } catch(e) { isEnabled = false; }");

		return isEnabled;
	}
	
	function getClientOnlyData(name,defValue,cacheElem)
	{
		if (typeof(defValue) == "undefined")
			defValue = null;
			
		if (this.isIE && this.browserVersion >= 5)
		{
			cacheElem = this.getCacheElem(cacheElem);
			if (cacheElem == null)
				return defValue;
				
			if (!this.isClientOnlyDataEnabled(cacheElem))
				// Browser has userdata disabled.
				return defValue;
							
			value = cacheElem.getAttribute(name);
		}
		else
			value = this.getCookie(name);

		if (value == null)
			value = defValue;

		return value;
	}

	function setClientOnlyData(name,value,cacheElem)
	{
		if (this.isIE && this.browserVersion >= 5)
		{
			cacheElem = this.getCacheElem(cacheElem);
			if (cacheElem == null)
				return;

			if (!this.isClientOnlyDataEnabled(cacheElem))
				// Browser has userdata disabled.
				return;

			cacheElem.setAttribute(name,value);
			cacheElem.save("MlClientOnlyData");
		}
		else
			this.setCookie(name,value);
	}

	function getLmsStylesheetHtml(pathPrefix)
	{
		if (typeof(pathPrefix) == "undefined")
			pathPrefix = "";

		var path = pathPrefix + "shared/Style/";
		return '<link rel="stylesheet" href="' + path + 'shared_files.css" type="text/css">';
	}
	
	function writeLmsStylesheetHtml(pathPrefix)
	{
		document.write(this.getLmsStylesheetHtml(pathPrefix));
	}

	function getCourseStylesheetHtml(skinDir)
	{
		if (typeof(skinDir) == "undefined")
			skinDir = "1";

		var courseStyleSheet=(ThisPage.isNav) ? "course_nn.css" : "course.css";
		return "<link rel=\"stylesheet\" href=\"" + rootDir +
			"/shared/images/skins/" + skinDir + "/" + courseStyleSheet + "\"" + 
			" type=\"text/css\">";
	}
	
	function writeCourseStylesheetHtml(skinDir)
	{
		document.write(this.getCourseStylesheetHtml(skinDir));
	}
	

	function getLmsObject(wnd)
	{
		if (typeof(wnd) == "undefined")
			wnd = self;

		if (wnd == null)
			return null;

		if (this.lmsObject == "")
			this.lmsObject = this.FindAPI(wnd);

		return this.lmsObject;
	}

	function isReferenceWindow(wnd)
	{
		if (wnd.name.indexOf("ref_window") != -1 || wnd.name == "search_results_window")
			return true;

		if (this.is3rdPartyLms(wnd) || wnd.parent == null || wnd == top || wnd == wnd.parent)
			return false;
		
		return this.isReferenceWindow(wnd.parent);
	}

	function is3rdPartyLms(wnd)
	{
		if (typeof(wnd) == "undefined")
			wnd = self;

		if (typeof(wnd.is3rdPartyLms) != "undefined")
			return true;

		if (wnd == top || wnd.parent == null || wnd == wnd.parent)
		  	return false;

		return this.is3rdPartyLms(wnd.parent);
	}
	
	function FindAPI(wnd)
	{
		var tWnd;
		
		if (typeof(wnd.API) != "undefined" && wnd.API != null)
		  	return wnd.API;

		if (this.is3rdPartyLms(wnd) || wnd == top || wnd.parent == null || wnd == wnd.parent)
		  	return null;

		return this.FindAPI(wnd.parent);
	}

	function findWnd(wnd,name)
	{
		var tWnd;
		
		// Am I the window?
		if (wnd.name == name)
		  	return wnd;

		// Are any of my children the window?
		for (var idx = 0; idx < wnd.frames.length; idx++)
		{
			tWnd = this.findWnd(wnd.frames[idx],name);
			if (tWnd != null)
				return tWnd;
		}

		// Is my opener or anybody in its lineage the window?
		if (wnd.opener != null && !wnd.opener.closed)
		{
			tWnd = this.findWnd(wnd.opener,name);
			if (tWnd != null)
				return tWnd;
		}
			
		if (this.is3rdPartyLms(wnd) || wnd == top || wnd.parent == null || wnd == wnd.parent)
		  	return null;

		// Is my parent the window?
		return this.findWnd(wnd.parent,name);
	}

	function floatWin(Destination,RemoteName,Parms)
	{
		var newWind;
		if (this.isNav)
		{
			newWind=window.open(Destination,RemoteName,Parms);
			if (newWind.opener == null)
				newWind.opener = window;
			else
	            newWind.focus();
		}
		else
			newWind=window.open(Destination,RemoteName,Parms);
			
		return newWind;
	}

	function getPassedParm(parm,lowerCase)
	{
		if (location.search == "")
			return "";
			
		if (typeof(lowerCase) == "undefined")
			// Maintains compatibility with code that called this without the parameter.
			lowerCase = true;
			
	    var parmList = "&" + location.search.substring(1,location.search.length) + "&";
		var re = new RegExp("&" + parm + "=([^&]*)&","i");
		var foundArray = re.exec(parmList);
		if (foundArray == null)
			return "";

		return lowerCase ? foundArray[1].toLowerCase() : foundArray[1];
	}

	function replaceText(theString,sFindText,sReplaceText)
	{
	   var lcString=theString.toLowerCase();
	   var whereFind= lcString.indexOf(sFindText.toLowerCase());
	   while (whereFind > -1)  {
	      var leftHalf=theString.substring(0,whereFind);
	      var rightHalf=theString.substring(whereFind+sFindText.length, theString.length);
	      theString=leftHalf + sReplaceText + rightHalf;
	      lcString=theString.toLowerCase();
	      whereFind= lcString.indexOf(sFindText.toLowerCase(), leftHalf.length + sReplaceText.length);
	   }
	   return theString;
	}

	function replaceSpecialCharacters(s)
	{
		var findChar = new Array("<", ">", "\"", "&lt;", "&gt;");
		var replaceChar = new Array("", "", "", "", "");
		for (var i=0;i<findChar.length; i++)
		{
			s=replaceText(s, findChar[i], replaceChar[i]);
		}
		return s;
	}

	function getScreenWidth()
	{
		return screen.availWidth - 10;  // for side borders
	}
	
	function getScreenHeight(isStatus)
	{
		if (typeof(isStatus) == "undefined")
			isStatus = true;
			
		var offset = isStatus ? 57 : 30;
		return screen.availHeight - offset; // for caption and status
	}

	function scrollElementIntoView(elem)
	{
		var body = elem.document.body;
		var elemTop = elem.offsetTop;

		var tElem = elem;
		while (tElem.offsetParent != body)
		{
			tElem = tElem.offsetParent;
			elemTop += tElem.offsetTop;
		}

		var elemBottom = elemTop + elem.offsetHeight;
		var elemHeight = elemBottom - elemTop + 1;
		var scrollTop = body.scrollTop;
		var scrollBottom = scrollTop + body.clientHeight
		var v;

		if (elemTop > scrollBottom || elemBottom > scrollBottom)
		{
			if (body.clientHeight > elemHeight)		
				v = elemTop - (body.clientHeight - elemHeight)
			else
				v = elemTop;
		}
		else if (elemBottom < scrollTop || elemTop < scrollTop)
		{
			v = elemTop;
		}
		else
			return;

		body.document.parentWindow.scrollTo(body.scrollLeft,v);
	}

	function isGettingStarted(cid)
	{
		return (cid == "de1" || cid == "di1" || cid == "dn1");
	}
}

