var abOK = 0;
var abYesNo = 1;
var abOKCancel = 2;
var abGeneral = 3;
var abNone = 4;

function AlertBox(message, btnType, completeAction, options, style)
{
	this.updateMsg = function(msg)
	{
		document.getElementById['MessageDiv'].innerHTML = msg;
		this.Display();
	}
	
	this.Display = function()
	{
		this.div.style.display = 'block';
		this.GreyOut(true, {'zindex':'98', 'bgcolor':'#FFFFFF', 'opacity':'70'});
		this.center(this.div);		
	};
	
	this.Close = function()
	{
		if(document.getElementById(this.div.id))	document.body.removeChild(this.div);
		var tdoc = document.getElementById("MessageDiv");
		if(!tdoc) {
					this.GreyOut(false);
		}
	};
	
	this.ReturnMsgBox = function()
	{
		return document.getElementById('MessageDiv');
	};
//Private functions
	this.addBtn = function(div, msg, evt)
	{
		var self = this;
		evt = evt || function(){self.Close(); self.Complete(msg)};
		var btnText = document.createElement("div");
		btnText.className = "button";
		btnText.innerHTML = msg;
		btnText.style.mouse = 'selector';
		addEvent(btnText, "click", evt);
		div.appendChild(btnText);
	};

	this.copyStyle = function(base, targ)
	{
	    var baseStyle = base.style;
	    var targStyle = targ.style;
	    
	    for(var prop in baseStyle)
	    {
	          var str = "targStyle." + prop + " = baseStyle." + prop + ";";
	
	          try
	          {
	                eval(str);
	          }
	          catch(e)
	          {
	                // Do nothing
	          }
	    }
	};
	
	this.center = function(div)
	{
	//div.style.marginLeft = "-" + parseInt(div.offsetWidth / 2) + "px";
  //div.style.marginTop = "-" + parseInt(div.offsetHeight / 2) + "px";
  	var winW;
  	var winH;
  	var x;
  	var y;
  	
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName === "Netscape") {
				winW = window.innerWidth;
				winH = window.innerHeight;
			}
			if (navigator.appName.indexOf("Microsoft")!== -1) {
				winW = document.body.offsetWidth;
				winH = document.body.offsetHeight;
			}
		}
		//setStyle(div, "width", getStyle(document.getElementById('MessageDiv'), "width")+50);
		x = (winW-getStyle(div,  "width"))/2;
		y = (winH-getStyle(div,  "height"))/2;
		if((y < 0)){
			setStyle(div, "overflow", "auto");
			y = 0
		}
		if(getStyle(div,  "height") + y > winH){
			y = 0;
			setStyle(div, "height", winH+'px');
			setStyle(div, "overflow", "auto");
		}
		setStyle(div, "position", "fixed");
		leftto(x, div);
		setStyle(div, "top", y+'px');
		window.status = 'width:'+getStyle(document.getElementById('MessageDiv'), "width");
	};
	
	this.GreyOut = function (vis,  options ) 
	{  
		// Pass true to gray out screen, false to ungray  
		// options are optional.  This is a JSON object with the following (optional) properties  
		// opacity:0-100         
		// Lower number = less grayout higher = more of a blackout   
		// zindex: #             
		// HTML elements with a higher zindex appear on top of the gray out  
		// bgcolor: (#xxxxxx)   
		// Standard RGB Hex color code  
		// grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});  
		// Because options is JSON opacity/zindex/bgcolor are all optional and can appear  
		// in any order.  Pass only the properties you need to set.  
		options = options || {};
		var zindex = options.zindex || 50;  
		var opacity = options.opacity || 80;  
		var opaque = (opacity / 100);  
		var bgcolor = options.bgcolor || '#000000';  
		var dark=document.getElementById('darkenScreenObject'); 
		var pageWidth;        
		var pageHeight;
		if (!dark) {    
			// The dark layer doesn't exist, it's never been created.  So we'll    
			// create it here and apply some basic styles.    
			// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917    
			var tbody = document.getElementsByTagName("body")[0];    
			var tnode = document.createElement('div');           
			// Create the layer.        
			tnode.style.position='absolute';                 
			// Position absolutely        
			tnode.style.top='0px';                           
			// In the top        
			tnode.style.left='0px';                          
			// Left corner of the page        
			tnode.style.overflow='hidden';                   
			// Try to avoid making scroll bars                    
			tnode.style.display='none';                      
			// Start out Hidden        
			tnode.id='darkenScreenObject';                   
			// Name it so we can find it later    
			tbody.appendChild(tnode);                            
			// Add it to the web page    
			dark=document.getElementById('darkenScreenObject');  
			// Get the object.  
		}  
		if (vis) {    
			// Calculate the page width and height     
			if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {        
				pageWidth = document.body.scrollWidth+'px';        
				pageHeight = '500%';    
			} else if( document.body.offsetWidth ) {      
				pageWidth = document.body.offsetWidth+'px';      
				pageHeight = document.body.offsetHeight+'px';    
			} else {       
				pageWidth='100%';       
				pageHeight='100%';    
			}       
			//set the shader to cover the entire page and make it visible.    
			dark.style.opacity=opaque;                          
			dark.style.MozOpacity=opaque;                       
			dark.style.filter='alpha(opacity='+opacity+')';     
			dark.style.zIndex=zindex;            
			dark.style.backgroundColor=bgcolor;     
			dark.style.width= pageWidth;    
			dark.style.height= pageHeight;    
			dark.style.display='block';
			document.body.scrolling = "No";                         
		} else {     
			document.body.removeChild(dark);
			document.body.scrolling = "Yes";
		}
		return dark;
	};

	this.msg = message;
	this.button = btnType || -1;
	
	var self = this;
	this.Complete = completeAction || function(){self.Close();};
	this.div = document.createElement("div");
	this.div.id = random(0, 100);
	this.div.style.position = 'fixed';
	this.div.className = 'MessageBox';
	this.div.style.zIndex = '99';
	this.options = options || {'titlebar':"Title", 'Display':true};
	this.style = style || {};

	addEvent(self.div, 'click', function(){self.center(self.div);}); 

	var titleb = document.createElement("div");

	if(this.style.title){
		this.copyStyle(this.style.title, titleb);
	}
	
	if(this.options.Title)
	{
		var titleWords = document.createElement('h1');
		titleWords.innerHTML = this.options.Title;
		titleb.appendChild(titleWords);
		this.div.appendChild(titleb);
	}
	
	var messagediv = document.createElement("div");
	if(this.style.Message){
		this.copyStyle(this.style.Message, titleb);
	}

	messagediv.innerHTML = message;
	messagediv.id = "MessageDiv";
	this.div.appendChild(messagediv);
	
	if(btnType !== -1)
	{
		var buttonDiv = document.createElement("div");
		buttonDiv.id = "buttonDiv";
		if(this.style.ButtonDiv){
			this.copyStyle(this.style.ButtonDiv, buttonDiv);
		}
	
		switch(btnType)
		{
			case 0:  		//OK
				this.addBtn(buttonDiv, "OK");
				break;
			case 1:			//yes no
				this.addBtn(buttonDiv, "Yes");
				this.addBtn(buttonDiv, "No");
				break;
			case 2:			//OK Cancel
				this.addBtn(buttonDiv, "OK");
				this.addBtn(buttonDiv, "Cancel");
				break;
			case 3:			//General
				this.addBtn(buttonDiv, this.options.ButtonWord);
				break;
			case 4:
				break;
			default:			
		}	
		this.div.appendChild(buttonDiv);	
	}


	document.getElementsByTagName("body")[0].appendChild(this.div);
	this.div.style.display = 'none';
	
	if(this.options.Display === true){
		this.Display();
	}
	
}

