// For Internet Explorer, we insert an Object which allows us to check if the Stream Theory Player is installed ...
if ((navigator.appName.indexOf("Microsoft") != -1) && (parseInt(navigator.appVersion) >= 4)) {
	document.writeln('<object ID="JukeboxPlayer" CLASSID="CLSID:04901553-169B-47E1-B999-DFE309283239" style="visibility:hidden"></object>');
}

// WARNING: IT IS RECOMMENDED THAT YOU SETUP A SECURE CONNECTION FOR IIS AS THE AUTHENTICATION
// METHOD SHOWN IN THIS SAMPLE IS BASIC AUTHENTICATION

function launchIt( user, licenseGuid )
{
	//TODO: Make link secure by changing to "https:..." if you setup SSL through IIS

	if( clientCheck() )
		window.location = "http://88.208.208.183/live/authandtoken.php?userName=" + user + "&licenseGuid=" + licenseGuid;

}

function clientCheck()
{
	//TODO: Change clientUpgradeUrl to point to appropriate page where users can download the Application Jukebox client.
	//TODO: Change minClientVerByServer to the minimum client version necessary to stream
	
	var minClientVerByServer = '8.1.2.0';
	var clientUpgradeUrl = 'http://www.endeavors.com/downloads/ajplayer812.msi';
	return CheckClientInstalled(minClientVerByServer, clientUpgradeUrl);
}

function CheckClientInstalled(minClientVerByServer, clientUpgradeUrl)
{
	var isInstalled = false;
	var clientVersion = "";
	
	if (navigator.appName.indexOf("Microsoft") != -1)
	{
		isInstalled = (JukeboxPlayer.IsInstalled == 1);
		clientVersion = JukeboxPlayer.ClientVersion;
	}
	else if (navigator.appName.indexOf("Netscape") != -1)
	{
		isInstalled = (navigator.mimeTypes["application/vnd.endeavors.token"] != null);
	}

	// allow force accept for debugging purposes
	if (!isInstalled) 
	{
		var doInstall = confirm("You must install the Application Jukebox Player before you can stream applications. Click OK to install the client now. \n\n" +
						    		"If you believe you have recieved this message in error and are sure the player is running, click 'Cancel' to stream anyway.");
		
		if (doInstall)
		{
			window.location = clientUpgradeUrl;
			return false;
		}
	} 
	else
	{
		if (clientVersion == "") 
			return true;
		
		var doUpgrade = isUpgradeRequired(clientVersion, minClientVerByServer);
		if (doUpgrade)
		{
			var doInstall = confirm("You need a newer version of the client in order to stream this application. \n" +
									"To upgrade, please uninstall your current version before clicking 'OK' to continue.");
		
			if (doInstall)
			{
				window.location = clientUpgradeUrl;
				return false;
			}
		}
	}

	return true;
}

function isUpgradeRequired(clientVersion, minClientVerByServer)
{
	var clientVersionArray = clientVersion.split(".");
	var minClientVerByServerArray = minClientVerByServer.split(".");
	
	for (i = 0; i < clientVersionArray.length; i++)
	{
		if (clientVersionArray[i] < minClientVerByServerArray[i])
			return true;
		else if (clientVersionArray[i] > minClientVerByServerArray[i])
			return false;	
	}
	return false;

}