	var objModalOverlord = new modalOverlord();

	function objStyle(cssDivWindow, cssDivTitle, cssDivMsg, cssDivButtons, cssButton, cssImgMask) {
		this.cssDivWindow = cssDivWindow;
		this.cssDivTitle = cssDivTitle;
		this.cssDivMsg = cssDivMsg;
		this.cssDivButtons = cssDivButtons;
		this.cssButton = cssButton;
		this.cssImgMask = cssImgMask;
	}
	objStyle.prototype.objStyle = objStyle;

	function modalBox(strName, strMsg, strTitle, arrButtons, callback, objCustomStyle) {
		this.name = strName;
		this.strMsg = strMsg;
		this.strTitle = strTitle;
		this.arrButtons = arrButtons;
		this.callback = callback;
		this.divWindow = document.createElement("div");
		this.divTitle = document.createElement("div");
		this.divMsg = document.createElement("div");
		this.divButtons = document.createElement("div");
		this.imgMask = document.createElement("div");
		this.buttons = new Array;
		
				
		// If user specifies a style object, use it if not use default object defined below
		this.objCustomStyle = (objCustomStyle == null) ? this.defaultObjStyle : objCustomStyle;
		
		// property set upon click
		this.value = null;
		
		//draw out box, but do not display yet
		this.modalDrawBox();
		
		
	}

	function modalDrawBox() {
		var buttonStyle = "";
		with (this) {
			
			divWindow.style.cssText = objCustomStyle.cssDivWindow;
			divTitle.style.cssText = objCustomStyle.cssDivTitle;
			divMsg.style.cssText = objCustomStyle.cssDivMsg;
			divButtons.style.cssText = objCustomStyle.cssDivButtons;
			imgMask.style.cssText = objCustomStyle.cssImgMask;
			
			//set styles that need to be set regardless of user preferences
			imgMask.style.position = "absolute";
			imgMask.style.top = "0px";
			imgMask.style.left = "0px";
			imgMask.style.width = "100%";
			imgMask.style.height = "100%";
			imgMask.style.zIndex = "4999";
			imgMask.style.filter = "Alpha(Opacity=80, FinishOpacity=80, Style=80, StartX=80, StartY=80, FinishX=80, FinishY=80)";
			imgMask.style.visibility = "hidden";
			imgMask.style.display = "none";
			
			divWindow.style.position = "absolute";
			divWindow.style.zIndex = "5000";
			divWindow.style.visibility = "hidden";
			divWindow.style.display = "none";
			
			//put text into divs
			divTitle.innerHTML = strTitle;
			divMsg.innerHTML = strMsg;
			
			
			
			for(var j = 0;j < arrButtons.length;j++) {
				buttons[j] = document.createElement("input");
				//
				//with (buttons) {
					buttons[j].id = "buttons" + j;
					buttons[j].name = j;
					buttons[j].type = "button";
					buttons[j].style.cssText = objCustomStyle.cssButton;
					buttons[j].value = arrButtons[j];
					buttons[j].onclick = this.onclick;
					buttons[j].title = this;
				//}
				divButtons.appendChild(buttons[j]);			
			}
			
			//append divs to divWindow
			divWindow.appendChild(divTitle);
			divWindow.appendChild(divMsg);
			divWindow.appendChild(divButtons);


			document.body.appendChild(divWindow);
			document.body.appendChild(imgMask);
			
		}
	}
	
	function modalShow() {
		
		with (this) {
			
			//reset value
			value = null;
			
			imgMask.style.visibility = "visible";
			imgMask.style.display = "block";
			divWindow.style.visibility = "visible";
			divWindow.style.display = "block";
			
			//center the window
			center();
		}
	}
	
	function modalHide() {
		with (this) {
			imgMask.style.visibility = "visible";
			imgMask.style.display = "none";
			divWindow.style.visibility = "visible";
			divWindow.style.display = "none";
		}
	}
	
	function modalSetText(strText) {
		this.strMsg = strText;
		this.divMsg.innerHTML = this.strMsg;
	}
	
	function modalBtn_onClick() {
		
		//set 
		var callingObject = objModalOverlord.currentModalObject
		callingObject.hide();
		callingObject.value = this.name;
		callingObject.callback(callingObject);
	}
	
	function requestQueueSpace() {
		
		objModalOverlord.add(this);
	}
	
	function releaseFromQueue() {
		objModalOverlord.dismiss(this);
	}
	
	function centerWindow() {
		
		//centers a window
		//object calling must have a div called divWindow
		
		var divWindow = this.divWindow
		if (navigator.appName == "Microsoft Internet Explorer"){
			var yposition = document.body.scrollTop;
			var xposition = document.body.scrollLeft;
			x = ( (document.body.clientWidth - divWindow.clientWidth) / 2) + xposition;
			y = ( (document.body.clientHeight - divWindow.clientHeight) / 2 ) + yposition;
		} else {
			var yposition = window.pageYOffset;
			var xposition = window.pageXOffset;
			x = ( (window.innerWidth - divWindow.clientWidth) / 2) + xposition;
			y = ( (window.innerHeight - divWindow.clientHeight) / 2 ) + yposition;
		}
		var x,y;
		
		
		//alert ("X: " + x + " Y:  " + y);
		//alert(window.innerWidth);
		divWindow.style.top = (y < 0 ) ? 0 : y + "px";
		divWindow.style.left = (x < 0 ) ? 0 : x + "px";
	
	}

	modalBox.prototype.modalBox = modalBox;	
	modalBox.prototype.modalDrawBox = modalDrawBox;
	modalBox.prototype.show = requestQueueSpace;
	modalBox.prototype.hide = releaseFromQueue;
	modalBox.prototype.onclick = modalBtn_onClick;
	modalBox.prototype.modalShow = modalShow;
	modalBox.prototype.modalHide = modalHide;
	modalBox.prototype.center = centerWindow;
	modalBox.prototype.setText = modalSetText;
	
	// This is the default style of the window if none is specified
	modalBox.prototype.defaultObjStyle = new objStyle(
		"/* Window */ border: 1px solid #000000; background:#CCCCCC;font-family:Arial, Helvetica, sans-serif;font-size:medium;color:#000000;min-width: 100px;",
		"/* Title */ background:#000099;color:#FFFFFF;font-size:inherit;font-weight:bold;padding:2px;",
		"/* Message */ font-family:inherit;font-size:inherit;text-align:left;padding: 5px;",
		"/* ButtonDiv */ ",
		"/* Button */ padding:3px; margin: 3px;",
		"/* Image Mask */ background: #a6aeff;"
		);
	
	function modalOverlord() {
		this.modalBusy = false;
		this.currentModalObject = null;
		this.queuedFunctions = new Array;
	}
	
	function addToQueue(modalBox) {
		//alert(this.checkQueue);
		this.queuedFunctions.push(modalBox);
		this.check();
	}
	
	function checkQueue() {
		if (this.queuedFunctions.length > 0 && ! this.modalBusy) {
			var modalBox = this.queuedFunctions.shift();
			this.currentModalObject = modalBox;
			this.modalBusy = true;
			modalBox.modalShow();
			return(true);
		} else {
			return(false);
		}
	}
	
	function dismissFromQueue(func) {
		func.modalHide();
		this.currentModalObject = null;
		this.modalBusy = false;
		this.check();

	}
	
	modalOverlord.prototype.add = addToQueue;
	modalOverlord.prototype.check = checkQueue;
	modalOverlord.prototype.dismiss = dismissFromQueue;