/*======================================================================
Define popup window variables.
====================================================================== */
var LastWindowOpened = "";
var BeforeLastWindowOpened = "";

/*======================================================================
   closePopup()
   Description:
		Closes the most recently opened window if one has been opened.
		As long as a window has been opened before and the window object
		for the window exists check to see if it's closed. If it isn't
		closed, then close it.
====================================================================== */
function closePopup(){
	if (LastWindowOpened != ""){
		if (eval("self." + LastWindowOpened)){
			if (!(eval("self." + LastWindowOpened + ".closed"))){
				eval ("self." + LastWindowOpened + ".close()");
			}
		}
	}
	if (BeforeLastWindowOpened != ""){
		if (eval("self." + BeforeLastWindowOpened)){
			if (!(eval("self." + BeforeLastWindowOpened + ".closed"))){
				eval ("self." + BeforeLastWindowOpened + ".close()");
			}
		}
	}
}

/*======================================================================
	doPopup()
	Description: 
		This function opens a new window in which to display content or
		brings an already opened window into focus. Windows are named
		uniquely based on the name of the document to be displayed in the
		window (passed as thisWindowName).  Check to see if a particular
		window has been opened before and whether or not it is currently
		opened before trying to open it.

	Argumments:
		thisWidth: width of window to be opened.
		thisHeight: height of window to be opened.
		thisDocument: name of document to load into popup window.
		thisWindowName: name to be used when naming this window.
====================================================================== */
function doPopup(thisWidth,thisHeight,thisDocument,thisWindowName) {
	doPopup(thisWidth,thisHeight,thisDocument,thisWindowName,true);
}

function doPopup(thisWidth,thisHeight,thisDocument,thisWindowName,resize,scorll) {

    OpenThisWindow = true;
	if (eval("self." + thisWindowName)){
		// If this window object exists check to see if it's closed.
		// If it isn't bring it into focus.
		if (!(eval("self." + thisWindowName + ".closed"))) {
			eval ("self." + thisWindowName + ".focus()");
			OpenThisWindow = false;
			if (eval ("self." + thisWindowName + ".document.URL.indexOf(\'"+thisDocument+"\') == -1")) 
				eval ("self." + thisWindowName + ".document.location = \'"+thisDocument+"\'");
		//} else {
			// it was opened and closed before
			//closePopup();
		}
	//} else {
		//closePopup();
	}

	if (OpenThisWindow){
		
		var windowtools = "toolbar=0,location=0,directories=0,status=0,";
                if (scorll == true) {
			windowtools += "scrollbars=yes,";
		} else {
			windowtools += "scrollbars=no,";
		}
		if (resize == false) {
			windowtools += "resizable=0,";
		} else {
			windowtools += "resizable=1,";
		}
		
		if (navigator.userAgent.indexOf("Mac") >= 0) {
			eval (thisWindowName + " = window.open(thisDocument,\"" + thisWindowName + "\",\"" + windowtools + "copyhistory=1,width=" + thisWidth + ",height=" + thisHeight + ",top=10,left=10,screeny=25,screenx=50\")");
			eval (thisWindowName + " = window.open(thisDocument,\"" + thisWindowName + "\",\"" + windowtools + "copyhistory=1,width=" + thisWidth + ",height=" + thisHeight + ",top=10,left=10,screeny=25,screenx=50\")");
		} else {
			eval (thisWindowName + " = window.open(thisDocument,\"" + thisWindowName + "\",\"" + windowtools + "copyhistory=1,width=" + thisWidth + ",height=" + thisHeight + ",top=10,left=10,screeny=25,screenx=50\")");
		}
		LastWindowOpened = thisWindowName;
	}
}

function focusPopup() { 
	if (LastWindowOpened != ""){
		if (eval("self." + LastWindowOpened)) {
			if (!(eval("self." + LastWindowOpened + ".closed"))) {
				eval("self." + LastWindowOpened+".focus()");
			}
		}
	}
}

