var aWindow = null;
var aWindowName = '';

function openPopup(URLtoOpen, windowName, width, height, scrollbars, resizable, toolbar) {
  var newWindowName = windowName+'_'+width+'x'+height;
  var positionStr = '';

  if (typeof arguments[7] != "undefined") {
    var top = arguments[7];
  } else {
    var top = '';
  }
  if (typeof arguments[8] != "undefined") {
    var left = arguments[8];
  } else {
    var left = '';
  }
  if (typeof arguments[9] != "undefined") {
    var menubar = arguments[9];
  } else {
    var menubar = 'no';
  }
  if (typeof arguments[10] != "undefined") {
    var location = arguments[10];
  } else {
    var location = 'no';
  }
  if (typeof arguments[11] != "undefined") {
    var status = arguments[11];
  } else {
    var status = 'no';
  }

  if (aWindow != null) {
    if (!aWindow.closed) {
      if (aWindowName != newWindowName) {
        if (aWindowName != '') {
          if (aWindow.screenX) {
             positionStr = ',screenX='+aWindow.screenX+',screenY='+aWindow.screenY+
                           ',left='+aWindow.screenX+',top='+aWindow.screenY;
          } else {
            positionStr = ',screenX='+aWindow.screenLeft+',screenY='+aWindow.screenTop+
                          ',left='+aWindow.screenLeft+',top='+aWindow.screenTop;
          }
        } else {
          positionStr = '';
        }
     	  aWindow.close();
      }
    }
  } else {
		if (left != '' && top != '') {
      positionStr = ',screenX='+left+',screenY='+top+
                    ',left='+left+',top='+top;
    } else if (left != '') {
      positionStr = ',screenX='+left+
                    ',left='+left;
    } else if (top != '') {
      positionStr = ',screenY='+top+
                    ',top='+top;
    }
  }

  var newWindow = null;
	newWindow = window.open(URLtoOpen, newWindowName,
    'width='+width+',height='+height+',scrollbars='+scrollbars+
    ',toolbar='+toolbar+',resizable='+resizable+',menubar='+menubar+
    ',location='+location+',status='+status+
    ',dependent=yes'+positionStr);
  aWindow = newWindow;

  if (URLtoOpen.match(/^[a-z]+:/)) {
    aWindowName = '';
  } else {
    aWindowName = newWindowName;
  }

  return false;
}
