﻿if(!Type.isNamespace("eSalesClient"))
    Type.registerNamespace("eSalesClient");

eSalesClient.GetClientBounds = function() 
{
    var clientWidth;
    var clientHeight;
    switch(Sys.Browser.agent) {
        case Sys.Browser.InternetExplorer:
            if(document.documentElement.clientWidth > 0)
            {
                clientWidth = document.documentElement.clientWidth;
                clientHeight = document.documentElement.clientHeight;
            }
            else
            {
                clientWidth = document.body.clientWidth;
                clientHeight = document.body.clientHeight;
            }
            break;
        case Sys.Browser.Safari:
            clientWidth = window.innerWidth;
            clientHeight = window.innerHeight;
            break;
        case Sys.Browser.Opera:
            clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
            clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
            break;
        default:  // Sys.Browser.Firefox, etc.
            clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
            clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
            break;
    }
    return new Sys.UI.Bounds(0, 0, clientWidth, clientHeight);
}

eSalesClient.GetLocation = function(element) 
{
    /// <summary>Gets the coordinates of a DOM element.</summary>
    /// <param name="element" domElement="true"/>
    /// <returns type="Sys.UI.Point">
    ///   A Point object with two fields, x and y, which contain the pixel coordinates of the element.
    /// </returns>

    // workaround for an issue in getLocation where it will compute the location of the document element.
    // this will return an offset if scrolled.
    //
    if (element === document.documentElement) {
        return new Sys.UI.Point(0,0);
    }

    // Workaround for IE6 bug in getLocation (also required patching getBounds - remove that fix when this is removed)
    if (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7) {
        if (element.window === element || element.nodeType === 9 || !element.getClientRects || !element.getBoundingClientRect) return new Sys.UI.Point(0,0);

        // Get the first bounding rectangle in screen coordinates
        var screenRects = element.getClientRects();
        if (!screenRects || !screenRects.length) {
            return new Sys.UI.Point(0,0);
        }
        var first = screenRects[0];

        // Delta between client coords and screen coords
        var dLeft = 0;
        var dTop = 0;

        var inFrame = false;
        try {
            inFrame = element.ownerDocument.parentWindow.frameElement;
        } catch(ex) {
            // If accessing the frameElement fails, a frame is probably in a different
            // domain than its parent - and we still want to do the calculation below
            inFrame = true;
        }

        // If we're in a frame, get client coordinates too so we can compute the delta
        if (inFrame) {
            // Get the bounding rectangle in client coords
            var clientRect = element.getBoundingClientRect();
            if (!clientRect) {
                return new Sys.UI.Point(0,0);
            }

            // Find the minima in screen coords
            var minLeft = first.left;
            var minTop = first.top;
            for (var i = 1; i < screenRects.length; i++) {
                var r = screenRects[i];
                if (r.left < minLeft) {
                    minLeft = r.left;
                }
                if (r.top < minTop) {
                    minTop = r.top;
                }
            }

            // Compute the delta between screen and client coords
            dLeft = minLeft - clientRect.left;
            dTop = minTop - clientRect.top;
        }

        // Subtract 2px, the border of the viewport (It can be changed in IE6 by applying a border style to the HTML element,
        // but this is not supported by ASP.NET AJAX, and it cannot be changed in IE7.), and also subtract the delta between
        // screen coords and client coords
        var ownerDocument = element.document.documentElement;
        return new Sys.UI.Point(first.left - 2 - dLeft + ownerDocument.scrollLeft, first.top - 2 - dTop + ownerDocument.scrollTop);
    }

    return Sys.UI.DomElement.getLocation(element);
}

eSalesClient.GetCurrentStyle = function(element, attribute, defaultValue) {
    /// <summary>
    /// eSalesClient.GetCurrentStyle is used to compute the value of a style attribute on an
    /// element that is currently being displayed.  This is especially useful for scenarios where
    /// several CSS classes and style attributes are merged, or when you need information about the
    /// size of an element (such as its padding or margins) that is not exposed in any other fashion.
    /// </summary>
    /// <param name="element" type="Sys.UI.DomElement" domElement="true">
    /// Live DOM element to check style of
    /// </param>
    /// <param name="attribute" type="String">
    /// The style attribute's name is expected to be in a camel-cased form that you would use when
    /// accessing a JavaScript property instead of the hyphenated form you would use in a CSS
    /// stylesheet (i.e. it should be "backgroundColor" and not "background-color").
    /// </param>
    /// <param name="defaultValue" type="Object" mayBeNull="true" optional="true">
    /// In the event of a problem (i.e. a null element or an attribute that cannot be found) we
    /// return this object (or null if none if not specified).
    /// </param>
    /// <returns type="Object">
    /// Current style of the element's attribute
    /// </returns>

    var currentValue = null;
    if (element) {
        if (element.currentStyle) {
            currentValue = element.currentStyle[attribute];
        } else if (document.defaultView && document.defaultView.getComputedStyle) {
            var style = document.defaultView.getComputedStyle(element, null);
            if (style) {
                currentValue = style[attribute];
            }
        }
        
        if (!currentValue && element.style.getPropertyValue) {
            currentValue = element.style.getPropertyValue(attribute);
        }
        else if (!currentValue && element.style.getAttribute) {
            currentValue = element.style.getAttribute(attribute);
        }       
    }
    
    if ((!currentValue || currentValue == "" || typeof(currentValue) === 'undefined')) {
        if (typeof(defaultValue) != 'undefined') {
            currentValue = defaultValue;
        }
        else {
            currentValue = null;
        }
    }   
    return currentValue;  
}    

Type.registerNamespace("Modal");
Modal.BackgroundElement = null;
Modal.ForegroundElement = null;
Modal._resizeHandler = null;
Modal._scrollHandler = null;
Modal._tagWithTabIndex = new Array('A','AREA','BUTTON','INPUT','OBJECT','SELECT','TEXTAREA','IFRAME');
Modal._saveTabIndexes = new Array();
Modal._saveDisableSelect = new Array();

Modal.Init = function()
{        
    var _scrollHandler = Function.createDelegate(this, Modal.Size);
    var _resizeHandler = Function.createDelegate(this, Modal.Size);
    
    if(Modal.BackgroundElement == null)
    {
        Modal.BackgroundElement = document.createElement('div');
        Modal.BackgroundElement.style.position = 'absolute';
        Modal.BackgroundElement.style.display = 'none';
        Modal.BackgroundElement.style.left = '0px';
        Modal.BackgroundElement.style.top = '0px';
        Modal.BackgroundElement.style.zIndex = 9998;
        Modal.BackgroundElement.style.backgroundColor = 'Gray';
        Modal.BackgroundElement.style.filter = 'alpha(opacity=70)';
        Modal.BackgroundElement.style.opacity = '0.7';
        document.getElementsByTagName("body")[0].appendChild(Modal.BackgroundElement);
    }
    
    $addHandler(window, 'resize', _resizeHandler);
    $addHandler(window, 'scroll', _resizeHandler);
}

Modal.Size = function()
{
    var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    var clientBounds = eSalesClient.GetClientBounds();
    var clientWidth = clientBounds.width;
    var clientHeight = clientBounds.height;
    Modal.BackgroundElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth)+'px';
    Modal.BackgroundElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight)+'px';    

    if(Modal.ForegroundElement != null)
    {            
        Modal.ForegroundElement.style.left = scrollLeft + ((clientWidth - Modal.ForegroundElement.offsetWidth) / 2) + 'px';        
        Modal.ForegroundElement.style.top = scrollTop + ((clientHeight - Modal.ForegroundElement.offsetHeight) / 2) + 'px';
    }
}

Modal.DisablePage = function(ctl)
{
    Modal.ForegroundElement = document.getElementById(ctl);
    Modal.ForegroundElement.style.position = 'absolute';
    Modal.ForegroundElement.style.display = '';
    Modal.ForegroundElement.style.zIndex = 9999;
    Modal.Size();
    Modal.DisableTab();
    Modal.BackgroundElement.style.display = '';
    
}

Modal.EnablePage = function()
{
    Modal.EnableTab();
    Modal.BackgroundElement.style.display = 'none';
    Modal.ForegroundElement.style.display = 'none';
}

Modal.DisableTab = function()
{
    /// <summary>
    /// Change the tab indices so we only tab through the modal popup
    /// (and hide SELECT tags in IE6)
    /// </summary>

    var i = 0;
    var tagElements;
    var tagElementsInPopUp = new Array();
    Array.clear(Modal._saveTabIndexes);

    //Save all popup's tag in tagElementsInPopUp
    for (var j = 0; j < Modal._tagWithTabIndex.length; j++) {
        tagElements = Modal.ForegroundElement.getElementsByTagName(Modal._tagWithTabIndex[j]);
        for (var k = 0 ; k < tagElements.length; k++) {
            tagElementsInPopUp[i] = tagElements[k];
            i++;
        }
    }

    i = 0;
    for (var j = 0; j < Modal._tagWithTabIndex.length; j++) {
        tagElements = document.getElementsByTagName(Modal._tagWithTabIndex[j]);
        for (var k = 0 ; k < tagElements.length; k++) {
            if (Array.indexOf(tagElementsInPopUp, tagElements[k]) == -1)  {
                Modal._saveTabIndexes[i] = {tag: tagElements[k], index: tagElements[k].tabIndex};
                tagElements[k].tabIndex="-1";
                i++;
            }
        }
    }

    //IE6 Bug with SELECT element always showing up on top
    i = 0;
    if ((Sys.Browser.agent === Sys.Browser.InternetExplorer) && (Sys.Browser.version < 7)) {
        //Save SELECT in PopUp
        var tagSelectInPopUp = new Array();
        for (var j = 0; j < Modal._tagWithTabIndex.length; j++) {
            tagElements = Modal.ForegroundElement.getElementsByTagName('SELECT');
            for (var k = 0 ; k < tagElements.length; k++) {
                tagSelectInPopUp[i] = tagElements[k];
                i++;
            }
        }

        i = 0;
        Array.clear(Modal._saveDisableSelect);
        tagElements = document.getElementsByTagName('SELECT');
        for (var k = 0 ; k < tagElements.length; k++) {
            if (Array.indexOf(tagSelectInPopUp, tagElements[k]) == -1)  {
                Modal._saveDisableSelect[i] = {tag: tagElements[k], visib: eSalesClient.GetCurrentStyle(tagElements[k], 'visibility')} ;
                tagElements[k].style.visibility = 'hidden';
                i++;
            }
        }
    }
}

Modal.EnableTab  = function()
{
    /// <summary>
    /// Restore the tab indices so we tab through the page like normal
    /// (and restore SELECT tags in IE6)
    /// </summary>

    for (var i = 0; i < this._saveTabIndexes.length; i++) {
        Modal._saveTabIndexes[i].tag.tabIndex = Modal._saveTabIndexes[i].index;
    }

    //IE6 Bug with SELECT element always showing up on top
    if ((Sys.Browser.agent === Sys.Browser.InternetExplorer) && (Sys.Browser.version < 7)) {
        for (var k = 0 ; k < Modal._saveDisableSelect.length; k++) {
            Modal._saveDisableSelect[k].tag.style.visibility = this._saveDisableSelect[k].visib;
        }
    }
}

function pageLoad()
{
    Modal.Init();
}