﻿//Overlays Div with another div
//this div blocks keystrokes
//also has 'wait' cursor
//areas outside the divs, such as the menu, are not affected

//todo - test - should not need the busy function!!!
//otherwise, get rid of prm and var ab can be included here!!!
//var ab = new AjaxBusyDiv('LoadingDiv', 'WrapperDiv');

function AjaxBusyDiv(LoadingDivId, WrapperDivId) {

    //todo - prm is used when ajax is triggered by an update panel
    // Get a reference to the PageRequestManager.
    var prm = Sys.WebForms.PageRequestManager.getInstance();

    // Using that prm reference, hook _initializeRequest
    // and _endRequest, to run our code at the begin and end
    // of any async postbacks that occur.
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);

    this.Busy = _Busy;

    var LoadingDiv = document.getElementById(LoadingDivId);
    var WrapperDiv = document.getElementById(WrapperDivId);
    var WrapperDivPosition = ObjectPosition(WrapperDiv);

    //initialize LoadingDiv
    LoadingDiv.style.position = 'absolute';
    EndRequest('', '');

    function _Busy(state) {
        if (state) {
            InitializeRequest('', '');
        }
        else {
            EndRequest('', '');
        }
    }

    // Executed anytime an async postback occurs.
    //change the cursor to an hourglass
    //make the LoadingDiv the same size as the WrapperDiv
    function InitializeRequest(sender, args) {
        if (LoadingDiv) {
            LoadingDiv.style.cursor = 'wait';
            LoadingDiv.style.top = WrapperDivPosition.y + 'px';
            LoadingDiv.style.left = WrapperDivPosition.x + 'px';
            LoadingDiv.style.height = WrapperDiv.offsetHeight + 'px';
            LoadingDiv.style.width = WrapperDiv.offsetWidth + 'px';
            LoadingDiv.style.backgroundColor = 'transparent';
            //LoadingDiv.style.backgroundImage = 'url(images/Transparent.png)';

            if (document.location.protocol == "https:") {
                LoadingDiv.style.backgroundImage = 'url(https://www.earthpoint.us/images/Transparent.png)';
            }
            else {
                LoadingDiv.style.backgroundImage = 'url(http://www.earthpoint.us/images/Transparent.png)';
            }
            //LoadingDiv.style.backgroundImage = 'url(http://www.earthpont.us/images/BAM.jpg)';
            LoadingDiv.style.backgroundRepeat = 'repeat';
            LoadingDiv.style.backgroundAttachment = 'scroll';
            LoadingDiv.style.backgroundPosition = 'top left';
            LoadingDiv.style.zIndex = '10000';
            LoadingDiv.style.visibility = "visible";
        }
    }

    // Executed when the async postback completes.
    function EndRequest(sender, args) {
        if (LoadingDiv) {
            LoadingDiv.style.cursor = 'default';
            LoadingDiv.style.height = 0 + 'px';
            LoadingDiv.style.width = 0 + 'px';
            LoadingDiv.style.background = '';
            LoadingDiv.style.visibility = "hidden";
        }
    }

    //add up offset of current div and all parents
    function ObjectPosition(obj) {

        var Top = 0;
        var Left = 0;

        while (obj != null) {
            Top += obj.offsetTop;
            Left += obj.offsetLeft;
            obj = obj.offsetParent;
        }

        return { x: Left, y: Top }

    }
   
}

//make synchronous AJAX request
//http://www.codeproject.com/KB/ajax/SynchronousAJAX.aspx
//usage example, pass the variable mnc and its value to webmethod CheckMnemonic
//var result = ExecutePageMethodSync("CheckMnemonic", '{"mnc":"' + mnc.value + '"}');
//additional notes for using XMLHttpRequest cross-browser
//http://developer.apple.com/internet/webcontent/xmlhttpreq.html

function ExecutePageMethodSync(methodName, jsonArgs) {
    var url = PageMethods.get_path() + "/" + methodName;
    var resp = GetSynchronousJSONResponse(url, jsonArgs);
    //result is a json expression, so we eval it to return the object
    var result = eval("(" + resp + ")");

    //value is returned in result.d
    if (typeof (result.d) == 'undefined') {
        alert(result.Message); //you can also have StackTrace and ExceptionType
        return;
    }
    else {
        return result.d;
    }
}

function GetSynchronousJSONResponse(url, postData) {
    var xmlhttp = null;
    if (window.XMLHttpRequest)
        xmlhttp = new XMLHttpRequest();
    else if (window.ActiveXObject) {
        if (new ActiveXObject("Microsoft.XMLHTTP"))
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        else
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    // to be ensure non-cached version of response
    url = url + "?rnd=" + Math.random();

    xmlhttp.open("POST", url, false); //false means synchronous
    xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    xmlhttp.send(postData);
    var responseText = xmlhttp.responseText;
    return responseText;
}

