// 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) {
		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';
	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 (document.getElementById) {
		var objAppend = document.getElementById(strId);
		
		// objToggles holds all of the toggling links and decorations
		var objToggles = document.createElement('span');
		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);
	}
}
