﻿var _updateProgressDiv;
var _backgroundDiv;
var _targetElement;

function pageLoad(sender, args) {
    //  register for our events
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);

    //  get the updateprogressdiv
    _updateProgressDiv = $get('updateProgressDiv');
    //  fetch the targetElement
    _targetElement = $get('divRequestForm');

    //  create the div that we will position over the targetElement
    //  during postbacks
    _backgroundDiv = document.createElement('div');
    _backgroundDiv.style.display = 'none';
    //_backgroundDiv.style.zIndex = 10000;
    _backgroundDiv.className = 'background';

    //  add the element to the DOM
    _targetElement.parentNode.appendChild(_backgroundDiv);
}

function beginRequest(sender, args) {
    // make it visible
    _updateProgressDiv.style.display = '';
    _backgroundDiv.style.display = '';

    // get the bounds of both the targetElement and the progress div
    var targetElementBounds = Sys.UI.DomElement.getBounds(_targetElement);
    var updateProgressDivBounds = Sys.UI.DomElement.getBounds(_updateProgressDiv);

    //  center of targetElement
    var x = _targetElement.offsetLeft + Math.round((targetElementBounds.width) / 2) - Math.round(updateProgressDivBounds.width / 2);
    var y = _targetElement.offsetTop + Math.round((targetElementBounds.height) / 2) - Math.round(updateProgressDivBounds.height / 2);

    //  set the dimensions of the background div to the same as the targetElement
    _backgroundDiv.style.width = targetElementBounds.width + 'px';
    _backgroundDiv.style.height = targetElementBounds.height + 'px';

    //	set the progress element to this position
    Sys.UI.DomElement.setLocation(_updateProgressDiv, x, y);

    //  place the div over the targetElement
    Sys.UI.DomElement.setLocation(_backgroundDiv, _targetElement.offsetLeft, _targetElement.offsetTop);
}

function endRequest(sender, args) {
    // make it invisible
    _updateProgressDiv.style.display = 'none';
    _backgroundDiv.style.display = 'none';
}