// AsgWindow



// Opens a new browser window and displays the specified URL in it
// Width, Height, Centre, Name and Attributes are all optional
// If either Width or Height are not specified or 0, a window with default size is opened
// If Centre is not specified, a centred window is opened
// If Name is not specified, the window name is taken from the specified Url
// The Name is important if you wish to reuse a window
// If Attributes is not specified, a simple unadorned window is opened unless Attributes is set to "Default"
// (which will use default browser window settings)
function AsgWindowOpen(Url, Width, Height, Centre, Name, Attributes)
{
	//alert("Url: " + Url + ", Width: " + Width + ", Height: " + Height + ", Name: " + Name + ", Attributes: " + Attributes);

	if(Width==null || Width==0)
		Width = 900;

	if(Height==null || Height==0)
		Height = 640;

	if(Centre == null)
		Centre = true;

	if(Name==null || Name=="")
	{
		//Name = window.name + "Child"	// The old way
		Name = Url;
		var i = Name.indexOf("?");
		if(i >= 0)
			Name = Name.substring(0, i);
		i = Name.lastIndexOf('/');
		if(i >= 0)
			Name = Name.substr(i + 1);
		else
		{
			i = Name.lastIndexOf('\\');
			if(i >= 0)
				Name = Name.substr(i + 1);
		}
		i = Name.lastIndexOf('.');
		if(i >= 0)
			Name = Name.substring(0, i);
		Name = Name.replace(/\./g, "");
	}

	if(Attributes == null)
		Attributes = "toolbar=0,menubar=0,status=0,location=0,scrollbars=0,resizable=1";
	else if(Attributes == "Default")
		Attributes = "toolbar=1,menubar=1,status=1,location=1,scrollbars=1,resizable=1";

	// Determine the window position
	if(Centre)
	{
		var Win = window.parent;
		if(Win == null)
			Win = window;	// Just a precaution - this never seems to get called
		Attributes += AsgWindowGetCenteredCoordinates(Win, Width, Height);
	}
	Attributes += ",height=" + Height + ",width=" + Width;

	// Create the window
	var Response = window.open(Url, Name + "Child", Attributes);
	Response.focus();

	return(Response);
}

function AsgWindowGetCenteredCoordinates(Parent, Width, Height)
{
	// Parent window dimensions
	var ParentWidth;
	if(Parent.innerWidth)
		ParentWidth = Parent.innerWidth;					// Firefox
	else if(Parent.document.body)
		ParentWidth = Parent.document.body.clientWidth;		// Internet Explorer

	var ParentHeight;
	if(Parent.innerHeight)
		ParentHeight = Parent.innerHeight;					// Firefox
	else if(Parent.document.body)
		ParentHeight = Parent.document.body.clientHeight;	// Internet Explorer

	// Parent window origin
	var OriginX, OriginY;
	if(Parent.innerHeight)
	{
		// Firefox
		OriginX = Parent.screenX + (Parent.outerWidth - Parent.innerWidth);
		OriginY = Parent.screenY + (Parent.outerHeight - Parent.innerHeight);
	}
	else
	{
		// Internet Explorer
		OriginX = Parent.screenLeft;
		OriginY = Parent.screenTop;
	}

	var Top = ((ParentHeight - Height) / 2) + OriginY;
	var Left = ((ParentWidth - Width) / 2) + OriginX;

	return(",top=" + Top + ", " + "left=" + Left);
}
