function Tags(container, formName){
	if (typeof container == 'string')
		var container = document.getElementById(container);
	var mainDiv = document.createElement('div');
	var statusDiv = document.createElement('div');
	var clearBothDiv = document.createElement('div');
	var self = this;
	
	mainDiv.className = 'tagTextboxMain';
	clearBothDiv.style.clear = 'both';
	clearBothDiv.style.fontSize = '1px';
	
	this.addClassName(container, 'tagTextbox');
	container.onclick = function(){
		if (self.limit != 0 && self.tags.length == self.limit) {}
		else self.inputElt.focus();
	}
	container.appendChild(mainDiv);
	
	statusDiv.className = 'status';
	
	this.container = container;
	this.mainDiv = mainDiv;
	this.statusDiv = statusDiv;
	this.limit = 0;
	this.labelLimit = false;
	
	this.formName = formName;
	this.tags = [];
	this.confirm = false;
	this.createInput();
	this.container.appendChild(statusDiv);
	this.container.appendChild(clearBothDiv);
}
Tags.prototype = {
	container: null,
	mainDiv: null,
	
	wordSpan: null,
	inputElt: null,

	limit: null,
	labelLimit: null,
	
	createInput: function() {
		try {
			var span = document.createElement('span');
			var input = document.createElement('input');
			var self = this;
			
			span.style.position = 'absolute';
			span.style.top = '-1000px';
			span.style.left = '-1000px';

			input.type = 'text';
			input.className = 'input';
			input.style.width = '20px';
			input.style.border = 'none';

			input.onfocus = function(){
				if (self.limit != 0 && self.tags.length == self.limit) {
					this.blur();
				}
				else if (self.search)
					self.search.focus();
			};
			input.onkeypress = function(event){
				if (getKeyCode(event) == 13 || getKeyCode(event) == 38) 
					return false;
				else {
					self.wordSpan.innerHTML = this.value;
					this.style.width = self.wordSpan.offsetWidth + 20 + 'px';
				}
			};
			input.onkeyup = function(event){
				var key = getKeyCode(event);
				// if quicksearch is enabled
				if (self.search) {
					self.search.inputElt.value = this.value;
					self.search.onKeyUp(key);
				}
				
				// if not backspace reset potential delete confirm
				if (key != 8) {
					var tag = self.getLastTag();
					if (tag)
						tag.deselect();
					self.confirm = false;
				}
				
				// enter
				if (key == 13 && this.value != '') {
					if (!self.search)
						self.addTag('', this.value);
					this.value = '';
					this.style.width = '20px';
				}
				// backspace
				else if (key == 8 && this.value == '') {
					var tag = self.getLastTag();
					if (tag) {
						if (self.confirm) {
							self.removeTag(tag);
							self.confirm = false;
						}
						else {
							self.confirm = true;
							tag.select();
						}
					}
				}
			};
			input.onblur = function(event){
				//this.value = '';
				
				var tag = self.getLastTag();
				if (tag)
					tag.deselect();
				self.confirm = false;
				
				if (self.search) {
					setTimeout(function(){
						//self.search.clearSearch();
					}, 200);
				}
			};
			this.mainDiv.appendChild(input);
			this.container.appendChild(span);
			this.wordSpan = span;
			this.inputElt = input;
		} 
		catch (e) {
			alert('Tags.createInput error: ' + e.message);
		}
	},
	
	setLabelLimit: function(value) {
		this.labelLimit = value;
	},
	
	setLimit: function(value) {
		this.limit = value;
	},

	limitReached: function() {
		this.addClassName(this.container, 'tagTextboxLimit');
	},
	
	limitNotReached: function() {
		this.removeClassName(this.container, 'tagTextboxLimit');
	},
	//QuickSearch properties
	search: null,
	searchDiv: null,

	addQuickSearch: function(addFunction) {
		var searchDiv = document.createElement('div');
		var search = new QuickSearch(searchDiv, '', addFunction);
		var self = this;
		
		search.setApproveFunction(function(id, name){
			self.addTag(id, name);
		});
		searchDiv.style.position = 'absolute';
		
		search.statusLink.onclick = function(){
			if (this.className == 'close')
				self.inputElt.value = '';
			search.statusClick(true);
		}
		
		this.search = search;
		this.container.appendChild(searchDiv);
		this.statusDiv.appendChild(search.statusLink);
	},
	
	getQuickSearch: function() {
		return this.search;
	},	
		
	//Tag properties
	tags: null,
	formName: null,
	confirm: null,

	_tagSelected: null,
	_tagsOnClick: null,
	_tagsOnClose: null,
	
	addTag: function(id, label, properties, otherData){
		try {
			if (this.limit != 0 && this.tags.length >= this.limit) {
				this.limitReached();
				return;
			}
			else {
				if (this.labelLimit)
					label = label.substring(0, this.labelLimit) + '...';
				var tag = new Tag(this.formName, id, label, properties, otherData, this);
					tag.disableSelect();
				this.tags[this.tags.length] = tag;
				if (this._tagsOnClick) 
					tag.onclick(this._tagsOnClick);
				this.mainDiv.insertBefore(tag.getContainer(), this.inputElt);
				if (this.limit != 0 && this.tags.length >= this.limit) {
					this.inputElt.blur();
					this.limitReached();
				}
			}
			this.inputElt.value = '';
		}
		catch (e) {
			alert('Tags.addTag error: ' + e.message);
		}
	},
	
	getLastTag: function() {
		if (this.tags.length > 0)
			return this.tags[this.tags.length - 1];
		else return false;
	},
	
	removeTag: function(tag) {
		if (tag)
			tag.destroy();
	},
	
	tagRemoved: function(tag) {
		var array  = [];
		var tags = this.tags;
		var j = 0;
		
		for (var i = 0; i < tags.length; i++) {
			if (tags[i] != tag) {
				array[j] = tags[i];
				j++;
			}
		}
		if (this._tagsOnClose)
			this._tagsOnClose(tag.id, tag.label, tag.otherData);
		this.tags = array;
		this.limitNotReached();
	},
	
	tagSelected: function(tag) {
		if (this._tagSelected == tag) 
			return;
		else if (this._tagSelected) {
			this._tagSelected.deselect();
		}
		this._tagSelected = tag;
	},
	
	tagsOnClick: function(name) {
		if (typeof(name) == 'function') {
			this._tagsOnClick = name;
		}
	},
	
	tagsOnClose: function(name) {
		if (typeof(name) == 'function') {
			this._tagsOnClose = name;
		}
	},
	
	//Helper functions
	addClassName: function(elt, className) {
		if (typeof elt == 'string')
			var elt = document.getElementById(elt);
		if (this.hasClassName(elt, className))
			return;
		else elt.className += ' ' + className;
	},

	removeClassName: function (elt, className) {
		if (typeof elt == 'string')
			var elt = document.getElementById(elt);
		if (this.hasClassName(elt, className)) {
			var classes = elt.className.split(' ');
			var tmp = '';
			for (var i = 0; i < classes.length; i++) {
				if (classes[i] != className) {
					tmp += classes[i];
					if (i + 1 < classes.length) tmp += ' ';
				}
			}
			elt.className = tmp;
		}
	},
	
	hasClassName: function(elt, className){
		if (elt.className.indexOf(className) > -1)
			return true;
		else return false;
	}
}