/**
 * The following functions implement a 'please wait' busybox using the AJAX
 * toolkit. pageLoad hooks onBeginRequest and onRequestDone event handlers into
 * the AJAX lifecycle. onBeginRequest locates and shows the busybox div if the
 * calling element is set to show it, as determined by the result of the
 * displayWait call; onRequestDone hides the div. displayWait checks the calling
 * element to determine if it has the CSS class 'PleaseWait', i.e. the busybox
 * will automatically be displayed for any AJAX request originating from a
 * button or other HTML element that has the CSS class 'PleaseWait'.
 */
function pageLoad(sender, args) {
	var sm = Sys.WebForms.PageRequestManager.getInstance();
	if (! sm.get_isInAsyncPostBack()) {
		sm.add_beginRequest(onBeginRequest);
		sm.add_endRequest(onRequestDone);
	}
}

function activateBusyBox() {

	var backName = busyboxElemName + "_back";
	var foreName = busyboxElemName + "_fore";
	var oBack =
    (document.getElementById ? document.getElementById(backName) :
       (document.all ? eval('document.all.' + backName) : null));
  var oFore =
    (document.getElementById ? document.getElementById(foreName) :
       (document.all ? eval('document.all.' + foreName) : null));
  if (oBack && oFore) {
  	oBack.style.display = "";
  	var winHeight = (top.innerHeight ? top.innerHeight :
  		document.body.clientHeight);
  	var winWidth = (top.innerWidth ? top.innerWidth :
  		document.body.clientWidth);
  	oFore.style.top = ((winHeight - busyboxHeight) / 2) + "px";
  	oFore.style.left = ((winWidth - busyboxWidth) / 2) + "px";
  	oFore.style.display = "";
  }
}

function deactivateBusyBox() {

	var backName = busyboxElemName + "_back";
	var foreName = busyboxElemName + "_fore";
	var oBack =
    (document.getElementById ? document.getElementById(backName) :
       (document.all ? eval('document.all.' + backName) : null));
	var oFore =
    (document.getElementById ? document.getElementById(foreName) :
       (document.all ? eval('document.all.' + foreName) : null));
	if (oBack && oFore) {
		oBack.style.display = "none";
		oFore.style.display = "none";
	}
}

function displayWait(elem) {

	if (! elem)
		return false;
	// if the element contains the class name PleaseWait, show the 'Please Wait'
	// dialog window when triggered
	if (elem.className.indexOf(busyboxClassName) >= 0)
		return true;

	return false;
}

function onBeginRequest(sender, args) {
	var elem = args.get_postBackElement();
	if (displayWait(elem))
		activateBusyBox();
}

function onRequestDone() {
	deactivateBusyBox();
}

