// The actual captured data is passed back to the server using an <img> tag.
//
// To use only a call to the captureBrowser function need be made, everything
// else is taken care of.
// Additionally the checkVersion function can be called, which will check/ the
// installed versions of flash and the JVM. If they are less than the defined
// required versions then one or two buttons will be presented to allow the 
// version to be upgraded.
//

// ***** CONFIGURATION NEEDED HERE *****
var serverDataUrl = "http://www.cornishheritagefarms.com/getimage.php";
var jvmVersionRequired = "1.4.0.09"; // Note '.09' and not '_09'.
var flashVersionRequired = "7.0.00";
var jvmDownloadLocation = "http://jdl.sun.com/webapps/download/AutoDL?BundleId=10750";
var flashDownloadLocation = "http://fpdownload.macromedia.com/get/flashplayer/current/install_flash_player.exe";

// With the exception of popupStatus, which is used internally, these variables
// are all set during the checking process.
var browser;
var agent;
var width;
var height;
var java;
var javaVersion;
var javaVendor;
var activeX;
var flash;
var flashVersion;
var cookies;
var popups;
var os;


// This function contains the logic to check whether the capture routine needs
// to be run (every 30 days) and, if so, then perform the checks. The 
function captureBrowser()
{
    // Run all of the necessary checks.
    runChecks();

    // Create the image tag that sends things back to the server.
    getImageString();
}

// This function actually controls what tests are performed.
function runChecks() 
{
  getBrowser();
  getAgent();
  getResolution();
  getOs();
  checkJavaEnabled();
  checkActiveX();
  checkFlashEnabled();
  checkCookies();
}

// The GetImageString function assumes that runChecks has been called and then
// takes the variables defined at the start of this file to create a <img> tag
// that is written to the loading HTML document and automatically calls the
// server with the browser client information.
function getImageString() {
    s = "b=" + escape(browser) + "&a=" + escape(agent) + "&w=" + width + "&h=" + 
      height + "&j=" + java + "&jv=" + javaVersion + "&n=" + escape(javaVendor) + 
      "&x=" + activeX + "&f=" + flash + "&fv=" + flashVersion + "&c=" + 
      cookies + "&p=" + popups + "&o=" + escape(os);

    document.write("<img src=\"" + serverDataUrl + "?" + s + 
      "\" width=\"0\" height\"0\">");
}

// Checks that the ELM cookie is present. If it's not then null is returned.
function getCookie()
{
  if (document.cookie.length > 0)
  { 
    start = document.cookie.indexOf("CHFCheck=");
    if (start != -1)
    {
      start += 9;
      end = document.cookie.indexOf(";", start);
      if (end == -1) 
      {
        end = document.cookie.length;
      }
      return unescape(document.cookie.substring(start, end)); 
    }
  }

  return null;
}

function setCookie()
{ 
  var exp = new Date();
  exp.setTime(exp.getTime() + (10 * 1000));
  document.cookie = "CHFCheck=true; expires=" + exp.toGMTString();
}

// Records the current browser name in lowercase. Unknown if not available.
function getBrowser() 
{
  browser = navigator.appName.toLowerCase();
  if (browser == null)
  {
    browser = "Unknown";
  }
}

// Records the current browser agent details. Unknown if not available.
function getAgent() 
{
  agent = navigator.userAgent.toLowerCase();
  if (agent == null)
  {
    agent = "Unknown";
  }
}

// Records the width and height of the screen as the browser report it. Both
// values are set to unknown if either is not available.
function getResolution() 
{
  if (screen.width && screen.height)
  {
    width = screen.width;
    height = screen.height;
  }
  else
  {
    width = "Unknown";
    height = "Unknown";
  }
}

function getOs()
{
  if ((agent.search(/windows/) !=-1) || ((agent.search(/win9\d{1}/) != -1)))
  {
    os = "Windows";
    if (agent.search(/nt\s5\.1/) != -1)
    {
      os += " XP";
    }
    else if (agent.search(/nt\s5\.0/) != -1)
    {
      os += " 2000";
    }
    else if (agent.search(/nt\s5\.2/) != -1) {
      os += " 2k3";
    }
    else if ((agent.search(/nt\s4\.0/) != -1) || (agent.search(/nt4\.0/) ) != -1) {
      os += " NT 4";
    }
  }
  else if (agent.search(/linux/) != -1) {
    os = "Linux";
  }
  else if (agent.search(/sunos/) != -1) {
    os = "Sun Os";
  }
  else if ((agent.search(/unix/) != -1) || (agent.search(/x11/) != -1))
  {
    os = "Unix";
  }
  else {
    os = "Unknown";
  }
}

// Records whether Java is available and what version it is at. The version will
// be recorded as Uknown if not available. This function has to use an Applet to
// get the java version number, believ it or not, as that is the only reliable 
// cross browser way of doing so.
function checkJavaEnabled() 
{
  java = true;
  javaVersion = "Unknown";
  javaVendor = "Unknown";

  // If the browser has all java support switched off then nothing is available.
  // Note: Java may be reported as available when in fact it is disabled, hence
  // the Applet.
  if (navigator.javaEnabled() == false) 
  {
    java = "disabled";
    return;
  }
}

// Check that ActiveX is enabled by seeing if a simple widely available
// object can be created.
function checkActiveX()
{
  activeX = false;

  // Only bother to test on ie (although technically it is possible to setup 
  // ActiveX in Mozilla).
  if (browser.match("^microsoft internet explorer.*$") != null) 
  {
    try
    {
    	var o = new ActiveXObject("JavaScript");
      activeX = true;
    }
    catch (ex)
    {
    }
  }
}

// This function checks that flash is available and what version it is at. 
function checkFlashEnabled() 
{
  flash = false;
  flashVersion = "Unknown";

  // IE uses ActiveX, so if that's not available there is no way of knowing
  // what flash version is available.
  if (browser.match("^microsoft internet explorer.*$") != null) 
  {
    // Basically we simply loop round all of the possible versions, starting 
    // from the latest to to oldest, to see which one is available as an
    // ActiveX object.
    for (i = 20; i > 0; i--) 
    {
      try 
      {
        // Create the object for this test version. If it exists then the 
        // version is obtained.
        var o = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);

        // Get the version number and break it down into an array so that 
        // it is easily readable.
        s = o.GetVariable("$version") // Given "WIN 2,0,0,11"
        tempArray = s.split(" ");     // ["WIN", "2,0,0,11"]
        tempString = tempArray[1];    // "2,0,0,11"
        versionArray = tempString.split(",");	// ['2', '0', '0', '11']

        flash = true;
        flashVersion = versionArray[0] + "." + versionArray[1] + "." + 
          versionArray[2];
        return;
      }
      catch (ex) 
      {
        // An exception means that Flash is not available
      }
    }
  }
  else 
  {
    // Decent browsers allow you to look at the plugins without having to 
    // create objects. We now take a look at the plugins avaialble and see if 
    // Flash is there, if so what version it is.
    try 
    {
      var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      var flashDescription = navigator.plugins["Shockwave Flash" + 
        swVer2].description;			
      var descArray = flashDescription.split(" ");
      var tempArrayMajor = descArray[2].split(".");
      var versionMajor = tempArrayMajor[0];
      var versionMinor = tempArrayMajor[1];
      if ( descArray[3] != "" ) 
      {
        tempArrayMinor = descArray[3].split("r");
      } 
      else 
      {
        tempArrayMinor = descArray[4].split("r");
      }
      var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
      var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;

      flash = true;
      if (flashDescription != "")
      {
        flashVersion = flashVer;
      }
    }
    catch (ex) 
    {
      // Flash not present
    }
  }
}

// Cookies are enabled only if we can set them and get them. The browser lies 
// about whether a cookie is available or not. The same cookie is set in this 
// function as the one used to check whether to run the test or not. Therefore
// calling this function is mandatory.
function checkCookies() 
{
  cookies = false;
  // Try to set the cookie and then get it again. If it's still null then
  // cookies cannot be set.
  setCookie();
  if (getCookie() != null)
  {
    cookies = true;
  }
}
