function toggleLabel(e) {

	// find the target
	var target = findTarget(e);
	
	// get some of the targets attributes
	var targetID = target.getAttribute('id');
	var targetName = target.getAttribute('name');
	var targetType = target.getAttribute('type');
	
	if (targetType == 'radio' && targetName != '') {
	
		// build up array of related radios
		var relatedRadioIDs = new Array();
		var inputs = document.getElementsByTagName('input');
		var arrayCount = 0;
			
		// loop through all the inputs
		for (var j = 0; j < inputs.length; j++) {
			var inputElement = inputs[j];
			var inputName = inputElement.getAttribute('name');
			var inputID = inputElement.getAttribute('id');
			// if the name matches the targetName (i.e. a grouping of radios), store it's ID in the relatedRadioIDs array
			if (inputName == targetName) {
				relatedRadioIDs[arrayCount] = inputID;
				arrayCount++;
			}
		}
		
	}
	
	// find all labels
	var labels = document.getElementsByTagName('label');
	// loop through all label elements
	for (var i = 0; i < labels.length; i++) {
		var label = labels[i];
		//var labelFor = label.getAttribute('for');
		var labelFor = label.htmlFor;
		var labelID = label.id;
		
		if (targetType == 'checkbox') {
			if (labelFor == targetID) {
				if (target.checked) {
					// add class
					label.className += ' checked';
				} else {
					// remove the class from the label
					label.className = label.className.replace(/\b ?checked\b/,'');
				}
			}
		}
		if (targetType == 'radio') {
		
			// remove class from all related Radios
			if (in_array(labelFor, relatedRadioIDs)) {
				// remove the class from the label
				label.className = label.className.replace(/\b ?checked\b/,'');
				
				// change label ID - add '_checked' to name
				if (labelID != '') {
					labelID = labelID.replace(/_checked/,'');
					label.id = labelID;
				}
			}
				
			if (target.checked) {
			
				if (labelFor == targetID) {
					label.className += ' checked';
					
					// change label ID - add '_checked' to name
					if (labelID != '') {
						labelID += '_checked';
						label.id = labelID;
					}
				}
			}
		}
	}
}

function setUpLabelHighlight() {
	// find all checkboxes
	var inputs = document.getElementsByTagName('input');
	for (var i = 0; i < inputs.length; i++) {
		inputElement = inputs[i];
		if (inputElement.getAttribute('type') == 'checkbox' || inputElement.getAttribute('type') == 'radio') {
			// attach onclick function
			addEvent(inputElement, 'click', toggleLabel, false);
			
			var targetID = inputElement.getAttribute('id');
			// find all labels
			var labels = document.getElementsByTagName('label');
			for (var x = 0; x < labels.length; x++) {
				label = labels[x];
				if (label.getAttribute('for') == targetID) {
				
					var labelID = label.id;
					
					if (inputElement.checked) {
						// add class
						label.className += ' checked';
						// change label ID - add '_checked' to name
						if (labelID != '') {
							labelID += '_checked';
							label.id = labelID;
						}
						
					} else {
						// remove class
						label.className = label.className.replace(/checked/g,'');
						// change label ID - removed '_checked' from string
						if (labelID != '') {
							labelID = labelID.replace(/_checked/,'');
							label.id = labelID;
						}
					}
				}
			}
		}
	}
}

addLoadEvent(setUpLabelHighlight);