﻿if (typeof(Sys) != "undefined") Sys.Application.add_load(UpdateProgress_Application_LoadHandler);

function UpdateProgress_Application_LoadHandler(sender, args){
    Sys.Application.remove_load(UpdateProgress_Application_LoadHandler);
    
    //  get the updateprogressdiv
    var _updateProgressDiv = Sys.Application._updateProgressDiv = $get('updateProgressDiv');
    if (!_updateProgressDiv) return;

    //  fetch the form
    var _view = Sys.Application._view = document.body;
    //  create the div that we will position over the form during postbacks
    var _backgroundDiv = Sys.Application._backgroundDiv = document.createElement('div');
    _backgroundDiv.style.display = 'none';
    _backgroundDiv.id = 'modalProgressGreyBackground';

    //  add the element to the DOM
    _view.appendChild(_backgroundDiv);

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_beginRequest(UpdateProgress_BeginRequest);
    prm.add_endRequest(UpdateProgress_EndRequest);
}

function UpdateProgress_BeginRequest(sender, args) {
    showUpdateProgress();
}

function UpdateProgress_EndRequest(sender, args) {
    hideUpdateProgress();
}

function showUpdateProgress() {
    if (typeof (Sys) === "undefined") return;
    var _updateProgressDiv = Sys.Application._updateProgressDiv;
    if (!_updateProgressDiv) return;
    var _backgroundDiv = Sys.Application._backgroundDiv;
    var _view = Sys.Application._view;

    // make it visible
    _backgroundDiv.style.display = '';
    _updateProgressDiv.style.display = '';

    // get the bounds of both the form and the progress div
    var formBounds = Sys.UI.DomElement.getBounds(_view);
    var x0 = formBounds.x;
    var y0 = formBounds.y;
    var width = formBounds.width;
    var height = formBounds.height;

    //  center of form
    var divBounds = Sys.UI.DomElement.getBounds(_updateProgressDiv);
    var x = x0 + Math.round((width - divBounds.width) / 2);
    var y = y0 + Math.round((height - divBounds.height) / 2);

    //  set the dimensions of the background div to the same as the form
    _backgroundDiv.style.width = width + 'px';
    _backgroundDiv.style.height = height + 'px';

    //  set the progress element to this position
    Sys.UI.DomElement.setLocation(_updateProgressDiv, x, y);
    //  place the div over the form
    Sys.UI.DomElement.setLocation(_backgroundDiv, x0, y0);
}

function hideUpdateProgress() {
    if (typeof (Sys) === "undefined") return;
    var _updateProgressDiv = Sys.Application._updateProgressDiv;
    if (!_updateProgressDiv) return;
    // make it invisible
    _updateProgressDiv.style.display = 'none';
    Sys.Application._backgroundDiv.style.display = 'none';
}

function setUpdateProgressText(text) {
    if (typeof (Sys) === "undefined") return;
    var _updateProgressDiv = Sys.Application._updateProgressDiv;
    if (!_updateProgressDiv) return;
    // clear div
    var c;
    while (c = _updateProgressDiv.firstChild) {
        _updateProgressDiv.removeChild(c);
    }
    _updateProgressDiv.appendChild(document.createTextNode(text));
}

if (typeof (Sys) != "undefined") Sys.Application.notifyScriptLoaded();

