function jsHasCookie(name) {
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0,name.length))) return false;
	if (start == -1) return false;
	return true;
}

function jsGetCookie(name) {
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
	if (start == -1) return null;
	var end = document.cookie.indexOf(";",len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len,end));
}

function jsFlashDetect() {
	var flashVersion = 0;
	var latestFlashVersion = 8;
	var agent = navigator.userAgent.toLowerCase(); 
	// NS3 needs flashVersion to be a local variable
	if (agent.indexOf("mozilla/3") != -1 && agent.indexOf("msie") == -1) {
		flashVersion = 0;
	}
	// NS3+, Opera3+, IE5+ Mac (support plugin array):  check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		var flashPlugin = navigator.plugins['Shockwave Flash'];
		if (typeof flashPlugin == 'object') { 
			for (var i = latestFlashVersion; i >= 3; i--) {
				if (flashPlugin.description.indexOf(i + '.') != -1) {
					flashVersion = i;
					break;
				}
			}
		}
	}
	// IE4+ Win32:  attempt to create an ActiveX object using VBScript
	else if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4 && agent.indexOf("win")!=-1 && agent.indexOf("16bit")==-1) {
		var doc = '<script language="VBScript"\> \n';
		doc += 'On Error Resume Next \n';
		doc += 'Dim obFlash \n';
		doc += 'For i = ' + latestFlashVersion + ' To 3 Step -1 \n';
		doc += '   Set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash." & i) \n';
		doc += '   If IsObject(obFlash) Then \n';
		doc += '      flashVersion = i \n';
		doc += '      Exit For \n';
		doc += '   End If \n';
		doc += 'Next \n';
		doc += '</script\> \n';
		document.write(doc);
	}
	// WebTV 2.5 supports flash 3
	else if (agent.indexOf("webtv/2.5") != -1) flashVersion = 3;
	// older WebTV supports flash 2
	else if (agent.indexOf("webtv") != -1) flashVersion = 2;
	// Can't detect in all other cases
	else {
		flashVersion = -1;
	}
	return flashVersion;
}

function jsBrowserContext(url, path) {
	var flashVersion;
	if (!path) path = '/';
	if (jsHasCookie('BrowserFlash')) flashVersion = jsGetCookie('BrowserFlash');
	else {
		flashVersion = jsFlashDetect();
		document.cookie = "BrowserFlash="+flashVersion+";path="+path+";";
		if (!jsHasCookie('BrowserFlash')) return;
	}
	if (flashVersion != -1 && !jsHasCookie('BrowserContext')) {
		var expires = new Date;
		expires.setFullYear(expires.getFullYear()+1);
		if (!flashVersion) document.cookie = "BrowserContext=html;expires="+expires.toGMTString()+";path="+path+";";
		else {
			document.cookie = "BrowserContext=flash;expires="+expires.toGMTString()+";path="+path+";";
			location.href = url;
		}
	}
}

function jsRobotDetect(url, path) {
	if (!path) path = '/';
	if (!jsHasCookie('RobotDetect')) {
		var expires = new Date;
		expires.setFullYear(expires.getFullYear()+1);
		document.cookie = "RobotDetect=1;expires="+expires.toGMTString()+";path="+path+";";
		location.href = url;
	}
}