/*/ THIS FILE CONTAINS FUNCTIONS THAT WILL WRAP THE POP-UP PROCESS /*/

/*/
/ / PURPOSE:
/ /		To create and center a pop-up window.
/ /
/ / COMMENTS:
/ /		It will replace old pop-up if called
/ / 	without calling DestroyWnd() first..
/*/


// This variable will hold the window object.
// We only allow one pop-up at a time.
var popup = null;

function CreateWnd (file, width, height, resizable)
{
	/*/
	/ /  width & height can be given fraction values in the range (0 < height < 1)
	/ /  which will make the window's dimensions a percentage of the screen's width/height.
	/ /  
	/ /  'resizable' can be set to true or false, to override all other considerations.
	/ /  Otherwise, the popup will only be resizable if deemed necessary.
	/*/

	// destroy any popup that's already on the screen
	if (popup != null) { // || (popup.closed)) {
		DestroyWnd();
	}
	
	attribs = "";

	// assemble some params
	size = "no";

	// if no dimensions provided, apply percentage dimensions and set the window to be resizable
	if (height == null) height = 0.6;
	if (width == null) width = 0.6;
	size = "yes";
	
	// do percentage values
	if(height <= 1) height = (screen.height * height);
	if(width <= 1) width = (screen.width * width);
	
	// force minimum dimensions if desired
	// if(height < 375) height = 375;
	// if(width < 600) width = 600;

	// if the screen is smaller than the proposed window, make the window resizable and adjust the proposed window's dimensions accordingly...
	if(screen.width < width || screen.height < height) size = "yes";
	if(screen.width < width) width = (screen.width * 0.9);
	if(screen.height < height) height = (screen.height * 0.8);

	// NOT DONE but recommended: if the proposed window dimensions are greater than a certain percentage of the screen size, make the window resizable.
	// 

	// if the screen is very small, ensure that the window resizable
	// if(screen.width <= 800 || screen.height <= 600) size = "yes";

	/*/ center the window /*/
	//find position
	WndTop  = Math.max(Math.round((screen.height - height) / 2),0);
	WndLeft = Math.max(Math.round((screen.width  - width)  / 2),0);

	// if resizable variable is set, do override.
	if (resizable) size = resizable;


	// TESTING OVERRIDE
	size = "yes";


	// collect the attributes
	attribs = "width=" + width + ",height=" + height + ",resizable=" + size + ",scrollbars=" + size + "," + 
	"status=yes,toolbar=yes,directories=yes,menubar=yes,location=yes,top=" + WndTop + ",left=" + WndLeft;

	// create the window
	popup = open(file, "", attribs);
}

/*/
/ / PURPOSE:
/ /		To destroy the pop-up window.
/ /
/ / COMMENTS:
/ /		This is available if wish to destroy
/ / 	the pop-up window manually.
/*/

function DestroyWnd ()
{
	// close the current window
	if(popup != null)
	{
		popup.close();
		popup = null;
	}
}

