
    function initSearchBox(receiverName, tableName, limited, precision) {
        var oContent = $("searchBox_content");
        
        var title    = "searchBox_title";
        var popup    = "searchBox_div";
        
        // make sure the given popup exists
        if(popup && oContent) {
            // load the help box form using AJAX
            var sURL = '/ajax-searchBox.php?receiverName='+receiverName+'&tableName='+tableName+'&limited='+limited;
            if(precision && precision != "") {
                sURL += "&precision="+precision;
            }
            if(showSearchBox.search && showSearchBox.search != "") {
                sURL += "&search=" + escape(showSearchBox.search);
                showSearchBox.search = null;
            }
            var oRequest = new Ajax.Request(sURL, {
                onComplete : function(p_oRequest)
                {
                    oContent.innerHTML = p_oRequest.responseText;
                    
                    if(typeof(replaceSearchBoxLabel) == 'function') {
                        replaceSearchBoxLabel(tableName);        
                    }
                    
                    var oSearchField = document.getElementById("searchBoxField");
                    if(oSearchField) {
                        oSearchField.onkeypress = stopReturnKey;
                    }
                    
                    showPopup(popup);
                    
                    if(oSearchField) {
                        oSearchField.focus();
                        oSearchField.select();
                    }
                    
                    initPopup(popup, title);
                }, 
                onException : function(p_oRequest, p_oException)
                {
                    throw p_oException;
                },
                onLoaded : function()
                {
                }
            });
        }
    }
    
    function refreshSearchBox(receiverName, tableName, limited, precision) {
        if(oSearch = document.getElementById('searchBoxField')) {
            displaySearchBox(receiverName, tableName, limited, 1, oSearch.value, precision);
        }
    }
    
    function displaySearchBox(receiverName, tableName, limited, page, searchValue, precision) {

        var oContent = $("searchBox_content");
        
        var title    = "searchBox_title";
        var popup    = "searchBox_div";
        
        // process precision
        var strPrecision = "";
        if(precision && precision != "") {
            // get each precision key
            var oPrecParts = precision.split("|");
            if(oPrecParts.length > 0) {
                for(var iPart = 0; iPart < oPrecParts.length; iPart += 1) {
                    var oPrecPart = oPrecParts[iPart];
                    var sKey = false;
                    var sValue = false;
                    
                    // try and fetch this key's value
                    if(oPrecPart.indexOf("=") >= 0) {
                        // if this key has a given value, keep the value
                        var oPrecPartComps = oPrecPart.split("=");
                        sKey               = oPrecPartComps[0];
                        // if(oPrecPartComps.length > 1) {
                            // sValue = oPrecPartComps[1];
                        // }
                    }
                    else {
                        sKey = oPrecPart;
                    }
                    
                    // if this key has no value, try and fetch their value from the form
                    if(!sValue) {
                        var sFilter;
                        if(sKey == "municipality") {
                            sFilter = "SB_filterMunicipality";
                        }
                        else if(sKey == "phone") {
                            sFilter = "SB_filterPhone";
                        }
                        else if(sKey == "address") {
                            sFilter = "SB_filterAddress";
                        }
                        else if(sKey == "enterprise") {
                            sFilter = "SB_filterEnterprise";
                        }
                        else if(sKey == "email") {
                            sFilter = "SB_filterEmail";
                        }
                        
                        if(sFilter != "") {
                            var oFilter = document.getElementById(sFilter);
                            if(oFilter) {
                                sValue = oFilter.value;
                            }
                        }
                    }
                    
                    if(strPrecision != "") strPrecision += "|";
                    strPrecision += sKey;
                    if(sValue && sValue != "") {
                        strPrecision += escape("="+sValue);
                    }
                }
            }
        }
        
        // make sure the given popup exists
        if(popup && oContent) {
            // load the help box form using AJAX
            var sURL = '/ajax-searchBox.php?receiverName='+receiverName+'&tableName='+tableName+'&limited='+limited+'&search='+searchValue+'&page='+page;
            if(strPrecision && strPrecision != "") {
                sURL += "&precision="+strPrecision;
            }
            var oRequest = new Ajax.Request(sURL, {
                onComplete : function(p_oRequest)
                {
                    oContent.innerHTML = p_oRequest.responseText;
                    
                    if(typeof(replaceSearchBoxLabel) == 'function') {
                        replaceSearchBoxLabel(tableName);
                    }
                    
                    var oSearchField = document.getElementById("searchBoxField");
                    if(oSearchField) {
                        oSearchField.onkeypress = stopReturnKey;
                    }
                }, 
                onException : function(p_oRequest, p_oException)
                {
                    throw p_oException;
                },
                onLoaded : function()
                {
                }
            });
        }
    }
    
    function addSearchBoxResult() {
        if(typeof(submitSearchBox) == 'function') {
            submitSearchBox();        
        }
        closePopup('searchBox_div');
    }
    
    function showSearchBox(receiverName, tableName, limited, precision) {
        if(showSearchBox.first) {
            initSearchBox(receiverName, tableName, limited, precision);
            showSearchBox.first = false;
        }
        else {
            showPopup("searchBox_div");
            
            var oSearchField = document.getElementById("searchBoxField");
            if(oSearchField) {
                oSearchField.focus();
                oSearchField.select();
            }
        }
    }
    showSearchBox.first = true;
    showSearchBox.search = null;
    
    /** When the SearchBox is limited, but the element used to select an item is not a radio button,
     * indicates the main form that an item has been selected.
     * @param itemIndex the index of the item that was selected.
     * @param entity the entity of the item that was selected. (for tabbed SearchBox). */
    function selectSearchBoxItem(itemIndex, entity) {
        if(typeof(submitSearchBox) == 'function') {
            submitSearchBox(itemIndex, entity);
        }
        //closePopup('searchBox_div');
    }

