// USER SETTINGS FOR THIS CLIENT CHECK 
var clientDownloadUrl = 'http://www.endeavors.com/common/downloads/ajplayer.msi';
var clientUpgradeUrl = 'http://www.endeavors.com/common/downloads/ajplayer.msi';
var minClientVerByServer = '8.3.0.0';
var use_confirm_dialogs = true;

// Dump the BHO into IE for client check
if ((navigator.appName.indexOf("Microsoft") != -1) && (parseInt(navigator.appVersion) >= 4)) {
	document.writeln('<object ID="JukeboxPlayer" CLASSID="CLSID:DE78969D-740F-4d78-89FE-624D1D75A3D8" style="visibility:hidden"></object>');
}
// Define some custom functions for editing 
// the default settings on a site-by-site basis
function setDownloadURL(newurl) {
  clientDownloadUrl = newurl;
  clientUpgradeUrl = newurl;
  use_confirm_dialogs = false;
}
function setUpgradeURL(newurl) {
  clientUpgradeUrl = newurl;
}
function enableConfirmations() {
  use_confirm_dialogs = true;
}
function disableConfirmations() {
  use_confirm_dialogs = false;
}

function clientCheck() {
	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);
	}

	// Redirect users if they do not have the client installed
	if (!isInstalled) {
    if (use_confirm_dialogs) {
      var doInstall = confirm("The Application Jukebox Player has not been detected on your system.\n" + 
                      "You must have the Application Jukebox Player installed before you can stream applications.\n" + 
                      "If you already have the Application Jukebox Player installed, click 'Cancel' to ignore this message.\n" + 
                      "Otherwise, click 'OK' to begin downloading the Application Jukebox Player now.\n\n" +
      				    		"If you experience any difficulties, please contact the site administrator.");
      if (doInstall) {
        window.location = clientDownloadUrl;
			return false;
			var params = 'resizable=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no,width=300,height=300';
        window.open(clientDownloadUrl,'Installer',params);
        return false;
      }
		} else { // No confirm, just redirect
      window.location = clientDownloadUrl;
			return false;
		}
	} else { // Player installed, check for upgrade
		if (clientVersion == "") // Non-IE, cant check
			return true;
		
		var doUpgrade = isUpgradeRequired(clientVersion, minClientVerByServer);
		if (doUpgrade) {
			if (use_confirm_dialogs) {
        var doInstall = confirm("The version of the Application Jukebox Player that is currently running on your system\n" + 
                        "is not sufficient to stream the selected application and an upgrade is required.\n" + 
                        "Click 'OK' to begin downloading the new version of the Application Jukebox Player now.\n\n" +
                        "If you experience any difficulties, please contact the site administrator.");
        if (doInstall) {
          var params = 'resizable=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no,width=300,height=300';
          window.open(clientUpgradeUrl,'Installer',params);
          return false;
        }
      } else { // No confirm, just redirect
        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;
}
