// Global JavaScript

String.prototype.trim = new Function("return this.replace(/^\\s+|\\s+$/g,'')");

function get_element(id)
{
	if (document.getElementById) return document.getElementById(id);
	else if (document.all) return document.all[id];
	else return null; // browser is too retarded
}

function open_window(url, name, width, height, extraAttribs)
{
	// set up the basic window attributes & size
	var attribs = 'location=no,menubar=no,toolbar=no,resizable=yes,scrollbars=yes';
	if (extraAttribs) attribs += ',' + extraAttribs;

	var w = parseInt(width, 10);
	var h = parseInt(height, 10);
	if (isNaN(w) || w <= 0) w = screen.width - 200;
	if (isNaN(h) || h <= 0) h = screen.height - 200;

	// make sure it's not too large
	var freeSpace = new Array(0, 0);
	if (is_windows() && !is_windows_xp(true)) freeSpace = new Array(12, 60);
	else if (is_mac()) freeSpace = new Array(100, 100);

	if (w > screen.width - freeSpace[0]) w = screen.width - freeSpace[0];
	if (h > screen.height - freeSpace[1]) h = screen.height - freeSpace[1];

	var left = Math.round((screen.width / 2) - ((w + freeSpace[0]) / 2));
	var top = Math.round((screen.height / 2) - ((h + freeSpace[1]) / 2));

	attribs += ',width=' + w + ',height=' + h + ',left=' + left + ',top=' + top;

	// try to open a window with that name
	var win = window.open('', name, attribs);
	if (!win) return null; // if popup blocker blocks it

	// if it's not already there, load the requested URL into the window
	if (win.location.href == 'about:blank') win.location.href = url;

	win.focus();
	return win;
}

// -------------------------------------------------------------------------------------------------------
// browser & OS-detection utilities

// finds out if the browser is gecko-based (mozilla, Firefox, ...)
function is_gecko()
{
	return (navigator.userAgent.indexOf('Gecko') != -1);
}

// finds out if the browser is Firefox.  also see is_gecko() for a more general test.
function is_firefox()
{
	return (navigator.userAgent.indexOf('Gecko') != -1 && navigator.userAgent.indexOf('Firefox') != -1);
}

// finds out if the browser is Internet Explorer.
function is_ie()
{
	return (navigator.userAgent.indexOf('MSIE') != -1);
}

// finds out if the browser is running under a Mac OS.
function is_mac()
{
	return (navigator.userAgent.indexOf('Macintosh') != -1);
}

// finds out if the browser is running under Windows.
function is_windows()
{
	return (navigator.userAgent.indexOf('Windows') != -1);
}

// finds out if the browser is running under Windows XP (optionally, or better).
function is_windows_xp(orBetter)
{
	var osIndicator = 'Windows NT';

	var pos1 = navigator.userAgent.indexOf(osIndicator);
	if (pos1 == -1) return false; // not a windows-NT-based OS

	var pos2 = navigator.userAgent.indexOf(';', pos1);
	if (pos2 == -1) return false; // can't parse version

	var versionStr = navigator.userAgent.substring(pos1 + osIndicator.length, pos2).trim();
	var version = parseFloat(versionStr, 10);
	if (isNaN(version)) return false; // can't parse version

	if (!orBetter) return (version == 5.1);
	else return (version >= 5.1);
}
