﻿var currentTaskMenu = null;
var init = true;

function validateNonEmpty(source, clientside_arguments) {
    if (clientside_arguments.Value != null && clientside_arguments.Value != "") {
        clientside_arguments.IsValid = true;
    } else {
        clientside_arguments.IsValid = false;
    }
}

function setBusy() {
    if (($get('BusyIcon') != 'undefined') && ($get('BusyIcon') != null)) {
        $get('BusyIcon').style.display = 'block';
    }
    document.body.className = 'Busy';
    window.scroll(0, 0);
}

function setBusyNoScroll() {
    var busy = $get('BusyIcon2');
    if (busy) {
        busy.style.display = "block";
    }
    document.body.className = "Busy";
}

function setDone() {
    document.body.className = "";
}

function toggleDisplay(domId) {
    if (init) {
        $addHandler(document, "click", function() {
            if (currentTaskMenu) {
                toggleDisplay(currentTaskMenu.id);
                currentTaskMenu = null;
            }
        });
        init = false;
    }
    var node = $get(domId);
    if (node) {
        node.style.display = node.style.display == 'block' ? 'none' : 'block';
        if (node.style.display == "block") {
            currentTaskMenu = node;
        }
    }
}

function toggleDisplayPerm(domId) {
    var node = $get(domId);
    if (node) {
        node.style.display = node.style.display == 'block' ? 'none' : 'block';
        if (node.style.display == "block") {
            currentTaskMenu = node;
        }
    }
}

function toggleInfo(id) {
    var node = $get(id);
    if (node) {
        node.style.display = node.style.display == 'block' ? 'none' : 'block';
    }
}

/**
* Find the center of the screen, so a new window can be placed in the middle.
* @param w the width of the new window
* @param h the height of the new window
* @return an object containing the top and left coordinates for the new window
*/
function vGetWindowCenter(w, h) {
    var o = new Object();
    var lft = ((screen.width - (w + 32)) / 2);
    var slft = 0;
    // handling multiple monitors
    if (window.screenLeft) {
        // if window is not maximized (or centered) the popup may be sligtly to one or the other side
        slft = window.screenLeft;
    } else if (screen.left) {
        slft = screen.left;
    }
    o.left = lft + slft;
    o.top = (screen.height - (h + 96)) / 2;
    return o;
}
/**
* Move the given element to the center of the browser. Uses fixed positioning if the browser supports it.
*/
function vMoveElementCenter(e) {
    var x = 0;
    var y = 0;
    if (typeof document.body.style.maxHeight != "undefined") { // IE 7, mozilla, safari, opera 9
        e.style.position = "fixed";
        x = (xClientWidth() - xWidth(e)) / 2;
        y = (xClientHeight() - xHeight(e)) / 2;
    } else {// IE6, older browsers
        e.style.position = "absolute";
        x = xScrollLeft() + (xClientWidth() - xWidth(e)) / 2;
        y = xScrollTop() + (xClientHeight() - xHeight(e)) / 2;
    }
    xMoveTo(e, x, y);
}
var vSPInterval = null;
function vShowPopup(u, p, h, w) {
    var pd = xGetElementById('PopupDiv')
    if (pd) {
        if (h) {
            try {
                pd.style.height = h + "px";
            } catch (e) {
                pd.style.height = h;
            }
        }
        if (w) {
            try {
                pd.style.width = w + "px";
            } catch (e) {
                pd.style.width = w;
            }
        }
        pd.style.visibility = "hidden";
        pd.style.display = "block";
        vMoveElementCenter(pd);
        pd.style.visibility = "visible";
        var pf = xGetElementById("PopupFrame");
        if (pf) {
            if (p != "" && !u.endsWith("?")) {
                u += "?";
            }
            pf.src = u + p;
            vSPInterval = setInterval(vCheckPopup, 125);
        }
    }
}
function vCheckPopup() {
    var pf = xGetElementById("PopupFrame");
    if (pf) {
        var loc = "";
        if (pf.contentDocument) {
            loc = pf.contentDocument.location.href;
        } else if (pf.contentWindow) {
            loc = pf.contentWindow.document.location.href;
        } else {
            loc = pf.document.location.href;
        }
        var idx = loc.indexOf("#");
        if (idx >= 0) {
            var c = loc.substring(idx);
            if (c == "#close") {
                vClosePopup(true);
            } else if (c.indexOf("_") >= 0) {
                var sc = c.split('_');
                switch (sc[0]) {
                    case "#close":
                        if (sc[1] == "noreload") {
                            vClosePopup(false);
                        } else {
                            vClosePopup(true);
                        }
                        break;
                }
            }
        }
    }
}
function vClosePopup(reload) {
    var pf = xGetElementById("PopupFrame");
    clearInterval(vSPInterval);
    pf.src = "";
    var pd = xGetElementById("PopupDiv");
    if (pd) {
        pd.style.display = "none";
    }
    if (reload) {
        window.location.href = window.location.href;
    }
}

function dump(a) {
    var srch = arguments.length > 1 ? arguments[1].toString() : ''; var s = ''; var i = 0; if ((null == a) || (a == 'undefined')) { alert(a); } else { for (var x in a) { if ((srch == '') || (x.toString().toLowerCase().indexOf(srch) > -1)) { s += x + ' = ' + (a[x] == null ? '<null>' : a[x]) + '\n'; if (i++ >= 20) { if (confirm(s)) { i = 0; s = ''; continue; }; s = ''; break; }; }; }; if (s != '') { alert(s); }; };
}

