/*
* CoreLib functions and popup functions adapted from JBEL, the JavaScript Browser Extension Library.
* Copyright (c) 2004-2009 Intui Design & Development.  All rights reserved.
*/

var g_oPopup;
var g_oPopupSource;
var g_oCalendar;
var g_oPopupWnd;
var g_dPopupWndLastClose;
var g_oPopupWndOnCloseCallback;
var g_iUniqueNumber = 0;
var g_oHtmlEncodeElement;
var undef;

function indexOf(aArray, oElement)
{
    for (var i = 0; i < aArray.length; i++)
    {
        if (aArray[i] == oElement) { return i; }
    }
    return -1;
}

function setColumnVisibilities(aoRows, iStartColIdx, iEndColIdx, bVisible)
{
    var sVisibility = (bVisible) ? "table-cell" : "none";
    for (var iRowIdx = 0; iRowIdx < aoRows.length; iRowIdx++)
    {
        var aoCells = aoRows[iRowIdx].cells;

        var iRealColIdx = 0;
        var aiVirtualToRealColIdxMap = [];

        for (var iRealColIdx = 0; iRealColIdx < aoCells.length; iRealColIdx++)
        {
            for (var iColSpan = (aoCells[iRealColIdx].colSpan || 1) + 0; iColSpan > 0; iColSpan--)
            {
                aiVirtualToRealColIdxMap.push(iRealColIdx);
            }
        }

        for (var iColIdx = iStartColIdx; iColIdx < iEndColIdx; iColIdx++)
        {
            aoCells[aiVirtualToRealColIdxMap[iColIdx]].style.display = sVisibility;
        }
    }
}

function isArray(oArray)
{
    return oArray && typeof (oArray) == "object" && oArray.length >= 0;
}

function go(url)
{
    if (!url) { return; }
    document.location.href = url + ((url.indexOf('?') >= 0) ? '&' : '?') + "_rnd=" + CoreLib_URLEncode(CoreLib_getUniqueNumber())
    return false;
}

function isUndefined(v)
{
    return v == undef;
}

function getById(id)
{
    return (document.all) ? document.all[id] : document.getElementById(id);
}

function isValidEmailAddress(str)
{
    return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str));
}

function trim(str)
{
    return str.replace(/^\s*|\s*$/g, "");
}

function showPopup(sUrl, iWidth, iHeight, bShowToolbar, sWindowName, bShowScrollbars, bResizable)
{
    var sDims = "";
    if (iWidth || iHeight)
    {
        if (!iWidth) { iWidth = 400; }
        if (!iHeight) { iHeight = 300; }
        var iLeftPos = (document.body.clientWidth - iWidth) / 2 + window.screenLeft;
        var iTopPos = (document.body.clientHeight - iHeight) / 2 + window.screenTop;
        sDims = ",width=" + iWidth + ",height=" + iHeight + ",top=" + iTopPos + ",left=" + iLeftPos;
    }

    if (bShowToolbar) { iHeight -= 50; }
    var sScrollbars = (isUndefined(bShowScrollbars) || bShowScrollbars) ? "yes" : "no";
    var sResizable = (bResizable || bShowScrollbars == "yes") ? "yes" : "no";

    var oWnd = window.open(sUrl, sWindowName, "location=no,resizable=" + sResizable + ",scrollbars=" + sScrollbars + ",menubar=no,toolbar=" + ((bShowToolbar) ? "yes" : "no") + ",status=no" + sDims);
    if (!oWnd || !oWnd.top)
    {
        // Popup blocker must have killed the popup window
        document.location.href = sUrl;
        return window;
    }
    else
    {
        oWnd.focus();
        return oWnd;
    }
}

function getFirstSelection(sFieldName, oSource)
{
    return getFirstSelectionAttribute(sFieldName, oSource, "value");
}

function getFirstSelectionAttribute(sFieldName, oSource, sAttributeName)
{
    var oForm = (oSource) ? oSource.form : document.forms[0];
    var aElements = oForm.elements[sFieldName];
    var oElement = null;

    if (aElements != null)
    {
        if (aElements.type == "select-one" || aElements.type == "select-many")
        {
            oElement = (aElements.selectedIndex >= 0) ? aElements.options[aElements.selectedIndex] : null;
        }
        else if (aElements.length > 0)
        {
            for (var i = 0; i < aElements.length; i++)
            {
                if (aElements[i].checked)
                {
                    oElement = aElements[i];
                    break;
                }
            }
        }
        else if (aElements.type == "radio" || aElements.type == "checkbox")
        {
            if (aElements.checked)
            {
                oElement = aElements;
            }
        }
        else
        {
            oElement = aElements;
        }

        return (oElement && sAttributeName) ? oElement[sAttributeName] : oElement;
    }
}

function appendParameter(sQs, sParamName, sParamValue)
{
    var sAnchor = "";
    if (sQs == null)
    {
        sQs = "?";
    }
    else
    {
        var iAnchorLoc;
        if ((iAnchorLoc = sQs.indexOf("#")) >= 0)
        {
            sAnchor = sQs.substr(iAnchorLoc, sAnchor.length - iAnchorLoc);
            sQs = (iAnchorLoc > 0) ? sQs.substr(0, iAnchorLoc - 1) : "";
        }
        if (sQs.indexOf("?") >= 0)
        {
            sQs += "&";
        }
        else
        {
            sQs += "?";
        }
    }
    sQs += sParamName;
    if (sParamValue != null && !isUndefined(sParamValue))
    {
        sQs += "=" + CoreLib_URLEncode(sParamValue);
    }
    return sQs + sAnchor;
}

function CoreLib_isObject(vItem)
{
    return (typeof (vItem) == 'object' && vItem != null);
}

function CoreLib_getElementById(sID, objDocument)
{
    objDocument = (CoreLib_isObject(objDocument)) ? objDocument : window.document;

    if (document.all)
    {
        return objDocument.all[sID];
    }
    else
    {
        return objDocument.getElementById(sID);
    }
}

function CoreLib_makeUniqueId()
{
    return document.uniqueID;
}

function CoreLib_createTag(sTagName)
{
    return document.createElement(sTagName);
}

function CoreLib_absoluteLeft(objElement)
{
    var iLeft = 0;

    for (var obj = objElement; obj.offsetParent != null; obj = obj.offsetParent)
    {
        iLeft += obj.offsetLeft;
    }

    return iLeft;
}

function CoreLib_absoluteTop(objElement)
{
    var iTop = 0;

    for (var obj = objElement; obj.offsetParent != null; obj = obj.offsetParent)
    {
        if (obj.offsetParent.style.position == "relative") { iTop -= 50; }  // temporary hack for PM relative positioned dates
        iTop += obj.offsetTop;
    }

    return iTop;
}

function CoreLib_escapeHtml(vValue)
{
    if (typeof (vValue) == "string")
    {
        return vValue.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    }
}

function CoreLib_getUniqueNumber()
{
    return (new Date()).getTime() + '.' + g_iUniqueNumber++;
}

function CoreLib_URLEncode(sValue)
{
    return (sValue == null) ? "" : encodeURIComponent(sValue);
}

function CoreLib_focusOnFirstField(vForm)
{
    var oForm = (CoreLib_isObject(vForm)) ? vForm : document.forms[(vForm) ? vForm : 0];
    if (oForm)
    {
        for (var i = 0; i < oForm.elements.length; i++)
        {
            var oControl = oForm[i];
            if (oControl.length > 0 && !oControl.options)
            {
                oControl = oForm[i][0];
            }
            if (oControl.disabled == '' && oControl.type != 'hidden')
            {
                oControl.focus();
                break;
            }
        }
    }
}

function CoreLib_setInnerHtml(element, sHtml)
{
    if (!element) { return; }
    if (!CoreLib_isObject(element))
    {
        element = CoreLib_getElementById(element);
        if (!element) { return; }
    }
    element.innerHTML = sHtml;
}

function CoreLib_setOuterHtml(element, sHtml)
{
    if (!element) { return; }
    if (!CoreLib_isObject(element))
    {
        element = CoreLib_getElementById(element);
        if (!element) { return; }
    }
    try
    {
        element.outerHTML = sHtml;
    }
    catch (e)
    {
        try
        {
            var oDivContainer = document.createElement("div");
            oDivContainer.innerHTML = sHtml;
            if (oDivContainer.firstChild)
            {
                element.insertAdjacentElement("afterEnd", oDivContainer.firstChild);
                element.removeNode(true);
            }
        }
        catch (e)
        {
            // Failure.  Do nothing
        }
    }
}

function findAncestor(oSrc, tagName)
{
    tagName = tagName.toLowerCase();
    for (; oSrc != null; oSrc = oSrc.parentNode)
    {
        if (oSrc.tagName && oSrc.tagName.toLowerCase() == tagName)
        {
            return oSrc;
        }
    }
    return null;
}

function findFirstDescendent(oSrc, sTagName)
{
    sTagName = sTagName.toLowerCase();
    for (; oSrc != null && oSrc.tagName; oSrc = oSrc.firstChild)
    {
        if (oSrc.tagName.toLowerCase() == sTagName)
        {
            return oSrc;
        }
    }
    return null;
}

function findPreviousSibling(oSrc, tagName)
{
    tagName = tagName.toLowerCase();
    for (; oSrc != null; oSrc = oSrc.previousSibling)
    {
        if (oSrc.tagName.toLowerCase() == tagName)
        {
            return oSrc;
        }
    }
    return null;
}

function popupWnd(sDivName, oSourceElement, bRightAlign, iLeftOffset, iTopOffset, fCloseCallback)
{
    g_oPopupWndOnCloseCallback = fCloseCallback;

    if (!g_oPopupWnd)
    {
        g_oPopupWnd = window.createPopup();
    }
    else if (new Date() - g_dPopupWndLastClose < 500)
    {
        g_oPopupWnd.hide();
        return null;
    }

    var iLeft = iLeftOffset;
    var iTop = oSourceElement.offsetHeight + iTopOffset;

    if (bRightAlign)
    {
        //iLeft -= oSourceElement.offsetWidth;
    }

    var html = CoreLib_getElementById(sDivName).outerHTML;
    var objDoc = g_oPopupWnd.document;
    objDoc.open();
    objDoc.write("<html><head><link rel='stylesheet' href='css/default.css' type='text/css'></head><body onunload='try{window.parent.onPopupWndClose()}catch(e){}' ondragstart='return false;' scroll='no' style='border:none' leftmargin='0' topmargin='0'>" + html + "<SCRIPT>document.oncontextmenu=function() { return false; }</SCRIPT></body></html>");
    objDoc.close();
    objDoc.onreadystatechange = function()
    {
        if (objDoc.readyState == "complete")
        {
            objDoc.body.firstChild.style.display = "block";
            var iWidth = objDoc.body.firstChild.offsetWidth;
            var iHeight = objDoc.body.firstChild.offsetHeight;
            if (iHeight > 600) { iHeight = 600; }
            if (bRightAlign) { iLeft -= iWidth - oSourceElement.offsetWidth; }
            g_oPopupWnd.show(iLeft, iTop, iWidth, iHeight, oSourceElement);
        }
    };
    g_oPopupWnd.show(iLeft, iTop, 1, 1, oSourceElement);
    objDoc.onreadystatechange(); // explicity fire event, since loading may have happened before we even set up the handler (local intranet scenario)
    return g_oPopupWnd;
}

function onPopupWndClose()
{
    g_dPopupWndLastClose = new Date();
    if (g_oPopupWndOnCloseCallback) { g_oPopupWndOnCloseCallback(); }
}

function popupDiv(sDivName, oSourceElement, bRightAlign, iMouseX, iMouseY)
{
    if (oSourceElement && g_oPopupSource == oSourceElement)
    {
        hidePopupDiv();
        return;
    }

    hidePopupDiv();
    g_oPopupSource = oSourceElement;
    if (oSourceElement)
    {
        if (!oSourceElement.src) { oSourceElement.src = ""; }
        g_oPopupSource.src = (g_oPopupSource.src.indexOf("List.gif") >= 0) ? "images/btnHideList.gif" : (g_oPopupSource.src.indexOf("ListSmall.gif") >= 0) ? "images/btnHideListSmall.gif" : "images/drop_mini_down.gif";
    }

    var oWnd;
    if (sDivName)
    {
        oWnd = CoreLib_getElementById(sDivName);
    }
    else if (oSourceElement && oSourceElement.contentDiv)
    {
        oWnd = oSourceElement.contentDiv;
        oSourceElement.contentLoadUrl = null;
    }
    else if (oSourceElement && oSourceElement.contentLoadUrl)
    {
        document.body.style.cursor = "wait";
        var objHttp = new HttpClient();
        objHttp.noCache = true;
        var html = objHttp.getAsText(oSourceElement.contentLoadUrl);
        oWnd = document.createElement("div");
        oWnd.innerHTML = html;
        oWnd = oWnd.firstChild;
        document.body.insertAdjacentElement('afterBegin', oWnd)
        oSourceElement.contentDiv = oWnd;
        setTimeout(function() { popupDiv(sDivName, oSourceElement, bRightAlign, iMouseX, iMouseY); });
        document.body.style.cursor = "default";
        return;
    }
    else
    {
        return;
    }

    var iOffsetX = 0, iOffsetY = 0;
    var oWndFrame = oWnd.frame;
    var oWndContainer = oWnd.container;

    if (!oWndFrame)
    {
        oWndContainer = oWnd.container = CoreLib_createTag("div");
        oWndContainer.style.filter = "progid:DXImageTransform.Microsoft.Fade(duration=0.3,overlap=1)";
        oWndContainer.style.position = "absolute";
        oWndContainer.style.display = "block";
        oWndContainer.style.zIndex = 9997;
        document.body.insertAdjacentElement('afterBegin', oWndContainer)

        oWnd.style.position = "absolute";
        oWnd.style.zIndex = 9999;
        oWnd.style.display = "block";
        oWnd.style.visibility = "hidden";
        if (!oWnd.currentStyle.filter)
        {
            oWnd.style.filter = "Alpha(opacity=100)";
        }
        oWnd.onclick = function() { event.cancelBubble = true; }
        oWndContainer.appendChild(oWnd);

        oWndFrame = oWnd.frame = CoreLib_createTag("iframe");
        oWndFrame.src = "bogus";
        oWndFrame.style.position = "absolute";
        oWndFrame.style.zIndex = 9998;
        oWndFrame.style.filter = "Alpha(opacity=0)";
        oWndFrame.style.display = "block";
        oWndFrame.style.visibility = "hidden";
        oWndContainer.appendChild(oWndFrame);
    }

    if (oWndContainer.style.visibility == "visible" && oWnd.style.visibility == "visible")
    {
        hidePopupDiv();
        return;
    }

    oWnd.style.overflowY = "auto";
    oWnd.style.height = "";
    if (oWnd.offsetHeight > 600)
    {
        oWnd.style.height = "600px";
    }

    var iLeft, iTop, iSourceElementTop;
    if (!isUndefined(iMouseX) && !isUndefined(iMouseY))
    {
        iLeft = iMouseX + document.body.scrollLeft;
        iTop = iSourceElementTop = iMouseY + document.body.scrollTop;
    }
    else if (oSourceElement)
    {
        iSourceElementTop = CoreLib_absoluteTop(oSourceElement);
        iLeft = CoreLib_absoluteLeft(oSourceElement) + 2;
        iTop = iSourceElementTop + oSourceElement.offsetHeight;
    }
    else
    {
        iLeft = event.clientX + document.body.scrollLeft;
        iSourceElementTop = iTop = event.clientY + document.body.scrollTop;
    }
    if (bRightAlign)
    {
        iLeft -= oWnd.offsetWidth - ((oSourceElement) ? oSourceElement.offsetWidth : 0);
    }

    var iSpaceAbove = iSourceElementTop - document.body.scrollTop;
    var iSpaceBelow = document.body.scrollTop + document.body.clientHeight - iSourceElementTop - ((oSourceElement) ? oSourceElement.offsetHeight : 0);

    // If there isn't enough room underneath the object, show the popup above the object		
    if ((iTop + oWnd.offsetHeight > document.body.scrollTop + document.body.clientHeight) &&
        iSpaceAbove > iSpaceBelow)
    {
        iTop = iSourceElementTop - oWnd.offsetHeight;
        if (oWnd.offsetHeight > iSpaceAbove)
        {
            iTop += (oWnd.offsetHeight - iSpaceAbove);
            oWnd.style.height = (iSpaceAbove - 2) + "px";
        }
    }
    else
    {
        if (oWnd.offsetHeight > iSpaceBelow - 3)    // 3px accounts for the shadow
        {
            oWnd.style.height = (iSpaceBelow - 3) + "px";
        }
    }

    // If the object exceeds the width of the page, push it left
    var iMaxLeft = document.body.scrollLeft + document.body.clientWidth - oWnd.offsetWidth;
    if (iLeft > iMaxLeft) { iLeft = iMaxLeft; }

    // If the object must move off the page, make sure it moves off to the right and bottom
    if (iLeft < document.body.scrollLeft) { iLeft = document.body.scrollLeft; }
    if (iTop < document.body.scrollTop) { iTop = document.body.scrollTop; }

    oWndContainer.style.visibility = "visible";
    oWndContainer.style.left = iLeft;
    oWndContainer.style.top = iTop;
    oWndContainer.style.width = oWnd.offsetWidth;
    oWndContainer.style.height = oWnd.offsetHeight;
    oWndContainer.filters[0].apply();
    oWnd.style.visibility = "visible";
    oWndFrame.style.width = oWnd.offsetWidth;
    oWndFrame.style.height = oWnd.offsetHeight;
    oWndFrame.style.visibility = "visible";
    oWndContainer.filters[0].play();

    if (event != null) { event.cancelBubble = true; }
    g_oPopup = oWnd;
}

function hidePopupDiv()
{
    if (g_oPopupSource)
    {
        if (!g_oPopupSource.contentLoadUrl)
        {
            g_oPopupSource.src = (g_oPopupSource.src.indexOf("List.gif") >= 0) ? "images/btnShowList.gif" : (g_oPopupSource.src.indexOf("ListSmall.gif") >= 0) ? "images/btnShowListSmall.gif" : "images/drop_mini.gif";
        }
        g_oPopupSource = null;
    }
    if (g_oPopup)
    {
        g_oPopup.style.visibility = "hidden";
        g_oPopup.frame.style.visibility = "hidden";
        g_oPopup.container.style.visibility = "hidden";
        g_oPopup = null;
    }
}

function submitForm(frm)
{
    document.forms(frm).submit();
}

function initCalendar(oBaseTag, bNoDisabledDates)
{
    var oElement = CoreLib_createTag();
    oElement.id = CoreLib_makeUniqueId();
    oElement.style.position = 'absolute';
    oElement.style.visibility = 'hidden';
    oElement.style.backgroundColor = 'white';

    if (oBaseTag == document.body)
    {
        document.body.insertAdjacentElement('afterBegin', oElement)
    }
    else
    {
        oBaseTag.appendChild(oElement);
    }

    var oCalendar = new CalendarPopup(oElement.id);
    if (bNoDisabledDates != true)
    {
        oCalendar.addDisabledDates(null, formatDate(new Date(new Date() - 1000 * 60 * 60 * 24), "yyyy-MM-dd"));
    }
    oCalendar.setCssPrefix("calendar");
    oCalendar.setReturnFunction("showCalendar_callback");
    oCalendar.showNavigationDropdowns();
    return oCalendar;
}

function showCalendar(oLink, oInput, bNoDisabledDates)
{
    if (!g_oCalendar)
    {
        g_oCalendar = initCalendar(document.body, bNoDisabledDates);
    }
    if (!oLink.name)
    {
        oLink.id = oLink.name = CoreLib_makeUniqueId();
    }

    var dDate = new Date(oInput.value);
    if (!isNaN(dDate))
    {
        g_oCalendar.currentDate = formatDate(dDate, "yyyy-M-d");
    }
    g_oCalendar._inputField = oInput;
    g_oCalendar.select(oInput, oLink.id, 'M/d/yyyy');
}

function showCalendar_callback(y, m, d)
{
    var sOldValue = g_oCalendar._inputField.value;
    if (y == 0 && m == 0 && d == 0)
    {
        g_oCalendar._inputField.value = "";
    }
    else
    {
        var dt = new Date(y, m - 1, d, 0, 0, 0);
        g_oCalendar._inputField.value = (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
    }
    if (g_oCalendar._inputField.onchange && sOldValue != g_oCalendar._inputField.value)
    {
        g_oCalendar._inputField.onchange();
    }
}

function showLinkInPopup(sUrl, oLink)
{
    var oPopupFrame = CoreLib_getElementById("iframePopup");
    if (!oPopupFrame)
    {
        oPopupFrame = CoreLib_createTag("iframe");
        oPopupFrame.id = "iframePopup";
        oPopupFrame.scrolling = "yes";
        oPopupFrame.style.position = "absolute";
        oPopupFrame.style.zIndex = 9998;
        oPopupFrame.style.display = "block";
        oPopupFrame.style.width = "500px";
        oPopupFrame.style.height = "175px";
        oPopupFrame.style.visibility = "hidden";
        document.body.insertAdjacentElement('afterBegin', oPopupFrame);
    }
    oPopupFrame.src = "";
    setTimeout(function() { oPopupFrame.src = sUrl; }, 0);
    popupDiv("iframePopup", oLink, false);
    return false;
}

function inputLabelClick(oLabel)
{
    if (oLabel && oLabel.previousSibling)
    {
        var oInput = oLabel.previousSibling;
        if (oInput.type == "radio")
        {
            if (!oInput.checked) { oInput.checked = true; }
        }
        else
        {
            oInput.checked = !oInput.checked;
        }
        if (oInput.onchange) { oInput.onchange.call(oInput); }
    }
    return false;
}

function onRadioChange(oOption)
{
    if (oOption && oOption.type == "radio")
    {
        if (oOption.checked)
        {
            if (oOption.wasChecked == "1" || oOption.wasChecked == true)
            {
                oOption.checked = false;
            }
            var aoOptions = oOption.form.elements[oOption.name];
            if (!isArray(aoOptions)) { aoOptions = [aoOptions]; }
            for (var i = 0; i < aoOptions.length; i++)
            {
                aoOptions[i].wasChecked = aoOptions[i].checked;
            }
        }
    }
}

function extractFormValues(oForm)
{
    var sValues = '';
    if (oForm)
    {
        for (var i = 0; i < oForm.elements.length; i++)
        {
            sValues = extractFormValuesFromTag(oForm.elements[i], sValues);
        }
    }
    return sValues;
}

function extractFormValuesFromChildren(oElement)
{
    var sValues = '';
    if (oElement)
    {
        for (var oChild = oElement.firstChild; oChild != null; oChild = oChild.nextSibling)
        {
            sValues = extractFormValuesFromTag(oChild, sValues);
        }
    }
    return sValues;
}

function extractFormValuesFromDescendents(oElement, sValues)
{
    var sValues = sValues || '';
    if (oElement)
    {
        for (var oChild = oElement.firstChild; oChild != null; oChild = oChild.nextSibling)
        {
            if (oChild.firstChild)
            {
                sValues = extractFormValuesFromDescendents(oChild, sValues);
            }
            sValues = extractFormValuesFromTag(oChild, sValues);
        }
    }
    return sValues;
}

function extractFormValuesFromTag(oChild, sValues)
{
    if (oChild.tagName && oChild.name)
    {
        var sTagName = oChild.tagName.toLowerCase();
        var asTagValues = '';

        if (sTagName == "input")
        {
            if (indexOf(["button", "submit", "reset"], oChild.type) >= 0 || ((oChild.type == "radio" || oChild.type == "checkbox") && !oChild.checked))
            {
                return sValues;
            }
            asTagValues = [oChild.value];
        }
        else if (sTagName == "select")
        {
            asTagValues = new Array();
            for (var i = 0; i < oChild.options.length; i++)
            {
                if (oChild.options[i].selected)
                {
                    asTagValues.push(oChild.options[i].value);
                }
            }
        }
        else if (sTagName == "textarea")
        {
            asTagValues = [oChild.innerText || oChild.value];
        }

        if (asTagValues)
        {
            for (var i = 0; i < asTagValues.length; i++)
            {
                sValues += oChild.name + "=" + CoreLib_URLEncode(asTagValues[i]) + "&";
            }
        }
        sValues += extractFormValuesFromChildren(oChild);
    }
    return sValues;
}

function toggleDetailsRow(btnSource, contentUrl)
{
    var oImg = btnSource.firstChild;
    var oRow = btnSource.parentElement.parentElement;
    var oTBody = oRow.parentElement;
    var bShow = oImg.src.indexOf("btnShowDetails.gif") >= 0;

    if (bShow)
    {
        oImg.src = "images/btnHideDetails.gif";
        var oNewCell;
        if (!oRow.hasExpanded)
        {
            oRow.hasExpanded = true;
            var oNewRow = oTBody.insertRow(oRow.rowIndex);
            var oNewCell = oNewRow.insertCell();
            oNewCell.colSpan = oRow.cells.length;
            oNewCell.innerHTML = "Please wait...";
            /*
            oNewCell.innerHTML = "<iframe border='0' width='100%' height='100%'></iframe>";
            var oFrame = oNewCell.firstChild;
            oFrame.src = contentUrl;
            */

            var objHttp = new HttpClient(function(result) { oNewCell.innerHTML = result; });
            objHttp.noCache = true;
            objHttp.allowAsync = true;
            objHttp.getAsText(contentUrl, "");
        }
        else
        {
            oNewCell = oRow.nextSibling.firstChild;
            var oNewRow = oNewCell.parentElement;
            oNewRow.style.display = "inline";
        }
        for (var i = 0; i < oRow.cells.length; i++)
        {
            var oStyle = oRow.cells[i].style;
            oRow.cells[i].oldCssText = oStyle.cssText;
            oStyle.borderBottom = "0px solid #ffffff";
        }
    }
    else
    {
        oImg.src = "images/btnShowDetails.gif";
        oRow.nextSibling.style.display = "none";


        for (var i = 0; i < oRow.cells.length; i++)
        {
            oRow.cells[i].style.cssText = oRow.cells[i].oldCssText;
        }
    }
    return false;
}

function getFieldSelectionCount(oForm, elementName)
{
    var iCount = 0;
    if (oForm)
    {
        var oElement = oForm.elements[elementName];
        if (oElement)
        {
            if (!oElement.length) { oElement = [oElement]; }
            for (var i = 0; i < oElement.length; i++)
            {
                if (oElement[i].checked) { iCount++; }
            }
        }
    }
    return iCount;
}

function loadFrame(sFrameName, sUrl)
{
    sUrl = appendParameter(sUrl, "_rnd", CoreLib_URLEncode(CoreLib_getUniqueNumber()));
    var oFrame = self.parent.frames[sFrameName];
    if (oFrame)
    {
        oFrame.document.location = sUrl;
    }
}

function GridView_showSpanningRowsAndCollapseOthers(oElementInRow, bDeselectAll)
{
    var oBody = findAncestor(oElementInRow, "tbody");
    var oTargetTr = findAncestor(oElementInRow, "tr");
    if (!oTargetTr || !oBody) { return; }
    for (var oTr = oBody.firstChild; oTr; oTr = oTr.nextSibling)
    {
        if (!oTr.isSpanning)
        {
            GridView_showSpanningRows(oTr, oTargetTr == oTr && !bDeselectAll);
        }
    }
}

function GridView_showSpanningRows(oImg, bShow)
{
    if (window['event']) { event.cancelBubble = true; }
    var oImgSeek = findFirstDescendent(oImg, "img");
    var iImageIdx;
    var oImg = (oImgSeek && oImgSeek.src && (iImageIdx = oImgSeek.src.indexOf("images/tree")) >= 0) ? oImgSeek : oImg;
    bShow = isUndefined(bShow) ? (!oImg.src || oImg.src.indexOf("tree_minus.gif") < 0) : bShow;
    oImg.src = oImg.src.substring(0, iImageIdx) + ((bShow) ? "images/tree_minus.gif" : "images/tree_plus.gif");
    var oRow = findAncestor(oImg, "tr");
    for (var i = 0; i < oRow.cells.length; i++)
    {
        if (bShow)
        {
            oRow.cells[i].oldBorder = oRow.cells[i].style.borderBottom;
            oRow.cells[i].style.borderBottom = "solid 0px #fff";
        }
        else
        {
            oRow.cells[i].style.borderBottom = (oRow.cells[i].oldBorder) ? oRow.cells[i].oldBorder : "solid 1px #c8d7e3";
        }
    }
    for (var oNextRow = oRow.nextSibling; oNextRow && getExpandoAttribute(oNextRow, 'isSpanning'); oNextRow = oNextRow.nextSibling)
    {
        oNextRow.style.display = (bShow) ? "table-row" : "none";
        if (bShow)
        {
            var oContentCell = oNextRow.lastChild;
            var sAsyncUrl = getExpandoAttribute(oContentCell, 'asyncUrl');
            if (sAsyncUrl)
            {
                asyncLoadContent(oContentCell, sAsyncUrl);
                setExpandoAttribute(oContentCell, 'asyncUrl', null);
            }
        }
    }
    return false;
}

function syncRequest(sUrl)
{
    var objHttp = new HttpClient();
    objHttp.noCache = true;
    objHttp.allowAsync = false;
    objHttp.getAsText(sUrl);
    return false;
}

function asyncLoadContent(oElement, sUrl, sPayload)
{
    var objHttp = new HttpClient(function(sContent)
    {
        if (oElement)
        {
            if (sContent && (sContent.substring(0, 6) == "<html>" || sContent.substring(0, 6) == "<!DOCT"))
            {
                alert("Sorry, a problem was encountered processing your request.");
                return;
            }
            oElement.innerHTML = sContent;
        }
    });
    objHttp.noCache = true;
    objHttp.allowAsync = true;
    objHttp.getAsText(sUrl, sPayload);
    return false;
}

function syncLoadContent(oElement, sUrl, sPayload)
{
    var x = 0; // dummy code to put in remmed out loop - Tim:  Fix this better - thanks!
    var objHttp = new HttpClient(function(sContent)
    {
        if (sContent != null && sContent.length > 5 && sContent.substr(0, 5) == "<html")
        {
            x = 1; // just to put something harmless here
            // alert("There was a problem processing your request.  You may need to logon again.");
        }
        else
        {
            oElement.innerHTML = sContent;
        }
    });
    objHttp.noCache = true;
    objHttp.allowAsync = false;
    objHttp.getAsText(sUrl, sPayload);
    return false;
}

function showSelectTip(oSelectTag, sTip)
{
    var oTooltip = CoreLib_getElementById("selectTooltip");
    if (!oTooltip)
    {
        oTooltip = CoreLib_createTag("div");
        oTooltip.style.display = "none";
        oTooltip.id = "selectTooltip";
        oTooltip.className = "Tooltip";
        document.body.insertAdjacentElement('afterBegin', oTooltip);
    }
    oTooltip.innerText = sTip + ":";
    oTooltip.style.width = oSelectTag.offsetWidth;
    var iLeft = CoreLib_absoluteLeft(oSelectTag) - document.body.scrollLeft;
    var iTop = CoreLib_absoluteTop(oSelectTag) - oTooltip.offsetHeight - document.body.scrollTop;
    popupDiv(oTooltip.id, oSelectTag, false, iLeft, iTop);
}

function hideSelectTip(oElement)
{
    hidePopupDiv();
}

function initTextAreaEmptyMessages()
{
    for (var iFormIdx = 0; iFormIdx < document.forms.length; iFormIdx++)
    {
        var aElements = document.forms[iFormIdx].elements;
        if (!aElements) { continue; }
        if (!aElements.length) { aElements = [aElements]; }
        for (var iElementIdx = 0; iElementIdx < aElements.length; iElementIdx++)
        {
            var oElement = aElements[iElementIdx];
            initTextAreaEmptyMessage(oElement);
        }
    }
}

function initTextAreaEmptyMessage(oElement)
{
    var sEmptyMessage;
    
    if (oElement
        && oElement.tagName
        && (oElement.tagName.toLowerCase() == "textarea" || oElement.tagName.toLowerCase() == "input")
        && (sEmptyMessage = getExpandoAttribute(oElement, 'emptyMessage'))
        && oElement.onfocus != textArea_onFocus)
    {
        oElement.emptyMessage = sEmptyMessage;
        if (!oElement.origClassName) 
        {
            oElement.origClassName = oElement.className;
        }
        oElement.onfocus = function() { textArea_onFocus.call(oElement); }
        oElement.onblur = function() { textArea_onBlur.call(oElement); }
        oElement.onblur();
    }
}

function isElementOfTag(oElement, sTagName)
{
    return (oElement && sTagName && oElement.tagName && oElement.tagName.toLowerCase() == sTagName.toLowerCase());
}

function textArea_onFocus()
{
    if (this.value == this.emptyMessage)
    {
        this.value = "";
        this.className = this.origClassName;
    }
}

function textArea_onBlur()
{
    this.blur();
    if (!this.value || this.value == this.emptyMessage)
    {
        this.value = this.emptyMessage;
        this.className = "DisabledText";
    }
}

function coerceToArray(vItem)
{
    return (isUndefined(vItem) || vItem == null) ? [] : (!vItem.length) ? [vItem] : vItem;
}

function submitParentForm(oElement, sAction)
{
    var oForm = findAncestor(oElement, "form");
    if (!oForm) { return; }
    if (sAction) { oForm.action = sAction; }
    oForm.submit();
    return false;
}

function isVisible(oElement)
{
    if (!oElement)
    {
        return false;
    }
    if (oElement.length)
    {
        oElement = oElement[0];
    }
    for (; oElement; oElement = oElement.parentElement)
    {
        if (oElement.currentStyle.display == "none" || oElement.currentStyle.visibility == "hidden")
        {
            return false;
        }
    }
    return true;
}

/* Client-side access to querystring name=value pairs
Version 1.2.3
22 Jun 2005
Adam Vandenberg
*/
function Querystring(qs)
{ // optionally pass a querystring to parse
    this.params = new Object()
    this.get = Querystring_get

    if (qs == null)
        qs = location.search.substring(1, location.search.length)

    if (qs.length == 0) return

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ')
    var args = qs.split('&') // parse out name/value pairs separated via &

    // split out each name=value pair
    for (var i = 0; i < args.length; i++)
    {
        var value;
        var pair = args[i].split('=')
        var name = unescape(pair[0])

        if (pair.length == 2)
            value = unescape(pair[1])
        else
            value = name

        this.params[name] = value
    }
}

function Querystring_get(key, default_)
{
    // This silly looking line changes UNDEFINED to NULL
    if (default_ == null) default_ = null;

    var value = this.params[key]
    if (value == null) value = default_;

    return value
}

// setStyleByClass: given an element type and a class selector,
// style property and value, apply the style.
// args:
//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value
function setStyleByClass(t, c, p, v)
{
    var elements;
    if (t == '*')
    {
        // '*' not supported by IE/Win 5.5 and below
        elements = (document.all) ? document.all : document.getElementsByTagName('*');
    } else
    {
        elements = document.getElementsByTagName(t);
    }
    for (var i = 0; i < elements.length; i++)
    {
        var node = elements.item(i);
        for (var j = 0; j < node.attributes.length; j++)
        {
            if (node.attributes.item(j).nodeName == 'class')
            {
                if (node.attributes.item(j).nodeValue == c)
                {
                    eval('node.style.' + p + " = '" + v + "'");
                }
            }
        }
    }
}

function changeStarRating(oTarget, bApplyChange)
{
    var oSpan;
    var iRating;
    if (oTarget.tagName.toLowerCase() == "a")
    {
        // A star is being clicked or hovered over
        oTarget.blur();
        oSpan = oTarget.parentNode;
        for (iRating = 0; oTarget; oTarget = oTarget.previousSibling, iRating++);
    }
    else
    {
        // The mouse is leaving the <span>
        oSpan = oTarget;
        iRating = parseInt(oSpan.previousSibling.value) & 0xff;
    }

    var oField = oSpan.previousSibling;
    if (oField.disabled)
    {
        // Field is disabled--do nothing
        return;
    }

    var aoStars = oSpan.children;
    if (bApplyChange)
    {
        if (parseInt(oField.value) == iRating)
        {
            // Toggle from rated to unrated
            iRating = 0;
        }
        oField.value = (oField.value & ~0xff) | (iRating & 0xff);
    }
    var sStarOnHref = getExpandoAttribute(oField, 'starOnHref');
    var sStarOffHref = getExpandoAttribute(oField, 'starOffHref');
    for (var i = 0; i < aoStars.length; i++)
    {
        aoStars[i].firstChild.src = (iRating > i) ? sStarOnHref : sStarOffHref;
    }
    return false;
}

function timePickerChange(oPicker)
{
    var oField = oPicker.parentElement.previousSibling;
    var aoSelects = new Array();
    for (var oSelect = oPicker.parentElement.firstChild; oSelect; oSelect = oSelect.nextSibling)
    {
        if (oSelect.tagName && oSelect.tagName.toLowerCase() == "select")
        {
            aoSelects.push(oSelect);
        }
    }

    if (oPicker.selectedIndex == 0)
    {
        for (var i = 0; i < aoSelects.length; i++)
        {
            aoSelects[i].selectedIndex = 0;
        }
        oField.value = "";
    }
    else
    {
        for (var i = 0; i < aoSelects.length; i++)
        {
            if (aoSelects[i].selectedIndex <= 0)
            {
                aoSelects[i].selectedIndex = 1;
            }
        }
        oField.value = aoSelects[0].options[aoSelects[0].selectedIndex].value + ":" + aoSelects[1].options[aoSelects[1].selectedIndex].value + " " + aoSelects[2].options[aoSelects[2].selectedIndex].value;
    }
}

function replaceContent(oPlaceholder, oContent)
{
    try
    {
        oPlaceholder.replaceNode(oContent);
        oContent.style.display = "";
    }
    catch (e)
    {
        // Ignore error.
    }
}

function htmlEncode(text)
{
    g_oHtmlEncodeElement = g_oHtmlEncodeElement || document.createElement("div");
    g_oHtmlEncodeElement.innerText = g_oHtmlEncodeElement.textContent = text;
    return g_oHtmlEncodeElement.innerHTML;
}

function htmlDecode(html)
{
    g_oHtmlEncodeElement = g_oHtmlEncodeElement || document.createElement("div");
    g_oHtmlEncodeElement.textContent = g_oHtmlEncodeElement.innerText = "";
    g_oHtmlEncodeElement.innerHTML = html;
    return g_oHtmlEncodeElement.innerText || g_oHtmlEncodeElement.textContent;
}

function toggleYesNoImage(oLink)
{
    var oImg = oLink.firstChild;
    var oHidden = oLink.nextSibling;
    oHidden.value = (oHidden.value == "1") ? "0" : "1";

    var sNewImgSrc = oImg.altSrc;
    oImg.altSrc = oImg.src;
    oImg.src = sNewImgSrc;

    var sNewTitle = oLink.altTitle;
    oLink.altTitle = oLink.title;
    oLink.title = sNewTitle;

    if (oHidden.onchange)
    {
        oHidden.onchange.call(oHidden);
    }

    return false;
}

function getExpandoAttribute(oTag, sAttribute)
{
    if (oTag)
    {
        if (oTag[sAttribute])
        {
            return oTag[sAttribute];
        }
        if (oTag.attributes)
        {
            var attrib = oTag.attributes.getNamedItem(sAttribute);
            if (attrib)
            {
                return attrib.value;
            }
        }
    }
}

function setExpandoAttribute(oTag, sAttribute, oValue)
{
    try
    {
        oTag.attributes.removeNamedItem(sAttribute);
    }
    catch (e)
    {
    }
    oTag[sAttribute] = (oValue === null) ? "" : oValue;
}

// Adds a getElementsByClassName method to the document object if the browser has not yet defined it.
// Currently FireFox defines this method but IE does not.
// The method returns an array of all DOM elements whose class attribute matches the className parameter.
document.getElementsByClassName = document.getElementsByClassName || function(className, parentElement)
{
    var classNamePattern = new RegExp().compile("(^|\\s)" + className + "(\\s|$)"); // regex finds the class name regardless of whether it's the only class or one amongst several
    var children = (document.getElementById(parentElement) || document.body).getElementsByTagName('*');
    var elements = [], child;
    for (var i = 0, length = children.length; i < length; i++)
    {
        child = children[i];
        if (classNamePattern.test(child.className))
        {
            elements.push(child);
        }
    }
    return elements;
};


/////////////////////////////////////////////////
// Selector control
/////////////////////////////////////////////////

function Selector_select(sName, bSelect, bInit)
{
    var iNumSelected = 0;

    var aoElements = document.forms[0][sName];
    if (aoElements)
    {
        if (!isArray(aoElements) === undefined)
        {
            aoElements = [aoElements];
        }

        for (var i = 0; i < aoElements.length; i++)
        {
            var oElement = aoElements[i];
            if (bSelect !== undefined)
            {
                oElement.checked = bSelect;
            }
            if (oElement.checked)
            {
                iNumSelected++;
            }
            if (bInit !== undefined)
            {
                Selector_hookSelect(sName, oElement, 'onclick');
                Selector_hookSelect(sName, oElement, 'onchange');
            }
        }
    }

    var divLabel = getById("Selector_" + sName);
    divLabel.innerHTML = iNumSelected + " of " + aoElements.length;
    return false;
}

function Selector_hookSelect(sName, oElement, sEvent)
{
    var oldChange = oElement[sEvent];
    oElement[sEvent] = function()
    {
        Selector_select(sName);
        if (oldChange)
        {
            oldChange.call(this, arguments);
        }
    };
}

function getInputFields(vName, vRef)
{
    var aCheckboxes = (typeof(vName) == "string") ? getById(vName) : vName;
    if (!aCheckboxes)
    {
        var oForm = findAncestor(vRef, "form");
        if (oForm)
        {
            aCheckboxes = oForm.elements[vName];
        }
        if (!aCheckboxes)
        {
            return null;
        }
    }

    if (aCheckboxes.length == undefined)
    {
        aCheckboxes = aCheckboxes.form.elements[aCheckboxes.name];
        if (aCheckboxes.length == undefined)
        {
            aCheckboxes = [aCheckboxes];
        }
    }
    return aCheckboxes;
}

function selectAll(vName, bSelectAll, vRef)
{
    var aCheckboxes = getInputFields(vName, vRef);
    for (var i = 0; i < aCheckboxes.length; i++)
    {
        aCheckboxes[i].checked = bSelectAll;
    }
    return false;
}

function selectAllText(oTextBox)
{
    oTextBox.focus();
    oTextBox.select();
}