﻿var currentPopupDiv = null;
function togglePopup(e, divId) {
    window.clearTimeout();
    var x = 0;
    var y = 0;
    var sp = getScrollXY();
    if (!e) {
        e = window.event;
        x = e.clientX - 106 + sp[0];
        y = e.clientY - 136 + sp[1];
    }
    else {
        x = e.clientX - 106 + sp[0];
        y = e.clientY - 136 + sp[1];
    }
    if (currentPopupDiv && (currentPopupDiv.id != divId) && currentPopupDiv.style && (currentPopupDiv.style.display != 'none')) {
        currentPopupDiv.style.display = 'none';
    }
    else if (currentPopupDiv && (currentPopupDiv.id == divId)) {
        currentPopupDiv.style.left = x + 'px';
        currentPopupDiv.style.top = y + 'px';
        return;
    }
    var d = document.getElementById(divId);
    if (d && d.style && (d.style.display == 'none')) {
        d.parentNode.removeChild(d);
        document.body.appendChild(d);
        d.style.display = 'block';
        d.style.left = x + 'px';
        d.style.top = y + 'px';
        d.onmouseout = function() {
            closePopup(divId);
        };
        setTimeout('closePopup(\'' + divId + '\')', 4000);
        currentPopupDiv = d;
    }
}
function hidePopup() {
    window.clearTimeout();
    if (currentPopupDiv && currentPopupDiv.style) {
        currentPopupDiv.style.display = 'none';
        currentPopupDiv = null;
    }
}
function closePopup(divId) {
    window.clearTimeout();
    var d = document.getElementById(divId);
    if (d && d.style) {
        d.style.display = 'none';
    }
}

function showWaitPopup(e, txt) {
    var popupId = 'ias7F7427DB0B4840aeBAFF34A6C052FC11';
    var popup = document.getElementById(popupId);
    var x = 0;
    var y = 0;
    if (!popup) {
        var pd = document.createElement('div');
        pd.setAttribute('id', popupId);
        pd.setAttribute('style', 'position: absolute; z-index: 9999999;');
        pd.setAttribute('class', 'ajaxWaiting');
        pd.appendChild(document.createTextNode(txt));
        document.documentElement.appendChild(pd);
    }
    popup = document.getElementById(popupId);
    if (popup) {
        var sp = getScrollXY();
        if (!e) {
            e = window.event;
            x = e.clientX - sp[0];
            y = e.clientY - sp[1];
        }
        else {
            x = e.clientX - sp[0];
            y = e.clientY - sp[1];
        }
        popup.style.display = 'block';
        popup.style.left = x + 'px';
        popup.style.top = y + 'px';
    }
}

function closeWaitPopup() {
    closePopup('ias7F7427DB0B4840aeBAFF34A6C052FC11');
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof(window.pageYOffset) != 'undefined' ) {
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } 
    else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } 
    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [ scrOfX, scrOfY ];
}

function popLink(url, winName, wopts) {
    if (typeof(url.href) != 'undefined') {
        url = url.href;
    }
    var w = window.open(url, (winName != null ? winName : 'iasPopupWindow'), (wopts != null ? wopts : 'status=1,toolbar=1,location=1,menubar=1,resizable=1,scrollbars=1'), true);
    if (w && (typeof(w.focus) != 'undefined')) {
        w.focus();
    }
    return false;
}

function selectAll(selectObjectName) {
    doSelectUpdate(selectObjectName, true);
}

function selectNone(selectObjectName) {
    doSelectUpdate(selectObjectName, false);
}

function doSelectUpdate(selectObjectName, selStatus) {
    var s = (document.getElementById(selectObjectName) != null ? document.getElementById(selectObjectName) : document.forms[0].elements[selectObjectName]);
    if (s != null) {
        for (var i = 0; i < s.options.length; i++) {
            s.options[i].selected = selStatus;
        }
    }
}

function forceSelectsWide(skipThese) {
    var nodes = document.getElementsByTagName('select');
    for (var i = 0; i < nodes.length; i++) {
        if ((nodes.item(i).size != null) && 
             (parseInt(nodes.item(i).size) > 1) && 
             (nodes.item(i).offsetWidth < nodes.item(i).parentNode.offsetWidth) && 
             ((skipThese == null) || ((',' + skipThese + ',').indexOf(',' + nodes.item(i).attributes['id'] + ',') < 0))) {
            //nodes.item(i).style.width = '100%';
            nodes.item(i).style.width = (nodes.item(i).parentNode.offsetWidth - 19) + 'px';
        }
    }
}

// Uses a non-XHTML compliant property to try and trick the browser (Firefox in particular)
// to not show a scrollbar for select boxes
function forceSelectsNoScroll(skipThese) {
    var nodes = document.getElementsByTagName('select');
    for (var i = 0; i < nodes.length; i++) {
        if ((skipThese == null) || ((',' + skipThese + ',').indexOf(',' + nodes.item(i).attributes['id'] + ',') < 0)) {
            nodes.item(i).scrolling = 'no';
            nodes.item(i).scroll = 'no';
        }
    }
}

/**
    Move an item UP in a select box.
    @param lbId - The ID of the select box.
    @param hfId - The ID of the hidden input where the new order (values only) should be stored.
*/
function moveItemUp(lbId, hfId, delimiter) {
    var lb = document.getElementById(lbId);
    var si = lb.selectedIndex;
    if (si > -1) {
        var sval = lb.options[si].value;
        var stxt = lb.options[si].text;
        if (si > 0) {
            lb.remove(si);
            lb.options.add(new Option(stxt, sval), si - 1);
            lb.selectedIndex = si - 1;
        }
    }
    if ((hfId != null) && (document.getElementById(hfId) != null)) {
        var hf = document.getElementById(hfId);
        serializeOrder(lb, hf, (delimiter != null ? delimiter : '|'));
    }
}
/**
    Move an item DOWN in a select box.
    @param lbId - The ID of the select box.
    @param hfId - The ID of the hidden input where the new order (values only) should be stored.
*/
function moveItemDown(lbId, hfId, delimiter) {
    var lb = document.getElementById(lbId);
    var si = lb.selectedIndex;
    if (si > -1) {
        var sval = lb.options[si].value;
        var stxt = lb.options[si].text;
        if (si < (lb.options.length - 1)) {
            lb.remove(si);
            lb.options.add(new Option(stxt, sval), si + 1);
            lb.selectedIndex = si + 1;
        }
    }
    if ((hfId != null) && (document.getElementById(hfId) != null)) {
        var hf = document.getElementById(hfId);
        serializeOrder(lb, hf, (delimiter != null ? delimiter : '|'));
    }
}
/**
    Serialize the order of items in a select box to a hidden field.
    @param lb - The select box.
    @param hf - The hidden input where the new order (values only) should be stored.
    @param delimiter - Th echaracter to split the values - defaults to |.
*/
function serializeOrder(lb, hf, delimiter) {
    var dl = delimiter || '|';
    var tval = '';
    for (var i = 0; i < lb.options.length; i++) {
        tval += dl + lb.options[i].value;
    }
    hf.value = (tval.length > dl.length ? tval.substring(dl.length) : '');
}
function forceSelectedToTop(s) {
    if ((s != null) && (s.options != null)) {
        for (var i = 0; i < s.options.length; i++) {
            if (s.options[i].selected) {
                s.options[i].scrollIntoView(true);
                break;
            }
        }
    }
}
function copyHrefToClipboard(href, linkTag) {
    var copied = false;
    if ((href || linkTag) && window.clipboardData) {
        if (href) {
            clipboardData.setData('Text', href);
        }
        else {
            var ahref = (linkTag.href ? linkTag.href : linkTag.getAttribute('href'));
            clipboardData.setData('Text', ahref);
        }
    }
    else if ((href || linkTag) && (typeof(Components.classes) != 'undefined')) {
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
        var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
        gClipboardHelper.copyString(href); 
    }
}
