// CAPXOUSOS JavaScript Library (Version 1.1.6 r20060818235118)	
var CAPXOUSOS=new Object();

CAPXOUSOS.COSSelect = Class.create();

CAPXOUSOS.COSSelect.prototype = {

	initialize: function(select) {
		this.select = $(select) ? $(select) : document.getElementsByName(select)[0];;
	},
	
	add: function(option) {
		this.select.options[this.select.options.length] = option;
	},
	
	remove: function(option) {
		for (var i = 0; i < this.select.options.length; i++) {
			var o = this.select.options[i];
			if ((o.text == option.text) && (o.value == option.value)) {
				this.select.remove(i);				
			}
		}
	},
	
	getElement: function() {
		return this.select;	
	},
	
	get: function(i) {
		return this.select.options[i];
	},
	
	getDisabled: function(i) {
		return this.select.options[i].disabled;
	},
	getLength: function(i) {
		return this.select.options.length;
	},
	
	getCOSSelected: function() {
		if (this.select.selectedIndex >= 0) {
			return this.select.options[this.select.selectedIndex];
		} else {
			return false;
		}
	},
	
	getAllCOSSelected: function() {
		return $A(this.select.options).findAll(function(option) {
			return option.selected;
		});
	},
	
	selectAll: function() {
		$A(this.select.options).each(function(option) {
			option.selected = true;
		});
	},
	
	clear: function() {
		this.select.options.length = 0;
	},
	
	disable: function() {
		this.select.disabled = true;
	},
	
	enable: function() {
		this.select.disabled = false;
	}	
};

CAPXOUSOS.DualList = Class.create();

CAPXOUSOS.DualList.prototype = {

	initialize: function(name) {
		this.name = name;		
		this.choosenList = $(name) ? $(name) : document.getElementsByName(name)[0];
				
		//this.availableList = $(this.getName('available'));
		
		//this.choseAllBtn = $(this.getName('choseAll'));
		//this.choseBtn = $(this.getName('chose'));
		this.removeAllBtn = $(this.getName('removeAll'));
		this.removeBtn = $(this.getName('remove'));				

		//this.availableList.multiple = this.choosenList.multiple = true;
		this.choosenList.multiple = true;
		
		//Event.observe(this.availableList, 'mouseup', this.onChange.bind(this));
		Event.observe(this.choosenList,   'mouseup', this.onChange.bind(this));		
		//Event.observe(this.availableList, 'click', this.onChange.bind(this));
		Event.observe(this.choosenList,   'click', this.onChange.bind(this));
		
		//this.availableList = new CAPXOUSOS.COSSelect(this.availableList);
		this.choosenList = new CAPXOUSOS.COSSelect(this.choosenList);

		//if (this.choseAllBtn)	Event.observe(this.choseAllBtn,  'click', this.choseAll.bind(this));
		//if (this.choseBtn)		Event.observe(this.choseBtn, 	 'click', this.choseCOSSelected.bind(this));	
		if (this.removeAllBtn)	Event.observe(this.removeAllBtn, 'click', this.removeAll.bind(this));
		if (this.removeBtn)	Event.observe(this.removeBtn, 	 'click', this.removeCOSSelected.bind(this));			
		
		$A(document.getElementsByTagName('form')).each(function(form) {
			Event.observe(form, 'submit', this.onSubmit.bind(this), true);
		}.bind(this));		
	},
	
	getName: function(prefix) {
		var name = this.name.charAt(0).toUpperCase() + this.name.substr(1);
		return (arguments.length == 0) ?  name : prefix + name;
	},	
	
	onSubmit: function() {
		this.choosenList.selectAll();
	},
	
	onChange: function() {
		setTimeout(this.updateButtons.bind(this), 10);
	},
	
	add: function(option) {
		//this.availableList.add(option);
		this.updateButtons();
	},
	
	updateButtons: function() {
		//if (this.choseAllBtn) 	this.choseAllBtn.disabled = !this.availableList.getLength();
		if (this.removeAllBtn) 	this.removeAllBtn.disabled = !this.choosenList.getLength();
		//if (this.choseBtn) 		this.choseBtn.disabled = !this.availableList.getAllCOSSelected().length;
		if (this.removeBtn) 	this.removeBtn.disabled = !this.choosenList.getAllCOSSelected().length;		
	},
	
	remove: function(option) {
		this.choosenList.remove(option);
		//this.availableList.add(option);		
		this.updateButtons();	
	},
	
	chose: function(option) {
		//this.availableList.remove(option);
		this.choosenList.add(option);
		this.updateButtons();	
	},
	
	removeAll: function() {
		var _length=this.choosenList.getLength();
		for (var i=0;i<_length;i++) {
			if (!this.choosenList.getDisabled(i)){
				this.remove(this.choosenList.get(i));
				--i;
				_length=this.choosenList.getLength();
			}
		}
	},
	
	removeCOSSelected: function() {
		var selected = this.choosenList.getAllCOSSelected();
		selected.each(function(option) {
			this.remove(option);
		}.bind(this));
	}/*,

	choseAll: function() {
		while (this.availableList.getLength()) {
			this.chose(this.availableList.get(0));
		}
	},
	
	choseCOSSelected: function() {
		var selected = this.availableList.getAllCOSSelected();
		selected.each(function(option) {
				this.chose(option);
		}.bind(this));
	}*/

};

CAPXOUSOS.COSOptions = Class.create();

CAPXOUSOS.COSOptions.prototype = {
	
	noneText: "NONE",
	
	initialize: function() {
		this.x = this.y = 0;
	
		this.args = arguments;
		
		for (var i = 0; i < this.args.length; i++) {
			var select = $(this.args[i]) ? $(this.args[i]) : document.getElementsByName(this.args[i])[0];
			select.onchange = this.onChange.bind(this, i);
			this.args[i] = new CAPXOUSOS.COSSelect(select);			
		}
		
		if (this.args.length < 1) return;
		
		this.optionsList = new Array();
		this.parentsList = new Array();
	},
	
	getEmpty: function() {
		var o = new Option(this.noneText);
		o.disabled = true; 
		return o;	
	},

	setEmptyText: function(text) {
		this.noneText = text;
	},
	
	getCurrentCOSSelect: function() {
		return this.args[this.x];
	},
		
	addOption: function(option) {
		if (arguments.length == 2) option = new Option(arguments[0], arguments[1]);
		if (!this.x) this.getCurrentCOSSelect().add(option);
		
		this.optionsList[this.optionsList.length] = [option];
		this.parentsList[this.x] = option;
		this.addRelation(option);
		this.onChange(0);
	},
	
	addChildOption: function(option) {
		this.next();
		this.addOption(arguments[0], arguments[1]);
		this.back();
	},
	
	addRelation: function(option) {
		if (this.x) {	
			var options = this.getChildCOSOptions(this.parentsList[this.x - 1]);
			if (options) options.push(option);				
		}	
	},
	
	next: function() {
		this.x++;
	},
	
	back: function() {
		this.x--;
	},

	getChildCOSOptions: function(option) {
		for (var i = 0; i < this.optionsList.length; i++) {
			var options = this.optionsList[i];
			var first = options.first();
			if (first == option) return options;
		}
		return false;
	},
	
	onChange: function(x) {
		if (x < this.args.length) {
		
			var childCOSOptions = this.getChildCOSOptions(this.args[x].getCOSSelected());
			
			if ((x + 1) < this.args.length)	this.updateCOSSelect(this.args[x + 1], childCOSOptions);
						
			for (var i = x + 2; i < this.args.length; i++) {
				var select = this.args[i];
				select.clear();
				select.disable();
			} 
		};
	},
		
	updateCOSSelect: function(select, options) {
		select.clear();
		select.enable();
		if (options.length > 1) {
			options.without(options.first()).each(function(option) {
				select.add(option);
			});
		} else if (options.length == 1) {
			select.add(this.getEmpty());
		}
	}
};
