
// Various utility functions used across webpages
// Some ideas to implement:
// See: http://www.dustindiaz.com/top-ten-javascript/
// ----------------------------------------------------------------

function fnMakeTopWindow()
{
	if (typeof(window) == 'undefined' || typeof(top) == 'undefined') return;
	if (window == top) return;
	top.location.href = '/';
}

// ----------------------------------------------------------------

function fnGetObjectById(elementId)
{
	if (document == null || typeof(document) == 'undefined') return null;
	var element = null;
	if		(document.getElementById)	element = document.getElementById(elementId);
	else if	(document.all)				element = document.all[elementId];				// Outdated IE support
	else if	(document.layers)			element = document.layers[elementId];			// Outdated Mozilla support
	if (element == null || typeof(element) == 'undefined') element = eval("document." + elementId);
	return element;
}
function fnSetCssClass(elementId, classname)
{
	var element = fnGetObjectById(elementId);
	if (element == null || typeof(element) == 'undefined') return;
	element.className = classname;
}
function fnSetOnClick(elementId, script)
{
	var element = fnGetObjectById(elementId);
	if (element == null || typeof(element) == 'undefined') return;
	element.onclick = script;
}
function fnToggleElementDisplay(elementId, elementVisibleStyle)
{
	if (elementVisibleStyle	== null || elementVisibleStyle	== undefined) return;

	var element = fnGetObjectById(elementId);
	if (element				== null || element				== undefined) return;
	if (element.style		== null || element.style		== undefined) return;

	if (element.style.display == "none")	element.style.display = elementVisibleStyle;
	else									element.style.display = "none";
}
function fnToggleTableRowDisplay(elementId) {fnToggleElementDisplay(elementId, "table-row");}

// ----------------------------------------------------------------

function fnSubmitFormTo(formname, action)
{
	var o = fnGetObjectById(formname);
	if (o == null || o == undefined) return;
	o.action = action;
	o.submit();
}

// ----------------------------------------------------------------

function fnHtmlEncode(html)
{
	html = html.replace(/&quot;/g,	'&quotx;');
	html = html.replace(/"/g,		'&quot;');
	html = html.replace(/&amp;/g,	'&ampx;');
	html = html.replace(/&/g,		'&amp;');
	html = html.replace(/&lt;/g,	'&ltx;');
	html = html.replace(/</g,		'&lt;');
	html = html.replace(/&gt;/g,	'&gtx;');
	html = html.replace(/>/g,		'&gt;');
	return html;
}
function fnHtmlDecode(html)
{
	html = html.replace(/&gt;/g,	'>');
	html = html.replace(/&gtx;/g,	'&gt;');
	html = html.replace(/&lt;/g,	'<');
	html = html.replace(/&ltx;/g,	'&lt;');
	html = html.replace(/&amp;/g,	'&');
	html = html.replace(/&ampx;/g,	'&amp;');
	html = html.replace(/&quot;/g,	'"');
	html = html.replace(/&quotx;/g,	'&quot;');
	return html;
}

// ----------------------------------------------------------------

function fnPopupHelptext(url, dx, dy)
{
	window.open(url, null, 'height=' + dx + ',width=' + dy + ',status=no,toolbar=no,menubar=no,location=no');
}

// ----------------------------------------------------------------

function fnGetCookieVal(sStart)
{
	var end = document.cookie.indexOf(";", start);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(start, end));
}
function fnGetCookie(name)
{
	var find = name + "=";
	var findlen = find.length;
	var cookielen = document.cookie.length;
	var start = 0;
	while (start < cookielen)
	{
		var end = start + findlen;
		if (document.cookie.substring(start, end) == find) return fnGetCookieVal(end);
		start = document.cookie.indexOf(" ", start) + 1;
		if (start == 0) break; 
	}
	return null;
}
function fnSetCookie(name, value, expires, path, domain, secure)
{
	document.cookie =	name + "=" + escape(value) +
						((expires) ? "; expires=" + expires.toGMTString() : "") +
						((path) ? "; path=" + path : "") +
						((domain) ? "; domain=" + domain : "") +
						((secure) ? "; secure" : "");
}
function fnDeleteCookie(name, path, domain)
{
	if (fnGetCookie(name))
	{
		document.cookie =	name + "=" +
							((path) ? "; path=" + path : "") +
							((domain) ? "; domain=" + domain : "") +
							"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
function fnSetOneYearCookie(name, value)
{
	var expiry = new Date();
	expiry.setTime(expiry.getTime() + (365 * 24 * 60*60*1000));
	fnSetCookie(name, value, expiry, '/');
}

// ----------------------------------------------------------------

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

