// Set all input boxes inside strId to on
function selectAll(strId) {
	toggleInputs(strId, true);
}

// Set all input boxes inside strId to off
function selectNone(strId) {
	toggleInputs(strId, false);
}

// Toggle the state of all input boxes inside strId. If blnCheck is true they 
// are set to on, if it is false they are set to off
function toggleInputs(strId, blnCheck) {
	// objContainer is an object that contains all the inputs we wish to
	// toggle
	var objContainer = document.getElementById(strId);
	if (objContainer) {
		objContainer = objContainer.parentNode;
	}
	
	if (objContainer) {
		var aryInputs = objContainer.getElementsByTagName("input");
		var intInputCount;
		for (intInputCount = 0; intInputCount < aryInputs.length; ++intInputCount) {
			aryInputs[intInputCount].checked = blnCheck;
		}
	}
}

// Create a "link button" called name that runs the given function fncPointer
function createButton(strName, fncPointer) {
	var objLink = document.createElement('span');
	objLink.className = 'toggle_link';
	objLink.onclick = fncPointer;
	objLink.appendChild(document.createTextNode(strName));

	return objLink
}

// Attach toggle "links" using the DOM to the object given by strId
function addToggles(strId){
	if (strId == 'projects_group_name_1') return false;

	if (document.getElementById) {
		var objAppend = document.getElementById(strId);
		
		// objToggles holds all of the toggling links and decorations
		var objToggles = document.createElement('span');
		objToggles.className = 'toggles';
		objToggles.appendChild(document.createTextNode(' (Select: '));
		
		objToggles.appendChild(
			createButton('All', function () { selectAll(strId) }));
		
		objToggles.appendChild(document.createTextNode(', '));
		
		objToggles.appendChild(
			createButton('None', function () { selectNone(strId) } ));
		
		objToggles.appendChild(document.createTextNode(')'));
		
		// If we find a strong element, then position the buttons
		// directly after the first one rather than at the very end
		var objBefore = null;
		var aryStrong = objAppend.getElementsByTagName("strong");
		if (aryStrong.length > 0) {
			objBefore = aryStrong[0].nextSibling;
		}
		objAppend.insertBefore(objToggles, objBefore);
	}
}

// Add toggle links to all the elements we are interested in
function findGroups(strId) {
		if (document.getElementById) {
			var objGroupsParent = document.getElementById(strId);
			
			if (objGroupsParent) {
					var arySubHeadings = objGroupsParent.getElementsByTagName("strong");
					var aryGroupHeadings = objGroupsParent.getElementsByTagName("h3");
					var intHeadingsCount;
					var intHeadingListCount;
					
					// Collect to gether arrays containing all the elements we are interested in
					var nlstHeadings = [aryGroupHeadings, arySubHeadings];
					for (intHeadingListCount = 0; intHeadingListCount < nlstHeadings.length; ++intHeadingListCount) {
						var nlstHeading = nlstHeadings[intHeadingListCount];
						for (intHeadingsCount = 0; intHeadingsCount < nlstHeading.length; ++intHeadingsCount) {
							addToggles(nlstHeading[intHeadingsCount].id);
						}
					}

			}
		}
}