// These globals set overall operation
// -----------------------------------
var DOCWIN  = "resizable=yes,width=780,height=450,scrollbars";
var FINDWIN = "resizable=yes,width=720,height=100,scrollbars";
var NORMWIN = "scrollbars";
// -----------------------------------

var gWindow = new Object();
    gWindowCount = 0;

function openChildWindow (itemURL, winstyle, winName) {
   // The following function is used (client side) to open a child window.
   // If docwin is set to true then another special document window with JS support
   // (i.e. this file) is assumed loaded into it. If winName is not present a new
   // window will always be opened, if it is present then if a window with the
   // same name exists it will be reused otherwise a new window opened
  var winOptions, docwin;
  var cascadable=false;

  if (winstyle==null)
    winstyle='norm';

  switch (winstyle) {
  case 'doc':
    docwin     = true;
    winOptions = DOCWIN;
    cascadable = true;
    break;
  case 'find':
    winOptions = FINDWIN;
    cascadable = false;
    break;
  case 'norm':
    winOptions = NORMWIN;
    cascadable = false;
    break;
  }
	if(arguments.length == 2) { 
		winName = ''+window.name+gWindowCount;
		gWindow[winName] = new Object();
		gWindow[winName].handle = window.open(itemURL,winName,winOptions);
		gWindow[winName].cascadable = cascadable;
		gWindow[winName].typeDisp = docwin;
		gWindowCount++;
	}
	else { //open a specific window
		if (gWindow[winName]) {
			if (gWindow[winName].handle.closed != true && gWindow[winName].handle.name != null) {
				gWindow[winName].handle.focus();
				gWindow[winName].cascadable = cascadable;
				gWindow[winName].typeDisp = docwin;		
				gWindow[winName].handle.document.location = itemURL;
			}
		}
		else	 
		{
			gWindow[winName] = new Object();
			gWindow[winName].handle = window.open(itemURL,winName,winOptions);
			gWindow[winName].handle.focus();
			gWindow[winName].cascadable = cascadable;
			gWindow[winName].typeDisp = docwin;
		}
	}
	return false;
}


// ev object holds control and shift booleans indicating if a modifier key has been pressed.
// If cascade parameter is not present and modifiers are false then it is assumed
// that this is the initial window call to this function
function closeMyWindow(ev,cascadeClose){
 	var closeChildren = false;
	var questionPosed = false;
	var closeSibs = false;

	if (arguments.length == 1 && top.opener) { //there is a top level window that opened us
		if (top.opener.closed != true && top.opener.name != null) { //thats still there
			if (top.opener.closeJustChildren) { // and has the function
				if (ev.control) {
					closeSibs = true;
					closeChildren = true;
					questionPosed = true;
				}
			}
		}
	}
	for (var winName in gWindow) {
		if (gWindow[winName].handle.closed != true && gWindow[winName].handle.name != null) {
			if (arguments.length == 1) {
				if (!questionPosed) {
					if (ev.shift) {
						closeChildren = true;
						questionPosed = true;
					}
					else {
						closeChildren = confirm("Close all windows created from links from this window as well?");
						questionPosed = true;
					}
				}
			}
			else closeChildren = cascadeClose;			
			if (closeChildren) { //then close the children!
				if (gWindow[winName].cascadable) gWindow[winName].handle.closeMyWindow(null,cascadeClose);
				else gWindow[winName].handle.close();
			}

		}
	}
	window.close();
	if (closeSibs) opener.closeJustChildren();
}

function closeJustChildren() {
	for (var winName in gWindow) {
		if (gWindow[winName].handle.closed == true || gWindow[winName].handle.name == null) {
			; //child already closed etc. so do nothing
		}
		else {
			if (gWindow[winName].cascadable) gWindow[winName].handle.closeMyWindow(null, true);
			else gWindow[winName].handle.close();
		}
	}
}

function printMe() {
	this.print();
	window.close();
}
	