var inputs;
var imgFalse = 'Common/Images/Publicimages/checkbox.gif';
var imgTrue = 'Common/Images/Publicimages/tick.gif';
var imgFalseDiv = 'Common/Images/Publicimages/checkboxDiv.gif';
var imgTrueDiv = 'Common/Images/Publicimages/tickDiv.gif';
var radioFalseDiv = 'Common/Images/Publicimages/radio_btn_off.gif';
var radioTrueDiv = 'Common/Images/Publicimages/radio_btn_on.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');
			img.id = 'checkImage'+i;
			img.width = '17';
			img.height = '17';
			if(inputs[i].getAttribute('id')=="newsletterCheck") {//check if the checkbox is checked
				if(inputs[i].checked) {
					img.src = imgTrueDiv;				
				} else {
					img.src = imgFalseDiv;
				}
				img.onclick = new Function('checkChangeDiv('+i+')');
			}else {//check if the checkbox is checked
				if(inputs[i].checked) {
					img.src = imgTrue;				
				} else {
					img.src = imgFalse;
				}
				img.onclick = new Function('checkChange('+i+')');
			}
			
			
			//set image ID and onclick action
			
			//set image 
			
			//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			
			//hide the checkbox
			inputs[i].style.display='none';
		}else if(inputs[i].getAttribute('type') == 'radio'){
			var img = document.createElement('img');
			img.id = 'checkImage'+i;
			img.width = '17';
			img.height = '17';
			if(inputs[i].checked) {
				img.src = radioTrueDiv;				
			} else {
				img.src = radioFalseDiv;
			}
			img.onclick = new Function('checkRadioDiv('+i+')');

			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 checkChangeDiv(i) {
	if(inputs[i].checked) {
		inputs[i].checked = '';
		document.getElementById('checkImage'+i).src=imgFalseDiv;
	} else {
		inputs[i].checked = 'checked';
		document.getElementById('checkImage'+i).src=imgTrueDiv;
	}
}
function checkRadioDiv(i) {
	//alert(inputs[i].value);
	inputs[i].checked = 'checked';
	document.getElementById('checkImage'+i).src=radioTrueDiv;
	if(inputs[i].value==1) {
		var next = parseInt(i+1);
		document.getElementById('checkImage'+next).src=radioFalseDiv;
	} else {
		var prev = parseInt(i-1);
		document.getElementById('checkImage'+prev).src=radioFalseDiv;
	}
}
window.onload = init;
