﻿//--------------------------------------------------------------------------------------------
// BrowserCheck.js
//
// Routines to check the version and installed applications of a browser.
//
// TODO:
//  -make browser version checking a litte more robust
//  -add ability to specify which errors are fatal and which are simply warnings
//--------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------
// Checks that the browsing environment has all the bells and whistles needed to use the site.
// Returns true if everything checks out (more or less), false if things aren't kosher.
// Will handle interaction with the user internally.  Note that returning true != no user
// interaction; if the res is set wrong for example, a suggestion will be made, but the user
// will still be allowed to proceed.
//
// Params:
//  elemErr: an element used for reporting errors; innerHTML will be set
//  bVersion: check browser version
//  nMinMajor / nMinMinor: numerical indication of min required browser version
//  bJava: check java enabled
//  bCookies: check cookies enabled
//  bResolution: check resolution
//  nMinWidth / nMinHeight: minimum required resolution
//  bSSL: check secure channel
//--------------------------------------------------------------------------------------------
function prereqCheck(elemErr, bVersion, nMinMajor, nMinMinor, bJava, bCookies,
      bResolution, nMinWidth, nMinHeight, bSSL)
{
  var sErrors = "";
  var bProceed = true;
  var sDisplay = "";

  // check for fatal errors first
  if (bVersion) {
    sErrors += browserVersionError(nMinMajor, nMinMinor)
  }

  // check for a valid java VM
  //
  if (bJava) {

    // The check is performed by attempting to run a java applet, and if the applet
    // is not present or not loaded (meaning a valid Java VM is not avaliable or
    // enabled), the call will throw an error
    // Since we are using try/catch wrap that's only avaliable for IE 5 or above,
    // we need to make sure the proper check has being done for browser version

    /*
    // the following applet needs to be defined in order for testJVM() to work
    <!-- This applet definition is for the Checking of the Virtual Machine -->
    <APPLET
      ID=JVMtester
      CODEBASE = "/processnet/html/pnjtrend"
      CODE     = "jvmtester.JVMTest.class"
      WIDTH    = 1
      HEIGHT   = 1
      STYLE = "position:absolute;left:-1px"
    >
    </APPLET>
    */

    if (("" == sErrors) && (nMinMajor >= 5)) {
      try {
        // This call was originally meant to test if the Java VM is a MSJVM,
        // but since we now accept both, we don't really care about the result
        // we are just testing if we can call it
        document.getElementById("JVMTester").isMicrosoftJVM();
      }
      catch (e) {
        sErrors += "A Java Virtual Machine is required \nto run Operational Insight properly.\n";
        sErrors += "Check if you have a JavaVM installed \nand enabled.\n";
        sErrors += "Please contact your administrator for \nmore details\n";
      }
    }
    else
    {
      // You enter this if you have not verify that IE version is above 5
      // Use window.navigator.javaEnabled() to check instead
      sErrors += browserJavaError();
    }
  }

  if (bCookies) {
    sErrors += browserCookieError();
  }

  if (sErrors != "") {
    bProceed = false;
  }

  // check for non-fatal errors
  if (bResolution) {
    sErrors += browserResolutionError(nMinWidth, nMinHeight);
  }
  if (bSSL) {
    sErrors += browserSSLError();
  }

  if (sErrors != "") {
    sDisplay = "<pre>The following errors occurred:\n" + sErrors;
    if (bProceed) {
      sDisplay += "You may be unable to use the site properly.";
    }
    sDisplay += "</pre>";
    elemErr.innerHTML = sDisplay;
  }
  return bProceed;
}

//---------------------------------------------------------------------------------------------
// browser*Error()
// These functions check the browsing environment to see if everything required is present.
// They all return strings; "" if there are no errors, and a newline-terminated error string
// if there is.
//---------------------------------------------------------------------------------------------

// Detects the Virtual Machine on the clients PC


function testJVM()
{
  if(document.getElementById("JVMTester").isMicrosoftJVM())
   return "";
  else
   return "The MS Java Virtual Machine is not enabled in your browser.\n Modify the Internet Options in the Tools menu to enable it and Uncheck the Sun Java Virtual Machine.\n";
}



function browserResolutionError(nReqWidth, nReqHeight)
{
  var width = screen.width;
  var height = screen.height;

  if(width >= nReqWidth && height >= nReqHeight) {
    return "";
  } // if
  else {
    return "A screen size of " + nReqWidth + " x " + nReqHeight + "\nor higher is recommended.\n";
  } // else
}

function browserSSLError()
{
  if(window.location.protocol.toUpperCase() == "HTTPS:") {
    return "";
  } // if
  else {
    return "SSL/TLS secure channels are recommended.\n";
  } // else
}

function browserCookieError()
{
  if (navigator.cookieEnabled) {
    return "";
  } else {
    return "Cookies must be enabled.\n";
  }
}

function browserJavaError()
{
  if(window.navigator.javaEnabled()) {
    return "";
  } // if
  else {
    return "Java is not enabled in your browser.  Modify the Internet Options in the Tools menu to enable it.\n";
  } // else
}


// returns error string relating to incompatible browser versions, or "" on no error
// reqVer* is the minimum required version
function browserVersionError(nReqVerMajor, nReqVerMinor) {

  if(window.navigator.userAgent.indexOf("MSIE") > 0) {
    var userAgent = window.navigator.userAgent;

    //if no ending semi-colan was found, assume version is in format "MSIE X.X"
    var nLength = userAgent.lastIndexOf(";");
    if(nLength <= 0)
      nLength = 8;

    var versionString = userAgent.substring(userAgent.indexOf("MSIE"), nLength);

    var sVersion = versionString.split(" ")[1];
    sVersion = sVersion.split(";")[0];

    //var sVersion = versionString.substring(5, nLength);
    var version = Number(sVersion);
    var versionToCompare = Number(nReqVerMajor + "." + nReqVerMinor);

    /*
    alert("userAgent = \t\t" + userAgent + "\r\n" +
          "Version String = \t\t" + versionString + "\r\n" +
           "Pre-Number Agent =\t[" +sVersion + "]\r\n" +
           "Agent version = \t\t[" + version + "]\r\n" +
           "Compare Version = \t\t[" + versionToCompare+"]");
    */

    if(version >= versionToCompare){
      return "";
    } else {
      return "Internet Explorer version " + nReqVerMajor + "." + nReqVerMinor + " or higher is required.\n";
    } // else
  } else {
    return "Internet Explorer version " + nReqVerMajor + "." + nReqVerMinor + " or higher is required.\n";
  } // else

} // isBrowserValid()


