/********
ABOUT:
	Event Selection Toggle
	version 0.1
	last rev: 14.11.2006

AUTHOR:
	gijtenbeek@terena.org

TODO:
	-- Error handling
********/
var toggler = Class.create();
toggler.prototype = {
	checkboxes: '',
	checkstate: '',
	total: '',
	count: 0,

	initialize: function() {
		this.checkboxes = Form.getInputs($('frmexport'), 'checkbox');
		this.total = this.checkboxes.length;
		this.addListeners();
	},

	addListeners: function() {
		Event.observe('toggler_button', 'click', this.toggle.bindAsEventListener(this));
		Event.observe('frmexport', 'click', this.stateCheck.bindAsEventListener(this));
	},

	countChecked: function() {
		count = 0;
		for (i=0; i < this.total; i++) {
			if (this.checkboxes[i].checked == true) {
		   	count++;
		  }
		}
		return count;
	},

	toggle: function() {
		for (i=0; i < this.checkboxes.length; i++) {
		   this.checkboxes[i].checked = this.checkstate;
		}
		this.checkstate = (this.checkstate == 'checked') ? '': 'checked';
	},

	stateCheck: function() {
		$('toggler_msg_container').style.display = 'block';
		if (this.countChecked() != 0 && this.countChecked() != this.total) {
			return this.msg(this.countChecked());
		}
		if ((this.countChecked() == 0) || (this.checkstate == 'checked')) {
			$('toggler_msg_container').style.display = 'none';
		}
		if ((this.countChecked() == this.total) || (this.checkstate == '')) {
			this.msg('all');
		}
	},

	msg: function(msg) {
		$('toggler_msg').innerHTML = msg;
	}
};


var et;
window.onload = function() {
	et = new toggler;
}