function ChargeCodeLookupDiv(oPositionAnchor /*:Anchor to use as position offset point */,
									oLeftOffset, oTopOffset) {
    this.selectionNodeStartIndex = 1;
    this.cur = this.selectNodeStartIndex - 1;
    this.leftOffset = oLeftOffset;
    this.topOffset = oTopOffset;

    /**
     * The dropdown list layer.
     * @scope private
     */
    this.layer = null;
    
    /**
     * An anchor to use as an offset position point
     * @scope private
     */
    this.positionAnchor /*:HTMLInputElement*/ = oPositionAnchor;
   /**
     * The selected location code
     * @scope private
     */
   	this.cellPrefix = "<nobr>";
   	this.cellSuffix = "&nbsp;</nobr>";
   	
   	this.estScrollBarWidth = 26;

    //initialize the control
    this.init();
    
}
/**
 * Creates Layer
 * Initializes layer with event functions
 * auto suggest functionality.
 * @scope private
 */
ChargeCodeLookupDiv.prototype.init = function () {

    //save a reference to this object
    var oThis = this;
    //create the suggestions dropdown
    this.createDiv();


    //assign onkeydown event handler
    this.layer.childNodes[1].onkeydown = function (oEvent) {
    
        //check for the proper location of the event object
        if (!oEvent) {
            oEvent = window.event;
        }    
        
        //call the handleKeyDown() method with the event object
        oThis.handleKeyDown(oEvent);
    };
    this.layer.childNodes[1].onmousedown = 
	this.layer.childNodes[1].onmouseup = 
	this.layer.childNodes[1].onmouseover = function (oEvent) {
        oEvent = oEvent || window.event;
        oTarget = oEvent.target || oEvent.srcElement;
       
        var eventX = oEvent.offsetX ? oEvent.offsetX : oEvent.layerX;
        var layerWidth = oThis.layer.childNodes[1].childNodes[0].offsetWidth;
        var eventY = oEvent.offsetY ? oEvent.offsetY : oEvent.layerY;
        var layerHeight = oThis.layer.childNodes[1].childNodes[0].offsetHeight;
			if (eventX > layerWidth || eventY > layerHeight) {
			   return;
			}
	    if (oTarget.nodeName != "TR") {
	    	oTarget = oThis.getSelectedRow(oTarget);
	    } 
        if (oEvent.type == "mousedown") {
            if (oTarget.rowIndex >= oThis.selectionNodeStartIndex) {
        		oThis.setSelectionFields(oTarget);
        	}
        } else if (oEvent.type == "mouseover") {
            oThis.highlightSuggestion(oTarget);
        } else {
        	if (oTarget.rowIndex >= oThis.selectionNodeStartIndex) {
            	oThis.hideSuggestions();
        	}
        }
    };
};

/**
 * Creates the dropdown layer to display multiple suggestions.
 * @scope private
 */
ChargeCodeLookupDiv.prototype.createDiv = function () {

    var oThis = this;
	this.layer = document.getElementById('chargeCodeLookupDiv');
	if (!this.layer) {
    	//create the layer and assign styles
    	//create the lookup fields inputs
    	this.layer = document.createElement("div");
    	this.layer.id = "chargeCodeLookupDiv";
    	this.layer.className = "chargeCodeLookupDiv";
    	this.layer.style.visibility = "hidden";
   	    document.body.appendChild(this.layer);
        this.addLookupFields();
        var resultsDiv = document.createElement("div");
        resultsDiv.className = "chargeCodeResultsDiv";
        this.layer.appendChild(resultsDiv);
        var	tableEl = document.createElement("table");
    	tableEl.className = "lookupResults";
    	tableEl.style.borderCollapse = "collapse";
    	tableEl.id = "chargeCodeResults";
    	resultsDiv.appendChild(tableEl);
	    this.layer.style.left = this.getLeft() + "px";
    	this.layer.style.top = this.getTop() + "px";
    }
    this.layer.style.visibility = "visible";
};
/**
 * Creates the dropdown layer to display multiple suggestions.
 * @scope private
 */
ChargeCodeLookupDiv.prototype.addLookupFields = function () {
  	var oThis = this;
    var tableEl = document.createElement("table");
    this.layer.appendChild(tableEl);
    var tbody = document.createElement("tbody");
    tableEl.appendChild(tbody);
    tbody.appendChild(this.getHeaderRow1());
    tbody.appendChild(this.getErrorRow());
    tbody.appendChild(this.getDescriptionInputRow());
};
ChargeCodeLookupDiv.prototype.getHeaderRow1 = function () {
    var oThis = this;
 	var tableRow = document.createElement("tr");
 	var cell = document.createElement("td");
 	cell.colSpan = 3;
 	cell.className = "titleText";
 	var closeWindow = document.createElement("img");
 	closeWindow.src = "/LTMSLite/images/closeLookup.gif";
 	closeWindow.width=150;
 	closeWindow.height=24;
 	closeWindow.hspace=25;
 	closeWindow.onclick = function (){
 		oThis.hideSuggestions();
 	}; 
	cell.appendChild(closeWindow);
	tableRow.appendChild(cell);
    return tableRow;
};
ChargeCodeLookupDiv.prototype.getErrorRow = function () {
	var tableRow = document.createElement("tr");
 	var cell1 = document.createElement("td");
 	cell1.className = "errors";
 	cell1.id = "lookupChargeDescError";
 	cell1.colSpan = 3;
 	tableRow.appendChild(cell1);
 	return tableRow;
};
ChargeCodeLookupDiv.prototype.getDescriptionInputRow = function () {
	var tableRow = document.createElement("tr");
 	var cell1 = document.createElement("td");
 	cell1.className = "label";
 	cell1.innerHTML  = "<nobr>Charge Description</nobr>";
 	tableRow.appendChild(cell1);
 	var cell2 = document.createElement("td");
 	var textEl = document.createElement("input");
 	textEl.type = "text";
 	textEl.name="lookupChargeDesc";
 	textEl.id = "lookupChargeDesc";
 	textEl.className = "text1";
    cell2.appendChild(textEl);
	tableRow.appendChild(cell2);
	var cell3 = document.createElement("td");
	var anchor = document.createElement("a");
	anchor.className = "imageAnchor";
	anchor.href = "javascript:autoRate('7');";
	var image = document.createElement("img");
	image.id="searchButton";
	image.src = "/LTMSLite/images/search.gif";
	image.height=24;
	image.width=83;
	image.border=0;
	image.onmousedown = "changeImage(this,'images/searchClicked.gif')";
	image.onmouseup = "changeImage(this,'images/search.gif')";
	anchor.appendChild(image);
	cell3.appendChild(anchor);
	tableRow.appendChild(cell3);
	return tableRow;
};
/**
 * Gets the left coordinate of the textbox.
 * @scope private
 * @return The left coordinate of the textbox in pixels.
 */
ChargeCodeLookupDiv.prototype.getLeft = function () {

    var oNode = this.positionAnchor;
    var iLeft = 0;
    
    while(oNode.tagName != "BODY" && oNode.tagName != "HTML") {
        iLeft += oNode.offsetLeft;
        oNode = oNode.offsetParent;        
    }
    
    return iLeft + this.leftOffset;
};

/**
 * Gets the top coordinate of the textbox.
 * @scope private
 * @return The top coordinate of the textbox in pixels.
 */
ChargeCodeLookupDiv.prototype.getTop = function ()  {

    var oNode = this.positionAnchor;
    var iTop = 0;
    
    while(oNode.tagName != "BODY" && oNode.tagName != "HTML") {
        iTop += oNode.offsetTop;
        oNode = oNode.offsetParent;
    }
    
    return iTop + this.topOffset;
};
/**
 * Returns the table rows in the div
 * @scope private
 */
ChargeCodeLookupDiv.prototype.getSelectionNodes = function ()  {
    return this.layer.childNodes[1].childNodes[0].childNodes[0].childNodes;
};

/**
 * Handles three keydown events.
 * @scope private
 * @param oEvent The event object for the keydown event.
 */
ChargeCodeLookupDiv.prototype.handleKeyDown = function (oEvent /*:Event*/) {
 		oEvent = oEvent || window.event;
        oTarget = oEvent.target || oEvent.srcElement;
    switch(oEvent.keyCode) {
        case 38: //up arrow
            this.previousSuggestion();
            this.layer.childNodes[1].focus();
            break;
        case 40: //down arrow 
            this.nextSuggestion();
            this.layer.childNodes[1].focus();
            break;
        case 13: //enter
            this.setSelectionFields(this.getSelectionNodes()[this.cur]);
            this.hideSuggestions();
            break;
    }

};
ChargeCodeLookupDiv.prototype.getSelectedRow = function(oTarget) {
    if (oTarget.nodeName == "TR") return oTarget;
	else if (oTarget.nodeName == "DIV" || oTarget.nodeName == "TABLE" || oTarget.nodeName == "TBODY") {
		return this.getSelectedRow(oTarget.childNodes[0]);
	} else {
	    return this.getSelectedRow(oTarget.parentNode);
	}
};

/**
 * Hides the suggestion dropdown.
 * @scope private
 */
ChargeCodeLookupDiv.prototype.hideSuggestions = function () {
    this.layer.style.visibility = "hidden";
};

/**
 * Highlights the given node in the suggestions dropdown.
 * @scope private
 * @param oSuggestionNode The node representing a suggestion in the dropdown.
 */
ChargeCodeLookupDiv.prototype.highlightSuggestion = function (tableRow) {
    //alert ("in highlight suggestion");
    var tableRows = this.getSelectionNodes();
    
    for (var i=this.selectionNodeStartIndex; i < tableRows.length; i++) {
        var oNode = tableRows[i];
        if (oNode == tableRow) {
            oNode.className = "currentSelection"
            this.cur = i;
        } else if (oNode.className == "currentSelection") {
            oNode.className = "";
        }
    }
    this.layer.childNodes[1].focus();
};



/**
 * Highlights the next suggestion in the dropdown
 * @scope private
 */
ChargeCodeLookupDiv.prototype.nextSuggestion = function () {
   
    var cSuggestionNodes = this.getSelectionNodes();
    if (cSuggestionNodes.length > this.selectionNodeStartIndex && this.cur < cSuggestionNodes.length-1) {
        var oNode = cSuggestionNodes[++this.cur];
        this.highlightSuggestion(oNode);
    }
    this.layer.childNodes[1].focus();
};

/**
 * Highlights the previous suggestion in the dropdown
 * @scope private
 */
ChargeCodeLookupDiv.prototype.previousSuggestion = function () {
    var cSuggestionNodes = this.getSelectionNodes();

    if (cSuggestionNodes.length > 0 && this.cur > this.selectionNodeStartIndex) {
        var oNode = cSuggestionNodes[--this.cur];
        this.highlightSuggestion(oNode);
    }
    this.layer.childNodes[1].focus();
};
/**
 * Builds the suggestion layer contents, moves it into position,
 * and displays the layer.
 * @scope private
 * @param aSuggestions An array of suggestions for the control.
 */
function showChargeCodeSearchResults (aSuggestions /*:Array*/) {
	var cellPrefix = "<nobr>";
   	var cellSuffix = "&nbsp;</nobr>";
    var lookupDiv = document.getElementById('chargeCodeLookupDiv');
    var resultsDiv = lookupDiv.childNodes[1];
    var tableEl = resultsDiv.childNodes[0];
    // to clear previous results
    if (tableEl.childNodes.length > 0)
    tableEl.removeChild(tableEl.childNodes[0]);
    var	tbody = document.createElement("tbody");
    tableEl.appendChild(tbody);
    tbody.appendChild(getResultsHeaderRow(cellPrefix,cellSuffix));
    if (aSuggestions.length == 0) {
    } else {
	    for (var i=0; i < aSuggestions.length; i++) {
	        tableRow = document.createElement("tr");
	        tbody.appendChild(tableRow);
	        for (var k = 0; k < aSuggestions[i].length; k++) {
	            var nextCell = document.createElement("td");
	            nextCell.className = "arChargeCol" + k;
	            tableRow.appendChild(nextCell);
	        	nextCell.innerHTML = cellPrefix + aSuggestions[i][k] + cellSuffix;
	        }
	    }
    }
    if (tableEl.rows.length > 1) 
   	tableEl.rows[1].className =  "currentSelection";
   	resultsDiv.focus();
}
function getResultsHeaderRow (cellPrefix, cellSuffix) {
 	var tableRow = document.createElement("tr");
    var header1 = document.createElement("td");
    header1.className="lookupHeader";
    header1.innerHTML = cellPrefix + 'Charge Code' + cellSuffix;
    tableRow.appendChild(header1);
    var header2 = document.createElement("td");
    header2.className="lookupHeader";
    header2.innerHTML = cellPrefix + 'Charge Description' + cellSuffix;
    tableRow.appendChild(header2);   
    return tableRow;
}
ChargeCodeLookupDiv.prototype.setSelectionFields = function (tableRow) {
    var chargeCode = tableRow.cells[0].innerHTML;
    setValue('add_chargeCode', chargeCode.substring(this.cellPrefix.length, chargeCode.length - this.cellSuffix.length));
	autoRate('2');
};

