//global variables that can be used by ALL the function son this page.
var inputs;
var imgFalse = 'images/false.png';
var imgTrue = 'images/true.png';
var imgInactive = 'images/inactive.png';

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;
			}

			//set image ID and onclick action
			img.id = 'checkImage'+i;
			//set image 			

			if(inputs[i].onclick) {
				img.onclick = inputs[i].onclick;
				img.onmousedown = new Function('checkChange('+i+');');
				}
			//
			
			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';
		}
	}
}

//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 replaceRadios() {
	
	//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') == 'radio') {
			
			//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].disabled){
				img.src = imgInactive;
			}
			else{
				//set image ID and onclick action
				img.id = 'checkImage'+i;			
				//set image 			
				if(inputs[i].onclick) {
					img.onclick = inputs[i].onclick;
					img.onmousedown = new Function('radioChange('+i+');');
					}
				//			
				else img.onclick = new Function('radioChange('+i+');');
			}
			//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);

			// special for inactivating nucleotide radio button
			if (inputs[i].id =='radioNuc'){
				document.nucButId = img.id;
				var img2 = document.createElement('img');
				img2.id = 'radioNucInactive';
				img2.src = imgInactive;
				img2.onclick = new Function('window.alert(\'You cannot select nucleotide for this sequence\')');
				img2.style.display = 'none';
				inputs[i].parentNode.insertBefore(img2, inputs[i]);
			}
			if (inputs[i].id =='radioPep'){
				document.pepInpId = i;
			}
			//
			// special for blast results jalview buttons

			if (inputs[i].id =='JVpro'){
				document.JVproButId = img.id;
				var img2 = document.createElement('img');
				img2.id = 'JVproInactive';
				img2.src = imgInactive;
				img2.onclick = new Function('window.alert(\'You cannot select this option if including the query sequence\')');
				img2.style.display = 'none';
				inputs[i].parentNode.insertBefore(img2, inputs[i]);
			}
			if (inputs[i].id =='JVcod'){
				document.JVcodButId = img.id;
				var img2 = document.createElement('img');
				img2.id = 'JVcodInactive';
				img2.src = imgInactive;
				img2.onclick = new Function('window.alert(\'You cannot select this option if including the query sequence\')');
				img2.style.display = 'none';
				inputs[i].parentNode.insertBefore(img2, inputs[i]);
			}
			if (inputs[i].id =='JVcon'){
				document.JVconButId = img.id;
				var img2 = document.createElement('img');
				img2.id = 'JVconInactive';
				img2.src = imgInactive;
				img2.onclick = new Function('window.alert(\'You cannot select this option if including the query sequence\')');
				img2.style.display = 'none';
				inputs[i].parentNode.insertBefore(img2, inputs[i]);
			}




			
			//hide the checkbox
			inputs[i].style.display='none';
		}
	}
}

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

	if(!inputs[i].checked) {
		for(var j=0; j < inputs.length; j++) {
			if(inputs[j].getAttribute('type') == 'radio') {
				if (inputs[j].name == inputs[i].name){
					inputs[j].checked == '';
					document.getElementById('checkImage'+j).src=imgFalse;
				}
			}
		}
		inputs[i].checked = 'checked';
		document.getElementById('checkImage'+i).src=imgTrue;
	}
}



