// Browser detection. Taken from www.apple.com/developer
// Use: var its = new its()
//      if (its.nn) { do something

function its() {
	var n = navigator;
	// string comparisons are much easier if we lowercase everything now.
	// to make indexOf() tests more compact/readable, we prepend a space
	// to the userAgent string (to get around '-1' indexOf() comparison)
	var ua = ' ' + n.userAgent.toLowerCase();
	var pl = n.platform.toLowerCase(); // not supported in NS3.0
	var an = n.appName.toLowerCase();

	// browser version
	this.version = n.appVersion;

    this.nn = ua.indexOf('mozilla') > 0;

	// 'compatible' versions of mozilla aren't navigator
	if(ua.indexOf('compatible') > 0) {
		this.nn = false;
	}

	this.opera = ua.indexOf('opera') > 0;
	this.webtv = ua.indexOf('webtv') > 0;
	this.ie = ua.indexOf('msie') > 0;
	this.aol = ua.indexOf('aol') > 0;

	this.major = parseInt( this.version );
	this.minor = parseFloat( this.version );

	// platform
	this.mac = ua.indexOf('mac') > 0;
	this.mac68k = (ua.indexOf('68k') > 0 || ua.indexOf('68000') > 0);
	this.macppc = (ua.indexOf('ppc') > 0 || ua.indexOf('powerpc') > 0);

	this.win = ua.indexOf('win') > 0;
	this.win16 = (ua.indexOf('16') > 0 && ua.indexOf('win') > 0);
	this.win31 = this.win16;
	this.win95 = (ua.indexOf('95') > 0 && ua.indexOf('win') > 0);
	this.win98 = (ua.indexOf('98') > 0 && ua.indexOf('win') > 0);
	this.win00 = (ua.indexOf('00') > 0 && ua.indexOf('win') > 0);
	this.winnt = (ua.indexOf('nt') > 0 && ua.indexOf('win') > 0);
	this.winme = (ua.indexOf('me') > 0 && ua.indexOf('win') > 0);

	this.os2 = ua.indexOf('os/2') > 0;

	this.sun = ua.indexOf('sunos') > 0;
	this.irix = ua.indexOf('irix') > 0;
	this.hpux = ua.indexOf('hpux') > 0;
	this.aix = ua.indexOf('aix') > 0;
	this.dec = (ua.indexOf('dec') > 0 || ua.indexOf('alpha') > 0 || ua.indexOf('osf1') > 0 || ua.indexOf('ultrix') > 0);
	this.sco = (ua.indexOf('sco') > 0 || ua.indexOf('unix_sv') > 0);
	this.vms = (ua.indexOf('vax') > 0 || ua.indexOf('openvms') > 0);
	this.linux = ua.indexOf('linux') > 0;
	this.sinix = ua.indexOf('sinix') > 0;
	this.reliant = ua.indexOf('reliantunix') > 0;
	this.freebsd = ua.indexOf('freebsd') > 0;
	this.openbsd = ua.indexOf('openbsd') > 0;
	this.netbsd = ua.indexOf('netbsd') > 0;
	this.bsd = ua.indexOf('bsd') > 0;
	this.unixware = ua.indexOf('unix_system_v') > 0;
	this.mpras = ua.indexOf('ncr') > 0;

	this.unix = ua.indexOf("x11") > 0;

	// workarounds
	// - IE5/Mac reports itself as version 4.0
   // This appears to be the case for PC as well and IE6.
	//if(this.ie && this.mac) {
	if (this.ie) {
		if ( (ua.indexOf("msie 5")) || (ua.indexOf("msie 6")) ) {
			this.major = 5;
			//var actual_index = ua.indexOf("msie 5");
			var first_comma = ua.indexOf(";");
			var second_comma = ua.lastIndexOf(";");
			//var actual_major = ua.substring(actual_index + 5, actual_index + 8);
			var actual_major = ua.substring(first_comma + 6, second_comma);
			this.minor = parseFloat(actual_major);
		}
	   // There appears to be a problem with PC/IE 4.01
		//if (isNaN(this.minor)) {
		else if(ua.indexOf("msie 4.01")) {
      	this.minor = "4.01";
      }
      //}
   }

	return this;
}

// Detect the browser type.
function detectBrowser() {
   var thing = new its();
   var browser = "incorrect";

   // If MAC
   // Valid browsers NN 4.7 to less than 6.2 and IE 5.1 to 5.14
	// MAC operating systems cannot be detected.
	if (thing.mac) {
  		if ((thing.nn && thing.minor >= 4.7 && thing.minor <= 6.2) || (thing.ie && thing.minor >= 5 || thing.minor <= 5.14)) {
      	browser = "correct";
    	}
  	}
   // If PC and either win95, win98 or winnt.
   // Valid browsers NN 4.7 to less than 6.2 and IE 4.01 to 6.0.
   else if (thing.win95 || thing.win98 || thing.winnt) {
		if ((thing.nn && thing.minor >= 4.7 && thing.minor <= 6.2) || (thing.ie && thing.minor >= 4.01 && thing.minor <= 6)) {
      	browser = "correct";
      }
   }
   // If PC and either winme or win00.
   // Valid browsers NN 4.7 to 6.2 and IE 5.0 to 6.0.
   else if (thing.winme || thing.win00) {
		if ((thing.nn && thing.minor >= 4.7 && thing.minor <= 6.2) || (thing.ie && thing.minor >= 4.01 && thing.minor <= 6)) {
      	browser = "correct";
      }
   }
   // If PC and winxp.
   // Valid browsers NN 4.7 to 6.2 and IE 6.0.
   else if (thing.winme || thing.win00) {
		if ((thing.nn && thing.minor >= 4.7 && thing.minor <= 6.2) || (thing.ie && thing.minor == 6)) {
      	browser = "correct";
      }
   }

   return browser;
}

