var popUpWin = 0;

function popUpWindow(Urlstr, left, top, width, height) {

  if (popUpWin && popUpWin.closed != true) {

    popUpWin.close();
    popUpWin = 0;
  }
  else {

    popUpWin = window.open(Urlstr,
                           "popUpWin",
                           "toolbar=no, " +
                           "location=no, " +
                           "directories=no, " +
                           "status=no, " +
                           "menubar=no, " +
                           "scrollbars=yes, " +
                           "resizable=no, " +
                           "copyhistory=no, " +
                           "left=" + left + ", " +
                           "top=" + top + ", " +
                           "width=" + width + ", " +
                           "height=" + height);
  }
}

function popClick(e, url, width, height) {

  //IE & Mozilla differences
  if (!e) e = window.event;
  if (!e.target) e.target = e.srcElement;

  //The REAL screen coordinate of the top left of the clicked element is calculated by:
  //The mouse screen coordinates minus the difference between where the mouse was clicked
  //on the browser and the top left corner of the element. Then the scroll bars of the browser
  //are taken into account by minusing their values from the previsouly calculated value.
  //This gives the real screen coordinates of the top left corner of the clicked element.
  var Xcoord = (e.screenX - (e.clientX - calcOffsetLeft(e.target))) - document.body.scrollLeft;
  var Ycoord = (e.screenY - (e.clientY - calcOffsetTop(e.target))) - document.body.scrollTop;

//  Xcoord = Xcoord + 0;
  Ycoord = Ycoord - height - 25;

  if ((Xcoord + width) > screen.availWidth) Xcoord = (screen.availWidth - width);
  if ((Ycoord + height) > screen.availHeight) Ycoord = (screen.availHeight - height);

  popUpWindow(url, Xcoord, Ycoord, width, height);
}

function calcOffsetLeft(curObj) {

  var offset = 0;

  while (curObj != null) {

    offset = offset + curObj.offsetLeft;
    curObj = curObj.offsetParent;   
  }

  return offset;
}

function calcOffsetTop(curObj) {

  var offset = 0;

  while (curObj != null) {

    offset = offset + curObj.offsetTop;
    curObj = curObj.offsetParent;      
  }

  return offset;
}