// JavaScript Document

// OTF_NOTF_detection.js v0.2.3  by Eric Gerds
//
// This script, together with PluginDetect and getJavaInfo.jar, is capable of doing 
// both OTF and NOTF Java detection. Note that NOTF detection can only be performed 
// if NOTF is enabled when generating the PluginDetect script.
// 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 following code in the <body> of your HTML page:
//       <div id="javaresult"></div>
//       <script type="text/javascript" src="OTF_NOTF_detection.js"></script>
//
//  3) Get a copy of the "getJavaInfo.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/getJavaInfo.jar
//     then jarfile = "stuff/getJavaInfo.jar"
//
//     If your web page is at      http://www.mysite.com/webpage.htm
//     and you have the jarfile at http://www.mysite.com/getJavaInfo.jar
//     then jarfile = "getJavaInfo.jar"
//



function addText(node, text){
     var N = document.getElementById(node);
     if (N){
        N.appendChild(document.createTextNode(text));
        N.appendChild(document.createElement('br'));
     }
};



var outputNode = 'javaresult';    // Id of output <div>. Detection results are placed in this div.

// GLOBAL JAVA VARIABLES
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
var jarfile = 'getJavaInfo.jar';


// This statement causes Java plugin detection to occur using the jarFile 'getJavaInfo.jar'.
// The detection results are stored in memory for quick retrieval.
// Only the very first java PluginDetect command needs to have the jarFile input argument.
// All subsequent java PluginDetect commands will not need to have the jarFile input argument.
//
// The path of the jar file is relative to the web page ( NOT relative to this
//  external javascript file ).
//
//
// The optional input argument after the jarfile is called 'verifyTags'.
// If the verifyTags input argument is not specified, then PluginDetect assumes
// a value of [2,2,2].
//
// PluginDetect.onDetectionDone() == 1 means detection has completed (OTF)
// == 0 means detection not completed yet (NOTF)
// == -1 means detection unable to proceed due to error
PluginDetect.onDetectionDone('Java', 0, jarfile, [2,2,2]);



// This function 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');
  }

  // Note: JavaStatus is never -0.5 or +0.5 here because by the time that displayResults()
  // is executed, the Java detection will already be completed.
  // Hence there is no need to check for JavaStatus == -0.5 or +0.5


  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('Java');

  if (typeof INFO.vendor != 'undefined'){  // verify that INFO object has information
  

     if (INFO.OTF==0) addText(outputNode, 'Java detection: ON THE FLY (OTF)');
     else if (INFO.OTF==2) addText(outputNode, 'Java detection: NOT ON THE FLY (NOTF)');
     else addText(outputNode, 'Java detection: NOT ON THE FLY (NOTF) but detection not completed yet.');


     addText(outputNode, 'Deployment Toolkit browser plugin installed & enabled: ' +
        (INFO.DeploymentToolkitPlugin ? 'true' : 'false'));

     // INFO.MimeType_versions array info comes from navigator.mimetypes array.
     // 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]' : '')
       );


     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]' : '')
       );



     // For IE, you can also use the
     //  <object> tag with classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
     // to display a Java applet.
     if (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]' : '')
       );



  };

  
  addText(outputNode, 'navigator.javaEnabled(): ' + navigator.javaEnabled());
  
  if ($.isIE) addText(outputNode,
     'ActiveX / ActiveX scripting enabled: ' + PluginDetect.ActiveXEnabled); 


// -----------------------------------------------------------------------------------------



};  // end of function displayResults()



// When Java detection has been completed, then run the displayResults() method.
// The onDetectionDone('Java') method can handle both OTF and NOTF.
//
// ON THE FLY (OTF):
//   If the java detection is being performed OTF, then the java detection results are
//   available on the fly and so displayResults() is executed right away.
//   [ie. PluginDetect.isMinVersion('Java') != -0.5 and != +0.5]
//
// NOT ON THE FLY (NOTF):
//   If the java detection is being performed NOTF, then the detection results are
//   not available on the fly, and so we wait until the results are available. 
//   In the meantime your javascript would continue executing
//   commands. And then when the java detection has finally completed (no later than
//   window.onload), the displayResults() function would be executed.
//  [While waiting for the java detection results, we have 
//     PluginDetect.isMinVersion('Java') == -0.5 or +0.5]
//  [After the java detection has completed, we have
//     PluginDetect.isMinVersion('Java') != -0.5 and != +0.5]
//
// NOTF must be enabled in order for NOTF java detection to occur. If NOTF is disabled then all
// java detection is OTF.
PluginDetect.onDetectionDone('Java', displayResults);



// Execute some function after the window has loaded
PluginDetect.onWindowLoaded(
 function(){
  addText(outputNode, "[window.onload event has fired successfully]");
   }
);






