// utility.js - javascript functions for general purposes

/*
 * This function opens the specified URL in a new browser window. The size and location of the window can be altered
 * by changing the code below. By specifying the size of the new browser window within this file, new windows will
 * open to a consistent size for any links that make use of this function.
 *
 * The number of child windows for each parent window is limited to one. When a user clicks on an external or PDF link
 * in the parent page a new window is opened. For any subsequent external or PDF link clicks from the parent page, where
 * the child window is still open, the page or PDF will be opened in the existing child window. This functionality keeps
 * new windows under control to limit confusion for the user.
 *
 * Note: "| 0" at the end of the var statements truncates the calculated values to integers.
 *
 * The syntax for calling this function in the in an html document is as follows:
 *
 * <a href="http://www.google.ca" target="_blank" onClick="openNewWindow(this.href); return false">Google Canada</a>
 */
 
function openNewWindow(url) {
	var newWindowWidth = 800;
	var newWindowHeight = (screen.availHeight * 0.6) | 0;
	var newWindowLeftPos = 0;
	var newWindowTopPos = 30;
	var newWindowArgs = "toolbar=yes, location=yes, directories=no, status=yes, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=yes";

	// For small screens use 100% window width.
	if(screen.availWidth < 780) {
		newWindowWidth = screen.availWidth;
	}

	if(typeof(newWin) != "object") {
		newWindowArgs = newWindowArgs + ", width=" + newWindowWidth + ", height=" + newWindowHeight + ", left=" + newWindowLeftPos + ", top=" + newWindowTopPos;
		newWin = window.open(url, "_blank", newWindowArgs);
		/*alert(newWindowArgs); //shows values of 'newWindowArgs' for testing purposes*/
	}

	// Follow this branch if: A child window object (newWin) does exist or has been created since the parent window was opened.
	else {
		// Follow this sub-branch if: A child window is currently open.
		if(!newWin.closed) {
			newWin.location.href=url;
		}
		// Follow this sub-branch if: A child window is not currently open.
		else {
			newWindowArgs = newWindowArgs + ", width=" + newWindowWidth + ", height=" + newWindowHeight + ", left=" + newWindowLeftPos + ", top=" + newWindowTopPos;
			newWin = window.open(url, "_blank", newWindowArgs);
			/*alert(newWindowArgs); //shows values of 'newWindowArgs' for testing purposes*/
		}
	}
	if (window.focus) {newWin.focus()}
	return false;
}