function NameSuggestion(varname, oTextbox, oProvider) {	
	this.varname = varname;
	this.cur = -1;
	this.layer = null;
	this.iframe = null;
	this.provider = oProvider;
	this.textbox = oTextbox;
	this.textbox = (!jsGetObject(this.textbox)) ? this.textbox : jsGetObject(this.textbox);
	this.table = '';
	this.table_fields = 'nombre';
	this.table_search_field = 'nombre';
	this.table_field_sep = '';
	this.hiddenObj = null;
	this.width = 200;
	this.min_chars = 3;
	this.timeout = null;
	
	this.names_txt = new Array();
	this.names_val = new Array();
	
	this.init();
}

NameSuggestion.prototype.autosuggest = function (aSuggestions, bTypeAhead) {
    if (aSuggestions.length > 0) {
        if (bTypeAhead) {
           this.typeAhead(aSuggestions[0]);
        }
		
        this.showSuggestions(aSuggestions);
    } else {
        this.hideSuggestions();
    }
};

NameSuggestion.prototype.createDropDown = function () {
	var oThis = this;
	
	this.layer = document.createElement("div");
	this.layer.className = "SuggestionContent";
	this.layer.style.visibility = "hidden";
	this.layer.style.width = this.width + 'px';
	
	this.layer.onmousedown = 
	this.layer.onmouseup = 
	this.layer.onmouseover = function (oEvent) {
		oEvent = oEvent || window.event;
		oTarget = oEvent.target || oEvent.srcElement;
	
		if (oEvent.type == "mousedown") {
			oThis.textbox.value = oTarget.childNodes[0].nodeValue;
			oThis.hiddenObj.value = oTarget.childNodes[1].nodeValue;
			oThis.hideSuggestions();
		} else if (oEvent.type == "mouseover") {
			oThis.highlightSuggestion(oTarget);
		} else {
			oThis.textbox.focus();
		}
	};
    
	window.onload = function() {
		document.body.appendChild(oThis.layer);
		
		/*if (_jslib_isIE) {
			oThis.iframe = document.createElement('iframe');
			oThis.iframe.style.position = 'absolute';
			oThis.iframe.style.filter = 'alpha(opacity=0)';
			oThis.iframe.style.zIndex = oThis.layer.zIndex - 10;
			
			document.body.insertBefore(oThis.iframe, oThis.layer);
		}*/
	}
};

NameSuggestion.prototype.getLeft = function () {
    var oNode = this.textbox;
    var iLeft = 0;
    
    while(oNode.tagName != "BODY") {
        iLeft += oNode.offsetLeft;
        oNode = oNode.offsetParent;        
    }
    
	if (!_jslib_isIE) iLeft += 16;
	
    return iLeft;
};

NameSuggestion.prototype.getTop = function () {
    var oNode = this.textbox;
    var iTop = 0;
    
    while(oNode.tagName != "BODY") {
        iTop += oNode.offsetTop;
        oNode = oNode.offsetParent;
    }
	
	if (!_jslib_isIE) iTop += 15;
    
    return iTop;
};

NameSuggestion.prototype.handleKeyDown = function(oEvent) {
	this.hiddenObj = (!jsGetObject(this.hiddenObj)) ? this.hiddenObj : jsGetObject(this.hiddenObj);
	
    switch(oEvent.keyCode) {
        case 38: //up arrow
            this.previousSuggestion();
            break;
        case 40: //down arrow 
            this.nextSuggestion();
            break;
        case 13: //enter
            this.hideSuggestions();
            break;
    }
};

NameSuggestion.prototype.requestSuggestions = function (bTypeAhead) {	
	var oThis = this;
	
	oThis.textbox.style.backgroundImage = 'url(' + __strIconsPath + 'blank.gif)';
	oThis.textbox.style.backgroundPosition = 'right';
	oThis.textbox.style.backgroundRepeat = 'no-repeat';
	
	if (this.textbox.value != '' && this.textbox.value.length >= this.min_chars) {
		clearTimeout(this.timeout);
		this.timeout = setTimeout(this.varname + '.name_search(' + bTypeAhead + ');', 500);
	} else {
		if (this.textbox.value == '') if (this.hiddenObj) this.hiddenObj.value = '';
		this.hideSuggestions();
	}
};


NameSuggestion.prototype.name_search = function (bTypeAhead) {
	var oThis = this;
	
	oThis.names_txt = new Array();
	oThis.names_val = new Array();

	var ajax = new SetAjax();
	ajax.FilePath = __strMainDir + "includes/procesos/process_call.php";
	ajax.Element = jsGetObject('sugg_temp');
	ajax.setVar = [["f", "name_suggestion"], ["tabla", this.table], ["s", this.textbox.value], 
	['search_field', this.table_search_field], ['fields', this.table_fields], ['sep', this.table_field_sep]];
	ajax.onLoading = function() {
		oThis.textbox.style.backgroundImage = 'url(' + __strIconsPath + 'indicator.gif)';
	}
	ajax.onCompletion = function() {
		oThis.textbox.style.backgroundImage = 'url(' + __strIconsPath + 'blank.gif)';
		var users = jsGetObject('sugg_temp').value.split('[|]');
		
		if (users.length > 1) {
			for (i = 0; i < users.length; i++) {
				users_s = users[i].split('[-]');
				if (users_s.length > 1) {
					oThis.names_val[i] = users_s[0];
					oThis.names_txt[i] = users_s[1];
				}
			}
			
			oThis.autosuggest(oThis.names_txt, bTypeAhead);
		}
	}
	
	ajax.Execute();
	delete ajax;
};

NameSuggestion.prototype.handleKeyUp = function (oEvent) {
    var iKeyCode = oEvent.keyCode;
	
    if (iKeyCode == 8 || iKeyCode == 46) {
        this.requestSuggestions(false);
    } else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        //ignore
    } else {
        this.requestSuggestions(false);
    }
};

NameSuggestion.prototype.hideSuggestions = function () {
    this.layer.style.visibility = "hidden";
	//if (_jslib_isIE) this.iframe.style.display = 'none';
};

NameSuggestion.prototype.highlightSuggestion = function(oSuggestionNode) {
    for (var i=0; i < this.layer.childNodes.length; i++) {
        var oNode = this.layer.childNodes[i];
        if (oNode == oSuggestionNode) {
            oNode.className = "SuggestionCurrent"
        } else if (oNode.className == "SuggestionCurrent") {
            oNode.className = "";
        }
    }
};

NameSuggestion.prototype.init = function() {
    var oThis = this;
	
    this.textbox.onkeyup = function (oEvent) {
        if (!oEvent) {
            oEvent = window.event;
        }    
        
        oThis.handleKeyUp(oEvent);
    };
    
    this.textbox.onkeydown = function (oEvent) {
        if (!oEvent) {
            oEvent = window.event;
        }
        
        oThis.handleKeyDown(oEvent);
    };
	
    this.textbox.onblur = function() {
        oThis.hideSuggestions();
    };
	
	this.textbox.onkeypress = function() {
		e = window.event;
		if (e.keyCode == 13) {
			return false;
		}
	};
    
    this.createDropDown();
};

NameSuggestion.prototype.nextSuggestion = function () {
    var cSuggestionNodes = this.layer.childNodes;
	
	if (cSuggestionNodes.length > 0 && this.cur < cSuggestionNodes.length-1) {
		var c = ++this.cur;
        var oNode = cSuggestionNodes[c];
        this.highlightSuggestion(oNode);
        this.textbox.value = oNode.firstChild.nodeValue;
		this.hiddenObj.value = this.names_val[c];
    }
};

NameSuggestion.prototype.previousSuggestion = function () {
    var cSuggestionNodes = this.layer.childNodes;
	
    if (cSuggestionNodes.length > 0 && this.cur > 0) {
        var c = --this.cur;
		var oNode = cSuggestionNodes[c];
        this.highlightSuggestion(oNode);
        this.textbox.value = oNode.firstChild.nodeValue;
		this.hiddenObj.value = this.names_val[c];
    }
};

NameSuggestion.prototype.selectRange = function (iStart, iLength) {
    if (this.textbox.createTextRange) {
        var oRange = this.textbox.createTextRange(); 
        oRange.moveStart("character", iStart); 
        oRange.moveEnd("character", iLength - this.textbox.value.length);      
        oRange.select();
    } else if (this.textbox.setSelectionRange) {
        this.textbox.setSelectionRange(iStart, iLength);
    }
	
    this.textbox.focus();      
}; 

NameSuggestion.prototype.showSuggestions = function (aSuggestions) {
    //var re = new RegExp('^(' + this.textbox.value + ')', "i");
	var oDiv = null;
    this.layer.innerHTML = "";
	var name = '';
	
	for (var i=0; i < aSuggestions.length; i++) {
        oDiv = document.createElement("div");
		oDiv.style.height = '16px';
		oDiv.style.paddingLeft = '3px';
		//name = aSuggestions[i].replace(re, '<strong>' + this.textbox.value + '</strong>');
		oDiv.appendChild(document.createTextNode(aSuggestions[i]));
		oDiv.appendChild(document.createTextNode(this.names_val[i]));
		this.layer.appendChild(oDiv);
    }
	
    this.layer.style.left = this.getLeft() + "px";
    this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px";
    this.layer.style.visibility = "visible";
	
	/*if (_jslib_isIE) {
		this.iframe.style.left = this.getLeft() + "px";
		this.iframe.style.top = (this.getTop()+this.textbox.offsetHeight) + "px";
		this.iframe.style.height = this.layer.offsetHeight + 'px';
		this.iframe.style.display = "block";
	}*/
};

NameSuggestion.prototype.typeAhead = function (sSuggestion) {
    if (this.textbox.createTextRange || this.textbox.setSelectionRange){
        var iLen = this.textbox.value.length; 
        this.textbox.value = sSuggestion; 
        this.selectRange(iLen, sSuggestion.length);
    }
};

document.write('<input type="hidden" name="sugg_temp" id="sugg_temp"/>');