//global variables that can be used by ALL the function son this page.
var inputs,radioInputs;
var imgFalse = 'images/inputs/check-box-unchecked.jpg';
var imgTrue = 'images/inputs/check-box-checked.jpg';
var radioImgFalse = 'images/inputs/radio-button-unchecked.jpg';
var radioImgTrue = 'images/inputs/radio-button-checked.jpg';

//this function runs when the page is loaded, put all your other onload stuff in here too.
function initCheckboxes() {
	replaceChecks();
}

function replaceChecks() {
	
	//get all the input fields on the page
	inputs = document.getElementsByTagName('input');

	//cycle trough the input fields
	for(var i=0; i < inputs.length; i++) {
		//check if the input is a checkbox
		if(inputs[i].getAttribute('type') == 'checkbox') {
			
			//create a new image
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(inputs[i].checked) {
				img.src = imgTrue;
			} else {
				img.src = imgFalse;
				
			}

			//set image ID and onclick action
			img.id = 'checkImage'+i;
			//set image 
			//img.onclick = new Function('checkChange('+i+');');
			img.onclick = new Function('showServiceDesc(\''+i+'\');');
			//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			
			//hide the checkbox
			inputs[i].style.display='none';
		}
	}
}

//change the checkbox status and the replacement image
function checkChange(i) {

	if(inputs[i].checked) {
		inputs[i].checked = '';
		document.getElementById('checkImage'+i).src=imgFalse;
	} else {
		inputs[i].checked = 'checked';
		document.getElementById('checkImage'+i).src=imgTrue;
	}
}


function initRadios() {
	replaceRadios();
}

function replaceRadios() {
	
	//get all the input fields on the page
	radioInputs = document.getElementsByTagName('input');

	//cycle trough the input fields
	for(var i=0; i < radioInputs.length; i++) {
		//check if the input is a checkbox
		if(radioInputs[i].getAttribute('type') == 'radio') {
			
			//create a new image
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(radioInputs[i].checked) {
				img.src = radioImgTrue;
			} else {
				img.src = radioImgFalse;
				
			}

			//set image ID and onclick action
			img.id = 'radioImage'+i;
			//set image 
			img.onclick = new Function('checkChangeRadio('+i+')');
			//place image in front of the checkbox
			radioInputs[i].parentNode.insertBefore(img, radioInputs[i]);
			
			//hide the checkbox
			radioInputs[i].style.display='none';
		}
	}
}

/* Change the checkbox status and the replacement image. 
   Called by clicking on the staff name. */
function radioChange(i) {
	var k = 0;
	for(var j=0; j<radioInputs.length; j++){
		if (radioInputs[j].getAttribute('type') == "radio"){			
			if (i == j-k) {
				radioInputs[j].checked = 'checked';
				document.getElementById('radioImage'+j).src=radioImgTrue;
			}
			else {
				radioInputs[j].checked="";	
				document.getElementById('radioImage'+j).src=radioImgFalse;
			}
		}
		else
			k++;
	}
}

/* Change the checkbox status and the replacement image.
   Called by clicking on the radio button. */
function checkChangeRadio(i) {
	for(var j=0; j<radioInputs.length; j++){
		if (radioInputs[j].getAttribute('type') == "radio"){
				radioInputs[j].checked="";
				document.getElementById('radioImage'+j).src=radioImgFalse;
		}
	}
	if(radioInputs[i].checked) {
		radioInputs[i].checked = '';
		document.getElementById('radioImage'+i).src=radioImgFalse;
	} else {
		radioInputs[i].checked = 'checked';
		document.getElementById('radioImage'+i).src=radioImgTrue;
	}
}
