/*

Browser sniffer  -  Version 0.11b
by Borgar Þorsteinsson (borgar@undraland.com)

The script is currently in testing mode. It has been confirmed to work 
correctly with the following browsers:

- Mac : Safari 2.0.4 (419.3)
- Mac : Firefox 1.5
- Mac : Opera 9.02
- Windows : MSIE 6
- Windows : Firefox 1.5

As is the standard with code like this, bit are stolen from various 
sources online and/or books. 

The script is not ment to serve as a excaustive browser checking but 
rather match the most common patterns. It was programmed for use with 
simple web log analysis software where there is no need for endless 
details.

If you are in need of a more precise script then I reccomend the
one available from WebReference.com:
  http://www.webreference.com/tools/browser/javascript.html



# 
# Copyright (C) 2006 Borgar Þorsteinsson <borgar@undraland.com>.
# All rights reserved.
# 
# This program is free software; you can redistribute it and/or modify it 
# under the terms of the GNU General Public License as published by the 
# Free Software Foundation; either version 2 of the License, or (at your 
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
# for more details.
# 
# http://www.gnu.org/licenses/gpl.html
#


*/


// search for a word in the user agent string
function mua(s) {
  var ua = navigator.userAgent.toLowerCase();
  return (ua.indexOf(s.toLowerCase()) > -1);
}

// get user agent version with a regexp
function regv(r) {
  var _rx = new RegExp(r);
  _rx.test(navigator.userAgent);
  return parseFloat(RegExp["$1"]);
}



// --- browser ---

isOpera  = mua("Opera"); // Opera
isSafari = mua("AppleWebKit"); // Konq & Safari
isKonq   = mua("Konqueror");
isKHTML  = mua("KHTML") || isKonq || isSafari;
isIE     = mua("compatible") && mua("MSIE") && !isOpera; // MSIE
isMoz    = mua("Gecko") && !isKHTML; // Mozilla
isFox    = isMoz && mua("Firefox"); // Firefox

var b  = "Unknown";
var bv = -1;
if (isIE) 
{
  b  = "Internet Explorer"; 
  bv = regv("MSIE (\\d+\\.\\d+)");
}
else if (isFox) 
{
  b  = "Firefox"; 
  bv = regv("Firefox\\/(\\d+\\.\\d+(?:\\.\\d+)?)");
} 
else if (isOpera) 
{
  b  = "Opera";
  bv = (navigator.appName == "Opera") 
     ? parseFloat(navigator.appVersion) 
     : regv("Opera (\\d+\\.\\d+)");
}
else if (isKonq) 
{
  b  = "Konqueror";
  bv = regv("Konqueror\\/(\\d+(?:\\.\\d+(?:\\.\\d)?)?)");
} 
else if (isSafari) 
{
  b  = "Safari";
  bv = regv("AppleWebKit\\/(\\d+(?:\\.\\d*)?)");
} 
else 
{
  b  = "Unknown";
  bv = -1;
  if (isMoz) 
  {
    b += " (Gecko based)";
    bv = regv("rv:(\\d+\\.\\d+(?:\\.\\d+)?)");
  } 
  else if (isKHTML)
  {
    b += " (KHTML based)";
  } 
}


// -- platform --

if (mua("Windows") || mua("Win32") ) 
{
  p = "windows";
} 
else if (mua("MacPPC") || mua("Mac68K") || mua("macintosh")) 
{
  p = "Macintosh";
} 
else if (mua("Linux") || mua("X11")) 
{
  p = "Linux";
} 
else 
{
  p = "Other";
}


// -- java enabled? --

var isJava = (navigator.javaEnabled());


// -- flash? & version? --

// Adaped from the script found at:
// http://www.webreference.com/tools/browser/javascript.html

var isFlash = false;
var flash = 0;
if (isOpera || isMoz || isFox || isKHTML) 
{ 
  var plugin = (navigator.mimeTypes &&
                navigator.mimeTypes["application/x-shockwave-flash"] &&
                navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin) ?
                navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
  if (plugin) 
  {
    is_Flash = true;
    flash = parseInt(plugin.description.substring(plugin.description.indexOf(".")-1));
  }
}
if (isIE)
{
   document.write(
      '<scr' + 'ipt language=VBScript>' + '\n' +
      'Dim hasPlayer, playerversion' + '\n' +
      'hasPlayer = false' + '\n' +
      'playerversion = 10' + '\n' +
      'Do While playerversion > 0' + '\n' +
         'On Error Resume Next' + '\n' +
         'hasPlayer = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & playerversion)))' + '\n' +
         'If hasPlayer = true Then Exit Do' + '\n' +
         'playerversion = playerversion - 1' + '\n' +
      'Loop' + '\n' +
      'flash = playerversion' + '\n' +
      'isFlash = hasPlayer' + '\n' +
      '<\/sc' + 'ript>'
   );
}


// -- request --
var url = window.location.href;
var ref = window.document.referrer;


// -- screen capablities --
if (window.screen) {
  var s = screen;
  var scr = s.width + 'x' + s.height + 'x' + s.colorDepth + "bit";
} else {
  var scr = "n/a";
}


// -- date --
var d = new Date();



// -- language -- 
/* (navigator.language) 
*/

// -- echo results --

// output a string to browser 
var ebuffer = "";
function echo(o) {
  ebuffer = ebuffer + o + "<br />";
}

function report() {
  echo( 'Time: ' + d.getTime() + '+' + d.getTimezoneOffset() );
  echo( 'Platform: ' + p );
  echo( 'Screen: ' + scr );
  echo( 'Browser: ' + b );
  echo( 'Browser version: ' + bv );
  echo( 'Flash: ' + flash );
  echo( 'Request: ' + url );
  echo( 'Referrer: ' + ref );  
}

function find_self(selfname) {
  var _elms = document.getElementsByTagName("script");
  var i = 0;
  while (_elm = _elms[i++]) {
    if (_elm.src.indexOf(selfname) != -1) { return _elm };
  }
  return false;
}

function report_xml() {
  var _script = find_self('sniffer.js');
  if (_script) {
    var _elm = document.createElement("span");
    _elm.innerHTML = ebuffer;
    _script.parentNode.appendChild(_elm);
  }
}

report();
if (isIE || !document.getElementById) {
  document.write(ebuffer);
} else {
  report_xml();
}





