﻿function navigateToURL(newURL)
{
    if (newURL != '') window.location.href = newURL;
}

function deleteDataGridRow(dataGridID, rowNum)
{
    document.getElementById(dataGridID).deleteRow(rowNum);
}

function setElementValue(elementID, newValue)
{
    document.getElementById(elementID).value = newValue;
}

function getElementBounds(element) {
    var offset = getElementLocation(element);
    return new Sys.UI.Bounds(offset.x, offset.y, element.offsetWidth || 0, element.offsetHeight || 0);
}

function getElementLocation(element) {
    // 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;
        
        // If we're in an iframe, get client coordinates too so we can compute the delta
        if (element.ownerDocument.parentWindow.frameElement) {
        
            // 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;
        var clientScrollLeft = ownerDocument.scrollLeft;
        var clientScrollTop = ownerDocument.scrollTop;
        if (clientScrollLeft == 0 && clientScrollTop == 0) {
            ownerDocument = element.document.body;
            clientScrollLeft = ownerDocument.scrollLeft;
            clientScrollTop = ownerDocument.scrollTop;
            if (clientScrollLeft < 0 || clientScrollTop < 0) {
                clientScrollLeft = 0;
                clientScrollTop = 0;
            }
        }

        var posLeft = first.left - 2 - dLeft + clientScrollLeft;
        var posTop = first.top - 2 - dTop + clientScrollTop;
        return new Sys.UI.Point(posLeft, posTop);
    }

    return Sys.UI.DomElement.getLocation(element);
}

function setElementLocation(element, point) {
    Sys.UI.DomElement.setLocation(element, point.x, point.y);
}