var the_timeout, timeOutPosition, wmtt;

// Returns a handle to the named layer.
function getLayer(name) {
 
	if (document.layers) 
		return(document.layers[name]);
	else if (document.all) {
		layer = eval('document.all.' + name + '.style');
		return(layer);
	} 
	else if (document.getElementById)
	{
		layer = document.getElementById(name).style;
		return(layer);
	}
	else
		return(null);    
}

// Get the width of a layer
function getWidth(name) {
	var layer = getLayer(name);

	if (document.layers)
		return(layer.width);
	else if (document.all)
		return(layer.pixelWidth);
	else if (document.getElementById) // FIX for Mozilla
		return document.getElementById(name).offsetWidth;
	else
		return(null);
}

// Get the height of a layer
function getHeight(name) {

	var layer = getLayer(name);

	if (document.layers)
		return(layer.height);
	else if (document.all)
		return(layer.pixelHeight);
	else if (document.getElementById) // FIX for Mozilla
		return document.getElementById(name).offsetHeight;
	else
		return(null);
}

// Get width of window
function getWinWidth() {

	if (document.layers)
		return(window.innerWidth);
	else if (document.all || document.getElementById )
		return(document.body.clientWidth);
	else
		return(null);
}


// Get the Height of a window
function getWinHeight() {

  if (document.layers)
    return(window.innerHeight);
  else if (document.all || document.getElementById )
    return(document.body.clientHeight);
  else
    return(null);

}

// Hiden eveything that is shown
function clearThings(e){
 getLayer('PleaseWait').visibility = 'hidden';

 if (the_timeout != null){
	 the_timeout = clearTimeout(the_timeout);
	 the_timeout = null;
 } 
}

// Move the layer to {x,y}
function moveLayer(name, x, y) {

  var layer = getLayer(name);

  if (document.layers)
    layer.moveTo(x, y);
  if (document.all || document.getElementById ) {
    layer.left = x;
    layer.top  = y;
  }
}

// Set the layer at desired position
function setLayerPos(id) {

  lyr = getLayer(id);
 
  if (lyr != null) {
    iqx2 = (getWinWidth() -	getWidth(id)) /2 + document.body.scrollLeft;
    iqy2 = (getWinHeight() - getHeight(id))/2 + document.body.scrollTop;
    moveLayer(id, iqx2, iqy2);
  }
}

// 
function showWMTT(id) {
  wmtt = document.getElementById(id);
  wmtt.style.visibility = "visible";
  wmtt = null;
}


var hasPassed = false;

// Display a layer
function showToolTip(id) {	
	
	if (!hasPassed) {
		hasPassed = true;
		the_timeout= setTimeout('showToolTip("PleaseWait")',600);
	} 
	else {
		the_timeout= clearTimeout(the_timeout);
		the_timeout= null;
		hasPassed = false;

		setLayerPos(id);
		showWMTT(id);
		document.body.style.cursor= 'wait';
		doLoop(id);
	}
}

// Main loop
function doLoop(id){
	
	setLayerPos(id);
	incrementProgressBar('progressBar');
	the_timeout= setTimeout('doLoop("PleaseWait")',150);
}


incCount = 0;

// Move progress bar image
function incrementProgressBar(id)
{
	lyr = document.getElementById(id);
	if (lyr != null){
		if (incCount == 6) incCount = 0;
		incPos = -15 * incCount;
	
		lyr.style.backgroundPosition = '0px ' + incPos+'px';
		incCount = incCount + 1;
	}
}

// Disable Please wait...
function disable() 
{
    var theForm =	document.forms[0];
    
    var limit = theForm.elements.length;
    for (i=0;i<limit;i++) 
    {
		if ( theForm.elements[i].id != "PleaseWait")
		{
			if (theForm.elements[i].parent != null && theForm.elements[i].parent.id != "PleaseWait")
			{
				theForm.elements[i].disabled = true;
				theForm.elements[i].style.cursor = 'wait';
				theForm.elements[i].visibility = 'hidden';
			}
		}
    }
}
   
  
function myDoPostBack(eventTarget, eventArgument)
{    
	if (__oldPostBack != null)
    {      
		//var validated = Page_ClientValidate();
        //if (validated){
		showToolTip("PleaseWait");
        __oldPostBack(eventTarget, eventArgument);
        //}             
	}
}


// Hack to reassign do postback to trigger display of 
// "Please wait" before posting back the page
var __oldPostBack = __doPostBack;
__doPostBack = myDoPostBack;


