//-------------------------------------

//var m_bShowDebug = false;
var m_bShowDebug = true;

var m_bShowInfo = false;
//var m_bShowInfo = true;

//-------------------------------------
// Add any global functions here.

//-------------------------------------
// Debug Print function

var DBG =
{
	Print : function(_Text)
	{
		if (m_bShowDebug == true)
		{
			Print(_Text);
		}
	}
}

//-------------------------------------
	
var INFO =
{
	Print : function(_Text)
	{
		if (m_bShowInfo == true)
		{
			Print(_Text);
		}
	}
}

//-------------------------------------

function Print(_Text)
{
	_Text = GetDateTime() + ' ' + _Text;

	if (!window.dbgwnd)
	{
		window.dbgwnd = window.open("","debug","status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,width=600,height=250");
		window.dbgwnd.document.write('<html><head></head><body style="background-color:black"><div id="main" style="color:green;font-size:12px;font-family:Courier New;"></div></body></html>');
	}

	var Console = window.dbgwnd.document.getElementById("main");

	if (Console.innerHTML != "")
	{
		Console.innerHTML = _Text + "<br/>" + Console.innerHTML;
	}
	else
	{
		Console.innerHTML = _Text;
	}
}

//-------------------------------------

function GetDateTime()
{
	var DT = new Date();
	return (Int2String(DT.getDate(),2) + '/' + Int2String(DT.getMonth() + 1,2) + '/' + DT.getFullYear() + ' ' + Int2String(DT.getHours(),2) + ':' + Int2String(DT.getMinutes(),2) + ':' + Int2String(DT.getSeconds(),2) + '.' + parseInt(((DT.getMilliseconds() % 1000) / 100) % 10));
}

//-------------------------------------

function Int2String(_Data,_Pad)
{
	_Data = _Data.toString();
	return (Array(_Pad + 1 - _Data.length).join('0') + _Data);
}

//-------------------------------------

function DecrementStringValue(_sValue)
{
	try
	{
		var Value = parseInt(_sValue);
		return ((--Value) + '');
	}
	catch (e)
	{
	}
	return (0);
}

//-------------------------------------

function TestAndDownloadFile()
{
	try
	{
		// If just downloaded a .pdf
		if ((window.name == "2") || (window.name == "1"))
		{
			var Index = document.URL.indexOf("FilePath=",0);
			if ((Index >= 0) && (Index < document.URL.length))
			{
				//DBG.Print("Index = " + Index + " URL = DownloadFile.aspx" + document.URL.substring(Index));
				window.location = "DownloadFile.aspx?" + document.URL.substring(Index);
			}

			// Used a counter to allow .pdf to be downloaded.
			// Set to 2 because IE may require 2nd pass for download confirmation.
			// And this still works otherwise, 'cos 2nd call is away from download page.
			window.name = DecrementStringValue(window.name);
		}

		if (m_bShowInfo == true)
		{
			StartInfoXfer();
		}
	}
	catch (e)
	{
	}
};
		
//-------------------------------------
//-------------------------------------

var UPDATE_TIME_MS			= 500; // 0.5 second
var UPDATE_PING_LIMIT_MS	= 100; // 0.1 second
var NUM_RANDOM_URL_CHARS	= 7;

//-------------------------------------

var m_PrevTime = -1;

//-------------------------------------

function SendTimedAjaxRequest(_sURL) 
{
	var CurrentDateTime = new Date();
	var CurrTime = CurrentDateTime.getTime();

	var UpdateTime = UPDATE_TIME_MS;
	var PingTime = (CurrTime - m_PrevTime);
	if (PingTime >= UPDATE_PING_LIMIT_MS)
	{
		UpdateTime += (PingTime * 2);
	}

	setTimeout("SendAjaxRequest(" + _sURL + ")",UpdateTime);
}

//-------------------------------------

function SendAjaxRequest(_sURL)
{
	var sURL = _sURL;

	if (sURL.indexOf("?") == -1)
	{
		sURL += "?";
	}
	else
	{
		sURL += "&";
	}

	sURL += GetRandomCharString(NUM_RANDOM_URL_CHARS);

	//DBG.Print("Request : " + sURL);

	if (window.XMLHttpRequest)
	{
		SendXMLHttpAjaxRequest(sURL);
	}
	else if (window.ActiveXObject)
	{
		SendActiveXAjaxRequest(sURL);
	}
}

//-------------------------------------

function GetRandomCharString(NumChars)
{
	var sASCII = "Rand=";

	var NUM_CHARS_IN_ALPHABET = 26;
	var ASCII_a_OFFSET = 97;

	for (var i = 0;i < NumChars;i++)
	{
		sASCII += String.fromCharCode(Math.floor(Math.random() * NUM_CHARS_IN_ALPHABET) + ASCII_a_OFFSET);
	}
	return (sASCII);
}

//-------------------------------------

function SendXMLHttpAjaxRequest(_URL)
{
	var Req = new XMLHttpRequest();
	if (Req != null)
	{
		Req.onreadystatechange = function()
		{
			if (Req.readyState == 4)
			{
				if (Req.status == 200)
				{
					GetAjaxResponse(Req.responseText);
				}
			}
		}

		Req.open("GET", _URL, true);
		Req.send(null);

		var CurrentDateTime = new Date();
		m_PrevTime = CurrentDateTime.getTime();
	}
}

//-------------------------------------

function SendActiveXAjaxRequest(_URL)
{
	var Req = new ActiveXObject("Microsoft.XMLHTTP");
	if (Req != null)
	{
		Req.onreadystatechange = function()
		{
			if (Req.readyState == 4)
			{
				if (Req.status == 200)
				{
					GetAjaxResponse(Req.responseText);
				}
			}
		}

		Req.open("GET", _URL, true);
		Req.send();
		
		var CurrentDateTime = new Date();
		m_PrevTime = CurrentDateTime.getTime();
	}
}

//-------------------------------------

function GetAjaxResponse(_ResponseText)
{
	//DBG.Print("Response : " + _ResponseText);

	var ID = _ResponseText.charAt(0);
	if (ID == 'i')
	{
		DisplayInfo(_ResponseText);
	}
	else
	{
		GetProgressBarResponse(_ResponseText)
	}
}

//-------------------------------------
//-------------------------------------

var m_sInfoXferURL = null;

function StartInfoXfer()
{
	if (m_sInfoXferURL == null)
	{
		m_sInfoXferURL = "InfoXfer.aspx";
		SendAjaxRequest(m_sInfoXferURL);
	}
}

function DisplayInfo(_ResponseText)
{
	var sData = _ResponseText.slice(1);
	if (sData != "")
	{
		var asData = sData.split("\n");
		var Length = asData.length;
		for (var Index = 0;Index < Length;Index++)
		{
			if (asData[Index].length > 0)
			{
				//INFO.Print(asData[Index]);
			}
		}
	}

	SendTimedAjaxRequest("m_sInfoXferURL");
}

//-------------------------------------
//-------------------------------------

