 function selectGroups()
   {
		//no checkboxes, leave as-is
		if(!document.getElementById("chGroup0")) return;

		removeTemporaryLinks();
		var selectedGroupArray = getSelectedGroupArray();	

		//check each link in the list of links
		var linkNumber = 1;
		var selectedLinkCount = 0;
		var selectedLinkIndexArray = new Array();

		while (document.getElementById("mapLinkNo" + linkNumber))
		{
			var mapNumberLink = document.getElementById("mapLinkNo" + linkNumber);
			//if selected in any of the checkboxes
			if(linkIsSelected(mapNumberLink, selectedGroupArray))
			{
			    selectedLinkCount++;	
				selectedLinkIndexArray.splice(selectedLinkIndexArray.length,0,linkNumber);
				
				//show the map marker
				gMarkerArray[linkNumber - 1].show();

			}
			else
			{
				//hide the map marker
				gMarkerArray[linkNumber - 1].hide();
			}
			linkNumber++;
		}

		addTemporaryLinks(selectedLinkIndexArray);
		
   }


	function addTemporaryLinks(selectedLinkIndexArray)
	{
		//integer division to get # of links in each column
		var linkColumnCount = getLinkColumnCount();
		var linksPerColumnRemainder = selectedLinkIndexArray.length % linkColumnCount;
		var linksPerColumn = (selectedLinkIndexArray.length - linksPerColumnRemainder) / linkColumnCount;
		//uneven amount = extra 1 in the each of the 1st column(s)
		if(linksPerColumnRemainder > 0) linksPerColumn++;

		var currentColumnCount = 0;
		var currentColumnIndex = 0;
		var currentCell = 1;

		for(var xx=0; xx<selectedLinkIndexArray.length; xx++)
	    {
			//move to the next column if this one is full
			if(currentColumnCount == linksPerColumn)
			{
				currentColumnCount = 0;
				currentColumnIndex++;
				currentCell = getFirstCellIdInColumn(currentColumnIndex);
			}			

			//add link to the next cell in this column
			addTemporaryLink(currentCell , selectedLinkIndexArray[xx], "mapLinkNo", "tempLinkNo");				
			addTemporarySpace(currentCell);
			addTemporaryLink(currentCell,  selectedLinkIndexArray[xx], "mapLinkText", "tempLinkText");	
			
			currentColumnCount++;
			currentCell++;
	    }
	}

	function getLinkColumnCount()
	{
		var testTR = document.getElementById("mapLinkCell1").parentNode;
		return testTR.getElementsByTagName("TD").length;
	}

	function getFirstCellIdInColumn(columnIndex)
	{
		var testTR = document.getElementById("mapLinkCell1").parentNode;
		var testTD = testTR.getElementsByTagName("TD")[columnIndex];
		if(testTD)
		{	
			return parseInt(testTD.id.substring("mapLinkCell".length));
		}
	}

	function addTemporaryLink(cellNumber, linkNumberToCopy, sourceLinkPrefix, destLinkPrefix)
	{



		//get the "source" link we will be copying
		var linkToCopy = document.getElementById(sourceLinkPrefix + linkNumberToCopy);
		


		//create a copy of that link
		var tempLink = 
			newElementWithSettings
				("A", destLinkPrefix + cellNumber, linkToCopy.innerHTML, linkToCopy.href);



		//get the target cell where we'll be adding the link
		var targetCell = document.getElementById("mapLinkCell" + cellNumber);

		//add the link to that cell
		targetCell.appendChild(tempLink);

	}

	function addTemporarySpace(cellNumber)
	{
		//create a span with a space in it
		var tempSpace = 
			newElementWithSettings("SPAN", "tempSpace" + cellNumber, "&nbsp;", "");

		//get the target cell where we'll be adding the space
		var targetCell = document.getElementById("mapLinkCell" + cellNumber);

		//add the space to that cell
		targetCell.appendChild(tempSpace);
	}

	function newElementWithSettings(tagName, newID, newInnerHTML, newHref)
	{
		//create new element -any tag name
		var newElement = document.createElement(tagName); 

		//assign ID and inner HTML
		newElement.id = newID;
		newElement.innerHTML = newInnerHTML;
		
		//and href if it has been supplied
		if(newHref != "") newElement.href = newHref;

		return newElement;
	}

	function getSelectedGroupArray()
	{
 		var resultArray = new Array();

		//go through the list of checkboxes
		var allCheckBoxes = document.getElementsByName("checkboxGroup");
		for(var xx=0; xx < allCheckBoxes.length; xx++)
		{
		    var ch = allCheckBoxes[xx];
			if(ch.checked)
			{ //add selected "group" (checkbox) to an array
			  var groupName = ch.id.substring('ch_'.length);
			  resultArray.splice(resultArray.length,0,groupName);
			}						
		}
		//return the resulting array
		return resultArray;
	}


   function linkIsSelected(link, selectedGroupArray)
   {	
	    //check whether a link should be displayed or not
		var groupList = "," + link.attributes.getNamedItem("groups").value + ",";
		var andMode = true;		
		var result = false
		
		//go through the checkboxes that are selected
		for(var xx=0; xx<selectedGroupArray.length; xx++)
		{
			//get category (is a prefix e.g. Facilities_ )
			var groupCategoryToCheck = 
				selectedGroupArray[xx].substring(0, selectedGroupArray[xx].indexOf("_"));

			if(groupCategoryToCheck.toLowerCase() == "location")
			{   
				//location is a special case where we do an OR match 
				//ASSUMPTION - location will be the last group of checkboxes
				andMode = false;
			}

			if(groupList.indexOf( "," + selectedGroupArray[xx] + ",") != -1)
			{	
				//one of the groups in the current category match
				//mark as a pass if we're doing an "OR" match
				if(!andMode) 
			    { result=true; }
			}
			else
			{
				//one of the groups in the current category DOESN'T match
				//if we are doing an "AND" match, fail
				if(andMode) return false;
			}
		}
		return result;
   }

	function removeTemporaryLinks()
	{
		//go through all cells, removing a temp link if one is there
		var cellCounter = 1
		while(document.getElementById("mapLinkCell" + cellCounter))
		{
			removeElementyId("tempLinkNo" + cellCounter);
			removeElementyId("tempSpace" + cellCounter);
			removeElementyId("tempLinkText" + cellCounter);
			cellCounter++;
		}

	}

   function removeElementyId(idToRemove)
   {	
		if(document.getElementById(idToRemove))
		{	
			var elementToRemove = 
			  document.getElementById(idToRemove);
			elementToRemove.parentNode.removeChild(elementToRemove);
		}
   }

   function toggleAll(index)
   {

		var toggleAllCheckBox =  document.getElementById("toggleAll" + index);
		if(!toggleAllCheckBox) return;
		var toggleValue = toggleAllCheckBox.checked;
		
		var containingDiv = document.getElementById("chGroup" + index);
		if(containingDiv)
		{
			var groupCheckBoxes = containingDiv.getElementsByTagName("INPUT");
			for(var xx=0; xx< groupCheckBoxes.length; xx++)
			{
				groupCheckBoxes[xx].checked=toggleValue;
			}
			selectGroups();
		}
   }

   function removeSelectAll(index)
   {
		var toggleAllCheckBox =  document.getElementById("toggleAll" + index);
		if(toggleAllCheckBox) toggleAllCheckBox.checked=false;
   }

