// JavaScript Library for simulating Modal Dialogs

// Global for brower version branching.
//var MD_Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4))
var MD_Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4 ))
var MD_Nav6 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 5))

// One object tracks the current modal dialog opened from this window.
var MD_dialogWin = new Object()

// Generate a modal dialog.
// Parameters:
//    url -- URL of the page/frameset to be loaded into dialog
//    width -- pixel width of the dialog window
//    height -- pixel height of the dialog window
//    returnFunc -- reference to the function (on this page)
//                  that is to act on the data returned from the dialog
//    args -- [optional] any data you need to pass to the dialog
//    evt  -- [optional] the event that triggered this
//	  openPosition -- [optional] opens popup left or right of cursor
function MD_openDialog(url, width, height, returnFunc, evt, args, openPosition) {

	if (!MD_dialogWin.win || (MD_dialogWin.win && MD_dialogWin.win.closed)) {
		if ( evt == null ){
			evt = window.event;
		}
		// Initialize properties of the modal dialog object.
		MD_dialogWin.returnFunc = returnFunc
		MD_dialogWin.returnedValue = ""
		MD_dialogWin.args = args
		MD_dialogWin.url = url
		MD_dialogWin.width = width
		MD_dialogWin.height = height
		// Keep name unique so Navigator doesn't overwrite an existing dialog.
		MD_dialogWin.name = (new Date()).getSeconds().toString()
		
		//alert(MD_dialogWin.args[0]);
		//get the first arg which defines if scrollbars should be
		//active or not
		isScrollable = 'no';
		isScrollableAndCentered = 'no';
		if(MD_dialogWin.args != null){
			if(MD_dialogWin.args[0] == 'scroll'){
				isScrollable = 'yes';
			} else if(MD_dialogWin.args[0] =='scrollAndCenter'){
				isScrollableAndCentered = 'yes';
				isScrollable = 'yes';
			}	
		}
		
		// Assemble window attributes and try to center the dialog.
				
		if (MD_Nav4 || MD_Nav6) {
			MD_dialogWin.left = evt.screenX;
			MD_dialogWin.top = evt.screenY;
			// Center on the main window.

			if(openPosition == 'left'){
				MD_dialogWin.left = MD_dialogWin.left - MD_dialogWin.width;				
			}
			
			if(openPosition == 'center'){				
				MD_dialogWin.left = window.screenX + 
			   	((window.outerWidth - MD_dialogWin.width) / 2)
				MD_dialogWin.top = window.screenY + 
			   	((window.outerHeight - MD_dialogWin.height) / 2)
			}

			if(isScrollableAndCentered == 'yes'){
				MD_dialogWin.left = window.screenX + 
			   	((window.outerWidth - MD_dialogWin.width) / 2)
				MD_dialogWin.top = window.screenY + 
			   	((window.outerHeight - MD_dialogWin.height) / 2)
			}			
			
			var attr = "screenX=" + MD_dialogWin.left + 
		   		",screenY=" + MD_dialogWin.top + ",resizable=yes,scrollbars=" + isScrollable + ",width=" + 
			   		MD_dialogWin.width + ",height=" + MD_dialogWin.height
   
		} else {			
			MD_dialogWin.top = evt.screenY;
		    MD_dialogWin.left = evt.screenX;
			// The best we can do is center in screen.

			if(openPosition == 'left'){
				MD_dialogWin.left = MD_dialogWin.left - MD_dialogWin.width;
			}

			if(openPosition == 'center'){				
				MD_dialogWin.left = (screen.width - MD_dialogWin.width) / 2
				MD_dialogWin.top = (screen.height - MD_dialogWin.height) / 2
			}			
			
			if(isScrollableAndCentered == 'yes'){
				MD_dialogWin.left = (screen.width - MD_dialogWin.width) / 2
				MD_dialogWin.top = (screen.height - MD_dialogWin.height) / 2
			}				
			
				var attr = "left=" + MD_dialogWin.left + ",top=" + 
				   MD_dialogWin.top + ",resizable=yes,scrollbars=" + isScrollable + ",width=" + MD_dialogWin.width + 
				   ",height=" + MD_dialogWin.height		    
		
		
		}
		
		// Generate the dialog and make sure it has focus.
		MD_dialogWin.win=window.open(MD_dialogWin.url, MD_dialogWin.name, attr)
		MD_dialogWin.win.focus()
	} else {
		MD_dialogWin.win.focus()
	}
	
	return MD_dialogWin;
}

// Close the dialog
function MD_closeDialog() {
	if ( opener && !opener.closed ){
//		alert("Telling parent to close the dialog");
		opener.MD_closeDialog();
	}
	else{
		if ( MD_dialogWin ){
//			alert("Parent is closing the dialog");
			MD_dialogWin.win.close();
		}
	}
}

// Handle click of OK button
function MD_handleOK() {
	if (opener && !opener.closed) {
//		top.dlogBody.transferData();
		if ( opener.MD_dialogWin.returnFunc ){
			opener.MD_dialogWin.returnFunc();
		}
	} else {
		alert("You have closed the main window.\n\nNo action will be taken on the choices in this dialog box.");
	}
	MD_closeDialog();
	return false
}

// Handle click of Cancel button
function MD_handleCancel() {
	MD_closeDialog();
	return false
}


// Event handler to inhibit Navigator form element 
// and IE link activity when dialog window is active.
function MD_deadend() {
	if (MD_dialogWin.win && !MD_dialogWin.win.closed) {
		MD_dialogWin.win.focus()
		return false
	}
}

// Since links in IE4 cannot be disabled, preserve 
// IE link onclick event handlers while they're "disabled."
// Restore when re-enabling the main window.
var MD_IELinkClicks

// Disable form elements and links in all frames for IE.
function MD_disableForms() {
	MD_IELinkClicks = new Array()
	for (var h = 0; h < frames.length; h++) {
		for (var i = 0; i < frames[h].document.forms.length; i++) {
			for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
				frames[h].document.forms[i].elements[j].disabled = true
			}
		}
		MD_IELinkClicks[h] = new Array()
		for (i = 0; i < frames[h].document.links.length; i++) {
			IELinkClicks[h][i] = frames[h].document.links[i].onclick
			frames[h].document.links[i].onclick = MD_deadend
		}
	}
}

// Restore IE form elements and links to normal behavior.
function MD_enableForms() {
	for (var h = 0; h < frames.length; h++) {
		for (var i = 0; i < frames[h].document.forms.length; i++) {
			for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
				frames[h].document.forms[i].elements[j].disabled = false
			}
		}
		for (i = 0; i < frames[h].document.links.length; i++) {
			frames[h].document.links[i].onclick = MD_IELinkClicks[h][i]
		}
	}
}

// Grab all Navigator events that might get through to form
// elements while dialog is open. For IE, disable form elements.
function MD_blockEvents() {
	if (MD_Nav4 || MD_Nav6 ) {
		window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
		window.onclick = MD_deadend
	} else {
		MD_disableForms()
	}
	window.onfocus = MD_checkModal;
	var obj = document.getElementById("content");
}
// As dialog closes, restore the main window's original
// event mechanisms.
function MD_unblockEvents() {
	if (MD_Nav4 || MD_Nav6) {
		window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
		window.onclick = null
	} else {
		MD_enableForms()
	}
	window.onfocus = null
}

// Invoked by onFocus event handler of EVERY frame,
// return focus to dialog window if it's open.
function MD_checkModal() {
   setTimeout("MD_finishChecking()", 50)
   return true
}

function MD_finishChecking() {
   if (MD_dialogWin.win && !MD_dialogWin.win.closed) {
      MD_dialogWin.win.focus() 
   }
}

