//---------------------------------------------------------------------------//
//									Flash Test								 //
//---------------------------------------------------------------------------//

var FlashTest = function(maxVersion)
{
	this.MaxVersion = maxVersion;
}

FlashTest.prototype.getVersion = function()
{
	var flashVersion = null;
	
	// check for netscape compatible plugin
	if (navigator.plugins && navigator.plugins.length)
	{
		for (p = 0; p < navigator.plugins.length; p++)
		{
			if (navigator.plugins[p].name.indexOf('Shockwave Flash') != -1)
			{
				var flashVersionStr = navigator.plugins[p].description.split('Shockwave Flash ')[1].split(' ')[0];
				
				flashVersion = parseInt(flashVersionStr);
				break;
			}
		}
	}
	else if (window.ActiveXObject)
	{
		// starting from flash 3
		for (v = 3; v < this.MaxVersion; v++)
		{
			try
			{
				var flashObj = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + v + "');");
				if (flashObj  != null)
					flashVersion = v;
			}
			catch(e) {}
		}
	}
	return flashVersion;
}

//---------------------------------------------------------------------------//
//								Media Player Test							 //
//---------------------------------------------------------------------------//

//TODO: this test doesn't work on firefox.
var MediaPlayerTest = function(containerId, playerId)
{
	this.WmpContainerId		= containerId;
	this.WmpPlayerId		= playerId;
}

MediaPlayerTest.prototype.getVersion = function()
{
	var WMP = this.getMediaPlayer();
	if(WMP != null && WMP.versionInfo != null)
		return parseInt(WMP.versionInfo);
	else 
		return null;		
}  
	
MediaPlayerTest.prototype.getMediaPlayer = function()
{
	var wmpPlayer = document.getElementById(this.WmpPlayerId);
	if (wmpPlayer == null)
	{
		var wmpPlayerTxt = '<object id="' + this.WmpPlayerId + '" width="1" height="1" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" viewastext></object>';
		writeElement(this.WmpContainerId, wmpPlayerTxt);
		wmpPlayer = document.getElementById(this.WmpPlayerId);
	}
	return wmpPlayer;
}

//---------------------------------------------------------------------------//
//								Popup test									 //
//---------------------------------------------------------------------------//

PopupClientTest = function (instanceName, objId, popupUrl, width, height)
{
	this.InstanceName		= instanceName;
	this.ResultElementId	= objId;
	this.PopupUrl			= popupUrl;
	this.Width				= width;
	this.Height				= height;
	
	this.PopupWin			= null;
	this.PopupWinName		= instanceName + '_Win';
	this.OpenPopupTimeout	= null;
	this.PopupTimeout		= null;
	this.IsEnabled			= false;
}

PopupClientTest.prototype.openPopupWin = function()
{
	this.PopupWin = window.open(this.PopupUrl, this.PopupWinName,'scrollbars=no,width=' + this.Width + ',height=' + this.Height);
	
	if(this.OpenPopupTimeout)
		window.clearTimeout(this.OpenPopupTimeout);
		
	//Wait for an amount of time and see if the testwindow exist. 
	this.PopupTimeout = setTimeout(this.InstanceName + '.isPopupEnabled()', 1500);	
}

PopupClientTest.prototype.failedOpenWindow = function()
{
	this.IsEnabled = false;
	completeTest('True');
}  

//async test
PopupClientTest.prototype.startTest = function()
{
	//Look for eventual window-errors.
	window.onerror = this.failedOpenWindow;
	//Wait for an amount of time and trigger opening the window.
	this.OpenPopupTimeout = setTimeout(this.InstanceName + '.openPopupWin()', 1000);
}

PopupClientTest.prototype.isPopupEnabled = function()
{
	//clear timeout
	if(this.PopupTimeout)
		window.clearTimeout(this.PopupTimeout);
		
	// at this step popup window must be opened
	if(this.PopupWin != null && !this.PopupWin.closed && (this.PopupWinName.indexOf(this.PopupWin.name) != -1))
		this.IsEnabled = true;
	else
		this.IsEnabled = false;
		
	//Close window, if it's opened
	if(this.PopupWin != null && !this.PopupWin.closed)
		this.PopupWin.close();
	
	writeResult(this.ResultElementId, this.IsEnabled);
	completeTest('True');
}

//---------------------------------------------------------------------------//
//								Bandwidth test								 //
//---------------------------------------------------------------------------//

// constructor for BandwidthTest
var BandwidthTest = function (instanceName, objId, frameId, dataPageUrlSmall, dataPageUrlMedium, dataPageUrlLarge)
{
	// Properties
	this.InstanceName				= instanceName;
	this.ResultElementId			= objId;
	this.FrameElementId				= frameId;
	this.DataPageUrlSmall			= dataPageUrlSmall;
	this.DataPageUrlMedium			= dataPageUrlMedium;
	this.DataPageUrlLarge			= dataPageUrlLarge;
	
	this.CriticalTimeout			= null;
	this.CriticalTimeoutMilliSec	= 30000;
	this.CurrentDataPageType		= null;
}

//start async test
BandwidthTest.prototype.startTest = function()
{
	this.setDataPage(1);
}

BandwidthTest.prototype.setDataPage = function(pageType)
{
	this.CurrentDataPageType = pageType;
	var streamFrame = document.getElementById(this.FrameElementId);
	switch (pageType)
	{
		case 1: streamFrame.src = this.DataPageUrlSmall; break; //strStreamFile + "?l=" + 65536;
		case 2: streamFrame.src = this.DataPageUrlMedium; break;
		case 3: streamFrame.src = this.DataPageUrlLarge; break;
	}
	
	if(this.CriticalTimeout)
		window.clearTimeout(this.CriticalTimeout);
		
	//In case client hasn't downloaded streaming source in a very long time (test failed)
	this.CriticalTimeout = window.setTimeout(this.InstanceName + '.criticalResponse()', this.CriticalTimeoutMilliSec);	
}

BandwidthTest.prototype.criticalResponse = function()
{
	if(this.CriticalTimeout)
		window.clearTimeout(this.CriticalTimeout);
	
	var streamFrame = document.getElementById(this.FrameElementId);
	streamFrame.src = "";
	
	completeTest('True');
}

BandwidthTest.prototype.setBandwidthResult = function(dataLength, time)
{
	if(this.CriticalTimeout)
		window.clearTimeout(this.CriticalTimeout);
	
	if (this.CurrentDataPageType >= 3 || time > 1)
	{
		if (time == 0)
			time = 0.1 // no division by zero
			
		var bandwidth = Math.round(dataLength / time) * 8;
		writeResult(this.ResultElementId, bandwidth);
		completeTest('True');
	}
	else
		this.setDataPage(this.CurrentDataPageType + 1);
}

//---------------------------------------------------------------------------//
//								Common functions							 //
//---------------------------------------------------------------------------//

function writeElement(elementId, text)
{
	var element = document.getElementById(elementId);
	if (element == null)
		element = document.getElementsByName(elementId)[0];
	if(element)
		element.innerHTML = text;
}

function writeResult(hiddenFieldId, text)
{
	// TODO: this on FF doesn't works. Find out why. 
	// getElementId returns null, even in previous place only with other id it returns not null
	var hiddenField = document.getElementById(hiddenFieldId);
	if (hiddenField == null)
		hiddenField = document.getElementsByName(hiddenFieldId)[0];
		
	if(hiddenField && text != null)
		hiddenField.value = text;
}

var testsCount;
var completedTests;
function initClientCapsTests(count)
{
	testsCount = count;
	completedTests = 0;
}

function completeTest(AutoPostBack)
{
	var isAutoPostBack;
	if (AutoPostBack == 'False')
	{
		isAutoPostBack = false
	}
	else
		isAutoPostBack = true;
		
	completedTests++;
	writeElement('progress', completedTests);
	
	if ((completedTests >= testsCount) && isAutoPostBack)
		__doPostBack('','');
}

