import javax.servlet.*;
import javax.servlet.http.*;
import com.cyscape.browserhawk.*;

import java.io.PrintWriter;
import java.io.IOException;

public class PopupCheck extends HttpServlet {

  // This sample is provided under the terms of the BrowserHawk End User
  // License Agreement. Copyright (c) 2003-2005 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.

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // 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 popups up a tiny window
    // of its own and immediately closes it.  On IE this is transparent and
    // the popup will not be seen at all.  On other browsers the window may be
    // seen opening for a split second before disappearing.

    // 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.asp, edit mypopup.asp 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.asp)
    //     options.setPopupFileName("/some/web/path/yourPopup.aspx");
    //   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!

    PrintWriter out = response.getWriter();
    out.println("<html><head><title>BrowserHawk Sample - Blocked Popup Detection</title></head>");
    out.println("<body><font face=arial>");
    out.println("<blockquote>");
    out.println("<h2>BrowserHawk Blocked Popup Detection Demo</h2>");
    out.println("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.");
    out.println("<p>");
    out.println("There is an advanced option (not demonstrated here) that allows you to popup your own window instead of the test window, and to have BrwoserHawk leave your window up or close it.");
    out.println("BrowserHawk then tells you whether your popup was successfully opened.");
    out.println("<p>");
    out.println("<hr>");
    out.println("<p>");
    out.println("<font size=3>");
    out.println("<b>Test results:  Your popups are ");

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

    out.println("</b>.");
    out.println("<p>");

    out.println("<b>Change your settings to");

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

    out.println("poups and click <a href='?'>here</a> to retry this test.");
    out.println("</b>");
    out.println("<p>");
    out.println("<hr>");
    out.println("<p>");
    out.println("</font></blockquote>");
    out.println("</body>");
    out.println("</html>");
  }
}
