var sMiscAttrib = ',scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes,directories=yes,status=yes,location=yes';
var uMiscAttrib = ',scrollbars=yes,resizable=yes';

var vReduceFactor = 0.875;

function openNewWin(pURL, pWinName, pAddOffX, pAddOffY) {

  var baseWinWidth  = 0;
  var baseWinHeight = 0;
  var baseWinOffX   = 0;
  var baseWinOffY   = 0;
  var browserType   = 'Other';

  if (parseInt(navigator.appVersion)>3) 
  {
   if (navigator.appName=="Netscape") 
   {
    baseWinWidth = window.innerWidth;
    baseWinOffX  = window.screenX;
    baseWinOffY  = window.screenY;
    browserType  = 'Netscape'; 
   }
   if (navigator.appName.indexOf("Microsoft")!=-1)
   {
    baseWinWidth = document.body.offsetWidth;
    browserType  = 'IE'; 
   }
  }
  // if base window big enough and not 'ofFull' in WinName
  // new window will be relative to base window size
  // otherwise new window will be relative to full screen size

  if (baseWinWidth  < 480 || pWinName.indexOf("ofFull")!=-1)
  {
    baseWinWidth  = screen.width; 
    baseWinOffX   = 0;
    baseWinOffY   = 0;
  };
  if (baseWinOffX < 0 ) {baseWinOffX = 0;};
  if (baseWinOffY < 0 ) {baseWinOffY = 0;}; 

  baseWinHeight = Math.floor(baseWinWidth * (screen.height / screen.width));

  if (browserType == 'IE') {baseWinHeight = baseWinHeight * 0.875};   // IE7 height looks too big

  var newWinW = Math.floor(baseWinWidth  * vReduceFactor * 0.25) * 4; // new dimensions
  var newWinH = Math.floor(baseWinHeight * vReduceFactor * 0.25) * 4; // divisible by 4
                                                                      // (for no particular reason)
  var sWidth       = ',width='       + newWinW;
  var sHeight      = ',height='      + newWinH;
  var sOuterWidth  = ',outerWidth='  + newWinW; 
  var sOuterHeight = ',outerHeight=' + newWinH; 


  // new window offsets (set to 2/8ths of available offset space)...

  if (browserType == 'Netscape') // new offsets from base window (to be added to baseWinOffs)
  { 
    var newWinOffX = Math.floor((2/8) * baseWinWidth  * (1 - vReduceFactor));  
    var newWinOffY = Math.floor((2/8) * baseWinHeight * (1 - vReduceFactor));
  }
  else // base window offsets unknown, so new offsets relative to screen - new window dimensions
  {
    var newWinOffX = Math.floor((2/8) * (screen.width  - (vReduceFactor * baseWinWidth )));
    var newWinOffY = Math.floor((2/8) * (screen.height - (vReduceFactor * baseWinHeight)));
  }


  // requested additional offsets (ie. based on passed parameter values)...

  if (pWinName.indexOf("XofY")==-1) // the new window name param doesn't suggest 'fractional' offsets
  {
    var vAddOffX = pAddOffX; // 'max'available calcs based on 1024x768 (offsets scaled later) 
    var vAddOffY = pAddOffY;
    if (vAddOffX < (-1/8) * 1024 * (1 - vReduceFactor))   // can't request neg.offset < 1/8th
    {   vAddOffX = (-1/8) * 1024 * (1 - vReduceFactor);}  // of'max'available, reset = -1/8th
    if (vAddOffY < (-1/8) *  768 * (1 - vReduceFactor))
    {   vAddOffY = (-1/8) *  768 * (1 - vReduceFactor);}
    if (vAddOffX > (4/8)  * 1024 * (1 - vReduceFactor))   // can't request add.offset > 4/8ths
    {   vAddOffX = (4/8)  * 1024 * (1 - vReduceFactor);}  // of'max'available, reset  = 4/8ths
    if (vAddOffY > (4/8)  *  768 * (1 - vReduceFactor))
    {   vAddOffY = (4/8)  *  768 * (1 - vReduceFactor);} 
  } 
  else // window name suggests use fractional offsets values 'x'/y (where'x'=x%(y+1))
  {
    var vX   = pAddOffX;
    var vY   = pAddOffY;
    if (vY <  5) {vY =  5};
    if (vY > 20) {vY = 20};
    var vOffsetFactor = ((vX % (vY + 1)) / (vY));          // a value between 0 and 1
    var vOffsetFraction = ((5/8) * vOffsetFactor) - (1/8)  // a value between -1/8th and 4/8ths

    vAddOffX = vOffsetFraction * 1024  * (1 - vReduceFactor);
    vAddOffY = vOffsetFraction *  768  * (1 - vReduceFactor);
  }
  
  vAddOffX = Math.floor(vAddOffX * (baseWinWidth  / 1024)); // scale requested offsets
  vAddOffY = Math.floor(vAddOffY * (baseWinHeight /  768)); // relative to 1024 x 768

  vOffX = baseWinOffX + newWinOffX + vAddOffX;
  vOffY = baseWinOffY + newWinOffY + vAddOffY;

  var sOffsets = 'left=' + vOffX + ',top=' + vOffY + ',screenX=' + vOffX + ',screenY=' + vOffY;

  var vAttributes   = sOffsets + sWidth + sHeight + sOuterWidth + sOuterHeight + sMiscAttrib;

  newinobjref = window.open(pURL, pWinName+'x'+vOffX+vOffY,vAttributes); // window name is made 'unique'
                                                                         // use e.g. alert(vAttributes);
  newinobjref.focus();                                                   // to check the values 
}

function aNewWin(pURL, pTitle, pWinName, pAddOffX, pAddOffY) {
  var qURL     = "'" + pURL + "',"; 
  var qWinName = "'" + pWinName + "',";
  var qTitle   = ' title="' + pTitle + '"';
  if (pTitle == '')  {qTitle = ' title="opens a new window"'};
  if (pTitle == 'x') {qTitle = ' title="opens a new window (cross-reference to another section)"'};
  if (pTitle == 'y') {qTitle = ' title="opens a new window (in another website)"'};
  if (pTitle == 'ynf') {qTitle = ' title="opens a new window (in another website)" rel="nofollow"'};
  if (pTitle == 'z') {qTitle = ' '};

  document.write
(
'<a href="' + pURL + '" onClick="openNewWin(' + qURL + qWinName + pAddOffX + ',' + pAddOffY + '); '
);
  document.write
(
'return false;"' + qTitle + '>'
)
}

function openPopWin(pURL, pWinName, pX, pY, pW, pH) {

 if (pW < 1) // the Width parameter suggests a fraction of (full) screen width
  {
    pW = screen.width * pW
  }
 if (pH < 1) // the Height parameter suggests a fraction of (full) screen height
  {
    pH = screen.height * pH
  }
 if (pX < 1) // the X position parameter  suggests 'fractional' offset
  {
    pX = (screen.width - pW) * pX // x position set to fraction of available 
  }
 if (pY < 1) // the Y position parameter  suggests 'fractional' offset
  {
    pY = (screen.height - pH) * pY // y position set to fraction of available 
  }

  var uWidth       = ',width='  + pW;
  var uHeight      = ',height=' + pH;
  var uInnerWidth  = ',innerWidth='  + pW; 
  var uInnerHeight = ',innerHeight=' + pH; 

  var uOffsetString = 'left=' + pX + ',top=' + pY + ',screenX=' + pX + ',screenY=' + pY;

  var uAttributes   = uOffsetString + uWidth + uHeight + uInnerWidth + uInnerHeight + uMiscAttrib;

  popwinobjref = window.open(pURL, pWinName, uAttributes);

  popwinobjref.focus();
}

function aPopWin(pURL, pTitle, pWinName, pX, pY, pW, pH) {
  var qURL     = "'" + pURL + "',"; 
  var qWinName = "'" + pWinName + "',";
  var qTitle   = ' title="' + pTitle + '"';
  if (pTitle == '')  {qTitle = ' title="opens a pop up window"'};
  if (pTitle == 'z') {qTitle = ' '};
  document.write
(
'<a href="' + pURL + '" onClick="openPopWin(' + qURL + qWinName + pX + ',' + pY + ',' + pW + ',' + pH + '); '
);
  document.write
(
'return false;"' + qTitle + '>'
)
}
