// 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 && (left > -1)))
    left = (window.screen.availWidth - width) / 2;
  
  // Centre vertically if directed by top = -1
  if (!(top && (top > -1)))
    top = (window.screen.availHeight - height) / 2;
  
   if (width && height) {
      // 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;
  }
  else {
      prefs = "resizable,toolbar,scrollbars,status,location,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;
}
