<%@ page import = "com.cyscape.browserhawk.*" %>

<%

	// This sample is provided under the terms of the BrowserHawk End User
	// License Agreement. Copyright (c) 2003-2006 cyScape, Inc. All rights
	// reserved.

  // This sample demonstrates how to detect whether a vistor has blocked popup
  // windows.

	// Although we have made this sample very verbose with comments for
	// educational purposes, note that it takes only a few lines of code to
	// detect blocked popups -  the creation of the ExtendedOptions class, 2)
	// using ExtendedOptions to set testing preferences and options, and calling
	// to getExtendedBrowserInfo() to obtain an ExtendedBrowserInfo instance
	// where the answer can be obtained via its PopupsBlocked property.

  // First we make sure the system executing this example has an
  // Enterprise Edition license as required for this test.
  // Note: This is NOT needed in your pages, only for this sample.
  if (BrowserHawk.getVersion().indexOf("Ent") == -1) {
    out.println("Enterprise License Required.  ");
    out.println("You are using " + BrowserHawk.getVersion() + ".  ");
    out.println("See <a href='http://www.cyscape.com/order/'>http://www.cyscape.com/order/</a> for upgrade information.");
    return;
  }

  // To run the test we create the ExtendedOptions object.  This object is used
  // to set various preferences and options, such as which extended properties
  // we want tested and related testing parameters.
  ExtendedOptions options = new ExtendedOptions();

  // Now we tell BrowserHawk we want to test for blocked popups.  If there are
  // other settings you want to check as well, such as disabled
  // cookies/JavaScript, plugins, etc, you can do so concurrent with the popup
  // detection by adding the appropriate values to the statement below.  See
  // the ExtendedOptions class in the BrowserHawk documentation for more
  // information.
  options.addProperties("PopupsBlocked");

  // In order to detect blocked popups, BrowserHawk pops up up a tiny window of
  // its own and immediately closes it. Therefore you should expect to see this
  // window appear for a split second when the user does not have popups
  // blocked.  You can also control the size, placement, and even the content
  // of this test window using the advanced option, as discussed below.

  // As an advanced option, it is possible to have BrowserHawk use one of your
  // existing popup windows to perform this test.  This option is ideal for
  // scenarios where you want to popup a window if BH detect popups that popups
  // are not blocked.  When used this way, BrowserHawk will attempt to popup
  // *your* window for you automatically, and tell you whether it was
  // successfully popuped up.

  // The benefit of this approach is that BrowserHawk does not need to first
  // pop up a test window for the test, only for you to then have to pop up
  // your real window.  Instead it can skip popping up its test window and
  // just pop up your real window to begin with.

  // In summary, the Advanced approach is recommended if you are ready to popup
  // your window and want to verify that it was opened successfully.  The basic
  // approach is recommended if you want to test whether popups are enabled so
  // you know in advance before trying to pop up a window later.

  // NOTE: To use this advanced approach and pop up your own window, follow
  // steps 1 and 2 below.  Otherwise skip ahead to the section below labeled
  // "CONTINUING ON WITH POPUP DETECTION".

  //   STEP 1 (Ignore this step if you want BH to use its own popup window for
  //   the test)
  //
  //   To have BrowserHawk use one of your own popup windows for the test,
  //   first YOU **MUST** ADD THE FOLLOWING JavaScript to the <HEAD> section
  //   of the code written out to the browser by your popup window.
  //      <script>function bhcheck() {}</script>
  //   For example if your popup runs from mypopup.jsp, edit mypopup.jsp so
  //   that the above line is added as a function to the JavaScript code
  //   written out in its <HEAD> section.  Example:
  //      <html>
  //      <head>
  //      <script>
  //         ... any javascript you have goes here as usual
  //      </script>
  //      <script>function bhcheck() {}</script>   <------- Must be added as is
  //      </head><body>my popup page content goes here</body></html>

  //   STEP 2 (Ignore this step if you want BH to use its own popup window for
  //   the test)
  //
  //   Edit the lines below accordingly
  //   This is a web based path, not a file path (not c:\something\file.jsp)
  //     options.setPopupFileName("/some/web/path/yourPopup.jsp");
  //   This can be whatever you want and is the name assigned via JS to the
  //   popped up window
  //     options.setPopupWindowName("MyPopup");
  //   Use popupWindowFeatures to control the size, location, and features
  //   (such as status bar, scroll bars, etc) as you wish
  //     options.setPopupWindowFeatures("width=300,height=150,location=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,top=300,left=300");

  // CONTINUING ON WITH POPUP DETECTION

  // Setting the popupTimeout value

  // When testing for the presence of the test popup window (or your real popup
  // window if using the advanced option as explained above BrowserHawk will
  // wait up to X milliseconds for this window to load.  By default this value
  // is 2000 ms (2 seconds). If the popup window is not loaded within this
  // amount of time, then the test will complete and PopupsBlocked will be set
  // to True. In the case where popups are not blocked, BrowserHawk will not
  // need to wait and the test will complete without waiting for this timeout
  // period, and PoupsBlocked will be set to False.

  // The default value should suffice in most cases.  However you may want to
  // tweak this for optimal efficiency.  The advantage to lowering this number
  // is that the time it takes to detect blocked popups will be less when poups
  // are blocked. For example, by default it will take 2 seconds to detect when
  // popups are blocked (and instant when they are not). If you lower the
  // popupTimeout value to say 1000 ms (1 second) then it will only take one
  // second to detect when popups are blocked.

  // The downside to lowering this number, however, is that even though the
  // test popup is no more than 1K of data, it still can take a second or two
  // for this page to be loaded from your web server once the BrowserHawk test
  // page attempts to pop it up, just as a result of typical latency.

  // This latency is a function of the visitor's Internet connection.  For
  // example, with a good broadband connection a setting of 1000 ms (1 second)
  // would likely be sufficient.  And a local network connection such as when
  // running on a LAN could use even lower settings, such as 250 ms.
  // Likewise a setting of 1500 or 2000 would likely be needed to accurately
  // test dial up users.

  // In summary, setting this timeout value is a tradeoff between testing time
  // and accuracy of the test.  Set it too low and you run a risk of detecting
  // popups as blocked when they are not.  Set it too high and it will take
  // longer than necessary to determine when popups are indeed blocked.  In
  // either event the test finishes immediately for visitors that have not
  // blocked popups (the popupTimeout value does not come into play).

  // We have found 2000 ms to be reliable setting. Generally we would not
  // recommend lowering it beyond 1250 but you can always experiment.  If you
  // find cases where it is detecting popups as blocked, when they indeed
  // are not, then you need to raise this value back toward 2000 (or possible
  // higher depending on the type of connections your visitors are using.
  // If a value is not specifically set than it will default to 2000 ms.

  // If you are using the advanced option to load your own popup window
  // instead of the default BrowserHawk popup, it may be necessary for you
  // to raise the timeout if you have several kilobytes of data you are
  // loading.

  // options.setPopupTimeout(2000);


  // OK, let's perform the check for blocked popups. See the ExtendedOptions
  // class in the documentation for details on options available such as
  // controlling the background color of the screen and page messages i.e.
  // "Checking your browser. Please wait...". TIP: Set the BGCOLOR parameter
  // below to your site's background color. Otherwise you may notice a color
  // flicker during the test.

options.setPageMessage("Checking your browser. Please wait...");
options.setBodyTag("BGCOLOR=#FFFFFF");
options.setPageTitle("Please wait...");

ExtendedBrowserInfo ebi =
    BrowserHawk.getExtendedBrowserInfo(request, response, options);

  // If ebi is null we must end the current request here.  The browser will
  // now process the client side tests and call this page back a second time,
  // and this time ebi will not be null.

if (ebi == null) return;

  // At this point the ExtendedBrowserInfo (ebi) PopupsBlocked property is set
  // to True if their popups are blocked, and False otherwise! If you used the
  // advanced option to have BrowserHawk pop up your own popup window, and the
  // PopupsBlocked property is False, then you can be sure that indeed your
  // popup window is now on the screen!

%>

<html><head><title>BrowserHawk Sample - Blocked Popup Detection</title></head>
<body><font face="arial">
<blockquote>
<h2>BrowserHawk Blocked Popup Detection Demo</h2>
BrowserHawk detects whether a user has popups blocked. In performing this 
test BrowserHawk attempts to popup its own window for a split second (not 
visible to IE browsers) and then closes it.
<p>
There is an advanced option (not demonstrated here) that allows you to 
popup your own window instead of the test window, and to have BrowserHawk
leave your window up or close it.  BrowserHawk then tells you whether your 
popup was successfully opened.
<p>
<hr>
<p>
<font size=3>
<b>Test results:  Your popups are 

<%
if (ebi.getPopupsBlocked())
   out.println("BLOCKED");
else
{
   out.println("not blocked");

   BrowserInfo bi = BrowserHawk.getBrowserInfo(request);
   if (bi.getBrowser().equals("IE")) {
%>
      <p>Special note if running this test on your local web server with
      IE</b>: IE 6 SP 2 and later by default blocks popups only for the
      Internet zone.  Therefore if you run this test on a local network (or
      same machine as your web server) in the Intranet zone, this test will
      return that popups are not blocked even if your IE blocker is on,
      because indeed IE is not blocking the popup under these conditions. To
      test detection of blocked popups with IE you need to do so over the
      Internet zone, or change your settings so that the local machine is
      not part of the Intranet zone.
<%
   }
}
%>

</b>.
<p>

<b>Change your settings to

<%
if (ebi.getPopupsBlocked())
   out.println("allow");
else
   out.println("block");
%>

poups and click <a href="popupcheck.jsp">here</a> to retry this test.
</b>
<p>
<hr>

<P>
<b><a href="popupcheck.jsp">Retry this test</a></b><P>
<b><a href="index.html">Return to list of other JSP samples</a></b><P>
<b><a href="http://www.cyscape.com/order/">BrowserHawk Pricing and Ordering</a></b><P>
<b><a href="http://www.cyscape.com/">cyScape home page</a></b><P>
</font></blockquote>
</body>
</html>
