/**
 * @author buenger
 */
function TextSelect (a_divId, a_formElementName) {
	
	this.id;
	this.name = "TextSelect";
	this.type = "FormElement"
	
	var div;
	var formElement;
	var label;
	var formElementName;
	var _isRequired;
	var _isCheckable = true;
	
	var _self = this;
	
	this.check = function() {
		return test();
	}
	
	this.value = function () {
		return formElement.selectedIndex;
	}
	
	this.setValue = function (a_value) {
		formElement.selectedIndex = a_value;
	}
	
	this.setForcedValue = function (a_value) {
		formElement.value = a_value;
	}
	
	this.formValue = function () {
		return formElement.options[formElement.selectedIndex].value;
	}
	
	this.textValue = function () {
		return formElement.options[formElement.selectedIndex].innerHTML;
	}
	
	this.setFormValue = function (a_value) {
		var i;
		$A(formElement.options).each (function (a_item, a_index){
			if (a_item.value != a_value) return;
			i = a_index;
		});
		_self.setValue(i);
	}
	
	this.replaceOptions = function(a_optionAry, a_selectedValue) {
		$A(formElement.options).each (function (a_item, a_index) {
			if (a_index != 0 && a_item.value != "") formElement.removeChild(a_item);
		});
		$A(a_optionAry).each (function (a_item, a_index) {
			formElement.appendChild(a_item);
		});
		var nr = a_selectedValue != undefined ? a_selectedValue: 0
		_self.setValue(nr);
	}
	
	this.getCheckable = function () {
		return _isCheckable;
	}
		
	this.setCheckable = function(a_isCheckable) {
		_isCheckable = a_isCheckable;
		if (!_isCheckable) _self.unerror();
	}
	
	this.error = function() {
		var i, option;
		formElement.style.backgroundColor = "#F1E322";		
		for (i = 0; i < formElement.options.length; i++) {
			option = formElement.options[i];
			option.style.backgroundColor = "#F1E322";
		}
		formElement.addEvent("change", this.unerror);
	}
	
	this.unerror = function() {
		var i, option;
		formElement.style.backgroundColor = "";		
		for (i = 0; i < formElement.options.length; i++) {
			option = formElement.options[i];
			option.style.backgroundColor = "";
		}
		formElement.removeEvent("change", this.unerror);
	}
	
	this.remove = function () {
		div.parentNode.removeChild(div);
	}
	
	this.addDependency = function (a_id, a_valueFnc, a_compareFnc, a_handleFnc) {
		var el = Form.idMap[a_id];
		el.addEvent("propChange", function () {
			var value = el[a_valueFnc]();
			var compareResult = CompareFunctions[a_compareFnc](value);
			_self[a_handleFnc](compareResult);
		});
	}
	
	function test () {		
		var ctrl = (_self.value() != 0 && formElement.options[_self.value()].value != "");
		return (isRequired()) ? ctrl: true;
	}
	
	function init (a_divId, a_formElementName) {
		_self.id = a_divId;
		formElementName = a_formElementName;
		window.addEvent("load", handleWindowLoad);
	}
	
	function isRequired  () {
		return _isRequired;
	}
	
	this.getLabel = function() {
		return (label !== null) ? label.innerHTML.replace(/\*/g,"") : false;
	}
	
	function handleWindowLoad () {
		window.removeEvent("load", handleWindowLoad);
		
		div = $(_self.id);
		label = div.getElements("label")[0];
		formElement = $$('select[name='+_self.id+']')[0];
		formElement.addEvent("change", handleValueChange);
		_isRequired = (div.getProperty("required") != null) ? true: false;
	}
	
	function handleValueChange () {
		_self.fireEvent("propChange");
	}
	
	//MOOTOOLS EVENTS
	$extend(this, new Events());
	
	init (a_divId, a_formElementName);
}
