﻿if (typeof(Sys) !== 'undefined')
{
	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(AJAX_RequestFinished);
	Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(AJAX_RequestStarted);
}

function AJAX_RequestFinished(sender, args)
{
	//Make sure we don't get any AJAX client side alert boxes.
	if(args.get_error() != 'undefined')
	{    
		//var errorMessage = args.get_error().message; 
		args.set_errorHandled(true);
	}
	setUpMouseOvers();
	showSelect();
}

function AJAX_RequestStarted(sender, args) 
{
    if (document.getElementById) 
    {
        try 
		{
		    var divProgressLoading = $get('divProgressLoading');
			var divProgressBackground = $get('divProgressBackground');
			var divProgressWrapper;
			if ($get('hdnProgressWrapper'))
				divProgressWrapper = $get($get('hdnProgressWrapper').value);

            if (divProgressBackground && divProgressLoading && divProgressWrapper) 
			{
			    //Hide any selects in the way.
			    divProgressLoading.style.display = 'none';
				divProgressLoading.style.width = '200px';
				divProgressLoading.style.height = '66px';
				
				//Position the background.
				divProgressBackground.style.width = divProgressWrapper.offsetWidth + 1 + 'px';
				divProgressBackground.style.height = divProgressWrapper.offsetHeight - 5 + 'px';
				divProgressBackground.style.top = getAscendingTops(divProgressWrapper) + 'px';
				if (divProgressWrapper.offsetLeft == '')
					divProgressBackground.style.left = getAscendingLefts(divProgressWrapper) - 1 + 'px';
				else
					divProgressBackground.style.left = getAscendingLefts(divProgressWrapper) - 1 - divProgressWrapper.offsetLeft + 'px';
				
				//Position the progress image.
				//Work out any scroll offset that's required.
				var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
				var scrollHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
				var clientHeight = document.documentElement.clientHeight;
				var backgroundTop = parseInt(divProgressBackground.style.top.replace('px', ''), 10);
				var backgroundHeight = parseInt(divProgressBackground.style.height.replace('px', ''), 10);
				if (window.innerHeight)
					clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
				
				//Get the correct top;
				var topOffset;
				if (scrollHeight > clientHeight)
				{
					var startingValue = 0;
					if (scrollTop > backgroundTop)
						startingValue = scrollTop;
					else
						startingValue = backgroundTop;	

					var middleValue = 0;
					if ((backgroundTop + backgroundHeight) > (scrollTop + clientHeight))
						if (scrollTop > backgroundTop)
							middleValue = parseInt(clientHeight / 2);
						else
							middleValue = parseInt((backgroundHeight - ((backgroundTop + backgroundHeight) - (scrollTop + clientHeight)))/2, 10);
					else
						if (scrollTop > backgroundTop)
							middleValue = parseInt((clientHeight - ((scrollTop + clientHeight) - (backgroundTop + backgroundHeight)))/2, 10); //parseInt((backgroundHeight - scrollTop)/2, 10);
						else
							middleValue = parseInt(backgroundHeight/2, 10);
					
					topOffset = startingValue + middleValue;
				}
				else
					topOffset = parseInt(divProgressBackground.style.top.replace('px', ''), 10) + parseInt(divProgressBackground.style.height.replace('px', '')/2, 10);
				
				//Finally, set the top equal to the offset less the height of the progress loading image.
				divProgressLoading.style.top =  topOffset - parseInt((divProgressLoading.style.height.replace('px', '')), 10) + 5 + 'px';
				divProgressLoading.style.left = parseInt(divProgressBackground.style.left.replace('px', ''), 10) + parseInt(divProgressBackground.style.width.replace('px', '') / 2, 10) - parseInt((divProgressLoading.style.width.replace('px', '') / 2), 10) + 'px';

				//Hide all comobos on the page.
				
				
				divProgressLoading.style.display = 'block';
			}
		}
		catch (e)
		{
		
			alert(e.message);
		}
	}
}