var ModalDialogApplicationRoot = "../";

function ModalDialogCancelEvent() {
    return false;
}

function ModalDialogGetDocWidth() {
    var sizer = document.getElementById('modalWindowSizer');
    sizer.style.display = "block";
    var docWidth = sizer.offsetLeft - 3;
    sizer.style.display = "none";
    return docWidth;
}

function ModalDialogGetDocHeight() {
    var sizer = document.getElementById('modalWindowSizer');
    sizer.style.display = "block";
    var docHeight = sizer.offsetTop - 18;
    sizer.style.display = "none";
    return docHeight;
}

//ModalDialog
//object controlling an html based dialog box in the window.
//use the "show()" method to display the dialog
//set the method property "onclose()" to a custom defined method to be notified when the dialog closes
//the dialog is modal only in regards to user input on the html document for the winParent specified
//
//parameters:
//  url                 The url to display in the dialog box
//  title               (optional - default is "") The title text to display on the dialog box
//  customArguments     (optional - default is null) custom arguments to provide to the child frame's document via window.dialogArguments
//  width               (optional - default is 400) The width of the dialog box in pixels; must be greater than 100
//  height              (optional - default is 300) The height of the dialog box in pixels; must be greater than 100
//  overlayColor        (optional - default is white) the hexidecimal color of the semi-transparent overlay preventing access to the rest of the page.
//
//
//Getting values/closing the popup
//  the returnValue property is set after the dialog is closed
//  the iframe property provides access to the popup's page, but will not be available after onclose() executes
//  assign a function to the onclose event of the returned object to capture when the dialog is closed.
function ModalDialog(owner, url, title, customArguments, width, height, overlayColor) {
    var winParent = window.top;
    var bodyTags = winParent.document.documentElement.getElementsByTagName("body");
    if (0 < bodyTags.length) {
        this.owner = owner;
        this.customArguments = customArguments;

        if (!title && title != 0) {
            title = "";
        }

        if (!width) {
            width = 400;
        }

        if (!height) {
            height = 300;
        }

        if (!overlayColor) {
            overlayColor = "white";
        }

        var docWidth = ModalDialogGetDocWidth();
        var docHeight = ModalDialogGetDocHeight();

        var top = (((docHeight - height) / 2));
        if (top < 0) {
            top = 0;
        }

        var left = (((docWidth - width) / 2));
        if (left < 0) {
            left = 0;
        }

        if (width > docWidth) {
            width = docWidth;
        }

        if (height > docHeight) {
            height = docHeight;
        }

        this.dialogOverlay = document.getElementById("modalDialogTemplate").cloneNode(true);
        this.dialogOverlay.id = "";
        this.dialogOverlay.getElementsByTagName("div").item(0).style.backgroundColor = overlayColor;
        bodyTags.item(0).appendChild(this.dialogOverlay);

        var dialogBox = this.dialogOverlay.getElementsByTagName("table").item(0);
        dialogBox.objRef = this;
        this.oldSelectStart = document.onselectstart;
        this.oldMouseDown = document.onmousedown;
        document.onselectstart = ModalDialogCancelEvent;
        document.onmousedown = ModalDialogCancelEvent;

        dialogBox.style.top = top + "px";
        dialogBox.style.left = left + "px";

        var headerCell = dialogBox.getElementsByTagName("th").item(0);
        headerCell.firstChild.data = title;

        var iframe = dialogBox.getElementsByTagName("iframe").item(0);
        iframe.parentNode.style.width = (width - 8) + "px";
        iframe.parentNode.style.height = (height - (headerCell.offsetHeight + 30)) + "px";
        iframe.objRef = this;

        this.objRef = this;
        this.iframe = iframe;

        var closeButton = dialogBox.getElementsByTagName("input").item(0);
        closeButton.objRef = this;
        closeButton.onclick = DialogControlCloseDialog;

        this.dialogBox = dialogBox;
        this.show = DialogControlShowDialog;
        this.onclose = null;

        if (iframe.addEventListener) {
            iframe.addEventListener("load", DialogControlSetupContentWindow, false);
            this.owner.addEventListener("unload", DialogControlUnloadWindow, false);
        }
        else if (iframe.attachEvent) {
            iframe.attachEvent("onload", DialogControlSetupContentWindow);
            this.owner.attachEvent("onunload", DialogControlUnloadWindow);
        }

        iframe.setAttribute("src", ModalDialogApplicationRoot + url);

        this.ConstrainToWindow = function() {
            var bottom = ModalDialogGetDocHeight();
            var right = ModalDialogGetDocWidth();
            var nonFrameHeight = this.dialogBox.offsetHeight - this.iframe.offsetHeight;
            var nonFrameWidth = this.dialogBox.offsetWidth - this.iframe.offsetWidth;

            if (this.dialogBox.offsetTop < 0) {
                this.dialogBox.style.top = "0px";
            }
            if (this.dialogBox.offsetLeft < 0) {
                this.dialogBox.style.left = "0px";
            }

            if ((this.dialogBox.offsetTop + this.dialogBox.offsetHeight) > bottom) {
                this.iframe.parentNode.style.height = (bottom - (this.dialogBox.offsetTop + nonFrameHeight)) + "px";
            }

            if ((this.dialogBox.offsetLeft + this.dialogBox.offsetWidth) > right) {
                this.iframe.parentNode.style.width = (right - (this.dialogBox.offsetLeft + nonFrameWidth)) + "px";
            }
        }

        this.AttachDragHandlers = function(drag, innerDrag, endDrag, innerEndDrag) {
            if (window.document.documentElement.addEventListener) {
                window.document.documentElement.addEventListener("mousemove", drag, false);
                window.document.documentElement.addEventListener("mouseup", endDrag, false);
                if (this.iframe.contentWindow && this.iframe.contentWindow.document && this.iframe.contentWindow.document.documentElement) {
                    this.iframe.contentWindow.document.documentElement.addEventListener("mousemove", innerDrag, false);
                    this.iframe.contentWindow.document.documentElement.addEventListener("mouseup", innerEndDrag, false);
                }
                else {
                    this.delayedInnerDragHandler = innerDrag;
                    this.delayedInnerEndDragHandler = innerEndDrag;
                }
            }
            else if (window.document.documentElement.attachEvent) {
                window.document.documentElement.attachEvent("onmousemove", drag);
                window.document.documentElement.attachEvent("onmouseup", endDrag);
                if (this.iframe.contentWindow && this.iframe.contentWindow.document && this.iframe.contentWindow.document.documentElement) {
                    this.iframe.contentWindow.document.documentElement.attachEvent("onmousemove", innerDrag);
                    this.iframe.contentWindow.document.documentElement.attachEvent("onmouseup", innerEndDrag);
                }
                else {
                    this.delayedInnerDragHandler = innerDrag;
                    this.delayedInnerEndDragHandler = innerEndDrag;
                }
            }
        }

        this.DetachDragHandlers = function(drag, innerDrag, endDrag, innerEndDrag) {
            if (window.document.documentElement.removeEventListener) {
                window.document.documentElement.removeEventListener("mousemove", drag, false);
                window.document.documentElement.removeEventListener("mouseup", endDrag, false);
                if (this.iframe.contentWindow && this.iframe.contentWindow.document && this.iframe.contentWindow.document.documentElement) {
                    this.iframe.contentWindow.document.documentElement.removeEventListener("mousemove", innerDrag, false);
                    this.iframe.contentWindow.document.documentElement.removeEventListener("mouseup", innerEndDrag, false);
                }
            }
            else if (window.document.documentElement.detachEvent) {
                window.document.documentElement.detachEvent("onmousemove", drag);
                window.document.documentElement.detachEvent("onmouseup", endDrag);
                if (this.iframe.contentWindow && this.iframe.contentWindow.document && this.iframe.contentWindow.document.documentElement) {
                    this.iframe.contentWindow.document.documentElement.detachEvent("onmousemove", innerDrag);
                    this.iframe.contentWindow.document.documentElement.detachEvent("onmouseup", innerEndDrag);
                }
            }

            this.delayedInnerDragHandler = null;
            this.delayedInnerEndDragHandler = null;
        }

        this.BeginDrag = function(left, top) {
            window.DragActiveDialogControl = this;
            this.iframe.contentWindow.DragActiveDialogControl = this;

            this.mouseDragOffsetTop = this.dialogBox.offsetTop - top;
            this.mouseDragOffsetLeft = this.dialogBox.offsetLeft - left;
            this.mouseDragInnerOffsetLeft = this.mouseDragOffsetLeft;
            this.mouseDragInnerOffsetTop = this.mouseDragOffsetTop;

            var innerElement = this.iframe;
            while (innerElement != this.dialogBox) {
                this.mouseDragInnerOffsetLeft += innerElement.offsetLeft;
                this.mouseDragInnerOffsetTop += innerElement.offsetTop;
                innerElement = innerElement.offsetParent;
            }

            this.AttachDragHandlers(this.DragHandler, this.InnerDragHandler, this.EndDragHandler, this.InnerEndDragHandler);
        }

        this.DoDrag = function(left, top) {
            this.dialogBox.style.left = left + "px";
            this.dialogBox.style.top = top + "px";
        }

        this.DragHandler = function(e) {
            if (!e) {
                e = event;
            }

            var dialog = window.DragActiveDialogControl;
            if (dialog) {
                dialog.DoDrag(e.clientX + dialog.mouseDragOffsetLeft, e.clientY + dialog.mouseDragOffsetTop);
            }
        }

        this.InnerDragHandler = function(e) {
            if (!e) {
                e = event;
            }

            var dialog = window.DragActiveDialogControl;
            if (dialog) {
                var box = dialog.dialogBox;
                dialog.DoDrag(e.clientX + box.offsetLeft + dialog.mouseDragInnerOffsetLeft, e.clientY + box.offsetTop + dialog.mouseDragInnerOffsetTop);
            }
        }

        this.DoEndDrag = function(e, isInner) {
            if (isInner) {
                this.InnerDragHandler(e);
            }
            else {
                this.DragHandler(e);
            }
            this.DetachDragHandlers(this.DragHandler, this.InnerDragHandler, this.EndDragHandler, this.InnerEndDragHandler);
            this.ConstrainToWindow();
        }

        this.EndDragHandler = function(e) {
            var dialog = window.DragActiveDialogControl;
            if (dialog) {
                dialog.DoEndDrag(e, false);
            }
        }

        this.InnerEndDragHandler = function(e) {
            var dialog = window.DragActiveDialogControl;
            if (dialog) {
                dialog.DoEndDrag(e, true);
            }
        }

        this.BeginResizeTop = function(yPos) {
            this.DoResizeVertical = this.DoResizeTop;
            this.referenceValueIFrameHeight = Number(this.iframe.parentNode.offsetHeight) + Number(yPos);
            this.referenceValueDialogBottom = this.dialogBox.offsetTop + this.dialogBox.offsetHeight;
            this.BeginResizeVertical(yPos);
        }

        this.DoResizeTop = function(yPos) {
            var newFrameHeight = this.DoResizeBottom(0 - yPos);
            this.dialogBox.style.top = this.referenceValueDialogBottom - (newFrameHeight + this.nonFrameHeight) + "px";
        }

        this.BeginResizeLeft = function(xPos) {
            this.DoResizeHorizontal = this.DoResizeLeft;
            this.referenceValueIFrameWidth = Number(this.iframe.parentNode.offsetWidth) + Number(xPos);
            this.referenceValueDialogRight = this.dialogBox.offsetLeft + this.dialogBox.offsetWidth;
            this.BeginResizeHorizontal(xPos);
        }

        this.DoResizeLeft = function(xPos) {
            var newFrameWidth = this.DoResizeRight(0 - xPos);
            this.dialogBox.style.left = this.referenceValueDialogRight - (newFrameWidth + this.nonFrameWidth) + "px";
        }

        this.BeginResizeRight = function(xPos) {
            this.DoResizeHorizontal = this.DoResizeRight;
            this.referenceValueIFrameWidth = this.iframe.parentNode.offsetWidth - xPos;
            this.BeginResizeHorizontal(xPos);
        }

        this.DoResizeRight = function(xPos) {
            var newFrameWidth = this.referenceValueIFrameWidth + xPos;

            if (newFrameWidth < this.minResizeWidth) {
                newFrameWidth = this.minResizeWidth;
            }

            this.iframe.parentNode.style.width = newFrameWidth + "px";
            if (this.dialogBox.offsetWidth > (newFrameWidth + this.nonFrameWidth)) {
                newFrameWidth = this.dialogBox.offsetWidth - this.nonFrameWidth;
                this.iframe.parentNode.style.width = newFrameWidth + "px";
            }

            return newFrameWidth;
        }

        this.BeginResizeBottom = function(yPos) {
            this.DoResizeVertical = this.DoResizeBottom;
            this.referenceValueIFrameHeight = this.iframe.parentNode.offsetHeight - yPos;
            this.BeginResizeVertical(yPos);
        }

        this.DoResizeBottom = function(yPos) {
            var newFrameHeight = this.referenceValueIFrameHeight + yPos;

            if (newFrameHeight < this.minResizeHeight) {
                newFrameHeight = this.minResizeHeight;
            }

            this.iframe.parentNode.style.height = newFrameHeight + "px";
            if (this.dialogBox.offsetHeight > (newFrameHeight + this.nonFrameHeight)) {
                newFrameHeight = this.dialogBox.offsetHeight - this.nonFrameHeight;
                this.iframe.parentNode.style.height = newFrameHeight + "px";
            }

            return newFrameHeight;
        }

        this.BeginResizeVertical = function(yPos) {
            window.DragActiveDialogControl = this;
            this.iframe.contentWindow.DragActiveDialogControl = this;

            this.nonFrameHeight = this.dialogBox.offsetHeight - this.iframe.parentNode.offsetHeight;
            this.minResizeHeight = 100;
            this.innerFrameOffsetY = 0;

            var innerElement = this.iframe;
            while (innerElement != this.dialogBox) {
                this.innerFrameOffsetY += Number(innerElement.offsetTop);
                innerElement = innerElement.offsetParent;
            }

            this.AttachDragHandlers(this.ResizeVerticalHandler, this.InnerResizeVerticalHandler, this.EndResizeVerticalHandler, this.InnerEndResizeVerticalHandler);
        }

        this.ResizeVerticalHandler = function(e) {
            if (!e) {
                e = event;
            }

            var dialog = window.DragActiveDialogControl;
            if (dialog) {
                dialog.DoResizeVertical(e.clientY);
            }
        }

        this.InnerResizeVerticalHandler = function(e) {
            if (!e) {
                e = event;
            }

            var dialog = window.DragActiveDialogControl;
            if (dialog) {
                dialog.DoResizeVertical(e.clientY + dialog.dialogBox.offsetTop + dialog.innerFrameOffsetY, true);
            }
        }

        this.BeginResizeHorizontal = function(xPos) {
            window.DragActiveDialogControl = this;
            this.iframe.contentWindow.DragActiveDialogControl = this;

            this.nonFrameWidth = this.dialogBox.offsetWidth - this.iframe.parentNode.offsetWidth;
            this.minResizeWidth = 100;
            this.innerFrameOffsetX = 0;

            var innerElement = this.iframe;
            while (innerElement != this.dialogBox) {
                this.innerFrameOffsetX += Number(innerElement.offsetLeft);
                innerElement = innerElement.offsetParent;
            }

            this.AttachDragHandlers(this.ResizeHorizontalHandler, this.InnerResizeHorizontalHandler, this.EndResizeHorizontalHandler, this.InnerEndResizeHorizontalHandler);
        }

        this.ResizeHorizontalHandler = function(e) {
            if (!e) {
                e = event;
            }

            var dialog = window.DragActiveDialogControl;
            if (dialog) {
                dialog.DoResizeHorizontal(e.clientX);
            }
        }

        this.InnerResizeHorizontalHandler = function(e) {
            if (!e) {
                e = event;
            }

            var dialog = window.DragActiveDialogControl;
            if (dialog) {
                dialog.DoResizeHorizontal(e.clientX + dialog.dialogBox.offsetLeft + dialog.innerFrameOffsetX, true);
            }
        }

        this.DoEndResizeVertical = function(e, isInner) {
            if (isInner) {
                this.InnerResizeVerticalHandler(e);
            }
            else {
                this.ResizeVerticalHandler(e);
            }
            this.DetachDragHandlers(this.ResizeVerticalHandler, this.InnerResizeVerticalHandler, this.EndResizeVerticalHandler, this.InnerEndResizeVerticalHandler);
            this.ConstrainToWindow();
        }

        this.EndResizeVerticalHandler = function(e) {
            var dialog = window.DragActiveDialogControl;
            if (dialog) {
                dialog.DoEndResizeVertical(e, false);
            }
        }

        this.InnerEndResizeVerticalHandler = function(e) {
            var dialog = window.DragActiveDialogControl;
            if (dialog) {
                dialog.DoEndResizeVertical(e, true);
            }
        }

        this.DoEndResizeHorizontal = function(e, isInner) {
            if (isInner) {
                this.InnerResizeHorizontalHandler(e);
            }
            else {
                this.ResizeHorizontalHandler(e);
            }
            this.DetachDragHandlers(this.ResizeHorizontalHandler, this.InnerResizeHorizontalHandler, this.EndResizeHorizontalHandler, this.InnerEndResizeHorizontalHandler);
            this.ConstrainToWindow();
        }

        this.EndResizeHorizontalHandler = function(e) {
            var dialog = window.DragActiveDialogControl;
            if (dialog) {
                dialog.DoEndResizeHorizontal(e, false);
            }
        }

        this.InnerEndResizeHorizontalHandler = function(e) {
            var dialog = window.DragActiveDialogControl;
            if (dialog) {
                dialog.DoEndResizeHorizontal(e, true);
            }
        }
    }
    else {
        alert("No HTML body found in the given window.\nDialog box could not be displayed.");
    }
}

function DialogControlDragEvent(e) {
    if (!e) {
        e = event;
    }

    this.clientX = e.clientX;
    this.clientY = e.clientY;
    this.target = null;

    if (e.target) {
        this.target = e.target;
    }
    else if (e.srcElement) {
        this.target = e.srcElement;
    }

    if (this.target && this.target.nodeType == 3) {
        this.target = this.target.parentNode;
    }
}

function GetDialogBoxTable(element) {
    if (element.toLowerCase != "input") {
        while (element.nodeName.toLowerCase() != "table" || element.getAttribute("dialogBox") != "true") {
            element = element.parentNode;
        }
    }
    else {
        element = null;
    }

    return element;
}

function DialogControlUnloadWindow(e) {
    if (this.ActiveDialogControl) {
        this.ActiveDialogControl.iframe.contentWindow.close();
    }
}

function DialogControlBeginDragHandler(e) {
    var evt = new DialogControlDragEvent(e);
    var element = GetDialogBoxTable(evt.target);

    if (element) {
        element.objRef.BeginDrag(evt.clientX, evt.clientY);
    }
}

function DialogControlBeginDragTopHandler(e) {
    var evt = new DialogControlDragEvent(e);
    var element = GetDialogBoxTable(evt.target);

    if (element) {
        element.objRef.BeginResizeTop(evt.clientY);
    }
}

function DialogControlBeginDragTopLeftHandler(e) {
    var evt = new DialogControlDragEvent(e);
    var element = GetDialogBoxTable(evt.target);

    if (element) {
        element.objRef.BeginResizeTop(evt.clientY);
        element.objRef.BeginResizeLeft(evt.clientX);
    }
}

function DialogControlBeginDragLeftHandler(e) {
    var evt = new DialogControlDragEvent(e);
    var element = GetDialogBoxTable(evt.target);

    if (element) {
        element.objRef.BeginResizeLeft(evt.clientX);
    }
}

function DialogControlBeginDragBottomLeftHandler(e) {
    var evt = new DialogControlDragEvent(e);
    var element = GetDialogBoxTable(evt.target);

    if (element) {
        element.objRef.BeginResizeBottom(evt.clientY);
        element.objRef.BeginResizeLeft(evt.clientX);
    }
}

function DialogControlBeginDragTopRightHandler(e) {
    var evt = new DialogControlDragEvent(e);
    var element = GetDialogBoxTable(evt.target);

    if (element) {
        element.objRef.BeginResizeTop(evt.clientY);
        element.objRef.BeginResizeRight(evt.clientX);
    }
}

function DialogControlBeginDragRightHandler(e) {
    var evt = new DialogControlDragEvent(e);
    var element = GetDialogBoxTable(evt.target);

    if (element) {
        element.objRef.BeginResizeRight(evt.clientX);
    }
}

function DialogControlBeginDragBottomRightHandler(e) {
    var evt = new DialogControlDragEvent(e);
    var element = GetDialogBoxTable(evt.target);

    if (element) {
        element.objRef.BeginResizeBottom(evt.clientY);
        element.objRef.BeginResizeRight(evt.clientX);
    }
}

function DialogControlBeginDragBottomHandler(e) {
    var evt = new DialogControlDragEvent(e);
    var element = GetDialogBoxTable(evt.target);

    if (element) {
        element.objRef.BeginResizeBottom(evt.clientY);
    }
}

function DialogControlSetupContentWindow(e) {
    var evt = new DialogControlDragEvent(e);
    if (!evt.target) {
        evt.target = this;
    }

    var frameWindow = evt.target.contentWindow;
    var dialog = evt.target.objRef;
    frameWindow.objRef = dialog;
    if (dialog.delayedInnerDragHandler) {
        if (frameWindow.document.documentElement.addEventListener) {
            frameWindow.document.documentElement.addEventListener("mousemove", dialog.delayedInnerDragHandler, false);
            frameWindow.document.documentElement.addEventListener("mouseup", dialog.delayedInnerEndDragHandler, false);
        }
        else if (frameWindow.document.documentElement.attachEvent) {
            frameWindow.document.documentElement.attachEvent("mousemove", dialog.delayedInnerDragHandler);
            frameWindow.document.documentElement.attachEvent("mouseup", dialog.delayedInnerEndDragHandler);
        }
    }

    frameWindow.owner = evt.target.objRef.owner;
    frameWindow.dialogArguments = evt.target.objRef.customArguments
    frameWindow.close = DialogControlCloseDialog;
    var headerCell = dialog.dialogBox.getElementsByTagName("th").item(0);
    headerCell.firstChild.data = frameWindow.document.title;
    var loadingMessage = evt.target.parentNode.getElementsByTagName("table").item(0);
    loadingMessage.style.visibility = "hidden";
    loadingMessage.style.display = "none";
}

function DialogControlBlank() { }

function DialogControlShowDialog() {
    if (this.dialogOverlay) {
        var winParent = window.top;

        this.owner.ActiveDialogControl = this;

        this.dialogOverlay.style.visibility = "visible";
        this.dialogOverlay.style.display = "block";
        var dialog = this;

        var closeFunction = function(e) {
            if (dialog.iframe && dialog.iframe.contentWindow) {
                dialog.iframe.contentWindow.close();
            }
        }

        if (this.owner.addEventListener) {
            this.owner.addEventListener("unload", closeFunction, false);
        }
        else if (this.owner.attachEvent) {
            this.owner.attachEvent("onunload", closeFunction);
        }
    }
    else {
        alert("Cannot open the dialog box; it appears to have been disposed.");
    }
}

function DialogControlCloseDialog() {
    this.objRef.returnValue = this.objRef.iframe.contentWindow.returnValue;
    if (this.objRef.onclose) {
        this.objRef.onclose();
    }

    var box = this.objRef.dialogBox;
    var overlay = this.objRef.dialogOverlay;
    var iframe = this.objRef.iframe;
    if (iframe.removeEventListener) {
        iframe.removeEventListener("load", DialogControlSetupContentWindow, false);
        this.objRef.owner.removeEventListener("unload", DialogControlUnloadWindow, false);
    }
    else if (iframe.detachEvent) {
        iframe.detachEvent("onload", DialogControlSetupContentWindow);
        this.objRef.owner.detachEvent("onunload", DialogControlUnloadWindow);
    }

    iframe.setAttribute("src", "");
    this.objRef.dialogOverlay = null;
    this.objRef.iframe = null;
    this.objRef.dialogBox = null;
    this.objRef.owner.ActiveDialogControl = null;

    box.style.display = "none";
    overlay.style.display = "none";
    overlay.parentNode.removeChild(overlay);

    if (this.oldSelectStart) {
        document.onselectstart = this.oldSelectStart;
    }
    if (this.oldMouseDown) {
        document.onmousedown = this.oldMouseDown;
    }
    this.oldSelectStart = null;
    this.oldMouseDown = null;
}
