// Handles launch of separate browser

// The Windows tool bar is 24 pixels high
// I have also found that IE thinks the available width is 10 pixels less than
// it should be on 800x600 mode
var windowsHOffset = 24;
var windowsWOffset = 10;

function tearoff(url, name, width, height, left, top)
{
	if (width > (screen.availWidth - windowsWOffset) || height > (screen.availHeight - windowsHOffset)) {
		return tearoffWithScroll(url, name, width, height, left, top);
	}
	return tearoff_WindowOpen(url, name, width, height, left, top, 'no', 'no', 'yes');
}  

function tearoff_WindowOpen(url, name, width, height, left, top, scrollbars, menubar, resizable)
{
	if (isNaN(parseInt(width))) width = 640;
	if (isNaN(parseInt(height))) height = 480;
	
	if (width > (screen.availWidth - windowsWOffset)) {
		width = screen.availWidth - windowsWOffset;
	}
	
	if (height > (screen.availHeight - windowsHOffset)) {
		height = screen.availHeight - windowsHOffset;
	}
	
	// If either "left" or "top" are unspecified or invalid, center the window.
	left = parseInt(left);
	top = parseInt(top);
	if (isNaN(left) || isNaN(top)) {
		var arrCtrWinPos = getCenteredWindowPosition(width, height);
		left = arrCtrWinPos[0];
		top = arrCtrWinPos[1];
	}

	var tearWin = window.open(url, name, 'personalbar=no,toolbar=no,status=no,scrollbars='
	                                    + scrollbars + ',location=no,resizable=' + resizable
	                                    + ',menubar=' + menubar + ',width=' + width + ',height='
	                                    + height + ',left=' + left + ',top=' + top);
	if (window.focus) {
		tearWin.focus();
	} 
	return tearWin;
}  

/* This function is used to generate a unique name for windows opened by any
 * of the "tearoff*" functions, we need to generate names to support
 *  -opening mulitple windows of the same layout (example: properties/form)
 *  -only one instance of the layout for each individual component
 *   (if you have two components of the same type, you can open both properties
      forms even though its the same layout, but not two instances of the
      layout for a single component)
 * We do this by generating a name based on the layout name and query string,
 * since the query string usually contains the OID of the compoment
 *
 * see SC00005293
 */
function genWindowID(url) {
	//this should get the layout name and query string
	var name = "";
	if(null != url)
		name = url.substring(url.lastIndexOf("/")); 
	else
		name = "undefined";
	//window.confirm(name);
	try {	
		name = name.replace(/com\.webridge\.entity\.Entity\%5BOID\%5B/g, "");
		name = name.replace(/com\.webridge\.entity\.Entity\[OID\[/g, "");
		name = name.replace(/[^a-zA-Z0-9]/g, "");
		/*name = name.replace(/\//g, "");		// remove problem, or useless characters
		name = name.replace(/\:/g, "");
		name = name.replace(/\&/g, "");
		name = name.replace(/\?/g, "");
		name = name.replace(/\=/g, "");
		name = name.replace(/\s/g, "");
		name = name.replace(/\%20/g, "");
		name = name.replace(/http/, "");
		name = name.replace(/com\.webridge\.entity\.Entity\%5BOID\%5B/g, "");
		name = name.replace(/com\.webridge\.entity\.Entity\[OID\[/g, "");
		name = name.replace(/\%5D/g, "");
		name = name.replace(/\%5B/g, "");
		name = name.replace(/\[/g, "");
		name = name.replace(/\]/g, "");
		name = name.replace(/\./g, "");*/
		
		// some browsers seem to have a length limit on names
		// for now we just hope 127 characters is unique enough
		if(name.length > 127) name = name.substring(0,127);
	} catch(e) {
		//name = "_blank";
	}
	//window.confirm(name);
	return name;
}

function tearoffWithScroll(url, name, width, height, left, top)
{
	return tearoff_WindowOpen(url, name, width, height, left, top, 'yes', 'no', 'yes');
}

function tearoffWithScrollAndMenu(url, name, width, height, left, top)
{
	return tearoff_WindowOpen(url, name, width, height, left, top, 'yes', 'yes', 'yes');
}

function tearoffWithScrollNoresize(url, name, width, height, left, top)
{
	return tearoff_WindowOpen(url, name, width, height, left, top, 'yes', 'no', 'no');
}

function getCenteredWindowPosition(iWinWidth, iWinHeight)
{
	// This works with all the browsers we've ever supported, including NS 4.79.
	var iWinLeft = 0;
	var iWinTop  = 0;
	// Ensure that the current browser supports the necessary features.
	if (typeof window.screen != "undefined") {
		iWinLeft = (window.screen.availWidth - iWinWidth) / 2;
		if (iWinLeft < 0) iWinLeft = 0;
		iWinTop = (window.screen.availHeight - iWinHeight) / 2;
		if (iWinTop < 0) iWinTop = 0;
   	}
   	return new Array(iWinLeft, iWinTop);
}
