function DialogBox(name, close, fade, listener, options)
{
	//alert('CONSTRUCTOR');
	this.name     = name;	
	this.left     = 0;
	this.top      = 0;
	this.fade	  = fade;
	this.options  = options;
	this.close	  = close;
	this.listener = listener;
	this.init();
}

DialogBox.prototype.init = function()
{	
	var i = 0;
	this.div                       = document.createElement('div');
	this.div.id                    = this.name;
	this.div.style.display         = 'none';
	this.div.style.position        = 'absolute';
	this.div.style.padding         = this.options.padding || '2px';
	this.div.style.backgroundColor = this.options.backgroundColor || '#FFFFFF';	
	this.div.style.border          = this.options.border || 'solid 0px #000000';
	this.div.style.color           = '#000000';
	this.div.style.font            = '12px arial';
	this.div.style.zIndex          = 700;						   	
	document.body.appendChild(this.div);
}

DialogBox.prototype.reposition = function()
{
	//alert('REPOSITION');
	this.getDiv().style.left = this.left + 'px';
	this.getDiv().style.top  = this.top + 'px';
}

DialogBox.prototype.resize = function()
{
	//alert('RESIZE');
	this.getDiv().style.width  = this.width + 'px';
	//this.getDiv().style.height = this.height + 'px';
}

DialogBox.prototype.moveTo = function(left,top,override)
{
	//alert('MOVETO');
	if ( !override ) {
		var page_top = this.getPageTop();
		this.left    = left - parseInt(this.width /2);
		this.top     = top + page_top - this.height;
		this.reposition();
	} else {
		this.left    = left;
		this.top     = top;
		this.reposition();
	}
	
	if (this.listener != '') {
		this.getDiv().focus();
		
		document.getElementById(this.listener).onmousedown = function() { close_box(); this.onmousedown = null; }
	}
}

DialogBox.prototype.setSize = function(width,height)
{
	//alert('SIZE');
	this.width  = width;
	this.height = height;
	this.resize();
}

DialogBox.prototype.getDiv = function()
{
	return this.div;
}

DialogBox.prototype.show = function()
{
	//alert('SHOW');
	if (this.fade) blackScreen(true);
	this.getDiv().style.display = 'inline';
}

DialogBox.prototype.hide = function()
{
	if (this.fade) blackScreen(false);
	if (document.getElementById(this.getDiv().id))
		document.body.removeChild(this.getDiv());
	//this.getDiv().style.display = 'none';
}

DialogBox.prototype.setContents = function(html)
{
	var close_bar = '';
	if (this.close)
		close_bar += '<div style="float:right; padding:0px; margin:0px;"><a href="" onClick="hide_box(); return false;">Close</a></div>';
	
	this.getDiv().innerHTML = close_bar + html;
}

DialogBox.prototype.setErrorMessage = function(message)
{
	document.getElementById('flag_error_message').innerHTML = message;
}

DialogBox.prototype.getPageTop = function()
{
	if ( document.all ) {
		return document.body.scrollTop;
	} else {
		return window.pageYOffset;
	}
}

var currentOpacity = 0;

function blackScreen(vis) {
	var options = options || {};
	var zindex = options.zindex || 600;
	var opacity = options.opacity || 60;
	var opaque = (opacity / 100);
	var fadebgDiv = document.getElementById('fadebg');
	
	if (!fadebgDiv) {
		var tbody = document.getElementsByTagName("body")[0];
		var fadebgDiv = createDiv(tbody, {'top':'0px','left':'0px','id':'fadebg'});
	}
	
	// show fade background
	if (vis) {
		var sizes = new getDimensions;
		var pageWidth = sizes.Width;
		var pageHeight = sizes.Height;
		
		window.onresize = window.onload = function () {
			var sizes = new getDimensions;
			var pageWidth = sizes.Width;
			var pageHeight = sizes.Height;
			fadebgDiv.style.width = pageWidth;
			fadebgDiv.style.height = pageHeight;
		}
		
		//alert(document.body.scrollHeight + "\\\n" + document.body.offsetHeight + "\\\n" + pageHeight + "..." + document.height);
		
		//fadebgDiv.style.opacity = opaque;
		//fadebgDiv.style.MozOpacity = opaque;
		// reset currentOpacity so it creates the fade
		currentOpacity = 0;
		createFade('fadebg', opacity);
		
		fadebgDiv.style.zIndex = zindex;        
		fadebgDiv.style.backgroundColor = '#000000';  
		fadebgDiv.style.width = pageWidth;
		fadebgDiv.style.height = pageHeight;
		fadebgDiv.style.display = 'block';
	// hide fade background
	} else {
		fadebgDiv.style.filter = 'alpha(opacity=0)'; 
		fadebgDiv.style.display = 'none';
		
		window.onresize = function () {}
	}
}

function getDimensions() {
	//Fix for IE7 (at then end)
	if (window.innerHeight && window.scrollMaxY ) { // Firefox
		//alert("first");
		var pageWidth = window.innerWidth + window.scrollMaxX+'px';
		var pageHeight = window.innerHeight + window.scrollMaxY+'px';
	} else if (document.body.scrollHeight > document.body.offsetHeight) {	// all but Explorer Mac
		//alert("second");
		var pageWidth = document.body.scrollWidth+'px';
		var pageHeight = document.body.scrollHeight+'px';
	} else {	// works in Explorer 6 Strict, Mozilla (not FF) and Safari
		//alert("third");
		var pageWidth ='100%';
		var pageHeight ='100%';
		// var pageWidth = document.body.scrollWidth+'px';
		// var pageHeight = document.body.scrollHeight+'px';
	}
	
	this.Width = pageWidth;
	this.Height = pageHeight;
}

function createFade(sam, opacity) {
	currentOpacity += 15;
	var opaque = (currentOpacity / 100);
	
	document.getElementById(sam).style.opacity = opaque;
	document.getElementById(sam).style.MozOpacity = opaque;
	document.getElementById(sam).style.filter = 'alpha(opacity='+currentOpacity+')'; 
	
	if (currentOpacity < opacity) {
		setTimeout('createFade(\'' + sam + '\', ' + opacity + ');', 1);
	}
}

function createDiv(parent, options) {
	var thisDiv = document.createElement('div');
	
	thisDiv.id = options.id;
	thisDiv.style.position = 'absolute';                 // Position absolutely
	thisDiv.style.top = options.top;                           // In the top
	thisDiv.style.left = options.left;                          // Left corner of the page
	thisDiv.style.overflow = 'hidden';                   // Try to avoid making scroll bars            
	thisDiv.style.display = 'none';                      // Start out Hidden
	
	parent.appendChild(thisDiv);  
	return thisDiv;
}