/*
 * Select KWJAX.
 */
function KWJAXSelect(pElement) {

	// Elemento
	this.lElement = new KWJAXUtil().getElement(pElement);

	// Verifica o elemento
	if (!this.lElement) {
		alert('Elemento "' + pElement + '" inválido');
		return false;
	} else {

		// Verifica o tipo do elemento
		if (new String(typeof(this.lElement)).toLowerCase() != 'object') {
			alert('O elemento "' + pElement + '" não é um objeto');
			return false;
		} else {

			// Verifica a tag do elemento
			if (new String(this.lElement.tagName).toLowerCase() != 'select') {
				alert('O elemento "' + pElement + '" não é um SELECT');
				return false;
			}
		}
	}

	/*
	 * Método de obtenção do elemento subjacente.
	 */
	this.getUnderlyingElement = function() {
		return this.lElement;
	}

	/*
	 * Obtém contagem de elementos.
	 */
	this.getItemCount = function() {
		return this.lElement.options.length;
	}

	/*
	 * Obtém o texto de um índice.
	 */
	this.getTextByIndex = function(pIndex) {
		if ((pIndex >= 0) && (this.lElement.options.length > pIndex)) {
			return this.lElement.options[pIndex].text;
		}
		return null;
	}

	/*
	 * Obtém o valor de um índice.
	 */
	this.getValueByIndex = function(pIndex) {
		if ((pIndex >= 0) && (this.lElement.options.length > pIndex)) {
			return this.lElement.options[pIndex].value;
		}
		return null;
	}

	/*
	 * Obtém índice por texto.
	 */
	this.getIndexByText = function(pText) {
		for (var i = 0; i < this.lElement.options.length; i++) {
			if (this.lElement.options[i].text == pText) {
				return i;
			}

		}
		return null;
	}

	/*
	 * Adiciona um item ao elemento.
	 */
	this.addItem = function(pName, pValue, pIndex) {
		try {
			this.lElement.add(new Option(pName, pValue), (pIndex != null ? this.lElement.options[pIndex] : null));
		} catch(e) {
			if (pIndex != null) {
				this.lElement.add(new Option(pName, pValue), pIndex);
			} else {
				this.lElement.add(new Option(pName, pValue));
			}
		}
	}

	/*
	 * Remove todos os itens do elemento.
	 */
	this.cmdRemoveAll = function() {
		for (var i = this.lElement.options.length - 1; i >= 0; i--) {
			this.lElement.remove(i);
		}
	}

	/*
	 * Obtém o valor da opção selecionada.
	 */
	this.getSelectedValue = function() {
		if ((this.lElement.selectedIndex >= 0) && (this.lElement.options.length > this.lElement.selectedIndex)) {
			return this.lElement.options[this.lElement.selectedIndex].value;
		}
		return null;
	}

	/*
	 * Obtém os valores das opções selecionadas.
	 */
	this.getSelectedValues = function() {
		var lSelectedValues = new Array();
		var lIndice = 0;
		for (var i = 0; i < this.lElement.options.length; i++) {
			if (this.lElement.options[i].selected) {
				lSelectedValues[lIndice++] = this.lElement.options[i].value;
			}
		}
		return lSelectedValues;
	}

	/*
	 * Obtém o primeiro índice por valor.
	 */
	this.getFirstIndexByValue = function(pValue) {
		for (var i = 0; i < this.lElement.options.length; i++) {
			if (this.lElement.options[i].value == pValue) {
				return i;
			}
		}
		return null;
	}

	/*
	 * Adiciona opção selecionada por valor.
	 */
	this.addSelectedByValue = function(pValue) {
		for (var i = 0; i < this.lElement.options.length; i++) {
			if (this.lElement.options[i].value == pValue) {
				this.lElement.options[i].selected = true;
			}
		}
	}

	/*
	 * Atribui a opção selecionada por valor.
	 */
	this.setSelectedByValue = function(pValue) {
		for (var i = 0; i < this.lElement.options.length; i++) {
			this.lElement.options[i].selected = (this.lElement.options[i].value == pValue);
		}
	}

	/*
	 * Obtém o primeiro índice por texto.
	 */
	this.getFirstIndexByText = function(pText) {
		for (var i = 0; i < this.lElement.options.length; i++) {
			if (this.lElement.options[i].text == pText) {
				return i;
			}
		}
		return null;
	}

	/*
	 * Adiciona opção selecionada por texto.
	 */
	this.addSelectedByText = function(pText) {
		for (var i = 0; i < this.lElement.options.length; i++) {
			if (this.lElement.options[i].text == pText) {
				this.lElement.options[i].selected = true;
			}
		}
	}

	/*
	 * Atribui a opção selecionada por texto.
	 */
	this.setSelectedByText = function(pText) {
		for (var i = 0; i < this.lElement.options.length; i++) {
			this.lElement.options[i].selected = (this.lElement.options[i].text == pText);
		}
	}

	/*
	 * Obtém o primeiro índice por busca.
	 */
	this.getFirstIndexBySearch = function(pSearch) {
		pSearch = new String(pSearch).toLowerCase();
		for (var i = 0; i < this.lElement.options.length; i++) {
			if (new String(this.lElement.options[i].text).toLowerCase().indexOf(pSearch) > -1) {
				return i;
			}
		}
		return null;
	}

	/*
	 * Obtém índices por busca.
	 */
	this.getIndexesBySearch = function(pSearch) {
		pSearch = new String(pSearch).toLowerCase();
		var lIndexes = new Array();
		var lIndice = 0;
		for (var i = 0; i < this.lElement.options.length; i++) {
			if (new String(this.lElement.options[i].text).toLowerCase().indexOf(pSearch) > -1) {
				lIndexes[lIndice++] = i;
			}

		}
		return lIndexes;
	}

	/*
	 * Adiciona opção selecionada por busca.
	 */
	this.addSelectedBySearch = function(pSearch) {
		pSearch = new String(pSearch).toLowerCase();
		for (var i = 0; i < this.lElement.options.length; i++) {
			if (new String(this.lElement.options[i].text).toLowerCase().indexOf(pSearch) > -1) {
				this.lElement.options[i].selected = true;
			}
		}
	}

	/*
	 * Atribui a opção selecionada por busca.
	 */
	this.setSelectedBySearch = function(pSearch) {
		pSearch = new String(pSearch).toLowerCase();
		for (var i = 0; i < this.lElement.options.length; i++) {
			this.lElement.options[i].selected = (new String(this.lElement.options[i].text).toLowerCase().indexOf(pSearch) > -1);
		}
	}

	/*
	 * Remove itens do elemento por valor.
	 */
	this.cmdRemoveByValue = function(pValue) {
		for (var i = 0; i < this.lElement.options.length; i++) {
			if (this.lElement.options[i].value == pValue) {
				this.lElement.remove(i);
			}
		}
	}

	/*
	 * Obtém o índice da opção selecionada.
	 */
	this.getSelectedIndex = function() {
		return this.lElement.selectedIndex;
	}

	/*
	 * Obtém os índices das opções selecionadas.
	 */
	this.getSelectedIndexes = function() {
		var lSelectedIndexes = new Array();
		var lIndice = 0;
		for (var i = 0; i < this.lElement.options.length; i++) {
			if (this.lElement.options[i].selected) {
				lSelectedIndexes[lIndice++] = i;
			}
		}
		return lSelectedIndexes;
	}

	/*
	 * Adiciona opção selecionada por índice.
	 */
	this.addSelectedByIndex = function(pIndex) {
		if (this.lElement.options.length > pIndex) {
			this.lElement.options[pIndex].selected = true;
		}
	}

	/*
	 * Atribui a opção selecionada por índice.
	 */
	this.setSelectedByIndex = function(pIndex) {
		if (this.lElement.options.length > pIndex) {
			this.lElement.selectedIndex = pIndex;
		}
	}

	/*
	 * Remove itens do elemento por índice.
	 */
	this.cmdRemoveByIndex = function(pIndex) {
		this.lElement.remove(pIndex);
	}

	/*
	 * Limpa seleção.
	 */
	this.cmdSelectNone = function() {
		for (var i = 0; i < this.lElement.options.length; i++) {
			this.lElement.options[i].selected = false;
		}
	}

	/*
	 * Seleciona todos.
	 */
	this.cmdSelectAll = function() {
		for (var i = 0; i < this.lElement.options.length; i++) {
			this.lElement.options[i].selected = true;
		}
	}

	/*
	 * Atribui foco ao elemento.
	 */
	this.setFocus = function() {
		this.lElement.focus();
	}

	/*
	 * Obtém ativação do elemento.
	 */
	this.getEnabled = function() {
		return !this.lElement.disabled;
	}

	/*
	 * Atribui ativação do elemento.
	 */
	this.setEnabled = function(pEnabled) {
		this.lElement.disabled = !(pEnabled ? true : false);
	}

	/*
	 * Obtém a visibilidade do elemento.
	 */
	this.getVisible = function() {
		return (this.lElement.style.display != 'none');
	}

	/*
	 * Atribui a visibilidade do elemento.
	 */
	this.setVisible = function(pVisible) {
		this.lElement.style.display = (pVisible ? '' : 'none');
	}
}
