// Al Khalel Le Beau
// http://www.actiononline.biz/web/the-power-of-css-and-javascript-in-the-palm-of-your-hands/
//global variables that can be used by ALL the function son this page.

var inputass;
var imgFalse = site_host + '/modules/prenumerata/js/' + 'chk-false.jpg';
var imgTrue =  site_host + '/modules/prenumerata/js/' + 'chk-true.jpg';
var imgFalses = site_host + '/modules/prenumerata/js/' + 'chk-falses.jpg';
var imgTrues =  site_host + '/modules/prenumerata/js/' + 'chk-trues.jpg';

//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
	inputass = document.getElementsByTagName('input');

	//cycle trough the input fields
	for(var i=0; i < inputass.length; i++) {

		//check if the input is a checkbox
		if(inputass[i].className == 'checkbox') {
			
			//create a new image
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(inputass[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+')');
			//place image in front of the checkbox
			inputass[i].parentNode.insertBefore(img, inputass[i]);
			
			//hide the checkbox
			inputass[i].style.display='none';
			
		}else if(inputass[i].className == 'check') {
			
			//create a new image
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(inputass[i].checked) {
				img.src = imgTrues;
			} else {
				img.src = imgFalses;
			}

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

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

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

	if(inputass[i].checked) {
		inputass[i].checked = '';
		document.getElementById('checkImage'+i).src=imgFalses;
	} else {
		inputass[i].checked = 'checked';
		document.getElementById('checkImage'+i).src=imgTrues;
	}
}
window.onload = init;