// Opens a window with specified url, target name, dimensions and location.
// Specify -1 for left and/or top to have the window centred in either direction.
// Prefs should thus not include width, height, left or top.
// Note behaviour is not guaranteed when loading in URLs you do not have
// control over.
 
function openWindow(url, name, width, height, left, top, prefs) {

  // Centre horizontally if directed by left = -1
  if (left < 0)
    left = (window.screen.availWidth - width) / 2;
  
  // Centre vertically if directed by top = -1
  if (top < 0)
    top = (window.screen.availHeight - height) / 2;
  
  // Check if we are concatenating prefs or creating it
  if (prefs)
    prefs += ",";
  else
    prefs = "";
  // Add parameters to prefs
  prefs += "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top;
  
  // Open the window
  newWindow = window.open(url, name, prefs);
  
  // Focus (or refocus) the window
  newWindow.focus();
  
  // Return the created window
  return newWindow;
}

