// Add the please wait behavior
function AddPleaseWaitBehavior()
{
	// Hide the div element
	document.getElementById("PleaseWaitDiv").className = "PleaseWaitHidden";
	
	// Add the behavior to the elements with the correct css class
	var node = document.forms[0];
  AddPleaseWaitBehaviorRec(node, "ShowPleaseWait");
}

// Add the please wait behavior to all nodes with the supplied css class
function AddPleaseWaitBehaviorRec(node, cssClass)
{        
	// check also the parent node to be able to add the functionality to a chlidren used for (checkbox)
	if ((node.className && node.className.indexOf(cssClass) > -1) || (node.parentNode.className && node.parentNode.className.indexOf(cssClass) > -1))
	{
		if (node.tagName == "A")
		{
			node.href = "javascript: ShowPleaseWaitValid();" + node.href;
		}
		else if (node.tagName == "INPUT" && node.type == "submit")
		{
			// register the please wait before the old click event handler
			var old = (node.onclick) ? node.onclick : function () {};
			node.onclick = function () {ShowPleaseWaitValid(); old()};
        }
        else if (node.tagName == "TABLE") {
        // register the please wait before the old click event handler
            var labels = node.getElementsByTagName("LABEL");
            var boxes = node.getElementsByTagName("INPUT");
            var x = 0;
            for (x; x < labels.length; x++) {
                labels[x].onclick= (function(old) {
                return function() {
                ShowPleaseWait();
                    if (old) {
                            old.call(this, arguments);
                        }
                    }
                })(labels[x].onclick);
            }
            x = 0;
            for (x; x < boxes.length; x++) {
                boxes[x].onclick = (function(old) {
                return function() {
                ShowPleaseWait();
                        if (old) {
                            old.call(this, arguments);
                        }
                    }
                })(boxes[x].onclick);
                
            }
        }
		else if (node.tagName == "INPUT" && node.type == "checkbox")
		{
			// register the please wait before the old click event handler
			var old = (node.onclick) ? node.onclick : function () {};
			node.onclick = function () {ShowPleaseWait(); old()};
		}
		else if (node.tagName == "SELECT" || (node.tagName == "INPUT" && node.type == "text"))
		{
			// register the please wait before the old changed event handler
			var old = (node.onchange) ? node.onchange : function () {};
			node.onchange = function () {ShowPleaseWait(); old()};
		}
	}
	for(var i=0; i<node.childNodes.length ; i++)
	{    
		AddPleaseWaitBehaviorRec(node.childNodes[i], cssClass);
	}
}

// Make the PleaseWaitDiv visible onli if the page is valid, used for submit buttons ...
function ShowPleaseWaitValid()
{
	// Check if the page is valid
	if (typeof(Page_ClientValidate) != 'function' ||  Page_ClientValidate())
	{
		ShowPleaseWait();
	}
}

// Make the PleaseWaitDiv visible, used for selection ...
function ShowPleaseWait()
{
	// Replace some elements
	ReplaceElementsRec(document.forms[0]);
	
	var divPleaseWait = document.getElementById("PleaseWaitDiv");
	divPleaseWait.style.left = Math.round(document.forms[0].offsetWidth/2-155);
	divPleaseWait.style.top = document.documentElement.scrollTop+Math.round(screen.availHeight/2)-350;
	divPleaseWait.className = "PleaseWaitVisible";
}

// Replace the entities that will show through layers
function ReplaceElementsRec(node)
{
	if (node.tagName == "SELECT" && node.style.display != "none")
	{
		var parentNode = node.offsetParent;
		var replacementText = document.createElement("input");
		replacementText.type= "text";
		replacementText.name = "temp";
		replacementText.id = "temp";
		if (node.selectedIndex > -1)
		{
			replacementText.value = node.options[node.selectedIndex].text;
		}
		replacementText.className = node.className;
		node.parentNode.insertBefore(replacementText, node);
		node.style.display = "none";
	}
	for(var i=0; i<node.childNodes.length ; i++)
	{    
		ReplaceElementsRec(node.childNodes[i]);
	}
}