/* Assigns an onclick handler to all (submit/reset/generic) buttons which "greys" the whole page out,
	disables all form controls belonging to the senders form and then submits the original request
	(which can be onclick itself or a form submittal).
	
	This script is based upon Lokesh Dhakar's Lightbox JS (http://huddletogether.com/projects/lightbox/)
   which in turn is based upon a lot of stuff from quirksmode.org.
   
   So all possible bugs in here come from those sources because all modifications are miracles of German engineering. Vorsprung durch Technik. */
   

var loadingImage = 'images/loading.gif';


function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

	
function disablePage(eventSender)
{
	var objOverlay = document.getElementById('overlayDiv');
	var objLoadingImage = document.getElementById('loadingImage');
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}
	
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	
	// Hide select boxes as they will 'peek' through the image in IE
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
	
	for (var f = 0; f < document.forms.length; f++) {
		form = document.forms[f];
		for (var i = 0; i < form.length; i++)
			if (form.elements[i].type=='submit' || form.elements[i].type=='reset' || form.elements[i].type=='button')
				form.elements[i].disabled= true;
	}	
	
	if (eventSender.form) {
		if (eventSender.type == 'submit') {
			var submitField = document.createElement('input');
			submitField.type = 'hidden';
			submitField.name = eventSender.name;
			submitField.value = eventSender.value;
			eventSender.form.appendChild(submitField);
		}		
		eventSender.form.submit();
	}
}


function initOverlay()
{	
	var objBody = document.getElementsByTagName("body").item(0);

	if (!document.getElementsByTagName){ return; }
	var elementsWhichDisable = document.getElementsByTagName("input");

	for (var i=0; i<elementsWhichDisable.length; i++){
		var elementWhichDisables = elementsWhichDisable[i];

		if (elementWhichDisables.getAttribute('type') == 'submit' || elementWhichDisables.getAttribute('type') == 'button') {
			if (elementWhichDisables.getAttribute('onclick')) {
				var previousOnclick = elementWhichDisables.onclick;
				elementWhichDisables.onclick = function () { disablePage(this); previousOnclick(); return false; }
			} else {
				elementWhichDisables.onclick = function () { disablePage(this); return false; }
			}
		}
	}	
	
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlayDiv');
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	var imgPreloader = new Image();
	imgPreloader.onload = function() {
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = loadingImage;
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '150';
		objOverlay.appendChild(objLoadingImage);
		imgPreloader.onload=function(){};
		return false;
	}
	imgPreloader.src = loadingImage;
}




var oldonload = window.onload;
if (typeof window.onload != 'function'){
	window.onload = initOverlay;
} else {
	window.onload = function(){
	oldonload();
	initOverlay();
	}
}