/*
 * crms.js - JavaScript functions for all CRMS Web Applications.
 *
 * $Id: crms.js,v 1.9 2007/03/19 13:59:02 duplaina Exp $
 */

var client = null;
var layerz = null;
var currentLayer = null;

function errorHandler(message, url, lineno)
{
	alert("JavaScript Error: " + message + " " + url + " line " + lineno);
	return true;
}

function init()
{
	window.onerror = errorHandler;
	layerz = 10;

	if (document.layers != null)
		client = "nn4";
	else if (document.all != null)
		client = "ie";
	else if (document.getElementById != null)
		client = "mozilla";
	else
		client = "unknown";
	sfHover();
}

function sfHover() 
  { 
		
	var obj = document.getElementById("nav");
	if (obj == null)
		return;

	if (client  == 'ie')
	{
		var sfEls = document.getElementById("nav").getElementsByTagName("LI"); 
		var ieULs = document.getElementById('nav').getElementsByTagName('UL');
		
		for (var j=0; j<ieULs.length; j++) 
		{

		var ieMat=document.createElement('iframe');

	
		if(document.location.protocol == "https:")
			ieMat.src="//0";
		else
			ieMat.src="javascript:false";
			
		ieMat.scrolling="no";
		ieMat.frameBorder=0;
		ieMat.className ='iframehack';
		//ieMat.left = 0;
		//ieMat.top = 0;
		//ieMat.top= ieULs[j].top + 'px';
		//ieMat.style.display = 'none';
  		//ieMat.style.position = "absolute";
		//ieMat.width='165px';
		//ieMat.height='100px';
		ieULs[j].insertBefore(ieMat, ieULs[j].childNodes[0]);
		ieULs[j].style.zIndex="101";
		}

	}	
		
	if (client == 'ie')
	{
		for (var i=0; i< sfEls.length; i++) 
		{ 
				
			 sfEls[i].onmouseover=
		 		function() 
		 		{
					
		 			this.className+=" sfhover"; 
				} 
	     	sfEls[i].onmouseout=
	     		function() 
		 		{ 
		 			this.className=this.className.replace(new RegExp(" sfhover\\b"), ""); 
				} 
		} // for
	} // if name
 }


function showLayer(name)
{
	switch (client)
	{
	case "nn4":
		document[name].visibility = "show";
		document[name].zIndex = layerz;
		break;
	case "ie":
		document.all[name].style.visibility = "visible";
		document.all[name].style.zIndex = layerz;
		break;
	case "mozilla":
		document.getElementById(name).style.visibility = "visible";
		document.getElementById(name).style.zIndex = layerz;
		break;
	default:
	}

	if (currentLayer != null && currentLayer != name)
		hideLayer(currentLayer);
	currentLayer = name;
	
	layerz++;
}

function hideLayer(name)
{
	switch (client)
	{
	case "nn4":
		document[name].visibility = "hidden";
		break;
	case "ie":
		document.all[name].style.visibility = "hidden";
		break;
	case "mozilla":
		document.getElementById(name).style.visibility = "hidden";
		break;
	default:
	}
}

function getColour(name)
{
	var colour = null;						 

	switch (client)
	{
	case "nn4":
		colour = document[name].color;
		break;
	case "ie":
		colour = document.all[name].style.color;
		break;
	case "mozilla":
		colour = document.getElementById(name).style.color;
		break;
	default:
	}
	
	return colour;
}		
	
function setColour(name, colour)
{
	switch (client)
	{
	case "nn4":
		document[name].color = colour;
		break;
	case "ie":
		document.all[name].style.color = colour;
		break;
	case "mozilla":
		document.getElementById(name).style.color = colour;
		break;
	default:
	}
}

function getBackground(name)
{
	var colour = null;						 

	switch (client)
	{
	case "nn4":
		colour = document[name].bgColor;
		break;
	case "ie":
		colour = document.all[name].style.backgroundColor;
		break;
	case "mozilla":
		colour = document.getElementById(name).style.backgroundColor;
		break;
	default:
	}
	
	return colour;
}		
	
function setBackground(name, colour)
{
	switch (client)
	{
	case "nn4":
		document[name].bgColor = colour;
		break;
	case "ie":
		document.all[name].style.backgroundColor = colour;
		break;
	case "mozilla":
		document.getElementById(name).style.backgroundColor = colour;
		break;
	default:
	}
}

function getBorder(name)
{
	var border = null;						 

	switch (client)
	{
	case "nn4":
		border = document[name].border;
		break;
	case "ie":
		border = document.all[name].style.border;
		break;
	case "mozilla":
		border = document.getElementById(name).style.border;
		break;
	default:
	}
	
	return border;
}

function setBorder(name, colour, style, width)
{
	switch (client)
	{
	case "nn4":
		document[name].border = colour + " " + style + " " + width;
		break;
	case "ie":
		document.all[name].style.borderBottomColor = colour;
		document.all[name].style.borderBottomStyle = style;
		document.all[name].style.borderBottomWidth = width;
		document.all[name].style.borderLeftColor = colour;
		document.all[name].style.borderLeftStyle = style;
		document.all[name].style.borderLeftWidth = width;
		document.all[name].style.borderRightColor = colour;
		document.all[name].style.borderRightStyle = style;
		document.all[name].style.borderRightWidth = width;
		document.all[name].style.borderTopColor = colour;
		document.all[name].style.borderTopStyle = style;
		document.all[name].style.borderTopWidth = width;
		break;
	case "mozilla":
		document.getElementById(name).style.border = colour + " " + style + " " + width;
		break;
	default:
	}
	
	return colour;
}

// Open a URL in a seperate window
function openwin(url, name, width, height)
{
	window.open(url, name,
		"resizable=yes,scrollbars=yes,width=" + width + ",height=" + height);	
}

// Open a URL in the window that opened the current window
function openwin_opener(url)
{
	if (window.opener == null)
	{
		alert("Unable to open URL; no opener property set in current window");
		return;
	}
	window.opener.location.href = url;
}

// Change to an external page
function gotoloc(location)
{
	if (location != null && location.length > 0)
	{
		document.location = location;
	}
}

// Jump to an internal link on the page (you cannot use <a href="#xxx"></a> on a CGI-generated page).
function jumpto(hash)
{
	if (hash != null && hash.length > 0)
	{
		document.location.hash = hash;
	}
}

function preloadImages()
{
	if (document.images == null)
		return;
	if (document.crms_p == null)
		document.crms_p = new Array();
	var i, j = document.crms_p.length, a = preloadImages.arguments;
	for(i = 0; i < a.length; i++)
	{
		if (a[i].indexOf("#") != 0)
		{
			document.crms_p[j] = new Image();
			document.crms_p[j++].src = a[i];
		}
	}
}

function swapImage()
{
	var i, j, a = swapImage.arguments;
	document.crms_sr = new Array();
	for (i = 0, j = 0; i < a.length - 2; i += 3)
	{
		var x = findObj(a[i]);
		if (x != null)
		{
			document.crms_sr[j++] = x;
			if (x.oSrc == null)
				x.oSrc = x.src;
			x.src = a[i + 2];
		}
	}
}

function restoreImage()
{
	if (document.crms_sr == null)
		return;
	var i;		
	for(i = 0; i < document.crms_sr.length; i++)
	{
		var x = document.crms_sr[i];
		if (x != null && x.oSrc != null)
			x.src = x.oSrc;
	}
}

function findObj(n, d)
{
	var p, i , x = null;
	if (d == null)
		d = document;

	if ((p = n.indexOf("?")) > 0 && parent.frames.length > 0)
	{
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0,p);
	}

	x = d[n];
	if (x != null)
		return x;

	if (d.all != null)
	{
		x = d.all[n];
		if (x != null)
			return x;
	}
	
	for (i = 0; i < d.forms.length; i++)
	{
		x = d.forms[i][n];
		if (x != null)
			return x;
	}
	
	if (d.layers != null)
	{
		for (i = 0; i < d.layers.length; i++)
		{
			x = findObj(n, d.layers[i].document);
			if (x != null)
				return x;
		}
	}

	if (d.getElementById != null)
		x = d.getElementById(n);

	return x;
}

//
// Floating header stuff.  All prefixed with "fh"
// To implement floating headers:
//		Add a call to fhInit() from pageinit()
//		Add onResize="fhResize()" to the <body> tag.
//		Generate the <div> and <table> elements using HtmlUtil.genFloatingHeader
//
var fhHeader = null;
var fhList = null;
var fhOldPageTop = -1;

function fhScrollIE(calledFromResize)
{
	if (fhHeader == null || fhList == null)
		return;

	var pageTop = document.body.scrollTop;
	var oldDisplay = fhHeader.style.display;
	var newDisplay =
		(pageTop > fhList.offsetTop &&
		pageTop < (fhList.offsetTop + fhList.clientHeight - fhHeader.clientHeight)) ?
		"block" : "none";
	
	if (newDisplay == "block")
		fhHeader.style.pixelTop = pageTop;

	if (oldDisplay != newDisplay)
	{
		fhHeader.style.display = newDisplay;
		if (!calledFromResize && oldDisplay == "none" && newDisplay == "block")
			fhResizeIE();
	}
}

function fhResizeIE()
{
	if (fhHeader == null || fhList == null || fhHeader.style.display == "none")
		return;

	fhHeader.style.left = fhList.style.left;

	// Set table width
	fhHeader.firstChild.style.width = (fhList.clientWidth + (fhList.border * 2)) + "px";

	// Access the array of <td> tags within table and the floating header
	var tableTds = fhList.firstChild.firstChild.childNodes;	
	var fhHeaderTds = fhHeader.firstChild.firstChild.firstChild.childNodes;

	for (i = 0; i < tableTds.length; i++)
	{
		fhHeaderTds[i].style.width = tableTds[i].clientWidth + "px";
	}
	
	fhScrollIE(true);
}

function fhScrollNN(calledFromResize)
{
	if (fhHeader == null || fhList == null)
		return;

	var pageTop = window.pageYOffset;
	if (pageTop == fhOldPageTop)
		return;			// No change
	fhOldPageTop = pageTop;
	var oldDisplay = fhHeader.style.display;
	var newDisplay =
		(pageTop > fhList.offsetTop &&
		pageTop < (fhList.offsetTop + fhList.offsetHeight - fhHeader.offsetHeight)) ?
		"block" : "none";
	
	if (newDisplay == "block")
		fhHeader.style.top = pageTop;

	if (oldDisplay != newDisplay)
	{
		fhHeader.style.display = newDisplay;
		if (!calledFromResize && oldDisplay == "none" && newDisplay == "block")
			fhResizeNN();
	}
}

function fhResizeNN()
{
	if (fhHeader == null || fhList == null || fhHeader.style.display == "none")
		return;

	fhHeader.style.left = fhList.style.left;

	// Set table width
	fhHeader.childNodes[1].width = fhList.offsetWidth + "px";

	// Access the array of <td> tags within table and the floating header
	var tableTds = fhList.childNodes[1].childNodes[0].cells;
	var fhHeaderTds = fhHeader.childNodes[1].childNodes[1].childNodes[0].cells;
	for (i = 0; i < tableTds.length; i++)
	{
		fhHeaderTds[i].style.width = tableTds[i].offsetWidth + "px";
	}
	
	fhScrollNN(true);
}

function fhResize()
{
	if (client == "ie")
		fhResizeIE();
	else
		fhResizeNN();
}

function fhInit(headerId, listId)
{
	if (client == "ie")
	{
		fhHeader = document.all[headerId];
		fhList = document.all[listId];
		if (fhHeader != null && fhList != null)
		{
			window.onscroll = fhScrollIE;
			fhScrollIE(false);
		}
	}
	else
	{
		fhHeader = document.getElementById(headerId);
		fhList = document.getElementById(listId);
		if (fhHeader != null && fhList != null)
		{
			fhScrollNN(false);
			window.setInterval("fhScrollNN(false)", 100);
		}
	}
}
