// Nicolas - 2003-04-24 10:46:42 - V1.0.1
// Reno - 2003-03-13 09:25:45 - V1.0.0
//*******************************************************************************
//-------------------------------------------------------------------------------
// DETECT.JS
//-------------------------------------------------------------------------------
//*******************************************************************************
/*************** Script functions in this file **************
Filename: detect.js; as of 09/24/2001
  FUNCTION:                                               LINE:
  Fl_DetectFlash()                                         72   
  Fl_DetectFlashVb()                                       61   
************ (end Script functions in this file) ***********/

// Determine Versions
var agent=navigator.userAgent.toLowerCase();
var browser=navigator.appName.toLowerCase();
var versMajor = parseInt(navigator.appVersion);
var versMinor = parseFloat(navigator.appVersion);

// Determine browser.
var isNS = (browser.indexOf("netscape") >= 0);
var isIE = (browser.indexOf("internet explorer") >= 0); // (agent.indexOf("msie") != -1);
var isOpera = (agent.indexOf('opera') != -1);
var isAOL = (agent.indexOf("aol") != -1);
var isWebTV = (agent.indexOf("webtv") != -1);

// NS
var isNS2 = (isNS && (versMajor == 2));
var isNS3 = (isNS && (versMajor == 3));
var isNS4 = (isNS && (versMajor == 4));
var isNS5 = (isNS && (versMajor == 5));
var isNS6 = (isNS && document.getElementById != null);
var isMinNS6 = isNS6;
var isMinNS4 = (isNS && (versMajor >= 4)) && ! isMinNS6;
var isMinNS5 = (isNS && (versMajor >= 5));

// IE
var isIE3 = (isIE && (versMajor < 4));
var isIE4  = (isIE && (versMajor == 4) && (agent.indexOf("msie 5.")==-1) && (agent.indexOf("msie 6.")==-1));
var isIE5  = (isIE && (versMajor == 4) && (agent.indexOf("msie 6.")==-1) && (agent.indexOf("msie 5.")!=-1));
var isIE55 = (isIE5 && (navigator.appVersion.indexOf("5.5") >= 0));
var isIE6 = (navigator.appVersion.indexOf("6.") >= 0);
var isIE7 = (isIE && navigator.appVersion.indexOf("7.") >= 0); 
var isMinIE4 = (isIE && versMajor >= 4);
var isMinIE6 = isIE6;
var isMinIE5 = (isMinIE4 && (navigator.appVersion.indexOf("5.") >= 0) || isMinIE6);
var isMinIE55 = (isMinIE4 && (navigator.appVersion.indexOf("5.5") >= 0));

var isIENS6 = isIE || isMinNS6 ;

// AOL
var isAOL3  = (isAOL && isIE3);
var isAOL4  = (isAOL && isIE4);

// Determine OS
var isMac = (navigator.appVersion.indexOf("Mac") >= 0);
var isWin = (navigator.appVersion.indexOf("Win") >= 0);
var isLinux = (navigator.appVersion.indexOf("Linux") >= 0);

// Versions
if (isNS2)
	browserVersion = "2";
if (isIE3 || isNS3)
	browserVersion = "3";
if (isIE4 || isNS4)
	browserVersion = "4";
if (isIE5)
	browserVersion = "5";
if (isMinIE55)
	browserVersion = "5.5";
if (isNS6)
	browserVersion = "6";
  
// JavaScript 1.1 min
// test if a variable is undefined or null
function isDefined(sVar) {
  if (typeof sVar == 'undefined') return false;
  else return true;
}

// Determine Flash
// ********************************** FALSH DETECT
var Fl_requiredVersion = 4;			// version the user needs to view site (max is 5, min is 2)
var Fl_flash2Installed = false;		// boolean. true if flash 2 is installed
var Fl_flash3Installed = false;		// boolean. true if flash 3 is installed
var Fl_flash4Installed = false;		// boolean. true if flash 4 is installed
var Fl_flash5Installed = false;		// boolean. true if flash 5 is installed
var Fl_maxVersion = 8;					// highest version we can actually detect
var Fl_actualVersion = 0;				// version the user really has
var Fl_hasRightVersion = false;		// boolean. true if it's safe to embed the flash movie in the page
var Fl_isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;		// true if we're on ie
var Fl_isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false; // true if we're on windows
function Fl_DetectFlashVb() {
  if(Fl_isIE && Fl_isWin){ // don't write vbscript tags on anything but ie win
  	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
  	document.write('on error resume next \n');
    for(i=2;i<=Fl_maxVersion;i++) {
    	document.write('Fl_flash'+i+'Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))) \n');
  	}
    document.write('</SCR' + 'IPT\> \n'); // break up end tag so it doesn't end our script
  }
}
function Fl_DetectFlash() {
  if (navigator.plugins){								// does navigator.plugins exist?
    if (navigator.plugins["Shockwave Flash 2.0"] 	// yes>> then is Flash 2 
    || navigator.plugins["Shockwave Flash"]){		// or flash 3+ installed?
    	var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
    	var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;
    	var flashVersion = parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 1));
    	Fl_flash2Installed = flashVersion == 2;		
    	Fl_flash3Installed = flashVersion == 3;
    	Fl_flash4Installed = flashVersion == 4;
    	Fl_flash5Installed = flashVersion == 5;
    	Fl_flash6Installed = flashVersion == 6;
    	Fl_flash7Installed = flashVersion == 7;
    	Fl_flash8Installed = flashVersion == 8;
    }
  }
  for (var i = 2; i <= Fl_maxVersion; i++) {	
  	if (eval("Fl_flash" + i + "Installed") == true) Fl_actualVersion = i;
  }
  if(navigator.userAgent.indexOf("WebTV") != -1) Fl_actualVersion = 2;	
  if (Fl_actualVersion >= Fl_requiredVersion) { 		// user has a new enough version
  	Fl_hasRightVersion = true;						// flag: it's okay to write out the object/embed tags later
    return true;
  }
  return false;
}  

// --------------------------------
// Plugin Detection
// --------------------------------

/*
Plugin Detector()

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Nadav Savio
Author Email: webmonkey@giantant.com
*/

// this is where we write out the VBScript for MSIE Windows
var WM_startTagFix = '</';
var msie_windows = 0;
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)){
  msie_windows = 1;
  document.writeln('<script language="VBscript">');
  document.writeln('\'This will scan for plugins for all versions of Internet Explorer that have a VBscript engine version 2 or greater.');
  document.writeln('\'This includes all versions of IE4 and beyond and some versions of IE 3.');
  document.writeln('Dim WM_detect_through_vb');
  document.writeln('WM_detect_through_vb = 0');
  document.writeln('If ScriptEngineMajorVersion >= 2 then');
  document.writeln('  WM_detect_through_vb = 1');
  document.writeln('End If');
  document.writeln('Function WM_activeXDetect(activeXname)');
  document.writeln('  on error resume next');
  document.writeln('  If ScriptEngineMajorVersion >= 2 then');
  document.writeln('     WM_activeXDetect = False');
  document.writeln('     WM_activeXDetect = IsObject(CreateObject(activeXname))');
  document.writeln('     If (err) then');
  document.writeln('        WM_activeXDetect = False');
  document.writeln('     End If');
  document.writeln('   Else');
  document.writeln('     WM_activeXDetect = False');
  document.writeln('   End If');
  document.writeln('End Function');
  document.writeln(WM_startTagFix+'script>');
}

function WM_pluginDetect(plugindescription, pluginxtension, pluginmime, activeXname){
  //This script block will test all user agents that have a real plug-in array
  //(i.e. Netscape) and set the variables, otherwise it directs the routine
  // to WM_activeXDetect to detect the activeX control.

  // First define some variables
  var i,plugin_undetectable=0,detected=0, daPlugin=new Object();
  plugin_undetectable = (msie_windows && WM_detect_through_vb)?0:1;

  // If it has a real plugins or mimetypes array, we look there for the plugin first
  if (navigator.plugins) {
    numPlugins = navigator.plugins.length;
    if (numPlugins > 1) {
	    if (navigator.mimeTypes && navigator.mimeTypes[pluginmime] && navigator.mimeTypes[pluginmime].enabledPlugin && (navigator.mimeTypes[pluginmime].suffixes.indexOf(pluginxtension) != -1)) { // seems like we have it, let's just make sure and check the version (if specified)
	      if ((navigator.appName == 'Netscape') && (navigator.appVersion.indexOf('4.0') != -1)) { // stupid, stupid Netscape can't handle the references to navigator.plugins by number, sooo...
    		  for(i in navigator.plugins) {
		        if ((navigator.plugins[i].description.indexOf(plugindescription) != -1) || (i.indexOf(plugindescription) != -1)) { // some versions of quicktime have no description. feh!
    			   detected=1;
		         break;
		        }
		      }
	      } else {
		      for (i = 0; i < numPlugins; i++) {
		        daPlugin = navigator.plugins[i];
		        if ((daPlugin.description.indexOf(plugindescription) != -1) || (daPlugin.name.indexOf(plugindescription) != -1)) {
			        detected=1;
			        break;
		        }
		      }
	      }

  	    // Mac weirdness
        if (navigator.mimeTypes[pluginmime] == null) {
    		  detected = 0;
  	    }
	    }
	    return detected;
      } else if((msie_windows == 1) && !plugin_undetectable){
	      return WM_activeXDetect(activeXname);
      } else { 		
	      return 0;
      }
    } else {
      return 0;
  }
}

// this next function just makes it easy to detect the common plugins
function WM_easyDetect(whichPlugin) {
  // this function just makes it easy to do basic plug-in detection without
  // knowing all the mimetypes and activeX names and such
  var isItThere = 0;
  if( (whichPlugin == 'flash') || (whichPlugin == 'Flash') ) {
    isItThere = WM_pluginDetect('Flash', 'swf', 'application/x-shockwave-flash', 'ShockwaveFlash.ShockwaveFlash');
  } else if( (whichPlugin == 'director') || (whichPlugin == 'Director') ) {
    isItThere = WM_pluginDetect('Shockwave', 'dcr', 'application/x-director', 'SWCtl.SWCtl.1');
  } else if( (whichPlugin == 'quicktime') || (whichPlugin == 'Quicktime') || (whichPlugin == 'QuickTime') ) {
    isItThere = WM_pluginDetect('QuickTime', 'mov', 'video/quicktime', 'QuickTimeCheckObject.QuickTimeCheck.1');
  } else if( (whichPlugin == 'realaudio') || (whichPlugin == 'Realaudio') || (whichPlugin == 'RealAudio') ) {
    isItThere = (WM_pluginDetect('RealPlayer', 'rpm', 'audio/x-pn-realaudio-plugin', 'RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)')) || (WM_pluginDetect('RealPlayer', 'rpm', 'audio/x-pn-realaudio-plugin','rmocx.RealPlayer G2 Control')) || (WM_pluginDetect('RealPlayer', 'rpm', 'audio/x-pn-realaudio-plugin','RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)')) || (WM_pluginDetect('RealPlayer', 'rpm', 'audio/x-pn-realaudio-plugin','RealVideo.RealVideo(tm) ActiveX Control (32-bit)'))
  } else {
    alert('You need to tell me which plug-in to look for, like so:\n\n' + '          WM_easyDetect(\'flash\')\n\n' + '          WM_easyDetect(\'director\')\n\n' + '          WM_easyDetect(\'quicktime\')\n\n' + '          WM_easyDetect(\'realaudio\')');
  }
  return isItThere;
}