// ----------------------------------------------------------------------------
// Copyright (c) 2007, Quia Corporation. All rights reserved.
//
// This script provides utilities for the detecting a user's system info
//
// $Id$;
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
function isNumeric(text) {
  validChars = "0123456789.";
  for (i = 0; i < text.length; i++) {
    if (validChars.indexOf(text.charAt(i)) == -1) {
      return false;
    }
  }
  return true;
}

// ----------------------------------------------------------------------------
function getBrowserName() {
  browserName = navigator.appName;

  if (browserName == "Microsoft Internet Explorer") {
    version = navigator.userAgent;
    msieIndex = version.indexOf("MSIE");
    if (msieIndex != -1) {
      startIndex = msieIndex + 5; 
      endIndex = version.indexOf(";", startIndex);
      versionString = version.substring(startIndex, endIndex);
      if (isNumeric(versionString)) {
        browserName = browserName + " " + versionString;
      }
    }
    return (browserName);
  }

  // If appName is not IE, then it will say Netscape, so
  // we extract the actual browser name from the
  // userAgent data
  userAgent = navigator.userAgent;
  
  // check for Google Chrome
  var chromeIdx = userAgent.indexOf("Chrome");
  if (chromeIdx > -1){
    var endIdx = userAgent.indexOf(' ', chromeIdx);
    var browserInfo = userAgent.substring(chromeIdx, endIdx).split("/");
    return ('Chrome ' + browserInfo[1]);
  }

  // First check for firefox
  var firefoxIndex = userAgent.indexOf("Firefox");
  if (firefoxIndex != -1) {
    startIndex = firefoxIndex + 8;
    version = userAgent.substring(startIndex, userAgent.length);
    if (isNumeric(version)) {
      return ("Firefox " + version);
    } else {
      return ("Firefox " + version);
    }
  }

  // Next check for safari
  var safariIdx = userAgent.indexOf("Safari");
  if (safariIdx > -1) {
    var versionIdx = userAgent.indexOf("Version");
    // bug 19231 - speng, 9/24/09
    // for safari version 2.0.4 or earlier, user agent string does not "Version"
    // as a parameter.  All newer Safari has it: format: Verion/[version number].
    var verInfoFlag = true;
    if (versionIdx < 0){
      versionIdx = safariIdx;
      verInfoFlag = false;
    }
    var v_endIdx = userAgent.indexOf(' ', versionIdx);
    if (v_endIdx < 0){
      v_endIdx = userAgent.length;
    }
    var versionInfo = userAgent.substring(versionIdx, v_endIdx).split("/");
    if (!verInfoFlag){
      // no version parameter in userAgent string, using other means to find version
      var tmpArr = versionInfo[1].split(".");
      var tmpVer = parseInt(tmpArr[0], 10);
      if (tmpVer > 400 && tmpVer < 500){
        versionInfo[1] = '2.0-2.0.4';
      }
      else if (tmpVer > 300 && tmpVer < 400){
        versionInfo[1] = '1.3-1.3.2';
      }
      else if (tmpVer > 80 && tmpVer < 300){
        versionInfo[1] = '1.2.4 or earlier';
      }
    } // if (!verInfoFlag)
    return('Safari ' + versionInfo[1]);
  }

  // Otherwise we will just return what the browser name
  // says it is, which will be Netscape or Konquerer or Opera
  // We take the version from the appVersion field - the
  // first thing in that field is a version number. For
  // Netscape this will be correct.
  version = navigator.appVersion;
  endIndex = version.indexOf(" ");
  if (endIndex != -1) {
    actualVersion = version.substring(0, endIndex);
    if (isNumeric(actualVersion)) {
      browserName = browserName + " " + actualVersion;
    }
  }

  return browserName;
}

// ----------------------------------------------------------------------------
function getIsCookiesEnabled(browser, os) {

  // This is very clunky but I want to make sure we never
  // return false information. This isn't perfect, of
  // course, since it does not take in to account
  // different browser version. However, hopefully the
  // number of false positives or false negatives will be
  // minimized. 

  // IE on windows, Firefox, and Netscape all support
  // the method of testing by setting a test cookies.
  if ((browser.indexOf("Internet Explorer") != -1 &&
       os.indexOf("Win") != -1) ||
      browser.indexOf("Netscape") != -1 ||
      browser.indexOf("Firefox") != -1) {
    document.cookie = "testcookie";
    if (document.cookie.indexOf("testcookie") != -1) {
      return ("true");
    } else {
      return ("false");
    }
  }

  // Safari does not support above method but does
  // support the navigator.cookieEnabled field
  if (browser.indexOf("Safari") != -1) {
    cookiesEnabled = navigator.cookieEnabled;
    if (cookiesEnabled) {
      return ("true");
    } else {
      return ("false");
    }
  }

  // If it is none of the above browsers, then we don't
  // know if either of the methods will work, so we just
  // return the empty string.
  // We can't use the cookiesEnabled field itself to 
  // determine whether
  // it is valid since it is a boolean and if it is not
  // supported it returns false, which is
  // indistinguishable from a valid false.
  return ("");
}

// ----------------------------------------------------------------------------
function getOS() {
  userAgent = navigator.userAgent;

  if (userAgent.indexOf("Win") != -1) {
    return ("Windows");
  }

  if (userAgent.indexOf("Mac") != -1) {
    return ("Mac");
  }

  if (userAgent.indexOf("Linux") != -1) {
    return ("Linux");
  }

  if (userAgent.indexOf("X11") != -1) {
    return ("Unix");
  }

  return ("");
}

// ----------------------------------------------------------------------------
function getIsJavaEnabled(browser, os) {

  // Also clunky. We know that the javaEnabled function
  // is supported in Netscape and Safari
  // Anything else, we don't know, so we just
  // return the empty string.
  // We can't use the function itself to determine whether
  // it is valid since it is a boolean and if it is not
  // supported it returns false, which is
  // indistinguishable from a valid false.

  if (browser.indexOf("Netscape") != -1 ||
      browser.indexOf("Safari") != -1) {
    javaEnabled = navigator.javaEnabled();
    if (javaEnabled) {
      return ("true");
    } else {
      return ("false");
    }
  }

  return ("");
}

// ----------------------------------------------------------------------------
function getReferrer() {
  // If it's not supported or the browser can't determine
  // it, this just returns the empty string
  return document.referrer;
}

// ----------------------------------------------------------------------------
function getScreenWidth() {
  return screen.width;
}

// ----------------------------------------------------------------------------
function getScreenHeight() {
  return screen.height;
}

// ----------------------------------------------------------------------------
function getLocalDate() {
  return (new Date());
}

// ----------------------------------------------------------------------------
// This method assumes that all the fields it is setting 
// already have been defined in the form 
function fillHiddenFields(formObj) {
  browserName = getBrowserName();
  os = getOS();

  formObj.detected_browser_name.value = browserName;
  formObj.detected_cookies_enabled.value = getIsCookiesEnabled(browserName, os);
  formObj.detected_os.value = os;
  formObj.detected_java_enabled.value = getIsJavaEnabled(browserName, os);
  formObj.detected_referrer.value = getReferrer();
  formObj.detected_screen_width.value = getScreenWidth();
  formObj.detected_screen_height.value = getScreenHeight();
  formObj.detected_date.value = getLocalDate();

  return true;
}

