FindNew = function(elementId, selectUrl, tekCtrlId, textFields, isSelectDefaultValue) {
    //alert(elementId+" "+selectUrl+" "+tekCtrlId+" "+textFields+" "+isSelectDefaultValue);
    this.element = null;
    this.text = null;
    this.value = null;

    this.elementId = elementId;
    this.selectUrl = selectUrl;
    this.tekCtrlId = tekCtrlId;
    this.textFields = textFields;
    this.isSelectDefaultValue = isSelectDefaultValue;

    this.virtualsVariableName = "virtuals" + this.elementId.replace(/-/g, ""); /* TODO REMOVE replace (see Ref field) */
    
    // Avoid re-creation:
    if (document.getElementById(this.GetListId()) == null) {
        var textBoxCtrl = document.getElementById(this.elementId + "_text");
        var eListSelect = document.createElement("SELECT");
        eListSelect.id = this.GetListId();
        eListSelect.style.position = "absolute";
        eListSelect.style.height = "150px";
        eListSelect.style.width = textBoxCtrl.style.width;
        eListSelect.style.marginTop = "-1px";
        eListSelect.style.zIndex = "2";
        eListSelect.style.visibility = "hidden";

        eListSelect["INITIAL-WIDTH"] = textBoxCtrl.style.width.substring(0, textBoxCtrl.style.width.indexOf("px"));
        eListSelect.tabIndex = "-1";
        eListSelect.multiple = true;

        eListSelect.findLeftOffset = FindNew.findLeftOffset;
        eListSelect.findTopOffset = FindNew.findTopOffset;

        try {
            document.body.insertAdjacentElement("AfterBegin", eListSelect);
            //alert("insert "+eListSelect.id);
        }
        catch (x) {
            document.body.appendChild(eListSelect);
            //alert("append "+eListSelect.id);
        }

        W3C.EventTarget.addEventListener(eListSelect, "click", FindNew.onListClick, false);
        W3C.EventTarget.addEventListener(eListSelect, "losecapture", FindNew.onListLoseCapture, false);
        W3C.EventTarget.addEventListener(eListSelect, "keyup", FindNew.onListKeyUp, false);
        W3C.EventTarget.addEventListener(eListSelect, "keydown", FindNew.onListKeyDown, false);
        W3C.EventTarget.addEventListener(eListSelect, "mouseout", FindNew.onListMouseOut, false);
    }
}

FindNew.prototype = new Control();

FindNew.prototype.GetListId = function ()
{
	return this.elementId + "_selectResult";
}

FindNew.findLeftOffset = function (elem)
{
	var left = 0;
	while (elem != null && elem.tagName != "BODY") {
		left += elem.offsetLeft - elem.scrollLeft;
		elem = elem.offsetParent;
	}
	return left;
}

FindNew.findTopOffset = function (elem) 
{
	var top = 0;
	while (elem != null && elem.tagName != "BODY") {
		top += elem.offsetTop - elem.scrollTop;
		elem = elem.offsetParent;
	}
	return top;
}

FindNew.prototype.getDocumentElement = function ()
{
	return this.element;
}

FindNew.prototype.setDocumentElement = function (newElement)
{
	this.element = newElement;
}

FindNew.prototype.setInvisible = function () 
{
    document.getElementById(this.GetListId()).style.visibility = "hidden";
}

FindNew.prototype.getText = function ()
{
	if (this.text == null || this.text == "")
		return "&nbsp;";
	else
		return this.text;
}

FindNew.prototype.setText = function (newText)
{
	this.text = newText;
}

FindNew.prototype.getValue = function ()
{
	var result = "";
	if (this.value != "null")
		result = this.value;
	return result;
}

FindNew.prototype.setValue = function (newValue)
{
	this.value = newValue;
}

FindNew.prototype.select = function ()
{
	var input = document.getElementById(this.element.id + "_text");
	input.focus();
	try
	{
		input.select();
	}
	catch (ex)
	{
	}
}

FindNew.prototype.setView = function ()
{
	var valueInput = document.getElementById(this.element.id + "_value");
	var textInput = document.getElementById(this.element.id + "_text");
	valueInput.value = this.value;
	textInput.value = this.text;
}

FindNew.prototype.setModel = function ()
{

	var valueInput = document.getElementById(this.element.id + "_value");
	var textInput = document.getElementById(this.element.id + "_text");
	this.text = textInput.value;
	this.value = valueInput.value;
}

//static variables and methods
FindNew.existent = new Array();

FindNew.create = function (findNewId, editable, selectUrl, ctrlId, textFields, isSelectDefaultValue)
{
	FindNew.existent[findNewId] = new FindNew(findNewId, selectUrl, ctrlId, textFields, isSelectDefaultValue);
	FindNew.existent[findNewId].setDocumentElement(document.getElementById(findNewId));
	FindNew.existent[findNewId].setEditable(editable);
	Control.register(findNewId, FindNew.existent[findNewId]);
	return FindNew.existent[findNewId];
}

FindNew.get = function (findNewId)
{
	return FindNew.existent[findNewId];
}

FindNew.onListClick = function(e)
{
    var evt = W3C.getEvent(e);
    var target = W3C.Event.getTarget(evt);
    try
    {
        var listId = target.id; //IE
        if (listId == "")
            listId = target.parentNode.id; //Firefox
        var findId = listId.substring(0, listId.lastIndexOf("_"));
        var findCtrl = FindNew.get(findId);
        findCtrl.ListClicked();
    }
    catch (ex)
    {
        W3C.Event.preventDefault(evt);
    }
    W3C.Event.stopPropagation(evt);
}

FindNew.onListLoseCapture = function (e)
{
	var evt = W3C.getEvent(e);
	var target = W3C.Event.getTarget(evt);
	try
	{
		var findId = target.id.substring(0, target.id.lastIndexOf("_"));
		var findCtrl = FindNew.get(findId);
		findCtrl.LostCapture();
	}
	catch (ex)
	{
		W3C.Event.preventDefault(evt);
	}
	W3C.Event.stopPropagation(evt);
}

FindNew.onListKeyUp = function (e)
{
	var evt = W3C.getEvent(e);
	var target = W3C.Event.getTarget(evt);
	try
	{
		var findId = target.id.substring(0, target.id.lastIndexOf("_"));
		var findCtrl = FindNew.get(findId);
		findCtrl.ListKeyUp();
	}
	catch (ex)
	{
		W3C.Event.preventDefault(evt);
	}
	W3C.Event.stopPropagation(evt);
}

FindNew.onListKeyDown = function (e)
{
	var evt = W3C.getEvent(e);
	var target = W3C.Event.getTarget(evt);
	//alert('key down');
	W3C.Event.preventDefault(evt);
	W3C.Event.stopPropagation(evt);
}

FindNew.onListMouseOut = function (e)
{
	var evt = W3C.getEvent(e);
	var target = W3C.Event.getTarget(evt);
	try
	{
		var findId = target.id.substring(0, target.id.lastIndexOf("_"));
		var findCtrl = FindNew.get(findId);
		findCtrl.ListMouseOut();
	}
	catch (ex)
	{
		W3C.Event.preventDefault(evt);
	}
	W3C.Event.stopPropagation(evt);
}

/* **** Extended Behaviour **** */
FindNew.prototype.ListClicked = function ()
{
    find_onSelectValue(this.selectUrl, this.elementId, this.virtualsVariableName, this.textFields, true);
}

FindNew.prototype.ListLostCapture = function ()
{
	find_hideSelect(this.GetListId(), this.elementId);
}

FindNew.prototype.ListKeyUp = function ()
{
    find_onSelect(event.which ? event.which : event.keyCode, this.selectUrl, this.GetListId(), this.elementId, this.virtualsVariableName, this.textFields);
}

FindNew.prototype.ListMouseOut = function ()
{
    find_onSelectDefault(this.selectUrl, this.elementId, this.elementId + "_value", this.virtualsVariableName, this.textFields, this.isSelectDefaultValue, true);
}