// The following code is used to create a modal window in Netscape or IE
// Global for browser version branching.
//var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4));
var Nav4 = !document.all;

//set flags for IE version checking
var appName= navigator.appName;
var appVer = navigator.appVersion.toLowerCase();
var iePos  = appVer.indexOf('msie');
if(iePos !=-1) { appVer = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos))) }
else { appVer = parseFloat(appVer); }

// function to create a pop window
function createWin(filename,name,width,height,scroll,toolbar,menubar) {
	if (window.screen) {
		topcenter = (screen.availHeight / 2) - (height / 2) - 10;
		leftcenter = (screen.availWidth / 2) - (width / 2);
	} else {
		topcenter = 100;
		leftcenter = 100;
	};
	options = "width=" + width + ",height=" + height + ",scrollbars=" + scroll + ",toolbar=" + toolbar + ",menubar=" + menubar + ",scrollBars=" + scroll + ",resizable=1,status=0,top=" + topcenter + ",left=" + leftcenter;
	window.open(filename, name, options);
}

// modal window code

// One object tracks the current modal dialog opened from this window.
var dialogWin = new Object();
var checkInterval = "";

// 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
// (filename,name,width,height,scroll,toolbar,menubar)
function openDialog(filename, name, width, height, scroll, toolbar, menubar) {
	if (!dialogWin.win || (dialogWin.win && dialogWin.win.closed)) {
		// Initialize properties of the modal dialog object.
		dialogWin.returnedValue = ""
		// dialogWin.args = args
		dialogWin.url = filename
		dialogWin.width = width
		dialogWin.height = height
		// Keep name unique so Navigator doesn't overwrite an existing dialog.
		dialogWin.name = name
		// Assemble window attributes and try to center the dialog.
		if (Nav4) {
			// Center on the main window.
			dialogWin.left = window.screenX + 
			   ((window.outerWidth - dialogWin.width) / 2)
			dialogWin.top = window.screenY + 
			   ((window.outerHeight - dialogWin.height) / 2)
			var attr = "scrollbars=" + scroll + ",toolbar=" + toolbar + 
				       ",menubar=" + menubar + ",screenX=" + dialogWin.left + 
			           ",screenY=" + dialogWin.top + ",resizable=yes,width=" + 
			           dialogWin.width + ",height=" + dialogWin.height 
		} else {
			// The best we can do is center in screen.
			dialogWin.left = (screen.width - dialogWin.width) / 2
			dialogWin.top = (screen.height - dialogWin.height) / 2
			var attr = "scrollbars=" + scroll + ",toolbar=" + toolbar + ",menubar=" + 
				menubar + ",left=" + dialogWin.left + ",top=" + 
			    dialogWin.top + ",resizable=yes,width=" + dialogWin.width + 
			    ",height=" + dialogWin.height
		}

		// Generate the dialog and make sure it has focus.
		dialogWin.win = window.open(dialogWin.url, dialogWin.name, attr)
		dialogWin.win.focus();
	} else {
		dialogWin.win.focus();
	}
	// DEBUG:: 	alert(attr);
}

// Event handler to inhibit Navigator form element 
// and IE link activity when dialog window is active.
function deadend() {
	if (dialogWin.win && !dialogWin.win.closed) {
		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 IELinkClicks;

// Disable form elements and links in all frames for IE.
function disableForms() {
	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
			}
		}
		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 = deadend
		}
	}
}

// Restore IE form elements and links to normal behavior.
function 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 = IELinkClicks[h][i];
		}
	}
}

// Grab all Navigator events that might get through to form
// elements while dialog is open. For IE, disable form elements.
function blockEvents() {
	if (Nav4) {
		window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS);
		window.onclick = deadend;
	} else {
		disableForms();
	}
	window.onfocus = checkModal;
	
	if (document.all && window.document.hasFocus) checkInterval = window.setInterval("checkWindow()", 250); // fix for IE
}
// As dialog closes, restore the main window's original
// event mechanisms.
function unblockEvents() {
	if (document.all) window.clearInterval(checkInterval);

	if (Nav4) {
		window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS);
		window.onclick = null;
		window.onfocus = null;
	} else {
		enableForms();
	}

	if(dialogWin.win.closed) window.focus();
}

// check periodically to make sure window doesn't have focus
function checkWindow() {
	if (window.document.hasFocus() && !dialogWin.win.closed) checkModal();
}

// Invoked by onFocus event handler of EVERY frame,
// return focus to dialog window if it's open.
function checkModal() {
	if (Nav4) {
		if (dialogWin.win && !dialogWin.win.closed) {
			dialogWin.win.focus(); 
		}
	} 
	else if (document.all) {
		if (appVer >= 4) {
			try {
				if (dialogWin.win && typeof(dialogWin.win.closed) != 'unknown' && !dialogWin.win.closed) {
					try { if (dialogWin.win.focus) dialogWin.win.focus(); } catch(e) {}  // if 5 or greater than we can run this in a try block, which we might need to do incase it bombs
				} else
					window.clearInterval(checkInterval);
			} catch (e) { window.clearInterval(checkInterval) }
		} else {
			dialogWin.win.focus(); // no error checking, so just wing it
		}
	}
}

// 4C Version createWin(filename,name,width,height,scroll,toolbar,menubar)
// function definition
function createModalWin(filename,name,width,height,scroll,toolbar,menubar) {
	openDialog(filename, name, width, height, scroll, toolbar, menubar);

/*
	if (window.screen) {
		topcenter = (screen.availHeight / 2) - (height / 2) - 10;
		leftcenter = (screen.availWidth / 2) - (width / 2);
	} else {
		topcenter = 100;
		leftcenter = 100;
	};

	options = "modal=yes,width=" + width + ",height=" + height + ",scrollbars=" + scroll + ",toolbar=" + toolbar + ",menubar=" + menubar + ",scrollBars=" + scroll + ",resizable=1,status=0,top=" + topcenter + ",left=" + leftcenter;

	if (window.showModalDialog) {
		window.showModalDialog(filename, name, options);
	} else {
		window.open(filename, name, options);
	}
*/
}

