/**
 * 功能：类似于java中的StringBuffer类的功能，用于字符串的拼接
 *       这种方式比直接用"+"性能要高出很多
 */
package("com.henry.ui");
com.henry.ui.Field = function(name, label, type, text, value) {
	var self = null;
	var _name = null;
	var _label = null;
	var _type = type;
	var _dataType = null;
	var _errorMessage = null;
	var _required = null;
	var _fieldElement = null;
			
	var Field = classBuilder.create(this.constructor);

	with(Field) {
		prototype.initialize = function() {
			self = this;
			_name = name;
			/*
			if (!(label instanceof com.henry.ui.SPAN)) {			
				label = new com.henry.ui.SPAN(label.toString());
			}
			*/
			
			_label = label;
			_type = type;
		}
		
		prototype.setName = function(theName) {
			_name = theName;
			if (_fieldElement) {
				_fieldElement.setId(_name);
			}
		}
		prototype.getName = function() {
			return _name;
		}
		
		prototype.setFieldLabel = function(theLabel) {
			/*
			if (!(theLabel instanceof com.henry.ui.SPAN)) {
				theLabel = new com.henry.ui.SPAN(theLabel.toString());
			}
			*/
			_label = theLabel;
		}
		prototype.getFieldLabel = function() {
			return _label;
		}

		prototype.setType = function(theType) {
			if (_type != theType) {
				_type = theType;
				_fieldElement = null;
			}
		}
		prototype.getType = function() {
			return _type;
		}

		prototype.getFieldElement = function() {
			if (_fieldElement) {
				return _fieldElement;
			}

			if (_type == "text") {
				_fieldElement = new com.henry.ui.Text(_name, text);
			}
			else if (_type == "select") {
				_fieldElement = new com.henry.ui.SELECT(_name, text, value);
			}
			else if (_type == "checkbox") {
				var keyedTable = new com.henry.ui.KeyedTable();
				
				for (var i=0; i<text.length; i++) {
					var theElement = new com.henry.ui.Checkbox(_name, text[i], value)
					keyedTable.addComponent("item" + i, theElement);
				}
				keyedTable.endLine();
				
				return keyedTable;
			}
			else if (_type == "radio") {
				var keyedTable = new com.henry.ui.KeyedTable();
				
				for (var i=0; i<text.length; i++) {
					var theElement = new com.henry.ui.Radio(name, text[i], value)
					keyedTable.addComponent(item + i, theElement);
				}
				keyedTable.endLine();
				
				return keyedTable;
			}
			else if (_type == "raw") {
				return text;
			}
			
			if (_label instanceof Array) {
				_fieldElement.setAttribute("label", _label[0]);
			}
			else {
				_fieldElement.setAttribute("label", _label);
			}
			
			return _fieldElement;
		}


		prototype.setDataType = function(theDataType) {
			_dataType = theDataType;
		}
		prototype.getDataType = function() {
			return _dataType;
		}

		prototype.setErrorMessage = function(theErrorMessage) {
			_errorMessage = theErrorMessage;
		}
		prototype.getErrorMessage = function() {
			return _errorMessage;
		}

		prototype.setRequired = function(theRequired) {
			_required = theRequired;
		}
		prototype.isRequired = function() {
			return _required;
		}
		
	}
	return new Field();
}
