/**
 * @alias ComboBox.class
 * @author WilC <wilz04@gmail.com>
 * @since 2008
 */

function ComboBox(id) {
	
	var canvas = document.getElementById(id);
	
	this.addItem = function (option) {
		canvas.options[this.getItemCount()] = option;
	};
	
	this.getItemAt = function (index) {
		return canvas.options[index];
	};
	
	this.addItems = function (options) {
		var index;
		var option;
		for (index=0; index<options.length; index++) {
			option = new Option(options[index]);
			option.value = options[index];
			this.addItem(option);
		}
	};
	
	this.setSelectedIndex = function (index) {
		var result = true;
		if (index < 0) {
			index = 0;
			result = false;
		}
		var length = this.getItemCount();
		if (index >= length) {
			index = length - 1;
			result = false;
		}
		canvas.selectedIndex = index;
		return result;
	};
	
	this.getSelectedIndex = function () {
		var index = canvas.selectedIndex;
		if (index != -1) {
			return index;
		}
		return null;
	};
	
	this.setSelectedItem = function (item) {
		var length = this.getItemCount();
		for (var index=0; index<length; index++) {
			if (this.getItemAt(index).text == item.text || this.getItemAt(index).value == item.value) {
				this.setSelectedIndex(index);
				return true;
			}
		}
		return false;
	};
	
	this.getValue = function () {
		if (this.getItemCount() > 0) {
			return canvas.options[canvas.selectedIndex].value;
		}
		return null;
	};
	
	this.setFocus = function () {
		canvas.focus();
	};
	
	this.getItemCount = function () {
		return canvas.options.length;
	};
	
	this.removeItemAt = function (index) {
		var option = canvas.options[index];
		canvas.options[index] = null;
		return option;
	};
	
	this.removeAllItems = function () {
		var items = new Array();
		for (var index=canvas.options.length-1; index>=0; index--) {
			items.push(canvas.options[index]);
			canvas.options[index] = null;
		}
		return items;
	};
	
	this.onChange = function (action) {
		canvas.onchange = action;
	};
	
}
