/*
                    OTF_NOTF_winLoaded.js v0.3.6  by Eric Gerds

 - This script, together with PluginDetect and getJavaInfo2.jar, is capable of doing
 both OTF and NOTF Java detection. 
 
 - NOTF detection can only be performed by this script if the PluginDetect script is
 generated with the "NOTF" checkbox enabled.

 - Java Detection is performed AFTER the browser window has loaded.

 - Feel free to change this script, remove comments, etc... to fit your own needs.


 USAGE:
  1) Insert the PluginDetect script in the <head> of your HTML page:
        <script type="text/javascript" src="PluginDetect.js"></script>

  2) Insert the output <div> in the <body> of your HTML page. This div will receive the
     plugin detection results:
        <div id="javaresult"></div>

  3) If you wish to use the plugindetect <div>, then place it wherever you wish in the
     <body>. For example:
        <div id="plugindetect" style="right:0px; top:0px; position:absolute;"></div>

  4) Insert the OTF_NOTF_winLoaded.js script anywhere after the PluginDetect script
     It does not matter too much where you place this script because it waits until the window
     has loaded before doing any detection.

  5) Get a copy of the "getJavaInfo2.jar" jarfile and put it in the same folder as your
     HTML page.

     If you put the jarfile in a different directory than the HTML page, then
     you must adjust the path of the jarfile.
     The jarfile path is always relative to the web page.

     For example, say your web page is at http://www.mysite.com/webpage.htm
     and you have the jarfile at         http://www.mysite.com/stuff/getJavaInfo2.jar
     then jarfile = "stuff/getJavaInfo2.jar"

     If your web page is at      http://www.mysite.com/webpage.htm
     and you have the jarfile at http://www.mysite.com/getJavaInfo2.jar
     then jarfile = "getJavaInfo2.jar"


*/


// GLOBAL VARIABLES
var PD = PluginDetect;
var outputNode = 'javaresult';    // Id of output <div>. Detection results are placed in this div.
var JavaInstalled;                // Tells if Java is installed or not
var JavaVersion;                  // Highest installed version
var minVersion = '1,4,2';         // minimum version of Java we are trying to detect

// The path of the jarfile is relative to the web page ( NOT relative to this
// external javascript file!!! ). Only the very first Java PluginDetect command that is executed
// needs to have the jarfile input argument. You do not have to specify this input arg in
// any subsequent Java PluginDetect commands.
var jarfile = '../Jarfiles/version2/getJavaInfo2.jar';

// If the verifyTags input argument is not specified or is null, then PluginDetect assumes
// a default value of [2,2,2]. Only the very first Java PluginDetect command
// would need to have the verifyTags input argument, if at all. You do not have to specify 
// this input arg in any subsequent Java PluginDetect commands.
var verifyTags = null;



function addText(node, text){
     var N = document.getElementById(node);
     if (N){
        N.appendChild(document.createTextNode(text));
        N.appendChild(document.createElement('br'));
     }
};


// If we use the beforeInstantiate method, then use it BEFORE any other PluginDetect method.
var F = function(){};
if (PD.beforeInstantiate) PD.beforeInstantiate('Java', F);




// This event handler sets the GLOBAL JAVA VARIABLES, and prints the results to the screen
function displayResults($){

  var JavaStatus;


  // -----------------------------------------------------------------------------------
  // Test whether Java is installed or not.


  // Note that PluginDetect.isMinVersion('Java') is equivalent to
  //    PluginDetect.isMinVersion('Java', '0').
  JavaStatus = $.isMinVersion('Java');

  JavaInstalled = JavaStatus >= 0 ? true : false;

  addText(outputNode, "Java (using <applet> and/or <object> tag) installed & enabled: " + (JavaInstalled ? 'true' : 'false'));


  // -----------------------------------------------------------------------------------
  // Display the highest installed Java version.

  JavaVersion = $.getVersion('Java');

  addText(outputNode, "Highest Installed Java Version: " + JavaVersion);


  // ------------------------------------------------------------------------------------
  // Check if some minimum Java version is installed

  JavaStatus = $.isMinVersion('Java', minVersion);

  if (JavaStatus == 1){
     addText(outputNode, 'Java ' + minVersion + ' or higher (using <applet> and/or <object> tag) is installed & enabled.');
  }

  else if (JavaStatus == 0){
     addText(outputNode, 'Java installed & enabled but version is unknown');
  }


  else if (JavaStatus == -0.2){
     addText(outputNode, 'Java installed but not enabled');
  }


  else if (JavaStatus == -0.5 || JavaStatus == 0.5){
      addText(outputNode, 'Java detection: not completed yet, requires NOTF detection.');
  }


  else if (JavaStatus == -1){
     addText(outputNode, 'Java version is < ' + minVersion + ' or not installed / not enabled');
  }


  // -----------------------------------------------------------------------------------------


  // Get extra info on the Java plugin.
  var INFO = $.getInfo ? $.getInfo('Java') : null;

  if (INFO && typeof INFO.vendor != 'undefined'){  // verify that INFO object has information
  

     if (INFO.OTF==0) addText(outputNode, 'Java detection: completed ON THE FLY (OTF)');
     else if (INFO.OTF==2) addText(outputNode, 'Java detection: completed NOT ON THE FLY (NOTF)');
     else addText(outputNode, 'Java detection: not completed yet, requires NOTF detection.');


     addText(outputNode, 'Deployment Toolkit browser plugin installed & enabled: ' +
        (INFO.DeploymentToolkitPlugin ? 'true' : 'false'));


     addText(outputNode, 'Next-Generation Java Plugin2 installed & enabled: ' +
       (INFO.isPlugin2 ==1 ? 'true': (INFO.isPlugin2==-1 ? 'false' : 'unknown'))
       );


     // INFO.All_versions array lists ALL Java versions found from all sources
     if (INFO.All_versions.length>1)
        addText(outputNode, 
           'Multiple Java versions detected: ' + INFO.All_versions.join(" & "));


     if (INFO.vendor.length > 0) addText(outputNode, "Java vendor: " + INFO.vendor);


     // Data from navigator.plugins array
     if (INFO.name.length > 0) 
         addText(outputNode, "Java plugin name: " + INFO.name);
     if (INFO.description.length > 0) 
         addText(outputNode, "Java plugin description: " + INFO.description);


     if (INFO.objectTag!==null)
       addText(outputNode,
         'Java (using <object> tag) installed & enabled: ' + (INFO.objectTag>=0?'true':'false') +
         (INFO.objectTag==0 ? '  [but Javascript/Java bridge is disabled for this tag OR your jarfile path/name is incorrect]' : '')
       );


     if (INFO.appletTag!==null)
       addText(outputNode,
         'Java (using <applet> tag) installed & enabled: ' + (INFO.appletTag>=0?'true':'false') +
         (INFO.appletTag==0 ? '  [but Javascript/Java bridge is disabled for this tag OR your jarfile path/name is incorrect]' : '')
       );



     // For IE, you can also use the
     //  <object> tag with classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
     // to display a Java applet.
     if ($.isIE && INFO.objectTagActiveX!==null)
       addText(outputNode,
         'Java (using <object> tag with Sun Java ActiveX) installed & enabled: ' + (INFO.objectTagActiveX>=0?'true':'false') +
         (INFO.objectTagActiveX==0 ? '  [but Javascript/Java bridge is disabled for this tag OR your jarfile path/name is incorrect]' : '')
       );
       


      var out, e, x, PLUGIN = INFO.PLUGIN, T =
          ['file.encoding', 'file.encoding.pkg', 'file.separator', 'java.class.path',
           'java.class.version', 'java.compiler', 'java.home', 'java.io.tmpdir',
           'java.version', 'java.vendor', 'java.vendor.url', 'user.dir', 'user.home',
           'java.vm.name', 'java.vm.specification.name', 'java.vm.specification.vendor',
           'java.vm.specification.version', 'java.vm.vendor', 'java.vm.version',
          'java.runtime.name', 'java.runtime.version', 'path.separator', 'java.specification.name',
           'java.specification.version', 'java.specification.vendor',
           'os.version', 'os.name', 'os.arch'];

      // Query instantiated Java applet to get extra info
      if (PLUGIN){
          addText(outputNode, "");
          addText(outputNode, "JAVA APPLET QUERY USING java.lang.System.getProperty(): ")
          for (x=0;x<T.length;x++){
             out=null;
             try{out = PLUGIN.getProp(T[x]) + " "}
             catch(e){};
             if (typeof out=='string' && out.length>1) addText(outputNode, T[x] + ": " + out);
          };
          addText(outputNode, "");
      };




  };

  
  addText(outputNode, 'navigator.javaEnabled(): ' + navigator.javaEnabled());
  
  if ($.isIE) addText(outputNode,
     'ActiveX / ActiveX scripting enabled: ' + PD.ActiveXEnabled); 


// -----------------------------------------------------------------------------------------



};  // end of displayResults()






// Execute some function after the window has loaded
PD.onWindowLoaded(
  function(){
     addText(outputNode, "[window.onload event has fired successfully]");

     // Start Java plugin detection, using the jarfile if needed.
     // When Java detection has been completed, then run the displayResults() method.
     // The onDetectionDone('Java') method can handle both OTF and NOTF.
     //
     // onDetectionDone() == 1 means detection has completed (OTF)
     // == 0 means detection not completed yet (and thus NOTF detection is being performed)
     // == -1 means detection unable to proceed due to error
     PD.onDetectionDone('Java', displayResults, jarfile, verifyTags);
  }
);


