/**
 * Overrides the ExpGroupBy function from core.js
 *
 * The original function did not expand elemens in Firefox browsers.
 *
 * @author Microsoft Corporation
 * @coauthor juergen.unfried@pxp.eu
 *
 * Differences to the original:
 * - removed if (browseris.nav) to enable the script in Firefox
 * - cleaned code (intenting, line breaks, readability
 * - checked and refactored code with JSLint (JavaScript Code Quality Tool)
 *   - "error" with duplicate variable definition still exists. 
 *     Did not fix it because no idea where this script is implemented across 
 *     sharepoint pages and therefore not enough test possibilites.
 *   - Did not fix != with !== because this stopped the expand/collapse from working.
 */
function ExpGroupBy(formObj)
{
	if ((browseris.w3c) && (!browseris.ie)) {
		document.all = document.getElementsByTagName("*");
	}

	docElts = document.all;
	numElts = docElts.length;
	images = formObj.getElementsByTagName("IMG");
	img = images[0];
	srcPath = img.src;
	index = srcPath.lastIndexOf("/");
	imgName = srcPath.slice(index + 1);
	
	if (imgName == 'plus.gif')
	{
		fOpen = true;
		displayStr = "";
		img.src = '/_layouts/images/minus.gif';
	}
	else
	{
		fOpen = false;
		displayStr = "none";
		img.src = '/_layouts/images/plus.gif';
	}

	oldName = img.name;
	img.name = img.alt;
	img.alt = oldName;
	spanNode = img;

	while(spanNode != null)
	{
		spanNode = spanNode.parentNode;
		
		if (spanNode != null &&
				spanNode.id != null &&
				spanNode.id.length > 5 &&
				spanNode.id.substr(0, 5) == "group")
		break;
	}

	parentNode=spanNode;	

	while(parentNode != null)
	{
		parentNode = parentNode.parentNode;
		
		if (parentNode != null &&
				parentNode.tagName == "TABLE")
		break;
	}
	
	lastNode = null;

	if (parentNode != null)
	{
		lastNode = parentNode.lastChild;
		
		if (lastNode != null && lastNode.tagName == "TBODY")
			lastNode=lastNode.lastChild;

		if (lastNode != null && lastNode.tagName == "TR" && lastNode.lastChild != null)
			lastNode=lastNode.lastChild;
	}
	
	for(var i = 0; i < numElts; i++)
	{
		var childObj = docElts[i];

		if (childObj == spanNode)
			break;
	}

	ID = spanNode.id.slice(5);

	for(var j = i + 1; j < numElts; j++)
	{
		var childObj = docElts[j];
		
		if (childObj.id.length > 5 &&
				childObj.id.substr(0, 5) == "group")
		{
			curID = childObj.id.slice(5);
		
			if (curID <= ID)
		     return;
		}

		parentNode = childObj;

		while(parentNode != null)
		{
			parentNode = parentNode.parentElement;
			if (parentNode == spanNode)
		     break;
		}
		
		if (parentNode == spanNode)
			continue;

		if (childObj != img &&
				childObj.tagName == "IMG" &&
				childObj.src &&
				childObj.src.slice(childObj.src.length - 25) =='/_layouts/images/plus.gif')
		{
			childObj.src = '/_layouts/images/minus.gif';
			oldName = childObj.name;
			childObj.name = childObj.alt;
			childObj.alt = oldName;
		}

		if (childObj.tagName == spanNode.tagName &&
				childObj.id != "footer")
		{
			childObj.style.display = displayStr;
		}
		
		if ((childObj.tagName == "TABLE" && lastNode == null) || childObj == lastNode)
			break;
	}
}