// ======================================================================
// Global Functions
// ======================================================================

// ----------------------------------------------------------------------
// setLogo()
// ----------------------------------------------------------------------
function setLogo(p_oImage)
{
	p_oImage.src = g_sCustomLogo;
}

// ----------------------------------------------------------------------
// setUsername
// ----------------------------------------------------------------------

function setUsername(p_sUsername)
{
	var oCurrentUsername = $("currentUsername");

	if (oCurrentUsername != null)
	{
		oCurrentUsername.update(p_sUsername);
	}
}

// ----------------------------------------------------------------------
// init()
// ----------------------------------------------------------------------
function init()
{
	$$('.logo').each(setLogo);
}


// ***********************************************************
// * validSelection()
// *
// * Returns whether or not the page is ready to be used,
// * based on the bSelectEnabled property and bContentLoaded.
// *
// * bSelectEnabled is set by the message box when it is
// * dislpayed (false) or hidden (true). It's used to lock out
// * this page while a message is displayed, so it appears to
// * be a modal dialog.
// *
// * bContentLoaded is set when the content iframe is loaded
// * (true) or unloaded (false). It's used to lock out this
// * page until the content page has fully loaded.
// *
// * See onContentUnload(), onContentLoad() and message_box.js
// *
// ***********************************************************
function validSelection()
{
	var bReturnVal = ((document.bSelectEnabled == true) && (document.bContentLoaded == true))

	return bReturnVal;
}


// ***********************************************************
// * onLogOut()
// *
// * Called by cpaint after 'performLogOut()'. If logout is
// * successful it first checks wehther a course window is
// * open, which it then closes, and then reloads this page.
// * Because this page is generated differently depending on
// * the users logged-in and admin status, the page reload
// * effectively takes them back to the sign in page.
// *
// ***********************************************************
function onLogOut(p_oResults, p_oXML)
{
	if (p_oResults.elumos[0].result[0].success[0].data == "y")
	{
		if (courseWindow != null)
		{
			// 'open' the window even if it already exists
			// just to get a reference to it.
			try
			{
				courseWindow = window.open ("","Course","width=32,height=32");
				courseWindow.close();
			}
			catch (e)
			{
			}
		}
		courseWindow = null;
		document.bContentLoaded = false;
		content.hideMessage();
		content.location = 'sign_in.php';
		location.replace('index.php');
	}
	else
	{
		content.showMessage(MSG_ERROR, p_oResults.elumos[0].result[0].message[0].data);
	}
}


// ***********************************************************
// * logOut()
// *
// * Called when user clicks on the log out toolbar button.
// * Show's confirmation message, which calls
// * 'performLogOut()' if OK'ed.
// *
// ***********************************************************
function logOut()
{
	if (validSelection())
	{
		content.showMessage(MSG_CONFIRM,"Are you sure you want to log out? Any course windows will be closed.", "parent.performLogOut");
	}
};


// ***********************************************************
// * performLogOut()
// *
// * Called when user has clicked on log out and ok'ed the
// * confirmation message.
// *
// * Calls backend logoout function which sends the response
// * to 'onLogOut()'.
// *
// ***********************************************************
function performLogOut()
{
	content.showMessage(MSG_COMM);

	g_oCPaint.call("lms_data.php", "logout", onLogOut);
}


// ***********************************************************
// * goHome()
// *
// * Called when user clicks on the elumos logo.
// *
// * If user is not on the sign_in screen it safely assumes
// * they're logged in and takes them to my_courses. Otherwise
// * it goes to the sign in page.
// *
// ***********************************************************
function goHome()
{
	if (content.location.href.indexOf('sign_in.php') != -1)
	{
		content.location = 'sign_in.php';
	}
	else
	{
		content.location = 'my_courses.html';
	}
};


// ***********************************************************
// * showHelp()
// *
// * Called when user clicks on the Help toolbar button.
// *
// * Simply launches the help file in a new window.
// *
// ***********************************************************
function showHelp()
{
	if (validSelection())
	{
		var oWin = openWindow('help/index.html','help','height=550,width=750,toolbar=no,resizable=yes,scrollbars=yes,status=yes', true);
	}
};

// ***********************************************************
// * calcHeight()
// *
// * Called mainly by content pages whenever they undergo
// * content alterations and resize events. There is also an
// * interval on this page which calls it once per second.
// *
// * Figures out the height of the page in the content iframe
// * and resizes the iframe to fit, with the intention the
// * iframe never needing a scrollbar.
// *
// * This should be called whenever anything happens to
// * change the dimensions of the content, i.e window resize,
// * content changes.
// *
// * It does not currently respond to text-size changes as
// * there is no associated event.
// *
// * See window.onResize and sizeTestInterval.
// *
// ***********************************************************
function calcHeight()
{
	//find the height of the internal page - offsetHeight for firefox, scrollHeight for ie
	if (document.bContentLoaded == true)
	{
		if (document.getElementById('content'))
		{
			if (document.getElementById('content').contentWindow)
			{
				if (document.getElementById('content').contentWindow.document)
				{
					if (document.getElementById('content').contentWindow.document.body)
					{
						nHeight = (navigator.userAgent.indexOf('MSIE') == -1) ? document.getElementById('content').contentWindow.document.body.offsetHeight : document.getElementById('content').contentWindow.document.body.scrollHeight;
						document.getElementById('content').height = nHeight + 20;
						document.getElementById('overlay').style.height = (nHeight + 86 + 20) +'px';
						document.getElementById('content').contentWindow.scrollTo(0,0);
					}
				}
			}
		}
	}
}


// ***********************************************************
// * goPage()
// *
// * Changes location of the content iframe to the passed
// * string.
// *
// ***********************************************************
function goPage(p_sPage)
{
	if (validSelection())
	{
		window.frames[0].location = p_sPage;
	}

}

// ***********************************************************
// * onContentUnload()
// *
// * Called by the content iframe when it's current document
// * is unloaded.
// *
// * It sets bContentLoaded to FALSE, which effectively locks
// * out the toolbar because every button action is dependant
// * on the 'validSelection()' function.
// *
// * See onContentLoad() and validSelection()
// *
// ***********************************************************
function onContentUnload()
{
	document.bContentLoaded = false;
}


// ***********************************************************
// * onContentLoad()
// *
// * Called by the content iframe when it's current document
// * is loaded.
// *
// * It sets bContentLoaded to TRUE, which effectively unlocks
// * the toolbar.
// *
// * See onContentUnload() and validSelection()
// *
// ***********************************************************
function onContentLoad()
{
	document.bContentLoaded = true;
	calcHeight();
}


// ***********************************************************
// * sizeTestInterval
// *
// * Used to call calcHeight once a second, generally only
// * useful when the user has changed text size causing the
// * content iframe to be the wrong size
// *
// * See calcHeight().
// *
// ***********************************************************
window.onload  = function()
{
	sizeTestInterval = setInterval(calcHeight,1000);
	if (navigator.userAgent.indexOf('MSIE') == -1)
	{
		document.getElementById("content").scrolling = "auto";
	}
	else
	{
		document.getElementById("content").scrolling = "no";
	}
}

window.onresize = calcHeight;

var sizeTestInterval;
var toolbar = document;
var courseWindow = null;

document.bContentLoaded = false;
document.bSelectEnabled = true;

