/***********************************************************************************
 * File: window.js
 * Dependencies: none
 * Description:
 *   Window popup functions
 *
 ***********************************************************************************/

// Win popup
function win_open(url, width, height, features, name, win) {
	if(!win)
		win = window;
	// Default width and height
	if(!width)
		width = 400;
	if(!height)
		height = 110;
	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);
}