﻿// JScript File

DMVersion.prototype				= new Object ;
DMVersion.prototype.constructor	= DMVersion ;
DMVersion.prototype.superclass	= Object.prototype ;

DMVersion.prototype.Version		= "1.0.0.1" ;

DMVersion.BrowserType			= {InternetExplorer:"IE", Firefox:"Firefox" } ;

DMVersion.CLSID					= "clsid:FC7F24C5-7DC3-48DF-A00D-F57DE89EADC5" ;
DMVersion.MimeType				= "application/entriq-version-check-npruntime" ;
DMVersion.PluginName			= "npruntime Entriq version check plugin" ;


// Static factory methods
DMVersion.CreateObject = function( sBrowserType, sObjectId, sCodebase ) 
{
	if ( typeof( sBrowserType) != 'string' || typeof(sObjectId) != 'string' ) 
	{
		return;
	}
	var head = document.getElementsByTagName('head').item(0);
	if ( sBrowserType == DMVersion.BrowserType.InternetExplorer )
	{
		  var obj = document.createElement('object');
		  obj.setAttribute('id', sObjectId );
		  obj.setAttribute('classid', DMVersion.CLSID);
		  if ( sCodebase != null && typeof(sCodebase) == 'string' && sCodebase != '' )
		  {
			obj.setAttribute('codebase', sCodebase);
		  }
		  obj.setAttribute('VIEWASTEXT');
		  head.appendChild(obj);
	}
	else
	{
		  var obj = document.createElement('embed');
		  obj.setAttribute('name', sObjectId );
		  obj.setAttribute('type', DMVersion.MimeType );
		  head.appendChild(obj);
	}
}

DMVersion.WriteObject = function( sBrowserType, sObjectId, sCodebase ) 
{
	if ( typeof( sBrowserType) != 'string' || typeof(sObjectId) != 'string' ) 
	{
		return;
	}
	var codebaseExists = (sCodebase != null && typeof(sCodebase) && sCodebase != "" ) ;
	if ( sBrowserType == DMVersion.BrowserType.InternetExplorer )
	{
		document.write(
			'<OBJECT id="' + sObjectId + '" ' +
			'classid="' + DMVersion.CLSID + '" ' +
			(codebaseExists?'codebase="' + sCodebase + '" ': '') + 
			'width="0" height="0" VIEWASTEXT></OBJECT>');
	}
	else
	{
		document.write(
			'<EMBED name="' + sObjectId + '" ' +
			'type="' + DMVersion.MimeType + '" ' +
			'HIDDEN></EMBED>');
	}
}

DMVersion.HasPlugin = function() 
{
	var exists = false;
	for ( var i in  navigator.plugins ) 
	{  
		var n = navigator.plugins[i].name ;

		if ( n == DMVersion.PluginName )
		{
			exists = true ;
			break ;
		}
	}
	return exists ; 
}


// DMVersion instance methods

function DMVersion( Browser, Id )
{
	this.init( Browser, Id ) ; 
}

DMVersion.prototype.init = function( Browser, Id )
{
	this.Browser = Browser ; 
	if ( this.Browser == DMVersion.BrowserType.InternetExplorer )
	{
		this.Control = document.getElementById(Id).object ; 
	}
	else
	{
		this.Control = document.embeds[Id]; 
	}
}

DMVersion.prototype.getControl = function( )
{
	return this.Control ; 
}

DMVersion.prototype.getVersion = function( type, custId )
{
	return this.Control.GetDMExtVersion( type, custId ) ;
}

DMVersion.prototype.checkVersion = function( current, self )
{
	if (current.indexOf('.') == -1) return false;
	
	var aVIn = current.split(".");
	var aVOut = self.split(".");
	if ( parseInt(aVIn[0]) < parseInt(aVOut[0]) ) return false;
	if ( parseInt(aVIn[1]) < parseInt(aVOut[1]) ) return false;
	if ( parseInt(aVIn[2]) < parseInt(aVOut[2]) ) return false;
	if ( parseInt(aVIn[3]) < parseInt(aVOut[3]) ) return false;
	return true ;
}

/* check to see whether the user passes */
DMVersion.hasPassed = function(browserType, oVersion)
{
	// check against these values
	var coreVersion = '3.8.2.9';
	var configVersion = '1.0.0.12174';
	var wmpVersion = '11.0.0.0';
	var drmVersion = '2.9.0.0';
	var custId = 'five';
	
	var o = {};
	o.pass = false;
	o.has = {};
	o.has.Plugin = false;
	o.has.Core = false;
	o.has.Config = false;
	o.has.Wmp = false;
	o.has.Drm = false;
	o.versionEnum = null;
	
	if ( browserType == this.BrowserType.Firefox && this.HasPlugin() ) {
		o.has.Plugin = true;
	}
	
	try {
		o.versionEnum = oVersion.getVersion("ENUM", custId);
		o.has.Plugin = true;
	} catch (e) {}
	
	// check versions
	if (o.versionEnum != null){
		var vers = o.versionEnum.split(',');
		
		for (var i = 0; i < vers.length; i++){
			var d = vers[i].split('=');
			var key = d[0];
			var val = d[1];
			
			if (key == 'CORE') {
				if ( oVersion.checkVersion( val, coreVersion )) o.has.Core = true;
			} else if (key == "CONFIG") {
				if ( oVersion.checkVersion( val, configVersion )) o.has.Config = true;
			} else if (key == "DRM") {
				if ( oVersion.checkVersion( val, wmpVersion )) o.has.Wmp = true;
			} else if (key == "DRMSECURITY") {
				if ( oVersion.checkVersion( val, drmVersion )) o.has.Drm = true;
			}
		}
	}
	
	if (o.has.Core && o.has.Config) o.pass = true;
	return o;
}