window.onload = init;

// Add trim functionality to string object
String.prototype.Trim = new Function("return this.replace(/^\\s+|\\s+$/g,'')");

onload_queue = new Array();
var text_focus = null;
var text_caps = 0;

// Initialise all the neat little features
function init()
{	
    tableEffect();
	inputEffect();
	
//	if(typeof(m_autoLogout) == "undefined")
//		m_autoLogout = 21;	// Automatically log out in 20 minutes - hopefully the server isn't set to less than 20!
//	if(m_autoLogout != 0)
//	{	
//	    setInterval(autoLogout, 60 * 1000);
//		autoLogout();
//		alert("m_autoLogout = " + m_autoLogout);
//	}
    if (typeof(auto)!="undefined") 
        myinit();

	onLoadFocusEx();

	onload_queue = onload_queue.reverse();

	while((ol = onload_queue.pop()) != null)
		ol();

}

function queue_onload(ol)
{	
    onload_queue.push(ol);
}

// Add automatic text selection to all text boxes on the page
function inputEffect()
{	var oInput, index;

	oInput = document.getElementsByTagName("input");
	for(index = 0; index < oInput.length; index++)
		if(oInput[index].type == "text"
		|| oInput[index].type == "password")
		{
			oInput[index].onfocus = onFocus;
			if(text_focus == null)
				text_focus = oInput[index];
		}

	oInput = document.getElementsByTagName("textarea");
	for(index = 0; index < oInput.length; index++)
		oInput[index].onfocus = onFocus;

}

// If a text box receives focus, automatically select all text
function onFocus()
{

	if(!(event.srcElement.tagName == "INPUT"
	&& (event.srcElement.type == "text"
	|| event.srcElement.type == "password")
	|| event.srcElement.tagName == "TEXTAREA"))
		return;

	text_focus = event.srcElement;
	event.srcElement.select();

}

function doKeydown()
{	var login, index;
	var oInput;
	var next;

	text_focus.focus();
	
	if(event.srcElement.innerText == "/")
	{
	
		login = document.getElementsByName("login");
		for(index = 0; index < login.length; index++)
			if(login[index].tagName == "FORM")
				login[index].submit();
				
	}
	else
	if(event.srcElement.innerText == "Tab")
	{
	
		next = false;
		oInput = document.getElementsByTagName("input");
		for(index = 0; index < oInput.length; index++)
			if(oInput[index].type == "text"
			|| oInput[index].type == "password")
				if(next == false)
				{
					if(oInput[index] == text_focus)
						next = true;
				}
				else
				{	
					text_focus = oInput[index]
					next = false;
				}

		if(next == true)
			text_focus = oInput[0];

		text_focus.focus();
	
	}
	else
	if(event.srcElement.innerText == "Caps")
	{
	
		if(text_caps == 1)
			text_caps = 2;
		else
		{
			if(text_caps == 0)
				text_caps = 2;
			else
				text_caps = 0;
			swapCase();
		}
	
	}
	else
	if(event.srcElement.innerText == "Shift")
	{
	
		if(text_caps == 2)
			text_caps = 1;
		else
		{
			if(text_caps == 0)
				text_caps = 1;
			else
				text_caps = 0;
			swapCase();
		}
	
	}
	else
	{
	
		if(event.srcElement.innerText == "<-")
			text_focus.value = text_focus.value.substring(0, text_focus.value.length - 1);
		else
		if(event.srcElement.innerText == "Del")
		{
			text_focus.value = "";
		}
		else
		if(event.srcElement.innerText == "")
			text_focus.value += " ";
		else
			text_focus.value += event.srcElement.innerText;

		if(text_caps == 1)
		{
			text_caps = 0;
			swapCase();
		}
		
	}

}

function swapCase()
{	var oTable;
	var oTd;
	var index;
	var swap;
	
	oTable = document.getElementsByTagName("table");
	
	for(index = 0; index < oTable.length; index++)
		if(oTable[index].className == "keyboard")
		{	oTable = oTable[index]
			index = -1;
			break;
		}
		
	if(index == -1)
	{
	
		oTd = oTable.getElementsByTagName("td");
		for(index = 0; index < oTd.length; index++)
			if(oTd[index].title != "")
			{
			
				swap = oTd[index].title;
				oTd[index].title = oTd[index].innerText;
				oTd[index].innerText = swap;
			
			}
	
	}

}

// Automatically add hilighting to tables with class="line"
function tableEffect()
{	var oTable, index;
	var oTr, index2;

	oTable = document.getElementsByTagName("table");
	for(index = 0; index < oTable.length; index++)
		if(oTable[index].className.search(/\bline\b/) != -1
		&& oTable[index].className.search(/\bnohl\b/) == -1)
		{	oTr = oTable[index].tBodies(0);
			if(oTr != null)
			{

				oTr = oTr.rows;

				for(index2 = 0; index2 < oTr.length; index2++)
				{	oTr(index2).onmouseover	= onTableHover;
					oTr(index2).onmouseout	= onTableLeave;
				}

			}

		}

}

// If mouse is over a class="line" table, do highlighting
function onTable()
{	var oTable;
	var oTr = event.srcElement;

	if(oTr.tagName == "TH")
		return null;

	while(oTr != null && oTr.tagName != "TR")
		oTr = oTr.parentNode;

	// If text is selected over a hilighted row, and selection is dragged out
	//	over a row on another table, regardless of that tables class, and
	//	by extension, its event handling, it will get this event - so tables
	//	that shouldn't be hilighted, are. This little block will check that
	//	the row to be hilighted is a row inside a table that supports it
	if(oTr != null)
	{

		oTable = oTr;
		while(oTable != null && oTable.tagName != "TABLE")
			oTable = oTable.parentNode;

		if(oTable != null)
			if(oTable.className.search(/\bline\b/) != -1
			&& oTable.className.search(/\bnohl\b/) == -1)
				;
			else
				oTr = null;

		if(oTr != null)
			if(oTr.className.search(/\bnohl\b/) == -1)
				oTr = oTr;
			else
				oTr = null;

	}

	return oTr;

}

// Add hilighting to a row on class="line" tables
function onTableHover()
{	oTr = onTable();
    var oT = event.srcElement;

	if((oT.tagName != "INPUT") & (oT.tagName != "TH"))
	{
		if(oTr != null)
	    {
    		oTr.style.oldbgColor = oTr.style.backgroundColor;
    		if(oTr.style.backgroundColor == "#8de")
    			oTr.style.backgroundColor = "#cee";
    		else
    		{	oTr.style.backgroundColor = "#cee";//"#ffe";
                oTr.style.BorderColor = "#8de";
            }
    	}
    }
}

// Remove hilighting from a row on class="line" tables
function onTableLeave()
{	oTr = onTable();
    var oT = event.srcElement;

	if((oT.tagName != "INPUT") & (oT.tagName != "TH"))
	{
	    if(oTr != null)
	    {
		    if(typeof(oTr.style.oldbgColor) != "undefined")
			    oTr.style.backgroundColor = oTr.style.oldbgColor;
		    else
    			oTr.style.backgroundColor = "transparent";
		}
    }
}

// Update a warning message about impending logout - and go to logout.aspx if time is up
//function autoLogout()
//{	var oSpan = document.getElementById("aulo");
//	if(m_autoLogout == 0)
//		return;
//	m_autoLogout--;
//	if(oSpan != null)
//		oSpan.innerText = m_autoLogout;
//	if(m_autoLogout <= 5)
//	{

//		oSpan = document.getElementById("aulotime");
//		if(oSpan != null)
//			if(m_autoLogout == 0)
//				oSpan.innerText = ", oh... right now ";
//			else
//			if(m_autoLogout == 1)
//				oSpan.innerText = "under 1 minute ";
//			else
//				oSpan.innerText = "in " + m_autoLogout + " minutes ";

//		oSpan = document.getElementById("aulowarn");
//		if(oSpan != null)
//			oSpan.style.display = "block";

//	}
//	if(m_autoLogout <= 0)
//		location.href = "logout.aspx";
//}

// Reset the auto-logout timer and 'prod' the server to make sure the server also resets its timer
//function resetAutoLogout()
//{	var oSpan;

//	if(m_autoLogout == 0)
//		return;

//	oSpan = document.getElementById("aulo");
//	if(oSpan != null)
//		oSpan.innerText = "contacting server...";

//	//window.open("prod.asp", "prod", "width=100,height=100,resizable=yes,scrollbars=yes");

//	oSpan = document.getElementById("aulowarn");
//	if(oSpan != null)
//		oSpan.style.display = "none";

//	m_autoLogout = 21;
//	autoLogout();

//}

// Set the focus in 0.1 seconds from now
function onLoadFocusEx()
{	window.setTimeout(onLoadFocus, 100, "javascript");
}

function onOptionChange(url)
{
	url = url.replace("#", event.srcElement.value);
	window.navigate(url);
}

// What receives the focus? If an HTML tag is found with the name 'focus', it receives focus
//	otherwise, if a <focus> fake empty HTML tag is found, the next sibling receives focus
//	otherwise, if a <focus></focus> fake container HTML tag is found, the first child receives focus
//	Examples
//		<input name="focus">	' named tag receives focus
//		<focus><input>			' next sibling receives focus
//		<focus><input></focus>	' first child receives focus
function onLoadFocus()
{	var oEl;

	oEl = document.getElementsByName("focus");
	if(oEl.length != 0)
	{	oEl[0].focus();
		oEl[0].setActive();
	}
	else
	{
	
		oEl = document.getElementsByTagName("focus");
		if(oEl.length != 0)
		{
		
			if(oEl.hasChildren == true)
				oEl = oEl[0].firstChild;
			else
				oEl = oEl[0].nextSibling;

			oEl.focus();
			oEl.setActive();
			
		}
		else
			;	// Do nothing - it would be cool if it would focus on the first input 
				//	control or link on the page as a last case fallback
				//	Yup, that sure would be cool - I'm just gonna sit back and have a
				//	think for a while about how cool that would be

	}

}

function onPagination(id)
{

	if(typeof(pi) == "undefined")
		pi = document.getElementById("pi");

	if(id != 0)
		if(pi.selectedIndex + id < 0)
			if(pi.selectedIndex == 0)
				return;
			else
				pi.selectedIndex = 0;
		else		
		if(pi.selectedIndex + id > pi.options.length - 1)
			if(pi.selectedIndex == pi.options.length - 1)
				return;
			else
				pi.selectedIndex = pi.options.length - 1;
		else
			pi.selectedIndex += id;
		
	id = 0;
    document.getElementsByName("event")[0].value = "paging";
	
	if(id == 0)
		doApply();
}

function sort(id)
{
	window.navigate(id);
}

function doMenuFormSubmit(id, warn)
{
	if(warn == "")
		document.getElementById(id).submit();
	else
	if(confirm(warn) == true)
		document.getElementById(id).submit();
}

function isShadowNinja(name)
{
	if(document.getElementById("ninja_" + name).style.display == "none")
		return true;
	else
		return false;
}

function doNinjaShadow(name)
{
	document.getElementById("ninja_" + name).style.display = "none";
}

function doNinjaStrike(name)
{
	document.getElementById("ninja_" + name).style.display = "inline";
}
