var hWnd = null;
var debugLineMax = 100;
var debugLineNb = 0;
var debugLine = new Array(debugLineMax);

function generateDebugString()
{
	var str = "";
	for (var i = debugLineNb - 1;i >= 0;i--) {
		str += debugLine[i];
	}
	
	return str;
}

function writeDebugMessage(msg)
{
	var el = document.__frm['__debug'];
	if (el != null)
	{
		if (debugLineNb < debugLineMax)
		{
			debugLine[debugLineNb] = msg + "\n";
			debugLineNb++;
		}
		else
		{
			for (var i = 1;i < debugLineMax;i++) {
				debugLine[i - 1] = debugLine[i];
			}
			debugLine[debugLineMax-1] = msg + "\n";
		}

		el.value = generateDebugString();
	}
}

function clearDebug()
{
	var el = document.__frm['__debug'];
	if (el != null)
	{
		for (var i = 0;i < debugLineNb;i++) {
			debugLine[i] = "";
		}
		debugLineNb = 0;
		el.value = "";
	}
}

function writeDebugStaticMessage(msg)
{
	var el = getElementId('__debug_static');
	if (el != null)
	{
		el.innerHTML = msg;
	}
}

function getFormElement(name)
{
	return document.__frm[name];
}

function getElementId(id)
{
	return document.getElementById(id);
}

function getDebug()
{
	var el = getFormElement('debug');
	if (el != null)
		return el.value;
	return '';
}

function getScheme()
{
	var el = getFormElement('scheme');
	if (el != null)
		return el.value;
	return 'http';
}

function getPort()
{
	var el = getFormElement('port');
	if (el != null)
		return el.value;
	return '80';
}

function getUserLanguage()
{
	var el = getFormElement('userLanguage');
	if (el != null)
		return el.value;
	return '';
}

function getUAGenericName()
{
	var el = getFormElement('ua_name');
	if (el != null)
		return el.value;
	return '';
}

function getUAVersion()
{
	var el = getFormElement('ua_version');
	if (el != null)
		return el.value;
	return '';
}

function getUAClass()
{
	var el = getFormElement('ua_class');
	if (el != null)
		return el.value;
	return '';
}

function getUAType()
{
	var el = getFormElement('ua_type');
	if (el != null)
		return el.value;
	return '';
}

function getUAOS()
{
	var el = getFormElement('ua_os');
	if (el != null)
		return el.value;
	return '';
}

function getUAOSClass()
{
	var el = getFormElement('ua_os_class');
	if (el != null)
		return el.value;
	return '';
}

function getCountryCode()
{
	var el = getFormElement('ua_countryname');
	if (el != null)
		return el.value;
	return '';
}

function getCountryName()
{
	var el = getFormElement('ua_countrycode');
	if (el != null)
		return el.value;
	return '';
}

function getIP()
{
	var el = getFormElement('ua_ip');
	if (el != null)
		return el.value;
	return '';
}

function getTWALocked()
{
	var el = getFormElement('twa_locked');
	if (el != null)
		return el.value;
	return "0";
}

function getTWAWelcomeCounter()
{
	var el = getFormElement('twa_welcome_counter');
	if (el != null)
		return el.value;
	return 1;
}
		
function getFlashMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	}
	else {
		return document[movieName]
	}
}

function setBackgroundColor(color) {
	if (document.body != null) {
		if (color == "black") {
			document.body.style.backgroundColor = "black";
		}
		else if (color == "white") {
			document.body.style.backgroundColor = "white";
		}
		else if (color == "night") {
			document.body.style.backgroundColor = "#070020";
		}
	}
}

function openPopupWindow(url, width, height, options)
{
	if (width == null) width = 800;
	if (height == null) height = 600;
	if (options == null) options = "";
	var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));

	if (hWnd != null) {
		if (!hWnd.closed)
			hWnd.close();
	}

	hWnd = window.open(url, "", "top=" + top + ",left=" + left + ",width=" + width + ",height=" + height + "," + options);
}

function getLoadSwf()
{
	return document.__frm.swf.value;
}
