// setting up global variables
var thestring;
var detect;
var place;

// http://www.quirksmode.org/js/detect.html
function detectBrowser() {
	// test to catch early errors
	if(!(navigator && navigator.userAgent && navigator.userAgent.toLowerCase)) {
		return false;
	} else {
		detect = navigator.userAgent.toLowerCase();
		var OS,browser,version,total;
		
		// assuming we are using a valid browser unless we find otherwise
		var valid = true;
	
		if (checkIt('konqueror')) {
			browser = "Konqueror";
			OS = "Linux";
		} else if (checkIt('safari')) {
			browser = "Safari";
		} else if (checkIt('omniweb')) {
			browser = "OmniWeb";
			// supporting omniweb only after it started using the safari rendering engine
			version = detect.charAt(place + thestring.length);		
			if(parseInt(version, 10) < 5) {
				valid = false;
			}
		} else if (checkIt('opera')) {
			browser = "Opera";
			version = detect.charAt(place + thestring.length);		
			if(parseInt(version, 10) < 7) {
				valid = false;
			}
		} else if (checkIt('webtv')) {
			browser = "WebTV";
			valid = false;
		} else if (checkIt('icab')) {
			browser = "iCab";
			valid = false;
		} else if (checkIt('msie')) {
			browser = "Internet Explorer";
			valid = checkVersion();
		} else if (!checkIt('compatible')){
			browser = "Netscape Navigator";
			version = detect.charAt(8);
			// Currently all Mozilla derivatives are detected as Netscape 5 [http://www.quirksmode.org/js/detect.html]
			if(parseInt(version, 10) < 5) {
				valid = false;
			}
		}
		
		else browser = "An unknown browser";
		
		if (!version) {
			version = detect.charAt(place + thestring.length);
		}
		
		if (!OS) {
			if (checkIt('linux')) OS = "Linux";
			else if (checkIt('x11')) OS = "Unix";
			else if (checkIt('mac')) OS = "Mac";
			else if (checkIt('win')) OS = "Windows";
			else OS = "an unknown operating system";
		}
		return valid;
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}


// http://msdn.microsoft.com/workshop/author/dhtml/overview/browserdetection.asp
function getInternetExplorerVersion() {
	// Returns the version of Internet Explorer or a -1
	// (indicating the use of another browser).
	var rv = -1; // Return value assumes failure
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null) {
			rv = parseFloat( RegExp.$1 );
		}
	}
	return rv;
}

function checkVersion() {
	var ver = getInternetExplorerVersion();
	var valid = true;
	// if the version can be found and the version is less than 6.0 it is invalid
	if ((ver > -1) && (ver < 6.0)) {
		valid = false;
	}
	return valid;
}

function displayWarning() {
	// display error message if result of test finds an invalid browser
	var warningMsg = document.getElementById('badBrowser');
	if(detectBrowser() == false) {
		warningMsg.style.display = 'block';
	}
}

addEvent(window, 'load', displayWarning);