function toggleLabel(e) {

	// find the target
	var target = findTarget(e);
	
	// get some of the targets attributes
	var targetID = target.getAttribute('id');
	var targetName = target.getAttributeNode('name').value;
	var targetType = target.getAttributeNode('type').value;
	
	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.getAttributeNode('name').value;
			var inputID = inputElement.getAttributeNode('id').value;
			// 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;
		
		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/,'');
			}
				
			if (target.checked) {
				if (labelFor == targetID) {
					label.className += ' checked';
				}
			}
		}
	}
}

function setUpLabelHighlight() {
	
	// find all checkboxes
	var inputs = document.getElementsByTagName('input');
	
	for (var i = 0; i < inputs.length; i++) {
	
		inputElement = inputs[i];
		
		if (inputElement.getAttributeNode('type').value == 'checkbox' || inputElement.getAttributeNode('type').value == 'radio') {
		
			// attach onclick function
			addEvent(inputElement, 'click', toggleLabel, false);
			
			var targetID = inputElement.getAttributeNode('id').value;
			// find all labels
			var labels = document.getElementsByTagName('label');
			for (var x = 0; x < labels.length; x++) {
				label = labels[x];
				if (label.getAttributeNode('for').value == targetID) {
					if (inputElement.checked) {
						// add class
						label.className += ' checked';
					} else {
						// remove class
						label.className = label.className.replace(/checked/g,'');
					}
				}
			}
		}
	}
}

addLoadEvent(setUpLabelHighlight);