// Returns the current end-user's id, or creates one if it doesn't exist //
function getEndUserID(appkey) {
	var endUserID = getCookie(appkey);
	if (endUserID == null) {
		endUserID = generateUniqueID();
		// Create a date far into the future
		var today = new Date();
		today.setTime(today.getTime()+(86400000*18000));
		setCookie(appkey, endUserID, today);
	}
	return endUserID;
}

// Generates a new unique end-user id on the fly //
function generateUniqueID() {
	var strId = "";
	for (i = 0; i < 5; i++) {
		strId += String.fromCharCode(getRandomValue(90, 65));
	}
	var today = new Date();
	strId += today.getTime()
	return strId;
}

// Generates a random number between 'min' and 'max' //
function getRandomValue(max, min) {
	var span = max-min;
	var d = span*Math.random();
	return d+min;
}

// Sets cookie values. Expiration date is optional // 
function setCookie(name, value, expire) {
	document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}

//// Returns the value of the cookie with the given name //
//function getCookie(name) {
//	var search = name + "=";
//	if (document.cookie.length > 0) { 
//		// if there are any cookies
//		offset = document.cookie.indexOf(search);
//		if (offset != -1) {
//			// if cookie exists
//			offset += search.length;
//			// set index of beginning of value
//			end = document.cookie.indexOf(";", offset);
//			// set index of end of cookie value
//			if (end == -1)
//				end = document.cookie.length;
//			return unescape(document.cookie.substring(offset, end));
//		}
//	}
//	// If we reach here, then the cookie was not found
//	return null;
//}


// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );
function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function writeAppletEndTag()
{
     var appletEndTag = "</applet>";
     alert('ok');
     if(BrowserDetect.browser == "Safari")
     {
        appletEndTag = "</object>";
        alert(BrowserDetect.browser);
     }
     document.writeln(appletEndTag);
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
		
