/*****************************************************************************
These functions are to be used for opening new windows when you can't use
the target attribute. As an added advantage, you can set the href
attribute to the same URL--if a Javascript-capable (and -enabled) browser
is in use, the new window opens; if Javascript is unavailable, the link is
followed like normal in the current window.

Usage:
<a href="the_url" onclick="return openblankwin('the_url')">link text</a>
(Replace openblankwin('the_url') with the appropriate function name and
parameter set.)

Note that each function always returns false. Thus, onclick evaluates to
simply "return false". If it did anything else, the browser would both
call the function (opening a new window) AND follow the normal href in the
current window. This is obviously not the intended effect.
*****************************************************************************/


/*****************************************************************************
openblankwin(url) is the basic window opening function. It's meant to
simply replace target="_blank" (or target="new") and nothing more.

Parameters:
url: the url (either relative or absolute) of the file to open.
*****************************************************************************/
function openblankwin(url) {
	// ^ url
	
	var wnd = window.open(url);
	// keep javascript browsers from also following the href
	return false;
}


/*****************************************************************************
openwin(url,w,h,sb) opens a new window with no toolbars, etc. and a specified
width and height. This is meant for small pages, such as pop-up help and the
like. If the window is too large for the screen, it is automatically shrunken
to fit and scrollbars are enabled.

Parameters:
url: the url (either relative or absolute) of the file to open.
w: the width of the window in pixels.
h: the height of the window in pixels.
sb: "yes" or "no" to enable or disable scrollbars by default. If "no" and the
    window is too large for the screen, scrollbars are forced and the window
    shrunken.
*****************************************************************************/
function openwin(url,w,h,sb) {
	// ^ url, width, height, scrollbars
	
	// screen width and height minus the safety padding
	var screenW = screen.width - SAFETY_W;
	var screenH = screen.height - SAFETY_H;
	
	// check scrollbars value
	sb = (sb + "").toLowerCase();
	if (sb != SB_NO)
		sb = SB_YES;
	
	// check width and height values
	w = parseInt(w);
	h = parseInt(h);
	if (isNaN(w) || w < 1)
		w = DEFAULT_W;
	if (isNaN(h) || h < 1)
		h = DEFAULT_H;
	
	// set window name to filename minus extension
	var slashPos = url.lastIndexOf("/")+1;
	var dotPos = url.indexOf(".",slashPos);
	var wndName = (dotPos != -1) ? 
		url.substring(slashPos,dotPos) :
		url.substring(slashPos,url.length);
	
	// if the window will be too wide, shrink it and add scrollbars
	if (screenW <= w) {
		w = screenW;
		sb = SB_YES;
	}
	
	// if the window will be too tall, shrink it and add scrollbars
	if (screenH <= h) {
		h = screenH;
		sb = "yes";
		
		// it'd be nice to widen the window to fit the new vert bar
		if (screenW > w+SB_W)
			w += SB_W;
	}
	
	// bust that new window out!!!!!
	var wnd = window.open(url, wndName, 
		"top=0,left=0,width="+w+",height="+h+",toolbar=no,"
		+"menubar=no,scrollbars="+sb+",resizable=yes,location=no," 
		+"directories=no,status=yes");
	
	// keep javascript browsers from also following the href
	return false;
}


/*****************************************************************************
openpicwin(url,w,h) opens a new window with no toolbars, etc. and a specified
width and height. This is meant for images, which most browsers add whitespace
around. Thus, you should just pass the image's actual attributes and let the
function add in the space. If the window is too large for the screen,
it is automatically shrunken to fit and scrollbars are enabled.

Parameters:
url: the url (either relative or absolute) of the picture to open.
w: the width of the picture in pixels.
h: the height of the picture in pixels.
*****************************************************************************/
function openpicwin(pic,w,h) {
	// ^ picture url, width, height
	
	// add margin space: 10x10 sides for mozilla,
	// 10x15 sides for ie, 0x0 for opera (take the biggest)
	w = parseInt(w);
	if (isNaN(w) || w < 1)
		w = DEFAULT_W;
	w += PADDING_W;
	h = parseInt(h);
	if (isNaN(h) || h < 1)
		h = DEFAULT_H;
	h += PADDING_H;
	
	// call the general opener
	return openwin(pic,w,h,SB_NO);
}


// these are just some constants
var DEFAULT_W = 300;
var DEFAULT_H = 200;
var PADDING_W = 20;
var PADDING_H = 30;
var SAFETY_W = 140;
var SAFETY_H = 120;
var SB_NO = "no";
var SB_YES = "yes";
var SB_W = 20;