/*
Function Requires JavaScript 1.2+ (any modern browser IE 5+ or Mozilla 1.0+ should do that.
(c) MMVII (2007) Bill Frederickson.
Used by Permission.
Released under GPL 1.0 & Later Versions.

* Firefox, Netscape 7+, Mozilla, and Opera 7+ all support version 1.6
* Netscape 6 supports version 1.5
* Opera 6 supports version 1.4
* Netscape 4.5-4.8, Opera 5, and Internet Explorer 6 support version 1.3
* Netscape 4-4.5 supports version 1.2

 Mozilla Definition
   Anything Based on gecko Runtime, eg:
     Mozilla Suite 1.0 - 1.7.13, later Seamonkey (Suite) 1.0 - 1.1
	  Firefox 1.0+ (previously Firebird & Phoenix)
	  Netscape 6,7,8,9 (8 & 9 are based more off Firefox)
	  K-meleon
	  Chimera (Mac)
	  Camino (Mac)
	  Safari (Firefox/Konqueror Clone) - Mac
	  Epiphany
	  Galeon
	  Konqueror -- Note, Konqueror also has what they call KHTML.
   - you get the idea there's alot!
	  
This Sniffer uses Regular Expressions.
 - now with better methods.
 - contains killer wabbits. 
 
 myBrowser defined as object
 samples:
  myBrowser.isIE()        - detect ie
  myBrowser.isIE(">=4")   - detect ie >=4
  myBrowser.isIE(">=6")   - detect ie >=6
  myBrowser.isIE(">=7")   - detect ie >=7
  myBrowser.isIE("<7")    - detect ie <7
  myBrowser.isIE("=7")    - detect ie =7
  myBrowser.isIE("7")     - detect ie =7
  myBrowser.isIE(7)       - detect ie =7
  myBrowser.isMoz()       - detect Mozilla 5 compatible browser
  myBrowser.isFF()        - detect ANY Firefox; also myBrowser.isFireFox()
  myBrowser.isFF("<=2")   - detect Firefox <= 2
  myBrowser.isFF(">=2")   - detect Firefox >= 2
  myBrowser.isFF("==2")   - detect Firefox == 2
  myBrowser.isFF("2")     - detect Firefox == 2
  myBrowser.isFF(2)       - detect Firefox == 2
  
  myBrowser.isDOM()       - detect DOM compatible browser
  ...
*/

//==
function BrowserInfo(){
 this.UA = navigator.userAgent.toLowerCase();
 //Expressions
 this.IEXpr = /msie/i;
 this.MOZXpr = /(rv:)|(seamonkey)/i;
 this.OPERAXpr = /opera/i;
 this.FFXpr = /(firefox)|(firebird)|(phoenix)|(GranParadiso)/i;
 this.NSXpr = /netscape/i;
 this.NS4Xpr = /mozilla\/4\./i;
 this.NS3Xpr = /mozilla\/3\./i;
 this.MOZ5 = /mozilla\/5\./i;
 this.EpXpr = /epiphany/i;
 this.galXpr = /galeon/i;
 this.KonqXpr = /konqueror/i;
 this.SafXpr = /safari/i;
 this.seaXpr = /seamonkey/i;
 this.aolXpr = /(aol)|(america online)|(america)/i;
 this.camchiXpr = /(camino)|(chimera)/i;
 this.kmXpr = /(kmeleon)|(k-meleon)/i;  
 this.wXpr = /win/i;
 this.mXpr = /mac/i;
 this.uXpr = /(unix)|(bsd)/i;
 this.lXpr = /linux/i;
 this.gko = /gecko/i;
 //
 var Xpr = "";
 var tmp = "";  
  // ==================================
  // Default Values
  this.BrowserName = "Unknown -- "+this.UA;
  this.BrowserVersion = "";
  this.FullBrowserName = "";
  this.useros = "";
  //
  if ( this.wXpr.test(this.UA) ) this.useros = "Windows";
  if ( this.mXpr.test(this.UA) ) this.useros = "Macintosh";
  if ( this.uXpr.test(this.UA) ) this.useros = "UNIX";
  if ( this.lXpr.test(this.UA) ) this.useros = "Linux";    
  if ( this.UA.indexOf("sun") >=0 ) this.useros = "Sun"; 
  
  //IE
  if (this.IEXpr.test(this.UA) && !this.OPERAXpr.test(this.UA) ) {
   Xpr = /(.*)(msie\s)(.*)(;)(.*)/i;
   Xpr2 = /;.*/ig;
   this.BrowserName = "Microsoft Internet Explorer";
   this.BrowserVersion = this.UA.replace(Xpr, "$3");
   // Adjust for data after ; -- Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)
   this.BrowserVersion = this.BrowserVersion.replace(Xpr2,""); 
  }
  //Opera 
  if ( this.OPERAXpr.test(this.UA) ) {
   Xpr = /(.*)(Opera[\s|\/])([0-9]+\.[0-9]+)(.*)/i;
   this.BrowserName = "Opera";
   this.BrowserVersion = this.UA.replace(Xpr,"$3");
  }     
  //Mozilla & Seamonkey suites
  if (this.MOZXpr.test(this.UA) && !this.OPERAXpr.test(this.UA)){
   this.BrowserName = (this.UA.indexOf("seamonkey") >= 0) ? "Seamonkey" : "Mozilla";   
   Xpr = (this.BrowserName == "Seamonkey" ) ? /(.*)(seamonkey\/)(.*)/i : /(.*)(rv:)([0-9\.]+)(.*)/i;  
   this.BrowserVersion = this.UA.replace(Xpr, "$3");
  }    
  //FireFox, Firebird, Phoenix
  if (this.FFXpr.test(this.UA) && !this.OPERAXpr.test(this.UA) ) {
   this.BrowserName = "Phoenix"; //  
   if (this.UA.indexOf("firebird") >= 0) this.BrowserName = "Firebird";
   if (this.UA.indexOf("firefox") >= 0) this.BrowserName = "Firefox";
   if (this.UA.indexOf("granparadiso") >= 0) this.BrowserName = "GranParadiso"; //ff3  
   if (this.BrowserName == "Phoenix" ) Xpr = /(.*)(Phoenix\/)(.*)/i;
   if (this.BrowserName == "Firebird" ) Xpr = /(.*)(Firebird\/)(.*)/i;
   if (this.BrowserName == "Firefox" ) Xpr = /(.*)(Firefox\/)(.*)/i;  
   if (this.BrowserName == "GranParadiso" ) Xpr = /(.*)(GranParadiso\/)(.*)/i;  
   this.BrowserVersion = this.UA.replace(Xpr, "$3");  
  }    
  //NS 6+
  if ( this.NSXpr.test(this.UA) ) {
   Xpr = /(.*)(.*\/)([0-9\.]+)/;
   this.BrowserName = "Netscape";
   this.BrowserVersion = this.UA.replace(Xpr,"$3");
  }   
  //NS 4
  if (document.layers || (this.NS4Xpr.test(this.UA) && !this.IEXpr.test(this.UA)) ){
   Xpr = /(mozilla\/)([0-9\.]+)(.*)/;
   this.BrowserName = "Netscape Communicator"; // Communicator
   this.BrowserVersion = this.UA.replace(Xpr,"$2");
  } 
  //ns3  
  if ((this.NS3Xpr.test(this.UA) && !this.isIE()) ){
   Xpr = /(mozilla\/)([0-9\.]+)(.*)/;
   this.BrowserName = "Netscape Gold"; // Communicator
   this.BrowserVersion = this.UA.replace(Xpr,"$2");
  }
  //Konqueror
  if ( this.KonqXpr.test(this.UA) ) {
   Xpr = /(.*)(konqueror[\s|\/]([0-9\.]+))(.*)/i;
   this.BrowserName = "Konqueror";
   this.BrowserVersion = this.UA.replace(Xpr,"$3");  
   }   
  //Safari
  if ( this.SafXpr.test(this.UA) ) {
   s412 = /412/;
   s419 = /419/;
   s521 = /(521\.24)|(52)/;
   vers = /version/i;
   this.BrowserName = "Safari";
   if (s412.test(this.UA)) this.BrowserVersion = "2.0";
   if (s419.test(this.UA)) this.BrowserVersion = "2.0.4";
   if (s521.test(this.UA)) this.BrowserVersion = "3.0";
   if (vers.test(this.UA)) this.BrowserVersion = this.UA.replace(/(.*)(version\/)([\d\.{3}]+)(.*)/i,"$3");
  }   
  //Chimera/Chimera
  if ( this.camchiXpr.test(this.UA) ) {
   Xpr = /camino/i;
   this.BrowserName = (Xpr.test(this.UA)) ? "Camino" : "Chimera";
   if (this.BrowserName == "Camino") Xpr = /(.*)(camino[\s\/])([0-9\.]+)/i;
   if (this.BrowserName == "Chimera") Xpr = /(.*)(chimera[\s\/])([0-9\.]+)/i;
   this.BrowserVersion = this.UA.replace(Xpr,"$3");   
   }   
  //AOL
  if ( this.aolXpr.test(this.UA) ) {
   Xpr = /(.*)(aol )([\d\.\d]+)(.*)/i;
   this.BrowserName = "America On Line (AOL)";
   this.BrowserVersion = this.UA.replace(Xpr,"$3");
  }
  //Galeon
  if ( this.galXpr.test(this.UA) ) {
   Xpr = /(.*)(galeon[\s|\/])([0-9\.]+)/i;
   this.BrowserName = "Galeon";
   this.BrowserVersion = this.UA.replace(Xpr,"$3");
   this.useros = "Linux"; 
   }  
  //Epiphany
  if ( this.EpXpr.test(this.UA) ) {
   Xpr = /(.*)(epiphany[\s|\/])([0-9\.]+)/i;
   this.BrowserName = "Epiphany";
   this.BrowserVersion = this.UA.replace(Xpr,"$3");
   }
  //kMeleon
  if ( this.kmXpr.test(this.UA) ) {
   Xpr = /(.*)(K-Meleon\/)(.*)/i;
   this.BrowserName = "K-Meleon";
   this.BrowserVersion = this.UA.replace(Xpr,"$3");   
  }
   
   if (this.UA.indexOf("flock") >=0){ // yep, you can use indexOf too.
    Xpr = /(.*)(flock\/)(.*)/i;
    this.BrowserName = "Flock";
    this.BrowserVersion = this.UA.replace(Xpr,"$3");
	//adjust for ff in Flock.
	if (this.UA.indexOf("firefox")) {
 	 this.BrowserVersion = this.BrowserVersion.replace(/(.*)(firefox.*)/i,"$1");
	}
   }
   
   //this.BrowserVersion = parseFloat(this.BrowserVersion);
   
   //alert("this.BrowserVersion: "+this.BrowserVersion);
   
   this.FullBrowserName = this.BrowserName+" "+this.BrowserVersion;
   // Add your own :)
} //end BrowserInfo

//======= other Methods
BrowserInfo.prototype.isIE = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.IEXpr.test(this.UA) && !this.OPERAXpr.test(this.UA) && this.isVer(iv) ) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isMoz = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.MOZ5.test(this.UA) && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isMozilla = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.MOZXpr.test(this.UA) && !this.isFireFox() && !this.isNetscape() && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isFireFox = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.FFXpr.test(this.UA) && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}
BrowserInfo.prototype.isFF = function(iVal){
 return this.isFireFox(iVal);
}

BrowserInfo.prototype.isOpera = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.OPERAXpr.test(this.UA) && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isGecko = function(){ // true for epiphany/galeon
 return (this.gko.test(this.UA) || this.isGaleon() ) ? 1 : 0;
}

BrowserInfo.prototype.isDOM = function(){
 return (this.isGecko() || this.isIE(">=6") || this.isOpera(">=8") ) ? 1 : 0; 	  
}

BrowserInfo.prototype.isKonqueror = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.KonqXpr.test(this.UA) && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isSeamonkey = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.seaXpr.test(this.UA) && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isSafari = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.SafXpr.test(this.UA) && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isAol = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.aolXpr.test(this.UA) && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isEpiphany = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.EpXpr.test(this.UA) && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isGaleon = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.galXpr.test(this.UA) && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isNetscape = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.NSXpr.test(this.UA) && !this.isFireFox() && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isNS4 = function(){
 return (document.layers || (this.NS4Xpr.test(this.UA) && !this.isIE() && !this.isOpera() ) ) ? parseFloat(this.BrowserVersion) : 0;
}
BrowserInfo.prototype.isNs4 = function(){
 return this.isNS4();
}

BrowserInfo.prototype.isCamino = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.camchiXpr.test(this.UA) && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isKmeleon = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.kmXpr.test(this.UA) && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

BrowserInfo.prototype.isFlock = function(iVal){
 var iv = (iVal) ? iVal : 0;
 return (this.UA.indexOf("flock") >=0  && this.isVer(iv)) ? parseFloat(this.BrowserVersion) : 0;
}

// Add your own :)

BrowserInfo.prototype.isVer = function(iVal){
 var valid = 0;
 var IV = (iVal) ? iVal : ">=0"; 
 if (IV){
  IV = IV.replace(/\s/,"");
  var funct = IV.replace(/[^<>=!]/ig,"").toLowerCase();
  var v = parseFloat(IV.replace(/[^0-9\.]/ig,""));
  var BV = parseFloat(this.BrowserVersion);
  // 
  switch(funct){
   case "<": 
	valid = (BV < v) ? 1 : 0;
   break;
   case ">": 
	valid = (BV > v) ? 1 : 0;
   break;
   case "<=": 
	valid = (BV <= v) ? 1 : 0;
   break;
   case ">=": 
	valid = (BV >= v) ? 1 : 0;
   break;
   case "!=": 
   case "<>": 
   case "><": 
	valid = (BV != v) ? 1 : 0;
   break;
   default:
	valid = (BV == v) ? 1 : 0;
   break;    
  }// end case/switch
 } // end if
 return valid;
} // end function

//Pre define an object to use;	  
myBrowser = new BrowserInfo();