if (typeof WsTools == "undefined") var WsTools = new Object();
WsTools.FilterParameters = function()
{
}
WsTools.FilterParameters.prototype = {
    getElement: function()
    {
        return document.getElementsByName("filterParameters")[0];
    },

    getValue: function()
    {
        return this.getElement().getAttribute("value");
    },

    getQueryString: function()
    {
        return "filterParameters=" + encodeURI(this.getValue());
    },

    clear: function()
    {
        this.getElement().setAttribute("value", "");
    },

    isEmpty: function()
    {
        return this.getValue() == "";
    }
}

WsTools.Toolbar = function(toolbarId)
{
    this.toolbarId = toolbarId;
    this.srcUrl = null;
    this.destUrl = null;
    this.selectedId = 0;
    this.selectedRow = null;
    this.buttons = new Array();
    this.buttonCount = 0;
}
WsTools.Toolbar.prototype = {
    init: function(srcUrl, destUrl)
    {
        this.srcUrl = srcUrl;
        this.destUrl = destUrl;
        this.selectedId = 0;
        this.selectedRow = null;
        this.buttons = new Array();
        this.buttonCount = 0;

        var element = document.getElementById(this.toolbarId);
        element.style.display = "block";
    },
    hideElement: function(name)
    {
        var elements = document.getElementsByName(name);
        if (elements != null)
        {
            elements[0].style.display = "none";
            return elements[0];
        }
        return null;
    },
    addButton: function(code, label, enabled, requiresSelectedRecord)
    {
        var index = this.buttonCount++;
        this.buttons[index] = new WsTools.TBButton(code, label, enabled, requiresSelectedRecord);
        this.buttons[index].index = index;
        return this.buttons[index];
    },
    addMenu: function(code, label, enabled, requiresSelectedRecord)
    {
        var index = this.buttonCount++;
        this.buttons[index] = new WsTools.TBMenu(code, label, enabled, requiresSelectedRecord);
        this.buttons[index].index = index;
        return this.buttons[index];
    },
    addSpacer: function()
    {
        var index = this.buttonCount++;
        this.buttons[index] = new WsTools.TBSpacer();
        this.buttons[index].index = index;
        return this.buttons[index];
    },
    drawToolbar: function()
    {
        var html = "<table class=\"toolbar\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\"><tbody><tr>";

        for (var i = 0; i < this.buttonCount; ++i)
        {
            html += this.buttons[i].getHtml(this.toolbarId);
            html += this.getSeparatorHtml();
        }

        html += "</tr></tbody></table>";
        var container = document.getElementById(this.toolbarId);
        container.innerHTML = html;
    },
    getSeparatorHtml: function()
    {
        return "<td style=\"width:3px;\"><img src=\"/images/spacer.gif\" alt=\"\" width=\"1\"></td>" +
               "<td style=\"background-color:#383838\"><img src=\"/images/spacer.gif\" alt=\"\" width=\"1\"></td>" +
               "<td style=\"width:3px;\"><img src=\"/images/spacer.gif\" alt=\"\" width=\"1\"></td>";
    },
    clickButton: function(buttonCode)
    {
        for (var i = 0; i < this.buttonCount; ++i)
        {
            if (this.buttons[i].code == buttonCode)
            {
                this.processOnClick(i);
                break;
            }
        }
    },
    processOnClick: function(index)
    {
        var button = this.buttons[index];
        button.func(this.selectedId);
    },
    processMenuItem: function(index, menuItemIndex)
    {
        var button = this.buttons[index];
        var menuItem = button.menuItems[menuItemIndex];
        menuItem.func(this.selectedId);
    },
    selectRecord: function(tableRow, id)
    {
        if (this.selectedRow != null)
        {
            selectedOff(this.selectedRow);
        }
        selectedOn(tableRow);
        this.selectedRow = tableRow;
        this.selectedId = id;

        this.updateButtonStates();
    },
    resetSelectedRow: function()
    {
        this.selectedRow = null;
        this.selectedId = 0;

        this.updateButtonStates();
    },
    getSelectedRow: function()
    {
        return this.selectedRow;
    },
    updateButtonStates: function ()
    {
        var enabled = (this.selectedId != 0);
        var button;
        for (var i = 0; i < this.buttonCount; ++i)
        {
            button = this.buttons[i];
            if (button.requiresSelectedId)
            {
                button.enabled = enabled;
            }
        }
        this.drawToolbar();
    },
    requestUri: function (uri)
    {
        var pageUrl = appendRedirectUrls(uri, this.srcUrl, this.destUrl);
        loadLeftContainer(pageUrl);
    }
}

WsTools.TBButton = function(code, label, enabled, requiresSelectedId)
{
    this.code = code;
    this.label = label;
    this.enabled = enabled;
    this.requiresSelectedId = requiresSelectedId;
    this.func = null;
    this.index = -1;
}
WsTools.TBButton.prototype = {
    setOnClick: function(func)
    {
        this.func = func;
    },
    getHtml: function(toolbarName)
    {
        var tableId = "menutbl" + this.index;
        var html = "<td class=\"toolbarButtonPanel\">" +
                   "<table id=\"" + tableId + "\" class=\"toolbarButton\"";

        var className = this.enabled ? "toolbarButtonEnabled" : "toolbarButtonDisabled";
        html += " class=\"" + className + "\"";

        if (this.enabled)
        {
//            html += " onclick=\"" + clickFunc + "('" + this.code + "')\"";
            html += " style=\"cursor: pointer;\" onclick=\"" + toolbarName + ".processOnClick('" + this.index + "')\"";
        }

        var buttonFile = "tb_" + this.code + (this.enabled ? "" : "-dis") + ".png";

        html += " title=\"\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr>" +
                "<td ><img src=\"/images/toolbar/" + buttonFile + "\" border=\"0\" height=\"22\" width=\"22\"></td>" +
                "<td  class=\"toolbarButtonText " + className + "\" nowrap=\"nowrap\">" + this.label + "</td>" +
                "<td><img src=\"/images/spacer.gif\" border=\"0\" height=\"7\" width=\"7\"></td>";

        html += "</tr></tbody></table></td>\n";
        return html;
    }
}

WsTools.TBSpacer = function(code)
{
    this.code = code;
    this.html = "&nbsp;";
    this.index = -1;
}
WsTools.TBSpacer.prototype = {
    setHtml: function(html)
    {
        this.html = html;
    },
    getHtml: function()
    {
        return "<td style=\"vertical-align:middle\">" + this.html + "</td>";
    }
}

WsTools.TBMenu = function(code, label, enabled, requiresSelectedId, width)
{
    this.code = code;
    this.label = label;
    this.enabled = enabled;
    this.requiresSelectedId = requiresSelectedId;
    this.width = width;
    this.menuItems = new Array();
    this.menuItemCount = 0;
    this.func = null;
    this.index = -1;
}
WsTools.TBMenu.prototype = {
    setOnClick: function(func)
    {
        this.func = func;
    },
    getTableId: function()
    {
        return "menutbl" + this.index;
    },
    getMenuId: function()
    {
        return this.getTableId() + "_menuItem";
    },
    addMenuItem: function(label, url)
    {
        var index = this.menuItemCount++;
        this.menuItems[index] = new WsTools.TBMenuItem(this.index, label, url);
        this.menuItems[index].index = index;
        return this.menuItems[index];
    },
    getHtml: function(toolbarName)
    {
        var tableId = this.getTableId();
        var menuId = this.getMenuId();

        var html = "<td class=\"toolbarButtonPanel\">\n" +
                   "<table id=\"" + tableId + "\" class=\"toolbarButton\"\n";

        if (this.enabled)
        {
            html += "onmouseover=\"buttonMouseover(event, '" + menuId + "', '" + tableId + "');\"\n" +
                    "onclick=\"return buttonClick(event, '" + menuId + "', '" + tableId + "');\"\n";
        }

        var className = this.enabled ? "toolbarButtonEnabled" : "toolbarButtonDisabled";
        var buttonFile = "tb_" + this.code + (this.enabled ? "" : "-dis") + ".png";

        html += "title=\"\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n" +
                "<tbody>\n" +
                "<tr>\n" +
                "<td class=\"toolbarButtomImage\"><img src=\"/images/toolbar/" + buttonFile + "\" border=\"0\" hheight=\"22\" wwidth=\"29\"></td>\n" +
                "<td class=\"toolbarButtonText " + className + "\" nowrap=\"nowrap\">" + this.label + "</td>\n";

        if (this.enabled)
        {
            html += "<td class=\"toolbarButtonArrow\"><img src=\"/images/toolbar/arrv_blue_2.gif\" border=\"0\" height=\"7\" width=\"7\"></td>";
        }
        else
        {
            html += "<td class=\"toolbarButtonArrow\"><img src=\"/images/spacer.gif\" border=\"0\" height=\"7\" width=\"7\"></td>\n"
        }

        html += "</tr>\n" +
                "</tbody>\n" +
                "</table>\n" +
                "</td>";

        html += "<div id=\"" + menuId + "\" class=\"menu\" onmouseover=\"menuMouseover(event)\" style=\"width:" + this.width + ";\">";

        for (var i = 0; i < this.menuItemCount; ++i)
        {
            html += this.menuItems[i].getHtml(toolbarName);
        }
        html += "</div>";

        return html;
    }
}
WsTools.TBMenuItem = function(parentIndex, label, url)
{
    this.parentIndex = parentIndex;
    this.label = label;
    this.url = url;
    this.func = null;
    this.index = -1;
}
WsTools.TBMenuItem.prototype = {
    setOnClick: function(func)
    {
        this.func = func;
    },
    getHtml: function(toolbarName)
    {
        if (this.url == null)
        {
            // Files will not download using IE6 with this code
            return "<a class=\"menuItem\" href=\"javascript:void(0);\" onclick=\"" + toolbarName + ".processMenuItem(" + this.parentIndex + "," + this.index + ")\">" + this.label + "</a>";
        }
        else
        {
            // IE6 fix
            return "<a class=\"menuItem\" href=\"" + this.url + "\">" + this.label + "</a>";
        }
    }
}

var mainToolbar = new WsTools.Toolbar("mainToolbar");
var formToolbar = new WsTools.Toolbar("formToolbar");

