//global variables that can be used by ALL the function son this page.
var inputs;
var imgFalse = 'gif/vinkUit.gif';
var imgTrue = 'gif/vinkAan.gif';

var imgRadioAan = 'gif/RadioAan.gif';
var imgRadioUit = 'gif/RadioUit.gif';

//this function runs when the page is loaded, put all your other onload stuff in here too.
function init() {
	//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;
			}
			
			if (!inputs[i].id){
			    inputs[i].id = "generatedCheckBox" + i;
			}

			//set image ID and onclick action
//			img.id = 'checkImage'+i;
			
			img.id = 'img'+inputs[i].id;
			
			//set image 
			if ( inputs[i].onclick ){
				img.onclick = new Function('checkChange('+i+');document.getElementById("'+inputs[i].id+'").onclick();');
			} else {
				img.onclick = new Function('checkChange('+i+');');
			}
    		//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			
			//hide the checkbox
			inputs[i].style.display='none';
		}
		
		if(inputs[i].getAttribute('type') == 'radio'){
		    //create a new image
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(inputs[i].checked) {
				img.src = imgRadioAan;
			} else {
				img.src = imgRadioUit;
			}
			
			if (!inputs[i].id){			   
			   if (inputs[i].name){
			       var alleRadios = document.getElementsByName( inputs[i].name )
			       for (j=0;j<alleRadios.length;j++){
			        if (inputs[i]==alleRadios[j]){
			            inputs[i].id = "generatedRadio==" + inputs[i].name + "==" + j;
			        }
			       }
			   }else {
			     inputs[i].id = "generatedRadio" + i;
			   }
			}

			//set image ID and onclick action
			img.id = 'img'+inputs[i].id;
			//set image 
			if ( inputs[i].onclick ){			
				img.onclick = new Function('checkRadio('+i+');document.getElementById("'+inputs[i].id+'").onclick();');
			} else {
				img.onclick = new Function('checkRadio('+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('img'+inputs[i].id).src=imgFalse;
	} else {
		inputs[i].checked = 'checked';
		document.getElementById('img'+inputs[i].id).src=imgTrue;
	}
}

function checkRadio(i) {
	inputs = document.getElementsByTagName('input');
    var alleRadios = document.getElementsByName( inputs[i].name )
    
    for (j=0;j<alleRadios.length;j++){
        if (document.getElementById("imggeneratedRadio==" + inputs[i].name + "==" + j) ){
            document.getElementById("imggeneratedRadio==" + inputs[i].name + "==" + j).src=imgRadioUit;
        }        
    }
        
    inputs[i].checked = 'checked';
    document.getElementById('img'+inputs[i].id).src=imgRadioAan;
}

//window.onload = init;

