// returns a handle to a newly-opened window with the specified attributes
// Parameters ...
//   wndUrl     : destination url of the new window
//   wndName    : name of the new window
//   wndAttribs : javascript attributes of the new window
//
function WndOpen(wndUrl, wndName, wndAttribs) {  // opens specified window
  var wndNew = open(wndUrl, wndName, wndAttribs);
  if (usingNN2 == true) {wndNew.isClosed = false;}
  return wndNew;
}

// closes the specified window
// Parameters ...
//   wndVarNm : string containing name of the window variable to close
//
function WndClose(wndVarNm) {  //closes specified window
  var wndHandle = eval(wndVarNm);
  if (wndHandle != null) {
    wndHandle.close();
    eval(wndVarNm + " = null"); //toast reference
  }
}

// Focuses the specified window
// Achieves focusing through different methods, based on different browsers.
// Parameters ...
//   wndVarNm   : string containing name of the window variable to focus
//   wndUrl     : destination url of the window
//   wndAttribs : javavscript attributes of the window (takes effect _if_ window is re-opened)
//   wndTimer   : timer delay between close and open of window (_if_ needed); defaults to 1/2 sec.
//
function WndFocus(wndVarNm, wndUrl, wndAttribs, wndTimer) {  //focuses window
  // wndUrl, wndAttribs, & wndTimer used only when close and re-open necessary
  var wndHandle = eval(wndVarNm);
  var defaultTimer = 500;
  if (usingMSIE) {  // close and re-open because focus() isn't consistent
    // save existing window properties, unless otherwise specified
    var savedName = wndHandle.name;  // save the window's current name before closing it
    if (wndUrl == null) {wndUrl = wndHandle.location.href;}  // no url specified, so get the current window's url before closing it
    if (wndTimer == null) {wndTimer = defaultTimer;}  // no timer specified, so use default time
      else {if (wndTimer < defaultTimer) {wndTimer = defaultTimer;}}  // specified time is too short, so use default time
    WndClose(wndVarNm);  // close the current window
    // use timer to wait because window may still be closing -- a Microsoft "feature" :\
    var delayedCall = wndVarNm + " = WndOpen('" + wndUrl + "','" + savedName + "','" + wndAttribs +"')";
    window.setTimeout(delayedCall, wndTimer);
  }
  else {
    if (usingNN2) {  // alas, no focus() - close and re-open
      // save existing window properties, unless otherwise specified
      var savedName = wndHandle.name;
      if (wndUrl == null) {wndUrl = wndHandle.location;}
      if (wndTimer == null) {wndTimer = defaultTimer;}
        else {if (wndTimer < defaultTimer) {wndTimer = defaultTimer;}}
      WndClose(wndVarNm);
      // use timer to wait because window may still be closing (Netscape 2 "feature")
      var delayedCall = wndVarNm + " = WndOpen('" + wndUrl + "','" + savedName + "','" + wndAttribs +"')";
      window.setTimeout(delayedCall, wndTimer);
    }
    else {
      if (usingNN) { // v3+ - simple enough
        if (wndUrl != wndHandle.location) wndHandle.location = wndUrl;
        wndHandle.focus();
      }
    }
  }
}

function WndOpened(wndVarNm) {  //tells if specified window is opened

  var wndHandle = eval(wndVarNm);

  if (usingNN) {
    if (usingNN2) {
      if (wndHandle == null) {return false;}
      if (wndHandle.isClosed == false) {return true;}
      if (wndHandle.isClosed == true) {return false;}
      return true;  // assume opened if custom property .isClosed does not exist
    }
    else {  // v3+
      if (wndHandle != null) {return (! wndHandle.closed);}
      else {return false;} // assume null means closed
    }
  }
  else {
    if (usingMSIE) {
      if (wndHandle != null) {return (! wndHandle.closed);}
      else {return false;}  // assume null means closed
    }
    else {  // unknown browser
      return false;  // assume not opened
    }
  }

  return false;
}

function WndClosed(wndVarNm) {  //tells if specified window is closed
  return (! WndOpened(wndVarNm));
}
