function LocationLookupDiv(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 = "";
   	this.cellSuffix = "&nbsp;";
   	
   	this.estScrollBarWidth = 26;

    //initialize the control
    this.init();
    
}
/**
 * Creates Layer
 * Initializes layer with event functions
 * auto suggest functionality.
 * @scope private
 */
LocationLookupDiv.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].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);
	    } 
        oThis.highlightSuggestion(oTarget);
    };
};

/**
 * Creates the dropdown layer to display multiple suggestions.
 * @scope private
 */
LocationLookupDiv.prototype.createDiv = function () {

	this.layer = document.getElementById('locationLookupDiv');
	if (!this.layer) {
    	//create the layer and assign styles
    	//create the lookup fields inputs
    	this.layer = document.createElement("div");
    	this.layer.id = "locationLookupDiv";
    	this.layer.className = "locationLookupDiv";
    	this.layer.style.visibility = "hidden";
   	    document.body.appendChild(this.layer);
        this.addLookupFields();
        var resultsDiv = document.createElement("div");
        resultsDiv.className = "locationResultsDiv";
        this.layer.appendChild(resultsDiv);
        var	tableEl = document.createElement("table");
    	tableEl.className = "lookupResults";
    	tableEl.id = "locationResults";
    	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
 */
LocationLookupDiv.prototype.addLookupFields = function () {
    var tableEl = document.createElement("table");
    tableEl.style.width="620px";
    this.layer.appendChild(tableEl);
    var tbody = document.createElement("tbody");
    tableEl.appendChild(tbody);
    tbody.appendChild(this.getHeaderRow1());
    tbody.appendChild(this.getTitleRow());
    tbody.appendChild(this.getLocNameCity());
};
LocationLookupDiv.prototype.getHeaderRow1 = function () {
    var oThis = this;
 	var tableRow = document.createElement("tr");
 	var cell = document.createElement("td");
 	cell.className = "titleText";
 	cell.id="closeLocLookup";
 	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;
};
LocationLookupDiv.prototype.getTitleRow = function () {
    var oThis = this;
 	var tableRow = document.createElement("tr");
 	var cell = document.createElement("td");
 	cell.className = "formTitle";
 	cell.innerHTML = "Location Lookup";
	tableRow.appendChild(cell);
    return tableRow;
};
LocationLookupDiv.prototype.getLocNameCity = function () {
    var searchRow = document.createElement("tr");
    var searchCell = document.createElement("td");
	var searchTable = document.createElement("table");
    var searchTbody = document.createElement("tbody");
    searchTable.appendChild(searchTbody);
    var errorRow = document.createElement("tr");
    var errorCell = document.createElement("td");
    errorCell.colSpan = 3;
    errorCell.display = "none";
    errorCell.id  = "locSearchError";
    errorCell.className = "errors";
    errorRow.appendChild(errorCell);
    searchTbody.appendChild(errorRow);
	var tableRow = document.createElement("tr");	
 	var cell1 = document.createElement("td");
 	cell1.className = "searchlabel";
 	cell1.innerHTML  = "<nobr>Name</nobr>";
 	tableRow.appendChild(cell1);
 	var cell2 = document.createElement("td");
 	cell2.colSpan = 2;
 	var textEl = document.createElement("input");
 	textEl.type = "text";
 	textEl.name="locationName";
 	textEl.id = "locationName";
 	textEl.className = "searchtext2";
 	textEl.onblur = function(){upCase(this)};
    cell2.appendChild(textEl);
    tableRow.appendChild(cell2);
   	searchTbody.appendChild(tableRow);
    var tableRow3 = document.createElement("tr");
    var cell3 = document.createElement("td");
    cell3.className = "searchlabel";
 	cell3.innerHTML  = "<nobr>City</nobr>";
	tableRow3.appendChild(cell3);
    var cell4 = document.createElement("td");
 	var textEl2 = document.createElement("input");
 	textEl2.type = "text";
 	textEl2.name="locationCity";
 	textEl2.id = "locationCity";
 	textEl2.className = "searchtext2";
 	textEl2.onblur = function(){upCase(this)};
    cell4.appendChild(textEl2);    
	tableRow3.appendChild(cell4);
	var cell5 = document.createElement("td");
	var anchor = document.createElement("a");
	anchor.className = "imageAnchor";
	anchor.href = "javascript:findLocationByNameCity();";
	var image = document.createElement("img");
	image.id="loc_findByNameButton";
	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);
	cell5.appendChild(anchor);
	tableRow3.appendChild(cell5);	
	searchTbody.appendChild(tableRow3);
	searchCell.appendChild(searchTable);
	searchRow.appendChild(searchCell);
	return searchRow;
};
/**
 * Gets the left coordinate of the textbox.
 * @scope private
 * @return The left coordinate of the textbox in pixels.
 */
LocationLookupDiv.prototype.getLeft = function () {

    var oNode = this.positionAnchor;
    var iLeft = 0;
    while(oNode.tagName != "BODY" && oNode.tagName != "HTML") {
        iLeft += oNode.offsetLeft;
        oNode = oNode.offsetParent;        
    }
    var retVal = iLeft + this.leftOffset;
    return iLeft + this.leftOffset;
};

/**
 * Gets the top coordinate of the textbox.
 * @scope private
 * @return The top coordinate of the textbox in pixels.
 */
LocationLookupDiv.prototype.getTop = function ()  {

    var oNode = this.positionAnchor;
    var iTop = 0;
    
    while(oNode.tagName != "BODY" && oNode.tagName != "HTML") {
        iTop += oNode.offsetTop;
        oNode = oNode.offsetParent;
    }
    var retVal = iTop + this.topOffset;
    return iTop + this.topOffset;
};
/**
 * Returns the table rows in the div
 * @scope private
 */
LocationLookupDiv.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.
 */
LocationLookupDiv.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;
    }

};
LocationLookupDiv.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
 */
LocationLookupDiv.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.
 */
LocationLookupDiv.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
 */
LocationLookupDiv.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
 */
LocationLookupDiv.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 showLocationSuggestions (aSuggestions /*:Array*/) {
	var cellPrefix = "";
   	var cellSuffix = "&nbsp;";
    var lookupDiv = document.getElementById('locationLookupDiv');
    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(getLocationHeaderRow2(cellPrefix,cellSuffix));
    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 = "locationCol" + k;
            nextCell.style.whiteSpace = "nowrap";
            tableRow.appendChild(nextCell);
        	nextCell.innerHTML = cellPrefix + aSuggestions[i][k] + cellSuffix;
        }
    }
    if (tableEl.rows.length > 1) 
   	tableEl.rows[1].className =  "currentSelection";
   	resultsDiv.focus();
}
function getLocationHeaderRow2 (cellPrefix, cellSuffix) {
   	var cellPrefix = "<nobr>";
   	var cellSuffix = "&nbsp;</nobr>";

 	var tableRow = document.createElement("tr");
    var header1 = document.createElement("td");
    header1.className="lookupHeader";
    header1.innerHTML = cellPrefix + 'Phone #' + cellSuffix;
    tableRow.appendChild(header1);
    var header2 = document.createElement("td");
    header2.className="lookupHeader";
    header2.innerHTML = cellPrefix + 'Name' + cellSuffix;
    tableRow.appendChild(header2);
    var header5 = document.createElement("td");
    header5.className="lookupHeader";
    header5.innerHTML = cellPrefix + 'City' + cellSuffix;
    tableRow.appendChild(header5);
    var header6 = document.createElement("td");
    header6.className="lookupHeader";
    header6.innerHTML = cellPrefix + 'State' + cellSuffix;
    tableRow.appendChild(header6);
    var header3 = document.createElement("td");
    header3.className="lookupHeader";
    header3.innerHTML = cellPrefix + 'Address1' + cellSuffix;
    tableRow.appendChild(header3);
    var header4 = document.createElement("td");
    header4.className="lookupHeader";
    header4.innerHTML = cellPrefix + 'Address2' + cellSuffix;
    tableRow.appendChild(header4);
    var header7 = document.createElement("td");
    header7.className="lookupHeader";
    header7.innerHTML = cellPrefix + 'Zip' + cellSuffix;
    tableRow.appendChild(header7);
    return tableRow;
}
