function popup(url, features) {
    // pops up a window containing url optionally named target, optionally having features
    var theWindow = window.open(url, "pop_up", features);
    theWindow.focus();
	
}

// Win popup
function win_open(url, width, height, features, name, win) {
	if(!win)
		win = window;
	// Default width and height
	if(!width)
		width = 400;
	if(screen.width < width)
		width = screen.width -10;
	if(!height)
		height = 110;
	if(screen.height < height)
		height = screen.height -10;
	if(!name && url.indexOf(".") > 0)
		name = url.substring(0, url.indexOf(".")).replace(/[ \/\\\(\)_]/g, "");
	
	var winl = win_getCentreOfLine(screen.width, width);
	var wint = win_getCentreOfLine(screen.height, height);
	
	// Append startup left and top values
	//url += (url.indexOf("?") > 0 ? "&" : "?") + "_win_defleft=" + winl + "&_win_deftop=" + wint;
	
	var popupWin = window.open(url, name,"height=" + height + ",width=" + width + ",left="+winl+",top="+wint+",screenX="+winl+",screenY="+wint + (features ? "," + features : ""));
	popupWin.focus();
	if(popupWin.opener == null)
		popupWin.opener = win;
	return popupWin;
}

// Centre a line on a line
function win_getCentreOfLine(line1, line2, noNegNumbers) {
	return (win_validateNum(line1) - win_validateNum(line2)) / 2;
}

// Validate a number (default to 0)
function win_validateNum(num) {
	if(num == null || num == "")
		return 0;
	if(isNaN(num)) {
		num = (num + "").replace(/px/ig, "");
		if(isNaN(num))
			return 0;
	}
	return parseFloat(num);
}

// View image
function win_viewImage(tag) {
	var children = tag.childNodes;
	var i = 0;
	while(i < children.length && (!children[i].tagName || children[i].tagName.toLowerCase() != 'img'))
		i++;
		
	if(i >= children.length) {
		alert("Unable to view image.");
		return;
	}
	
	var img = children[i].src.replace(/_s\./, '_b.');
	win_open('popup_auto.php?file='+img, 10, 10, 'resizable=1,scrollbars=1,status=1');
}