//global variables that can be used by ALL the function son this page.
var IJNchecks = {
	checkboxes: {},
	imgFalse: 'view/images/false',
	imgTrue: 'view/images/true',
	imgType: '.png',
	postfix: '',
	setImagesPostfix: function(postfix) {
		this.postfix = postfix;
	},
	getImagename: function(trueFalse) {
		var imgsrc;
		if (trueFalse) {
			imgsrc = this.imgTrue + this.postfix + this.imgType;
		} else {
			imgsrc = this.imgFalse + this.postfix + this.imgType;
		}
		return imgsrc;
	},
	init: function() {
		// init
		IJNchecks.checkboxes = {};
		//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' && inputs[i].style.display!='none') {
				//alert("checkbox name="+inputs[i].name);
				//create a new image
				var img = document.createElement('img');
				//check if the checkbox is checked
				if(inputs[i].checked) {
					img.src = gMainskinweb+IJNchecks.getImagename(true);
				} else {
					img.src = gMainskinweb+IJNchecks.getImagename(false);
				}
				//set image ID and onclick action
				img.id = 'checkImage'+i;
				//set image
				img.onclick = new Function('IJNchecks.checkChange('+i+')');
				//place image in front of the checkbox
				inputs[i].parentNode.insertBefore(img, inputs[i]);
				//hide the checkbox
				inputs[i].style.display='none';
				IJNchecks.checkboxes[i] = inputs[i];
			}
		}
	},
	checkChange: function(i) {
		var name=IJNchecks.checkboxes[i].name,checked=false,otherchecked=false,cnt=0;
		// check first other existing checkboxes from this name
		for (var j in IJNchecks.checkboxes) {
			if (IJNchecks.checkboxes[j].name==name) {
				if (j==i) checked = IJNchecks.checkboxes[j].checked;
				if (j!=i && IJNchecks.checkboxes[j].checked) otherchecked = IJNchecks.checkboxes[j].checked;
				IJNchecks.checkboxes[j].checked = false;
				document.getElementById('checkImage'+j).src=gMainskinweb+IJNchecks.getImagename(false);
				cnt += 1;
			}
		}
		if (cnt!=1 && !otherchecked) {
			// uncheck op enige aangeklikt niet toelaten
			checked = !checked;
		}
		if(!checked) {
			IJNchecks.checkboxes[i].checked = true;
			document.getElementById('checkImage'+i).src=gMainskinweb+IJNchecks.getImagename(true);
			// user trigger set -> if yes then call with checkbox input htmlelement
			if (gUserCheckTrigger!=null) gUserCheckTrigger(IJNchecks.checkboxes[i]);
		} else {
			// uncheck (single option)
			// user trigger set -> if yes then call with checkbox input htmlelement
			if (gUserCheckTrigger!=null) gUserCheckTrigger(IJNchecks.checkboxes[i]);
		}
	}
}
