/* Obtained from http://www.brainerror.net/scripts_js_checkbox2.php */
/* License Text: 

Scripts shown here can be copied, used and altered as you please. However, I don't like other people taking credit for my work. It really pisses me off. I'm a real bad ass person, once I feel pissed off. So here are the rules:

   1. Don't distribute the scripts publicly, I am already doing that for you.
   2. Don't claim that they're your own, because they're not (unless you altered them a lot). That means when you get money for it, I have done work for you and you have to give all your money to me.

*/

/* Note that this has been heavily modified to support radio lists, 
   to allow for toggling of all checkboxes simultaneously, and the use 
   of alternate elements instead of images

   Changes (c) 2007 Solomon Peachy */

//global variables that can be used by ALL the function son this page.
var inputs;
var imgFalse = 'themes/aqua/check.gif';
var imgTrue = 'themes/aqua/check.a.gif';
var imgUnsel = 'themes/aqua/button.option.gif';
var imgSel = 'themes/aqua/button.option.a.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') {
	// IE needs className, FF needs class
			if (!/slide_cb/i.exec(inputs[i].getAttribute('class')) && 
			    (!/slide_cb/i.exec(inputs[i].getAttribute('className')))) {
				//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 
				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';
		} else 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 = imgSel;
			} else {
				img.src = imgUnsel;
			}

			//set image ID and onclick action
			img.id = 'SelImage'+i;
			//set image 
			img.onclick = new Function('checkChangeSel('+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;
	}
}

//change the checkbox status and the replacement image
function checkChangeSlide(i) {
	j = document.getElementById('cb' + i);
	k = document.getElementById('slide_cb' + i);

	if(j.checked) {
		j.checked = '';
		k.style.backgroundColor = '';
	} else {
		j.checked = 'checked';
		k.style.backgroundColor = '#cceecc';
	}
}

//change the checkbox status and the replacement image
function checkChangeSel(j) {
	for(var i=0; i < inputs.length; i++) {	
		if(inputs[i].getAttribute("name") == inputs[j].getAttribute("name")) {	
			if (i == j) {
				inputs[i].checked = 'checked';
				document.getElementById('SelImage'+i).src=imgSel;
			} else {
				inputs[i].checked = '';
				document.getElementById('SelImage'+i).src=imgUnsel;
			}
		}
	}
}

window.onload = init;

// merging in our select javascript..
var checked = '';
function toggleChecksSlide() {

	if (checked != '') {
		checked = '';
	} else {
		checked = 'checked';
	}

	for(var i=0; i < inputs.length; i++) {	
		if(inputs[i].getAttribute('type') != 'checkbox') {
			continue;
		}
		inputs[i].checked = checked;
		k = document.getElementById('slide_' + inputs[i].id);
		if (checked) {	
			k.style.backgroundColor = '#cceecc';
		} else {
			k.style.backgroundColor = '';
		}
	}
}

function toggleChecksList() {

	if (checked != '') {
		checked = '';
	} else {
		checked = 'checked';
	}

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


function changeText(name, uncheckedText, checkedText) {
  if (checked != "") {
    document.getElementById(name).innerHTML = checkedText;
    return;
  }

  document.getElementById(name).innerHTML = uncheckedText;
}


