﻿function Statistics()
{
	var THIS = this;

	var divStats = document.getElementById("stats");

	if (divStats == null)
		return;
	
	var divStatistics = divStats.getElementsByTagName("DIV")[0];
	var h2Heading = divStats.getElementsByTagName("H1")[0];
	var arrStats = new Array();
	var intSecond = 1000;
	var intRefreshHandle = 0;

	this.Refreshing = false;

	//==========================================
	// Initialise the control
	//==========================================
	this.Init = function Init()
	{
		try
		{
			var arrStatisticValues = divStatistics.getElementsByTagName("SPAN");

			for (var intIndex = 0; intIndex < arrStatisticValues.length; intIndex++)
			{
				var spnStatistic = arrStatisticValues[intIndex];
				var strKey = spnStatistic.id.toString().replace("statValue_", "");

				AddStatistic(strKey, spnStatistic);
			}

			intRefreshHandle = window.setInterval("m_objStatistics.Refresh()", 15 * intSecond);
			h2Heading.title = h2Heading.innerHTML + " as at " + new Date().toLocaleTimeString();
		}
		catch (exc)
		{
			if (intRefreshHandle != 0)
				window.clearInterval(intRefreshHandle);
			HtmlPopup.Error(exc);
		}
	}

	//==========================================
	// Refresh the statistics
	//==========================================
	this.Refresh = function Refresh()
	{
		try
		{
			//check that the alerts window is not refreshing
			//if it is the soap client can forward the response to the wrong method - data cross over

			if (m_objAlerts && m_objAlerts != null && m_objAlerts.Refreshing && m_objAlerts.Refreshing == true)
				return; //wait until next time...

			if (this.Refreshing == true)
				return; //still refreshing from last time

			this.Refreshing = true;

			var params = new SOAPClientParameters();
			SOAPClient.invoke("/webservices/newDesign.asmx", "GetStatistics", params, true, DisplayStatisticValues, null);
		}
		catch (exc)
		{
			window.clearInterval(intRefreshHandle);
			HtmlPopup.Error(exc);
		}
	}

	//==========================================
	// Display the statistics
	//==========================================
	function DisplayStatisticValues(strXML, docXML, sender)
	{
		try
		{
			if ((typeof(strXML) == "object" && strXML != null) || strXML == "ERROR" || strXML == null)
			{
				window.clearInterval(intRefreshHandle);				
				return;
			}
		
			if (typeof (strXML) == "object" && strXML != null)
				throw new Error(docXML);
		
			THIS.Refreshing = false;

			var recordset = new Recordset();
			recordset.LoadXML(strXML);

			while (!recordset.EOF())
			{
				var strKey = recordset["Key"];
				var strValue = recordset["Value"];

				var objStatistic = this[strKey];
				if (objStatistic && objStatistic != null)
					objStatistic.SetValue(strValue);

				recordset.MoveNext();
			}

			h2Heading.title = h2Heading.innerHTML + " as at " + new Date().toLocaleTimeString();
		}
		catch (exc)
		{
			window.clearInterval(intRefreshHandle);
			HtmlPopup.Error(exc);
		}
	}

	//==========================================
	// Add a statistic
	//==========================================
	function AddStatistic(strKey, spnStatistic)
	{
		var objStat = new Statistic(this, strKey, spnStatistic);
		this[strKey] = objStat;
		return objStat;
	}

	this.Init();
}

var m_objStatistics = null;
Statistics.Create = function Create()
{
	try
	{
		if (m_objStatistics == null)
			m_objStatistics = new Statistics();

		return m_objStatistics;
	}
	catch (exc)
	{
		HtmlPopup.Error(exc);
	}
}

addInitMethod(Statistics.Create);

function Statistic(objStatistics, strKey, spnDetail)
{
	this.Key = strKey;

	//==========================================
	// Set the value for a statistic
	//==========================================
	this.SetValue = function SetValue(strValue)
	{
		if (!isNaN(Number(strValue)))
			strValue = toNumeric(strValue);

		spnDetail.innerHTML = strValue;
	}

	//==========================================
	// Convert the numeric value to a human radable number, commas will be added as thousand separators
	//==========================================
	function toNumeric(intValue)
	{
		var strSource = (intValue + "");

		var intIndex = strSource.length - 1;

		var intCount = 0;
		var strValue = "";
		var strTemp = "";

		while (intIndex >= 0)
		{
			if (intCount < 3)
				strTemp = strSource.charAt(intIndex) + strTemp;

			intCount++;

			if (intCount >= 3)
			{
				intCount = 0;

				var strComma = ",";
				if (intIndex <= 2)
					strComma = "";

				strValue = strComma + strTemp + strValue;
				strTemp = "";
			}

			intIndex--;
		}

		if (strTemp != "")
		{
			var strComma = ",";

			if (strValue == "")
				strComma = "";

			strValue = strTemp + strComma + strValue;
			strTemp = "";
		}

		return strValue;
	}
}
