﻿var modalDialogs = new Array();

var transparentImage = document.createElement('IMG');
transparentImage.src = 'i/tr.gif';

var __postponedModalDiv = null;
var __postponedSenderId = null;

var hideModalDialogSource = null;

function GetModalDialogZIndex(control)
{
    var resultIndex = typeof(control.style) == 'undefined' ? 0 : typeof(control.style.zIndex) == 'undefined' ? 0 : control.style.zIndex;
    
    if (resultIndex == '') resultIndex = 0;
    
    var resultControl = this;
        
    if (control.hasChildNodes())
    {
        var childZIndex = 0;
        
        for (var i = 0; i < control.childNodes.length; i++)
        {
            childZIndex = GetModalDialogZIndex(control.childNodes[i]);
            
            if (childZIndex.ZIndex > resultIndex) 
            {
                resultIndex = childZIndex.ZIndex;
                resultControl = childZIndex.Control;
            }
        }
    }
    
    if (typeof(result) == 'string') result = parseInt(result);
    
    return { ZIndex: resultIndex, Control: resultControl };
}
function CorrectBackgroundDivs()
{
    if (modalDialogs.length == 0) return;
    
    var modalDiv = modalDialogs[0];
    var hideDiv = modalDiv.hideDiv;
    
    if (typeof(hideDiv) == 'undefined') return;
    
    hideDiv.style.width = GetHideDivWidth(document.body, true);
    hideDiv.style.height = GetHideDivHeight(document.body, true);
}
function GetModalOpacityProperty()
{
    if (typeof document.body.style.opacity == 'string') // CSS3 compliant (Moz 1.7+, Safari 1.2+, Opera 9, IE7)
        return 'opacity';
    else if (typeof document.body.style.MozOpacity == 'string') // Mozilla 1.6 и младше, Firefox 0.8 
        return 'MozOpacity';
    else if (typeof document.body.style.KhtmlOpacity == 'string') // Konqueror 3.1, Safari 1.1
        return 'KhtmlOpacity';
    else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) // Internet Exploder 5.5+
        return 'filter';

    return false; 
}
function MakeTransparentDiv(div)
{
    if ((typeof(IsTestMode) == 'function') && IsTestMode()) return;

    var opacityProp = GetModalOpacityProperty();
    
    if (opacityProp)
    {
        div.style.backgroundColor = 'White';
    
        if (opacityProp == 'filter') div.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=50)'; 
        else
            div.style[opacityProp] = 0.5;
    }
    else
    {
        div.style.backgroundImage = 'url(' + transparentImage.src + ')';
    }
}
function BodySizeChanged()
{
    CorrectBackgroundDivs();
}
function HideDropDownLists(hideDiv, modalDiv, control)
{
    if (control == modalDiv) return;
    
    if (typeof(control.tagName) == 'string')
    {
        var tagName = control.tagName.toLowerCase();
        
        if (tagName == 'select')
        {
            if (typeof(hideDiv.replacedLists) == 'undefined')
            {
                hideDiv.replacedLists = new Array();
            }
            
            hideDiv.replacedLists.push(control);
            
            var controlStyle = GetNodeStyle(control);
            
            var replaceInput = document.createElement('INPUT');
            
            replaceInput.type = 'text';
            
            replaceInput.style.left = controlStyle.left == "" ? "0px" : controlStyle.left;
            replaceInput.style.top = controlStyle.top == "" ? "0px" : controlStyle.top;        
            replaceInput.style.width = controlStyle.width == "" ? (parseInt(control.scrollWidth) + 1) + 'px' : controlStyle.width;
            replaceInput.style.height = controlStyle.height == "" ? (parseInt(control.scrollHeight) + 1) + 'px' : controlStyle.height;
            
            replaceInput.style.backgroundColor = controlStyle.backgroundColor;
            replaceInput.style.borderStyle = controlStyle.borderStyle;
            replaceInput.style.borderWidth = controlStyle.borderWidth;
            replaceInput.style.borderColor = controlStyle.borderColor;
            
            control.replaceInput = replaceInput;
            
            control.parentNode.replaceChild(replaceInput, control);
        }
    }
    
    if (control.hasChildNodes())
    {
        for (var childIndex = 0; childIndex < control.childNodes.length; childIndex++)
        {
            HideDropDownLists(hideDiv, modalDiv, control.childNodes[childIndex]);
        }
    }
}
function RestoreDropDownLists(hideDiv)
{
    if (typeof(hideDiv.replacedLists) == 'undefined') return;
    
    var currentList;
    
    while (hideDiv.replacedLists.length > 0)
    {
        currentList = hideDiv.replacedLists.pop();
        
        currentList.replaceInput.parentNode.replaceChild(currentList, currentList.replaceInput);
    }
}
function GetHideDivWidth(hidedDiv, isFirstDiv)
{
    if (isFirstDiv)
    {
        var result;
        
        if (typeof(window.innerWidth) != 'undefined')
        {
            result = parseInt(window.innerWidth);
            
            if (typeof(window.scrollMaxX) != 'undefined') 
            {
                result += parseInt(window.scrollMaxX);
            
                if (window.scrollMaxY > 0) result -= 18;
            }
        }
        else
        {
            result = parseInt(hidedDiv.scrollWidth);
        }
        
        return result + 'px';
    }
    else
    {
        return (parseInt(hidedDiv.scrollWidth) + 2) + 'px';
    }
}
function GetHideDivHeight(hidedDiv, isFirstDiv)
{
    if (isFirstDiv)
    {
        var result;
        
        if (typeof(window.innerHeight) != 'undefined')
        {
            result = parseInt(window.innerHeight);
            
            if (typeof(window.scrollMaxY) != 'undefined') 
            {
                result += parseInt(window.scrollMaxY);
            
                if (window.scrollMaxX > 0) result -= 18;
            }
        }
        else
        {
            result = parseInt(hidedDiv.scrollHeight);
        }
        
        return result + 'px';
    }
    else
    {
        return (parseInt(hidedDiv.scrollHeight) + 2) + 'px';
    }
}
var resizeListenerAssigned = false;

function AssignResizeListener()
{
    if (document.body.addEventListener)
    {
        document.body.addEventListener("resize", BodySizeChanged, true);
    }
    else if (document.body.attachEvent)
    {
        document.body.attachEvent('onresize', BodySizeChanged);
    }
    else
    {
        document.body.onresize = BodySizeChanged;
    }

    resizeListenerAssigned = true;
}

function ShowModalDialog(divId, senderId, postponedShow, showDivFunction)
{
    var modalDiv = document.getElementById(divId);
    if (modalDiv == null) return;
    
    if ((modalDialogs.length > 0) && (modalDialogs[modalDialogs.length - 1].id == divId))
    {
        modalDialogs.pop();
    }
    
    modalDialogs.push(modalDiv);
    
    var hidedDiv = modalDialogs.length == 1 ? document.body : modalDialogs[modalDialogs.length - 2];
    
    if (hidedDiv != null)
    {
        var hideDiv;
        
        if (typeof(modalDiv.hideDiv) == 'undefined')
        {
            hideDiv = document.createElement('DIV');
            hideDiv.style.position = 'absolute';
        }
        else
        {
            hideDiv = modalDiv.hideDiv;
        }
        
        if (isIE) HideDropDownLists(hideDiv, modalDiv, hidedDiv);
        
        hideDiv.style.left = hidedDiv.style.left == "" ? "0px" : hidedDiv.style.left;
        hideDiv.style.top = hidedDiv.style.top == "" ? "0px" : hidedDiv.style.top;        
        hideDiv.style.width = GetHideDivWidth(hidedDiv, modalDialogs.length == 1);
        hideDiv.style.height = GetHideDivHeight(hidedDiv, modalDialogs.length == 1);
        
        var maxZIndex = GetModalDialogZIndex(document.body);
        
        if (maxZIndex.Control != hideDiv)
        {
           hideDiv.style.zIndex = maxZIndex.ZIndex + 1;
        }
        
        MakeTransparentDiv(hideDiv);
               
        modalDiv.hideDiv = hideDiv;
        
        modalDiv.parentNode.appendChild(hideDiv);
        
        if (modalDialogs.length == 1) CorrectHideDivPos(hideDiv);
        
        modalDiv.style.zIndex = hideDiv.style.zIndex + 1;
    }
    
    if (typeof(showDivFunction) == 'function') showDivFunction(divId);
    else
        if (typeof(showDivFunction) == 'string') eval(showDivFunction);
        
    if (postponedShow)
    {
        __postponedModalDiv = modalDiv;
        __postponedSenderId = senderId;
    }
    else
    {
        modalDiv.style.display = 'block';
        
        CorrectDivPos(modalDiv, senderId);
    }
}
function ShowPostponedDialog()
{
    if (__postponedModalDiv == null) return;
    
    var modalDiv = __postponedModalDiv;
    
    __postponedModalDiv == null;
    
    modalDiv.style.display = 'block';
    
    CorrectDivPos(modalDiv, __postponedSenderId);
}
function HideModalDialog(hideScript)
{
    if (modalDialogs.length == 0) return;
    
    var modalDiv = modalDialogs.pop();
    
    if (modalDiv == null) return;
    
    modalDiv.style.display = 'none';
    
    if ((typeof(modalDiv.hideDiv) != 'undefined') && (modalDiv.hideDiv.parentNode != null))
    {
        if (isIE) RestoreDropDownLists(modalDiv.hideDiv);
        
        modalDiv.hideDiv.parentNode.removeChild(modalDiv.hideDiv);
    }
    
    if (typeof(hideScript) == 'string') eval(hideScript);
    
    hideModalDialogSource = null;
}
function ClearHideModalDialogSource()
{
    hideModalDialogSource = null;
}
function SubmitModalDialog(source)
{
    hideModalDialogSource = source;
}
function CancelModalDialog(source)
{
    hideModalDialogSource = source;
}
function IsSelfOrParentControl(control, parentCandidate)
{
    if (parentCandidate == null) return false;
    if (parentCandidate.id == '') return false;
    if (parentCandidate.id == control.id) return true;
    
    for (var i = 0; i < parentCandidate.childNodes.length; i++)
    {
        if (IsSelfOrParentControl(control, parentCandidate.childNodes[i])) return true;
    }
    
    return false;
}
function EnableControls(controlsArray)
{
    while (controlsArray.length > 0)
    {
        controlsArray.pop().disabled = false;
    }
}
function GetModalNodeStyle(node)
{
    if (typeof(node.tagName) == 'undefined') return null;
    
    if (node.currentStyle) return node.currentStyle;
    else
        if (typeof(getComputedStyle) == 'function') return node.ownerDocument.defaultView.getComputedStyle(node, null);
        else
            return node.style;
}
function GetModalTopParent(node, mostTopParent)
{
    if (node == null) return null;
    if (node == mostTopParent) return node;
    
    var nodeStyle = GetModalNodeStyle(node);
    
    if ((typeof(nodeStyle) != 'undefined') && (nodeStyle.position == 'absolute')) return node;
    
    return GetModalTopParent(node.parentNode, mostTopParent);
}
function GetModalNodeLeft(topParent, node)
{
    if (node == null) return 0;
    if (node == topParent) return parseInt(node.offsetLeft);
    
    var selfLeft = 0;
    
    var nodeStyle = GetModalNodeStyle(node);
    
    if ((nodeStyle.display == 'inline') && (nodeStyle.position == 'relative'))
    {
        if (node.offsetParent != null) 
        {
            var parentStyle = GetModalNodeStyle(node.offsetParent);
            
            if (parentStyle.display == 'block') selfLeft = parseInt(node.offsetLeft);
        }
    }
    else
    {
        selfLeft = parseInt(node.offsetLeft);
    }
    
    return isNaN(selfLeft) ? 0 : selfLeft + GetModalNodeLeft(topParent, node.offsetParent);
}
function GetModalNodeTop(topParent, node)
{
    if (node == null) return 0;
    if (node == topParent) return parseInt(node.offsetTop);
    
    var selfTop = parseInt(node.offsetTop);
    
    return isNaN(selfTop) ? 0 : selfTop + GetModalNodeTop(topParent, node.offsetParent);
}
function CorrectDivPos(div, senderId)
{
    var sender = document.getElementById(senderId);
    
    var leftPos = 0;
    var topPos = 0;
    
    if (sender == null)
    {
        var screenWidth = document.all ? document.body.offsetWidth : (window.innerWidth ? window.innerWidth : 0);
        var screenHeight = document.all ? document.body.offsetHeight : (window.innerHeight ? window.innerHeight : 0);
        
        leftPos = (screenWidth - div.clientWidth) / 2;
        topPos = (screenHeight - div.clientHeight) / 2;
    }
    else
    {
        var topParent = GetModalTopParent(sender, div.parentNode);
        
        var senderLeft = GetModalNodeLeft(topParent, sender);
        var senderTop = GetModalNodeTop(topParent, sender) + 5;
        
        leftPos = senderLeft;
        topPos = senderTop + sender.offsetHeight;
    }
    
    if (leftPos < 0) leftPos = 0;
    if (topPos < 0) topPos = 0;
    
    div.style.left = leftPos + 'px';
    div.style.top = topPos + 'px';
}
function CorrectHideDivPos(div)
{
    var divLeft = GetModalNodeLeft(document.body, div);
    var divTop = GetModalNodeTop(document.body, div);
    
    div.style.marginLeft = -divLeft + 'px';
    div.style.marginTop = -divTop + 'px';
}

var modalStartLeft;
var modalStartTop;
var mouseStartX = -1000;
var mouseStartY = -1000;
var modalMouseCaptured = false;

function EventObject(e)
{
    return e ? e : window.event;
}

function EventSource(e)
{
    return e.target ? e.target : e.srcElement;
}

function CancelEvent(e)
{
    if (typeof(e.preventDefault) == 'function') e.preventDefault();
    else
        e.cancelBubble = true;
}
function CaptureMouseEvents(div)
{
    if (typeof(Event) != 'undefined') 
    {
        window.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP);
        
        window.onmousemove = ModalDivMouseMove;
        window.onmouseup = ModalDivMouseUp;
    }
    else
    {
        div.setCapture();
        
        div.onmousemove = ModalDivMouseMove;
        div.onmouseup = ModalDivMouseUp;
    }
}
function ReleaseMouseEvents(div)
{
    if (typeof(Event) != 'undefined')
    {
        window.releaseEvents(Event.MOUSEDOWN | Event.MOUSEUP);
        
        window.onmousemove = null;
        window.onmouseup = null;
    }
    else
    {
        div.releaseCapture();
        
        div.onmousemove = null;
        div.onmouseup = null;
    }
}
function ModalDivMouseDown(e)
{
    if (modalDialogs.length == 0) return;
    
    var eventObject = EventObject(e);
    var modalDiv = modalDialogs[modalDialogs.length - 1];
    
    if (!eventObject) return;
    if (!modalDiv) return;
    
    var eventSource = EventSource(eventObject);
    
    if (eventSource.className == 'modalDivHeader')
    {
        modalStartLeft = parseInt(modalDiv.style.left);
        modalStartTop = parseInt(modalDiv.style.top);
        mouseStartX = parseInt(eventObject.screenX);
        mouseStartY = parseInt(eventObject.screenY);
        
        modalMouseCaptured = true;
        
        CaptureMouseEvents(modalDiv, eventSource);
        
        CancelEvent(eventObject);
    }
}

function ModalDivMouseMove(e)
{
    if (modalDialogs.length == 0) return;
    
    if (!modalMouseCaptured) return;
    
    var eventObject = EventObject(e);
    var modalDiv = modalDialogs[modalDialogs.length - 1];
    
    if (!eventObject) return;
    if (!modalDiv) return;
    
    var dx = parseInt(eventObject.screenX) - mouseStartX;
    var dy = parseInt(eventObject.screenY) - mouseStartY;
    
    modalDiv.style.left = (modalStartLeft + dx) + 'px';
    modalDiv.style.top = (modalStartTop + dy) + 'px';
}

function ModalDivMouseUp(e)
{
    if (modalDialogs.length == 0) return;
    
    modalMouseCaptured = false;
    
    var modalDiv = modalDialogs[modalDialogs.length - 1];
    
    if (!modalDiv) return;
    
    ReleaseMouseEvents(modalDiv)
}

if (typeof(AddLoadStateFunction) == 'function') AddLoadStateFunction('CorrectBackgroundDivs', CorrectBackgroundDivs);
