/*
 * The size of the last fitWindow operation are stored in a cookie
 * named "lws". This size can be used as width and height definition
 * for the window_open call.
 */
opDlg.USE_LAST_WINDOW_SIZE_COOKIE = false;

function getWindowNamePostfixForServer () {

  if (top.gPostFixForServer)
    return top.gPostFixForServer;
  else
    if (top.opener && typeof top.opener.top.gPostFixForServer == "string")
      return top.opener.top.gPostFixForServer;
  return "";
}

function opDlg (url, target, winstylep) {
  if (typeof window_open == "undefined")
    window_open = function (p1, p2, p3) {
      return window.open(p1, p2, p3);
    }

  if (target=="_top") {
    top.location.href = url;
    return false;
  }

  if (target=="HW_Mainwindow") {
    // try to navigate in frameset and in case main frame cannot
    // be found open an extra window
    if(typeof listview_navigate == "function")
    {
      if (!listview_navigate(url))
      {
        return window_open(url,"HW_Mainframe");
      }
    }
    else
    {
      return window_open(url,"HW_Mainframe");
    }
    return false;
  }

  if (target=="HW_DataRequestFrame") {
    if (top.frames.HW_DataRequestFrame) {
      top.frames.HW_DataRequestFrame.location.href = url;
      return false;
    }
    else {
      if ((top.window.opener && typeof(top.window.opener) == "object" && !window.opener.closed)
          && (opener.top.frames.HW_DataRequestFrame)) {
        opener.top.frames.HW_DataRequestFrame.location.href = url;
        return false;
      }
      else
        return window_open(url,"HW_DataRequestFrame");
    }
  }
  var winstyle=winstylep||{};

  var use_cookie = opDlg.USE_LAST_WINDOW_SIZE_COOKIE &&
                   !winstyle.minWidth && !winstyle.maxWidth && !winstyle.width &&
                   !winstyle.minHeight && !winstyle.maxHeight && !winstyle.height;

  if (!winstyle.minHeight) winstyle.minHeight=100;
  if (!winstyle.maxHeight) winstyle.maxHeight=window.screen.height;
  if (!winstyle.minWidth) winstyle.minWidth=100;
  if (!winstyle.maxWidth) winstyle.maxWidth=window.screen.width;

  var h = ""+((typeof winstyle.height != "undefined") ? winstyle.height : window.screen.height*0.80);
  if (h.indexOf("%")>=0)
    h = parseInt(h.substring(0,h.indexOf("%")))/100*window.screen.height;
  h=Math.min(h,winstyle.maxHeight);
  h=Math.max(h,winstyle.minHeight);

  var w = ""+((typeof winstyle.width != "undefined") ? winstyle.width : 800);
  if (w.indexOf("%")>=0)
    w = parseInt(w.substring(0,w.indexOf("%")))/100*window.screen.width;
  w=Math.min(w,winstyle.maxWidth);
  w=Math.max(w,winstyle.minWidth);

  var win=window;
  if (target)
    target = target.replace(/\W+/g,'_'); // remove not allowed charcters
  var target_ip_port_dep = target+getWindowNamePostfixForServer();
  if ((self.name == target_ip_port_dep || self.name==target || target=="")) {
    self.location.href=url;
  }
  else {
    var cookie;
    if (use_cookie &&
        typeof getCookie == "function" &&
        (cookie = getCookie("lws")) &&
        cookie.match(/^(\d+)x(\d+)$/)) {
      w = RegExp.$1;
      h = RegExp.$2;
    }

    var t = Math.round(screen.availHeight / 2 - h / 2);
    var l = Math.round(screen.availWidth / 2 - w / 2);

    if (typeof winstyle=="undefined") {
      win = window_open(url,target_ip_port_dep,
                        "status=yes,toolbar=no,scrollbars=yes,resizable=yes,width="+w+",height="+h+"top="+t+",left"+l);
    }
    else
    {
      var winstyle_string=
        "status="+((winstyle.status) ? winstyle.status : "yes")+
        ",toolbar="+((winstyle.toolbar) ? winstyle.toolbar : "no")+
        ",scrollbars="+((winstyle.scrollbars) ? winstyle.scrollbars : "yes")+
        ",resizable="+((winstyle.resizable) ? winstyle.resizable : "yes")+
        ",location="+((winstyle.location) ? winstyle.location : "no")+
        ",menubar="+((winstyle.menubar) ? winstyle.menubar : "no");
      winstyle_string+=",top="+ (winstyle.top || t);
      winstyle_string+=",left="+ (winstyle.left || l);
      if (winstyle.copyhistory)
        winstyle_string+= ",copyhistory="+ winstyle.copyhistory;
      winstyle_string+=",width="+w+",height="+h;
      win=window_open(url,target_ip_port_dep,winstyle_string);
    }
  }
  return win;
}
  

