// When an user launches this application from a window that was
// previously created by this application, it can be confusing to the
// user when comes time for this application to create a new window. If
// the new window's name is the same as name of the current window, the
// browser won't create a new window. Instead, the browser will reuse
// the current window. To avoid that, we tag a suffix to the window
// name when this application is run from within a window that was
// previously created by this window.
var util_sWinNameSuffix = ((window.opener)
			   ? '_' + (Date.parse(new Date())).toString()
                           : '');

var oWin;
function util_startWin(sUrl, sName, sAttr, iWidth, iHeight, iPosX, iPosY) {
    oWin = window.open(sUrl, sName, // + util_sWinNameSuffix, 
    		       sAttr + ((iWidth > 0) 
				? ",width=" + iWidth
				: "") + 
			       ((iHeight > 0)
				? ",height=" + iHeight
				: ""));
    if ((iPosX == 0) && (iPosY == 0)) {
        iPosX = screen.availWidth/2 - ((iWidth > 0)
				       ? iWidth / 2
				       : 0);
	iPosY = screen.availHeight/2 - ((iHeight > 0)
					? iHeight / 2
					: 0);
    }
    oWin.focus();

    if ((typeof(iPosX) != 'undefined') && (typeof(iPosY) != 'undefined')) {
	// ie6 sometimes does not return after a window.moveTo. This can
	// cause trouble to the routine caller. So we use setTimeout() to
	// ensure that by the time IE is called to move the window, we are
	// already back to the caller.
	setTimeout("oWin.moveTo(" + iPosX + "," + iPosY + ")", 100);
    }
    return oWin;
}

function util_setWinPos(oWinSize, evt) {
    var iPosX = 0;
    var iPosY = 0;
    if (evt) {
	iPosX = evt.screenX;
	iPosY = evt.screenY;
	var iGapHeight = 50;
	var iGapWidth = 25;
	if (((iPosX-0) + oWinSize.iWidth + iGapWidth) > screen.availWidth) {
	    iPosX = screen.availWidth - oWinSize.iWidth - iGapWidth;
	}
	if (((iPosY-0) + oWinSize.iHeight + iGapHeight) > screen.availHeight) {
	    iPosY = screen.availHeight - oWinSize.iHeight - iGapHeight;
	}
    }
    return {iX: iPosX, 
            iY: iPosY};
}

function util_startHelperWin(sUrl, sWinName, evt) {
    var oWinSize = {iWidth: parseInt(screen.availWidth * .5),
                    iHeight: parseInt(screen.availHeight * .5)};
    var oWinPos = util_setWinPos(oWinSize, evt);
    return util_startWin(sUrl, sWinName, "scrollbars,resizable",
	                 oWinSize.iWidth, oWinSize.iHeight, 
		         oWinPos.iX, oWinPos.iY);
}

function util_startContentWin(sUrl, sWinName, evt) {
    var oWinSize = {iWidth: parseInt(screen.availWidth * .9),
                    iHeight: parseInt(screen.availHeight * .5)};
    var oWinPos = util_setWinPos(oWinSize, evt);
    util_startWin(sUrl, sWinName, "scrollbars,resizable",
	          oWinSize.iWidth, oWinSize.iHeight, 
		  oWinPos.iX, oWinPos.iY);
    return false;
}
    


