
/*

*/

function autoCompleteDB() {
	this.aNames = {};
	//this.aNames = this.aNames.sort();
}
autoCompleteDB.prototype.assignArray = function(aList) {
	this.aNames = eval('(' + aList + ')');
}
function autoComplete(urlReq, id, oText, aTitle) {
	this.urlReq = urlReq;
	this.id = id;
	this.oTextMin = oText;
	this.oTextID = document.getElementById(this.oTextMin + '_tid');
	this.oText = document.getElementById(this.oTextMin + '_inp');
	if (!this.oText || !this.oTextID) return;
	this.oDiv = document.getElementById(this.id);
	if (!this.oDiv) {
		this.oDiv = document.createElement('div');
		this.oDiv.className = 'suggestdd';
		var t = OTS.$('empty');
		if (!t) return;
		t.appendChild(this.oDiv);
	}
	if (!this.oDiv) return;
	this.oDiv.id = this.id;
	this.maxSize = 10;
	this.cur = -1;
	this.aTitle = aTitle;
	this.aNamesL = '';
	this.xtras = 0;
	
	this.oText.onkeyup = this.keyUp;
	this.oText.onkeydown = this.keyDown;
	this.oText.autoComplete = this;
	this.oText.onblur = this.hideSuggest;
}
autoComplete.prototype.loadRequest = function(url) {
	if (window.XMLHttpRequest) {
		this.req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		this.req = new ActiveXObject('Microsoft.XMLHTTP');
	}
	if (this.req) {
		var _this = this;
		this.req.onreadystatechange = function() {
			if (_this.req.readyState == 4) {
				if (_this.req.status == 200) {
					_this.aNamesL = _this.req.responseText;
					_this.writeAutoComplete();
				} else {
					//alert('There was a problem retrieving your request. ' + this.req.statusText);
				}
			}
		};
		this.req.open('GET', url, true);
		this.req.send(null);
	} else {
		return;
	}
}
autoComplete.prototype.hideSuggest = function() {
	var ref = this.autoComplete || this;
	ref.oDiv.style.visibility = "hidden";
	/*
	if (typeof(ref.oDiv) != "undefined") {
		document.body.removeChild(ref.oDiv);
	}
	*/
}
autoComplete.prototype.selectText = function(iStart, iEnd) {
	/* For IE */
	if (this.oText.createTextRange) {
		var oRange = this.oText.createTextRange();
		oRange.moveStart("character", iStart);
		oRange.moveEnd("character", iEnd - this.oText.value.length);
		oRange.select();
	} else if (this.oText.setSelectionRange) {
		/* For Mozilla */
		this.oText.setSelectionRange(iStart, iEnd);
	}
	this.oText.focus();
}
autoComplete.prototype.textComplete = function(sFirstMatch) {
	if (this.oText.createTextRange || this.oText.setSelectionRange) {
		var iStart = this.oText.value.length;
		this.oText.value = sFirstMatch;
		this.selectText(iStart, sFirstMatch.length);
	}
}
autoComplete.prototype.moveDown = function() {
	if (this.oDiv.childNodes.length > 0) {
		if (this.cur < (this.oDiv.childNodes.length - (1 + this.xtras))) {
			++this.cur;
			this.move();
		}
	}
}
autoComplete.prototype.moveUp = function() {
	if (this.oDiv.childNodes.length > (0 + this.xtras)) {
		//if (this.cur > 0) {
		if (this.cur > -1) {
			--this.cur;
			this.move();
		}
	}
}
autoComplete.prototype.move = function() {
	for (var i = 0; i < (this.oDiv.childNodes.length - 0); ++i) {
		if (i == this.cur) {
			this.oDiv.childNodes[i].className += ' over';
			this.oText.value = this.oDiv.childNodes[i].s;
			//this.oText.value += ' #' + this.oDiv.childNodes[i].i;
			this.oTextID.value = this.oDiv.childNodes[i].i;
			this.showBtn(this.oTextMin);
		} else {
			if (this.cur < 0) {
				this.oText.value = this.toText;
				this.hideBtn(this.oTextMin);
			}
			this.oDiv.childNodes[i].className = this.oDiv.childNodes[i].className.replace('over', '');
		}
	}
}
autoComplete.prototype.keyDown = function(oEvent) {
	oEvent = window.event || oEvent;
	var iKeyCode = oEvent.keyCode;
	switch (iKeyCode) {
		//up arrow
		case 38:
			this.autoComplete.moveUp();
		break;
		//down arrow
		case 40:
			this.autoComplete.moveDown();
		break;
		//return key
		case 13:
			window.focus();
		break;
	}
}
autoComplete.prototype.keyUp = function(oEvent) {
	oEvent = oEvent || window.event;
	var iKeyCode = oEvent.keyCode;
	if (iKeyCode == 8 || iKeyCode == 46) {
		/* without autocomplete */
		this.autoComplete.onTextChange(false);
	} else if (iKeyCode < 32 || (iKeyCode >=  33 && iKeyCode <=  46) || (iKeyCode >=  112 && iKeyCode <=  123)) {
		//ignore
	} else {
		/* with autocomplete */
		this.autoComplete.onTextChange(true);
	}
}
/* to calculate the appropriate poistion of the dropdown */
autoComplete.prototype.positionSuggest = function() {
	var oNode = this.oText;
	this.oDiv.style.top = (this.realOffset(oNode, 'offsetTop') + (oNode.offsetHeight - 0)) + 'px';
	this.oDiv.style.left = (this.realOffset(oNode, 'offsetLeft') + 1) + 'px';
	//this.oDiv.style.width = (oNode.offsetWidth - 2) + 'px';
}
autoComplete.prototype.realOffset = function(el, offsetType) {
	var offset = 0;
	while(el) {
		offset += el[offsetType]; 
		el = el.offsetParent;
	}
	return offset;
}
autoComplete.prototype.onTextChange = function(bTextComplete) {
	this.toText = this.oText.value;
	this.bTextComplete = bTextComplete;
	if (this.toText.length > 0) {
		if (this.toText.length < 3) {
			this.oDiv.innerHTML = "Loading...";
			this.loadRequest(this.urlReq + this.toText);
		} else {
			this.writeAutoComplete();
		}
	} else {
		this.oDiv.innerHTML = "";
		//this.oDiv.style.visibility = "hidden";
		this.hideSuggest();
	}
}
autoComplete.prototype.writeAutoComplete = function() {
	this.oDiv.innerHTML = "";
	
	var oThis = this;
	this.cur = -1;
	
	if (this.aNamesL == "undefined" || this.aNamesL == '') {
		return;
	}
	
	this.db = new autoCompleteDB();
	this.db.assignArray(this.aNamesL);
	
	var aStr = new Array();
	this.db['getMatches' + this.aTitle](this.toText, aStr, this.maxSize);
	if (aStr.length == 0) {
		//this.oDiv.style.visibility = "hidden";
		this.hideSuggest();
		return;
	}
	if (this.bTextComplete) {
		//this.textComplete(aStr[0].client_name);
	}
	
	for (var i = 0; i < aStr.length; ++i) {
		var oNew = document.createElement('div');
		this.oDiv.appendChild(oNew);
		
		this['format' + this.aTitle](this.toText, aStr[i]);
		
		oNew.t = this.html_t;
		oNew.s = this.html_s;
		oNew.i = this.html_i;
		oNew.onmouseover = oNew.onmouseout = oNew.onmousedown = function(oEvent) {
			oEvent = oEvent || window.event;
			oSrcDiv = oEvent.target || oEvent.srcElement;
			//debug : window.status = oEvent.type;
			if (oEvent.type == "mousedown") {
				oThis.oText.value = this.s;
				//oThis.oText.value += ' #' + this.i;
				oThis.oTextID.value = this.i;
				oThis.showBtn(oThis.oTextMin);
			} else if(oEvent.type == "mouseover") {
				this.className = "over";
			} else if(oEvent.type == "mouseout") {
				this.className = "";
			} else {
				this.oText.focus();
			}
		};
		oNew.innerHTML = oNew.t;
	}
	this['formatXtras' + this.aTitle]();
	this.positionSuggest();
	this.oDiv.style.visibility = "visible";
}

