/***************************************************************************
* Input Auto Complete
* Copyright 2002 by David Schontzler | www.stilleye.com
* Free to use under GNU General Public License as long as this message
*  remains intact.
* Description:  Automatically fills in text inputs with a predefined value set
*  Script only for Win/IE
* URI: http://www.stilleye.com/projects/dhtml/autocomplete/
***************************************************************************
* Version: 1.2.1
* - Setup to automatically work with Youngpup's Listener script (not required)
***************************************************************************/
AutoComplete = function(oFormElm, aValues) {
	if(oFormElm && (oFormElm.tagName.toUpperCase() == "INPUT" && oFormElm.type.toUpperCase() == "TEXT" || oFormElm.tagName.toUpperCase() == "TEXTAREA") && oFormElm.createTextRange) {
		this.o = oFormElm;
		this.o.AUTOCOMPLETE = "off"; // for IE
		if(window.Listener && typeof Listener.add == "function") {
			Listener.add(this.o, "onkeyup", this.CheckValue, this.o, false);
			Listener.add(this.o, "onkeydown", this.NullSubmit, this.o, false);
			Listener.add(this.o, "onblur", this.IsAutoValue, this.o, false);
		} else {
			this.o.onkeyup = this.CheckValue;
			this.o.onkeydown = this.NullSubmit;
			this.o.onblur = this.IsAutoValue;
		}
		this.o._autocomplete = this;
		this._usingAutoValue = false;
		if(aValues && aValues.length > 0 && aValues.concat)
			this.AddValues(aValues);
	}
	return this;
}

AutoComplete.prototype = {
	list : [],
	
	AddValues : function(aValues) {
		this.list = this.list.concat(aValues);
		this.list.sort();
		while(this.list.length > 0 && this.list[0] == "") this.list.slice();
	},
	
	CheckValue : function(e) {
		var key = this._autocomplete.GetKey(e);
		this._autocomplete._checkValue(key);
		return false;
	},
	
	_checkValue : function(key) {
		var val = this.o.value.toLowerCase(), len = val.length, matchNum = -1;
		var textarea = this.o.tagName.toUpperCase() == "TEXTAREA";
		if(len > 0  && key.type != "COMMAND") {
			var i = 0;
			while(i < this.list.length && matchNum < 0) {
				if(this.list[i] && this.list[i].substring(0, len).toLowerCase() == val)
					this.matchNum = matchNum = i;
				i++;
			}
			if(key.type == "CHAR") {
				if(matchNum >= 0) {
					var addedVal = this.list[matchNum].substring(len);
					if(addedVal.length) {
						this.o.value += addedVal;
						var range = this.o.createTextRange();
						range.findText(this.o.value.substring(len), this.o.value.length, 4);
						if(textarea) range.moveStart("character", len);
						range.select();
						range = null;
						this._usingAutoValue = true;
					}
				}
				else this._usingAutoComplete = false;
			} else if(key.type == "ENTER") {
				if(matchNum > -1) {
					this.o.value = this.list[matchNum];
					this._usingAutoValue = true;
					var range = this.o.createTextRange();
					range.collapse(false);
					range.select();
					range = null;
				} else this._usingAutoValue = false;
			} else if(key.type == "BACKSPACE") {
				this._usingAutoValue = false;
			} else
				this._usingAutoValue = false;
		}
	},
	
	NullSubmit : function(e) {
		var key = this._autocomplete.GetKey(e), proceed = true;
		if(key.type == "ENTER") {
			proceed = this.onkeyup();
			if(this.tagName.toUpperCase() != "TEXTREA") event.returnValue = false;
		}
		return proceed;
	},
	
	GetKey : function(e) {
		if(!e) e = event;
		var c = e.keyCode, key;
		if(e.altKey || e.ctrlKey || c == 45 || (c >= 33 && c <= 36) ) key = "COMMAND";
		else if(c == 8 || c == 46) key = "BACKSPACE";
		else if(c == 13) key = "ENTER";
		else key = "CHAR";
		return { code : c, type : key };
	},
	
	IsAutoValue : function() {
		this._autocomplete._isAutoValue();
	},
	
	_isAutoValue : function() {
		if(this._usingAutoValue) this.o.value = this.list[this.matchNum];
	}
}
