
var chkboxjs_inputs;
var imgFalse = '';
var imgTrue = '';
var imgClicking = '';
var radioGroups = new Array();

function replaceChecks() {
    
    chkboxjs_inputs = document.getElementsByTagName('input');

    for(var i=0; i < chkboxjs_inputs.length; i++) {

        if((chkboxjs_inputs[i].getAttribute('type') == 'checkbox' || chkboxjs_inputs[i].getAttribute('type') == 'radio')
        	&& chkboxjs_inputs[i].className != 'noreplace') {
            
            var img = document.createElement('img');
            var imga = document.createElement('a');
            
            if(chkboxjs_inputs[i].checked) {
                img.src = imgTrue;
            } else {
                img.src = imgFalse;
            }
            
            var update_group = '';
			if (chkboxjs_inputs[i].getAttribute('type') == 'radio') 
				update_group = " updateGroup('" + chkboxjs_inputs[i].getAttribute('name') + "');"

            img.id = 'checkImage'+i;
            imga.href = '#';
            imga.style.outline = 'none';
            imga.onmousedown = new Function('checkMouseDown('+i+');');
            imga.onmouseout = new Function('checkMouseOut('+i+');');
            imga.onmouseup = new Function('checkChange('+i+');'+update_group);
			imga.onclick = new Function('return false;');
			
			if (chkboxjs_inputs[i].getAttribute('type') == 'radio')
			{
				if (radioGroups[chkboxjs_inputs[i].getAttribute('name')] == undefined) 
					radioGroups[chkboxjs_inputs[i].getAttribute('name')] = new Array();
					
				radioGroups[chkboxjs_inputs[i].getAttribute('name')][i] = i;
			}

			imga.appendChild(img);
            chkboxjs_inputs[i].parentNode.insertBefore(imga, chkboxjs_inputs[i]);
	        chkboxjs_inputs[i].onchange = new Function('updateCheck('+i+');'+update_group);
            chkboxjs_inputs[i].style.display='none';
        }
    }
}

function checkChange(i) {

    if(chkboxjs_inputs[i].checked) {
        chkboxjs_inputs[i].checked = '';
        document.getElementById('checkImage'+i).src=imgFalse;
    } else {
        chkboxjs_inputs[i].checked = 'checked';
        document.getElementById('checkImage'+i).src=imgTrue;
    }
}
function updateCheck(i)
{
	if(chkboxjs_inputs[i].checked) {
        document.getElementById('checkImage'+i).src=imgTrue;
    } else {
        document.getElementById('checkImage'+i).src=imgFalse;
    }
}
function updateGroup(name)
{
	var i;
	for (i in radioGroups[name])
	{
		if(chkboxjs_inputs[i].checked) {
	        document.getElementById('checkImage'+radioGroups[name][i]).src=imgTrue;
	    } else {
	        document.getElementById('checkImage'+radioGroups[name][i]).src=imgFalse;
	    }
    }
}
function checkMouseDown(i)
{
	document.getElementById('checkImage'+i).src=imgClicking;
}
function checkMouseOut(i)
{
	if (document.getElementById('checkImage'+i).src==imgClicking) 
		updateCheck(i);
}
