input = {
		init: function() {
							arrLabels = document.getElementsByTagName('label');
							searchLabels: for(var i=0; i<arrLabels.length; i++) {			
											// get the input element based on the for attribute of the label tag
											if(
													arrLabels[i].getAttributeNode('for') && 
													arrLabels[i].getAttributeNode('for').value != ''
												) {
													labelElementFor = arrLabels[i].getAttributeNode('for').value;	
													inputElement = document.getElementById(labelElementFor);
												}else{				
													continue searchLabels;
												}	
											
												inputElementClass = inputElement.className;	
								
											// if the input is specified to be hidden intiate it
											if(inputElementClass == 'hideInputNoActive'){
												inputElement.className = 'hideInputActive';
												inputElementType = inputElement.getAttributeNode('type').value;	
												// add the appropriate event listener to the input element
												if(inputElementType == 'checkbox'){
													inputElement.onclick = input.toggleCheckboxLabel;
												}
												if(inputElementType == 'radio'){
													inputElement.onclick = input.toggleRadioLabel;
												}
												// set the initial label state
												if(inputElement.checked) {
													if(inputElementType == 'checkbox'){ 
														arrLabels[i].className = 'checkedCheckbox'
													}
													if(inputElementType == 'radio'){ 
														arrLabels[i].className = 'checkedRadio' 
													}
												}else{
													if(inputElementType == 'checkbox'){ 
														arrLabels[i].className = 'noCheckedCheckbox'
													}
													if(inputElementType == 'radio'){ 
														arrLabels[i].className = 'noCheckedRadio'
													}
												}
											}else if(
														inputElement.nodeName != 'SELECT' && 
														inputElement.getAttributeNode('type').value == 'radio'
													){
													// this so even if a radio is not hidden but belongs to a group of hidden radios it will still work.
														arrLabels[i].onclick = input.toggleRadioLabel;
														inputElement.onclick = input.toggleRadioLabel;
													}
							}
		},	
	
		findLabel: function (inputElementID) {
			arrLabels = document.getElementsByTagName('label');
			searchLoop: for (var i=0; i<arrLabels.length; i++) {
							if(
									arrLabels[i].getAttributeNode('for') && 
									arrLabels[i].getAttributeNode('for').value == inputElementID
								){	
								return arrLabels[i];
								break searchLoop;				
							}
						}		
		},	
	
		toggleCheckboxLabel: function () {
			labelElement = input.findLabel(this.getAttributeNode('id').value);
			if(labelElement.className == 'checkedCheckbox') {
				labelElement.className = "noCheckedCheckbox";
			}
			else{
				labelElement.className = "checkedCheckbox";
			}
		},	
	
		toggleRadioLabel: function () {			 
			clickedLabelElement = input.findLabel(this.getAttributeNode('id').value);
			clickedInputElement = this;
			clickedInputElementName = clickedInputElement.getAttributeNode('name').value;
			arrInputs = document.getElementsByTagName('input');
	
			// uncheck (label class) all radios in the same group
			for(var i=0; i<arrInputs.length; i++) {			
				inputElementType = arrInputs[i].getAttributeNode('type').value;
				if(inputElementType == 'radio') {
					inputElementName = arrInputs[i].getAttributeNode('name').value;
					inputElementClass = arrInputs[i].className;
					// find radio buttons with the same 'name' as the one we've changed and have a class of chkHidden
					// and then set them to unchecked
					if(
						inputElementName == clickedInputElementName && 
						inputElementClass == 'hideInputActive'
						){				
						inputElementID = arrInputs[i].getAttributeNode('id').value;
						labelElement = input.findLabel(inputElementID);
						labelElement.className = 'noCheckedRadio';
					}
				}
			}
			// if the radio clicked is hidden set the label to checked
			if (clickedInputElement.className == 'hideInputActive') {
				clickedLabelElement.className = 'checkedRadio';
			}
		},
	
		addEvent: function(element, eventType, doFunction, useCapture){
			if(element.addEventListener){
				element.addEventListener(eventType, doFunction, useCapture);
				return true;
			}else if(element.attachEvent){
				var r = element.attachEvent('on' + eventType, doFunction);
				return r;
			}else{
				element['on' + eventType] = doFunction;
			}
		}
}

input.addEvent(window, 'load', input.init, false);
