// Purpose:  Generic Modal Dialogs for JavaScript
// Created:  2009/08/04; JTankersley;


// Purpose: detect window.open closed
var md_child;
function md_waitClosed() {
	if (md_child.closed) {
		alert('refresh');
		md_reload();  // old: window.location.reload(true);
	} else {
		setTimeout('md_waitClosed()',1000);
	}
}


// Purpose: Reload
function md_reload() {
	if (window.location.reload) {  
		window.location.reload(true);  // Standard
	} else {
		window.location = window.location;  // IE
	}
}


// Purpose: Modal Popup (Modal Window and thread)
// Return:  Reload (0|1)
 // TODO:  Add <base target="_self"/> to modal window, return 1 to reload
function md_popup(_url, _width, _height) {
	var reload = 0;
	if (window.showModalDialog) {
		var retValue = md_popupModal(_url, _width, _height, '', '');
		if (typeof(retValue) == "undefined" || retValue == 1) { 
			reload = 1;
			md_reload(); 
		}	
	} else {
		var _features =	"directories=no,menubar=no,location=no,resizable=no,scrollbars=no,status=no,toolbar=no,modal=yes";
		md_child = md_popupNonModal(_url, _width, _height, _features);
		reload = 1;
		md_waitClosed();
	}	
	return reload;
}


// Purpose: non-Modal popup
function md_popupNonModal(_url, _width, _height, _features) {
	var _taskbarHeight=64;  	 // Account for task bar avail size related bug with IE8	
	var _left;
	var _top;
	if (_width > screen.availWidth) { 
		_width = screen.availWidth; 
		_left=screen.availLeft;	 // Account for task bar avail size related bug with IE8	
	} else {
		_left=(screen.availWidth/2)-(_width/2);
	}
	if (_height > (screen.availHeight - _taskbarHeight)) { 
		_height = screen.availHeight - _taskbarHeight;
		_top=screen.availTop;	 // Account for task bar avail size related bug with IE8	
	} else {
		_top=(screen.availHeight/2)-(_height/2);
	}
	var _location = "height=" + _height + ",width=" + _width + ",left=" + _left + ",top=" + _top; 
	if (_features.length == 0) {
		_features =	"directories=no,menubar=no,location=no,resizable=no,scrollbars=no,status=no,toolbar=no";
	}
	var child = window.open(_url,"popupWindow",_features + "," + _location);
	child.opener = window.self;
	return child;
}


// Purpose: Modal Popup (Modal Window and thread) 
// TODO:  Add <base target="_self"/> to header of popup dialog (required for IE security)
function md_popupModal(_url, _width, _height, _arguments, _features) {
	var _taskbarHeight=64;  	 // Account for task bar avail size related bug with IE8	
	var _left;
	var _top;
	if (_width > screen.availWidth) { 
		_width = screen.availWidth; 
		_left=screen.availLeft;	 // Account for task bar avail size related bug with IE8	
	} else {
		_left=(screen.availWidth/2)-(_width/2);
	}
	if (_height > (screen.availHeight - _taskbarHeight)) { 
		_height = screen.availHeight - _taskbarHeight;
		_top=screen.availTop;	 // Account for task bar avail size related bug with IE8	
	} else {
		_top=(screen.availHeight/2)-(_height/2);
	}
	var _location = '';
	_location += 'dialogHeight:' + _height + 'px; ';
	_location += 'dialogLeft:' + _left + 'px; ';
	_location += 'dialogTop:' + _top + 'px; ';
	_location += 'dialogWidth:' + _width + 'px; ';
	_features += 'center:no; ';
	if (_features.length == 0) {
		_features += 'dialogHide:no; ';
		_features += 'edge:raised; ';
		_features += 'resizable:no; ';
		_features += 'scroll:no; ';
		_features += 'status:no; ';
		_features += 'unadorned:yes; ';
	}
	return window.showModalDialog(_url, _arguments, _location + _features);
}


// Legacy
function popupWindow(_url, _width, _height, _scrollbars) {
	var _left=(screen.availWidth/2)-(_width/2);
	var _top=(screen.availHeight/2)-(_height/2);
	window.open(_url,"popupWindow","directories=no,menubar=no,location=no,resizable=yes,scrollbars="+_scrollbars+",status=no,toolbar=no,height="+_height+",width="+_width+",left="+_left+",top="+_top);
}