/// ------------------  Odyssey Activex Functions ------------------------


//  Global Variables
var Gamer;
var ActivexErrorMessage = 'Your security settings prohibit running ActiveX controls';
var usersManualFeatures = 'toolbar=no, menubar=no,scrollbars=yes,resizable=yes';
// Consts fro getGamesList Array
var gameSKU = 0;
var gameExpiredTime = 1;
var gameIsExpired = 2; 

// Initialize
createGamer();

function createGamer()
{
    try
    {
        Gamer = new ActiveXObject("Odyssey.OdysseyActiveX.1"); 
    }
    catch(err)
    {
	
    //  alert(ActivexErrorMessage); //Do nothing
    }
} 

// ---------------------------------------------------------------------------------
// Odessey Avtivex Interface 
// ---------------------------------------------------------------------------------  

// -------- ActiveX Properties --------------//

//FirstGame SKU
function firstGameSKU()
{
	if(isGamerOK())
	{
		return Gamer.FirstGameSKU();
	}
}

//HardwareFingerprint
function hardwareFingerprint()
{
	if(isGamerOK())
	{
		return Gamer.HardwareFingerprint();
	}
}

// HasMoreGames
function hasMoreGames()
{
	if(isGamerOK())
	{
		return Gamer.HasMoreGames();
	}
}

//NextGameSKU
function nextGameSKU()
{
	if(isGamerOK())
	{
		return Gamer.NextGameSKU();
	}
}

//NumberOfInstalledGames
function numberOfInstalledGames()
{
	if(isGamerOK())
	{
		return Gamer.NumberOfInstalledGames();
	}
}

// -------- ActiveX Methods -----------------//

// Refresh Tree
function refreshTree()
{
	if(isGamerOK())
	{
		Gamer.RefreshTree();
	}
}

// Set Activation Key
function setActivationKey(sku,key)
{
	if(isGamerOK())
	{
		Gamer.SetActivationKey(sku,key);
	}
}

// Set Activation Name
function setActivationName(sku,sActivationName)
{
	if(isGamerOK())
	{
		Gamer.SetActivationName(sku,sActivationName);
	}
}
// Available Usage Unit
function availableUsageUnits (sku)
{
	if(isGamerOK())
	{
		return Gamer.AvailableUsageUnits(sku);
	}
}

// Distribution Channel
function distributionChannel(sku)
{
	if(isGamerOK())
	{
		return Gamer.DistributionChannel(sku);
	}
}

// Is Expired (Game trial expired)
function isExpired(sku) 
{

	if(isGamerOK())
	{
		return Gamer.IsExpired(sku);
	}
}

// Is Registered (is the game registered)
function isRegistered(sku)
{
	if(isGamerOK())
	{
		return Gamer.IsRegistered(sku);
	}
}

//lanches the game
function launchGame(sku)
{
    if(isLaunchGameEnable && privateIsGameInstalled(sku))
    {
        isLaunchGameEnable = false;
        Gamer.LaunchGame(sku);
        setTimeout('enableLaunchGame();',10000); 
    }
}

//Maximum Usage (units)
function maximumUsageUnits(sku)
{
	if(isGamerOK())
	{
		return Gamer.MaximumUsageUnits(sku);
	}
}


// Misc Info
function miscInfo(sku) 
{
	if(isGamerOK())
	{
		return Gamer.MiscInfo(sku);
	}
}

// Usage Unit Type
function usageUnitType()
{
	if(isGamerOK())
	{
		return Gamer.UsageUnitType(sku);
	}
}

// ValidCheck sum
function validChecksum(sku)
{
	if(isGamerOK())
	{
		return Gamer.ValidChecksum(sku);
	}
}


// ---------------------------------------------------------------------------------
// Other functions 
// ---------------------------------------------------------------------------------  

// isGamer o/k - check activex object
function isGamerOK()
{

    if(Gamer == null)
    {
        return false;
    }
    else
    {
        return true;
    }
}

// get Games List - Get Array containing all SKUs installed on client
function getGamesList()
{
 if(isGamerOK())
	{
	    createGamer();
	    var InstalledGames = new Array();
		var tempSku = Gamer.FirstGameSKU;
		var gameProperties = new Array();
				
		gameProperties.push(tempSku);
		gameProperties.push(availableUsageUnits(tempSku));
		gameProperties.push(isExpired(tempSku));
		gameProperties.push(isRegistered(tempSku));
		
		InstalledGames.push(gameProperties);
		
		while(Gamer.HasMoreGames)
		{
			
			tempSku = Gamer.NextGameSKU;
			if (isNumeric(tempSku)==true)
			{
				var gameProperties = new Array();
				gameProperties.push(tempSku);
				gameProperties.push(availableUsageUnits(tempSku));
				gameProperties.push(isExpired(tempSku));
				gameProperties.push(isRegistered(tempSku));
				
				InstalledGames.push(gameProperties);
			}
		}
	}
  return InstalledGames;	
}

// get Games List for specific Channel - Get Array containing all SKUs installed on client
function getGamesListForChannel(channelID)
{
 if(isGamerOK())
	{
	    createGamer()
	    var InstalledGames = new Array();
		var tempSku = Gamer.FirstGameSKU;
		var gameProperties = new Array();
		
		var gameChannel = trim(distributionChannel(tempSku));
		if (channelID.indexOf(gameChannel))
		{
			gameProperties.push(tempSku);
			gameProperties.push(availableUsageUnits(tempSku));
			gameProperties.push(isExpired(tempSku));
			gameProperties.push(isRegistered(tempSku));
			
			InstalledGames.push(gameProperties);
		}
		while(Gamer.HasMoreGames)
		{
			
			tempSku = Gamer.NextGameSKU;
			gameChannel = distributionChannel(tempSku);
			if (isNumeric(tempSku)==true && isEqual(gameChannel,channelID))
			{
				var gameProperties = new Array();
				gameProperties.push(tempSku);
				gameProperties.push(availableUsageUnits(tempSku));
				gameProperties.push(isExpired(tempSku));
				gameProperties.push(isRegistered(tempSku));
				
				InstalledGames.push(gameProperties);
			}
		}
	}
  return InstalledGames;	
}

function getGamesListByChannel(channelID)
{
  var InstalledGames = [];

 if(isGamerOK())
	{
	    createGamer()
		var tempSku = Gamer.FirstGameSKU;
		var gameChannel, game;
		channelID = String(channelID);

		do
		{
			gameChannel = trim(distributionChannel(tempSku));
			if (isNumeric(tempSku)==true && channelID.contains(gameChannel))
			{
				InstalledGames[InstalledGames.length] = 
						{ sku					: tempSku
						, availableUsageUnits	: availableUsageUnits(tempSku)
						, maxUsageUnits			: maximumUsageUnits(tempSku)
						, isExpired				: isExpired(tempSku)
						, isRegistered			: isRegistered(tempSku)
						, channel				: gameChannel
						};
			}
			
			if (! Gamer.HasMoreGames ) 
				break;
		
			tempSku = Gamer.NextGameSKU;
		}
		while(tempSku) ;
	}
	return {games: InstalledGames };	
}


// trim the same as in VB
function trim(str)
{
    if(str == null){
        return null;
    }
    
    switch(typeof(str)){
        case 'string':
            return str.replace(/^\s*|\s*$/g,'');
            break;
        default:
            return String(str);
    }
}

// Isnumeric - the same as VB
function isNumeric(s) {
  if(!/^-*[0-9\.]+$/.test(s)) {
   		return false
   }
   else {
		return true;
   }
}

// is Equal - true/false 
function isEqual(sku1, sku2)
{
    var sku1Trimed = trim(sku1);
    var sku2Trimed = trim(sku2);
    
	if(sku1Trimed == sku2Trimed)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//privateIsGameInstalled - check if game is installed
function privateIsGameInstalled(sku)
{
	if(isGamerOK())
	{
	    createGamer();
		var tempSku = Gamer.FirstGameSKU;

		while(!isEqual(tempSku, sku) && Gamer.HasMoreGames)
		{
			tempSku = Gamer.NextGameSKU;
		}

		if(isEqual(tempSku, sku))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

//IsGameInstalled - check if game is installed
function isGameInstalled(sku)
{
    if(privateIsGameInstalled(sku))
    {
        var stat = 'true';
    }
    else
    {
        var stat='false';
    }
    
}

// Enable lunch a game
var isLaunchGameEnable = true;
function enableLaunchGame()
{
    isLaunchGameEnable = true;
}



