var clientProfileVersion	= 12;
var requiredVersion			= 8;
var kcUID;

var d = new Date();
d.setTime(d.getTime()+(24*60*60*1000)); // remove 180 days. Now just 24 hours
var CookieExpiryDate = d.toGMTString();

function ReadCookie (name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return "";
}

function WriteCookie (name,value,date)
{
	document.cookie = name+"="+value+"; expires="+date+"; path=/";
}

try
{
	if(kcUID!=null) WriteCookie(kcCookie,kcUID,CookieExpiryDate);
}
catch(e){}

function SetCPFlags (ajax,flash)
{
	WriteCookie("t","1",CookieExpiryDate);
	var ck = ReadCookie("t")=="" ? "0" : "1";
	var fullFlag		= "cpflgs=1111";
	var params			= [];
	var newCPFlags		= "cpflgs="+ck+"1"+ajax+flash;
	var newLoc;

	if(window.location.search.length==0)
	{
		params.push(fullFlag);
	} else {
		params = window.location.toString().split("?")[1].split("&");
		if(window.location.search.indexOf("cpflgs")==-1 && newCPFlags!=fullFlag ) params.push(fullFlag);
	}
	
	var redir = false;
	for(var i=0;i<params.length;i++)
	{
		if(params[i].indexOf("cpflgs")!=-1)
		{
			// rebuild the url with the new cpflgs value
			if(newCPFlags!=params[i]&&newCPFlags!=fullFlag)
			{
				params[i]	= newCPFlags;
				newLoc		= document.location.toString().split("?")[0]+"?"+params.join("&");
				document.location = newLoc;
				break;
			}
			// remove cpflgs from the querystring
			else if (window.location.search.length>0&&newCPFlags==fullFlag)
			{
				var u = document.location.toString().split("?")[0]+"?"+params.join("&");
				params.splice(i,1);
				newLoc = params.length>0 ? document.location.toString().split("?")[0]+"?"+params.join("&") : document.location.href.split("?")[0];
				document.location = newLoc;
				break;
			}
		}
	}
}

var hasFlash		= 0;
var hasAJAX			= 0;
var continueTest	= true;

if(!CPLoopBack)
{
	var f		= ReadCookie("FlashVersion");
	var cpv		= ReadCookie("CPV");
	if(cpv!="")	cpv = parseInt(cpv);
	
	if(f!=""&&cpv>=clientProfileVersion)
	{
		hasAJAX	= ReadCookie("AJAXEnabled");
		hasFlash = parseInt(f)>=requiredVersion ? "1" : "0";
		SetCPFlags(hasAJAX,hasFlash);
		continueTest = false;
	}
}

var testVer = 0;

if(continueTest)
{
	var maxVersion = 10;
	var actualVersion = 0;
	var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	var isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false;
	var ua	= navigator.userAgent.toLowerCase();
	
	// write vbscript detection if we're not on mac.
	if(isIE && isWin){
		document.write('<SCR'+'IPT LANGUAGE=VBScript\> \n');
		document.write('on error resume next \n');
		for(var i=2;i<=maxVersion;i++)
		{
			document.write('If (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))) Then actualVersion='+i+' End If \n');
		}
		document.write('</SCR'+'IPT\> \n');
	}

	if (navigator.plugins){
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;
			try
			{
				actualVersion = parseInt(flashDescription.split(" ")[2].split(".")[0]);
			}
			catch(e){};
		}
	}

	if((ua.indexOf("opera")!=-1&&!isWin)) actualVersion = 0;
	if(ua.indexOf("WebTV") != -1) actualVersion = 2;
	
	hasAJAX = 0;
	if(isIE&&isWin)
	{
		try
		{
			m_XHR = new ActiveXObject("Msxml2.XMLHTTP.4.0");
			hasAJAX = 1;
		}
		catch(e)
		{
			try
			{
				m_XHR	= new ActiveXObject("Microsoft.XMLHTTP");
				hasAJAX = 1;
			}
			catch(e)
			{
				hasAJAX = 0;
			}
		}
	}
	else
	{
		try
		{
			m_XHR	= new XMLHttpRequest();
			hasAJAX = 1;
		}
		catch(e)
		{
			hasAJAX = 0;
		}
	}
	hasFlash = actualVersion>=requiredVersion ? "1" : "0";
	WriteCookie("FlashVersion",actualVersion,CookieExpiryDate);
	WriteCookie("AJAXEnabled",hasAJAX,CookieExpiryDate);
	
	// only write cpv if we have flash. keep testing otherwise
	if(actualVersion>=requiredVersion) WriteCookie("CPV",clientProfileVersion,CookieExpiryDate);
	
	SetCPFlags(String(hasAJAX),String(hasFlash));
}

var bFlashEnabled = hasFlash=="1";