﻿var m_xmlhttp = null;
var m_nMaxCols = 1;
var m_nCols = 1;
var m_nCheckCacheInterval = null;
var m_nTimeoutID = null;
var m_strPageSearch = null;
var m_divXML = null;
var m_divWait = null;
var m_bIE = window.ActiveXObject ? true : false;
//var m_dtRequest = null;

function Init(nCols, nCheckCacheInterval, nTimeout)
{
	m_nMaxCols = m_nCols = nCols;
	if(m_nCols > 1) m_nCols = 0;

	m_nCheckCacheInterval = nCheckCacheInterval;
	m_strPageSearch = window.location.search.substring(1);

	m_divXML = document.getElementById("divXML");
	m_divWait = document.getElementById("divWait");

	if(window.XMLHttpRequest) // code for Mozilla, etc & IE 7.0.
		m_xmlhttp = new XMLHttpRequest();
	else if(window.ActiveXObject) // code for IE 6.0
		try
	{
		m_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e)
	{
		m_xmlhttp = null; //security block etc
	}

	m_nTimeoutID = window.setTimeout("checkCache()", nTimeout);
}

function checkCache()
{
	m_nTimeoutID = null;

	if(m_xmlhttp == null)
		alert("XMLHttpRequest object not created.");
	else
		try
	{
		//m_dtRequest = new Date();
		m_xmlhttp.open("POST", window.location.pathname, true);
		m_xmlhttp.onreadystatechange = checkCacheResponse;
		m_xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		m_xmlhttp.send(m_strPageSearch);
	}
	catch(e)
	{
		alert(e);
	}
}

function checkCacheResponse()
{
	if(m_xmlhttp.readyState != 4) // Complete
		return;

	var nTimeout = m_nCheckCacheInterval, nStatus = -1;

	try { nStatus = m_xmlhttp.status; } catch(e) { }

	switch(nStatus)
	{
		case 200:
		{
			var oRoot = m_xmlhttp.responseXML.documentElement;
			nTimeout = oRoot.getAttribute("Timeout");

			var oNode = oRoot.firstChild;
			while(oNode)
			{
				if(oNode.firstChild)
				{
					var oTarget = document.getElementById(oNode.getAttribute("PlatformTag"));
					if(m_bIE)
						oTarget.innerHTML = oNode.firstChild.xml;
					else
					{
						oTarget.replaceChild(document.importNode(oNode.firstChild, true), oTarget.firstChild);
						oTarget.innerHTML = oTarget.innerHTML;
					}
				}

				oNode = oNode.nextSibling;
			}

			if(m_divWait.style.display != "none")
			{
				m_divXML.style.display = "block";
				m_divWait.style.display = "none";
			}
			break;
		}
		default:
			m_divXML.style.display = "none";
			m_divWait.style.display = "block";
			break;
	}

	m_nTimeoutID = window.setTimeout("checkCache()", nTimeout);
}

function onResize()
{
	var nCols = Math.min(m_nMaxCols, Math.max(1, Math.floor(document.body.offsetWidth / 390)));
	if(m_nCols == nCols)
		return;

	m_nCols = nCols;
	var strTable = "<table><tr>";
	var n = -1;
	var oTable = m_divXML.firstChild;
	var oRow = oTable.firstChild.firstChild;
	while(oRow)
	{
		var oCell = oRow.firstChild;
		while(oCell)
		{
			var nClass;
			if(++n == 0)
				nClass = 0;
			else if(n < nCols)
				nClass = 1;
			else if(n % nCols == 0)
			{
				nClass = 2;
				strTable += "</tr><tr>";
			}
			else
				nClass = 3;

			strTable += "<td class=\"cellPlatform" + nClass + "\" id=\"" + oCell.id + "\">" + oCell.innerHTML + "</td>";

			oCell = oCell.nextSibling;
		}
		oRow = oRow.nextSibling;
	}

	m_divXML.innerHTML = strTable + "</tr></table>";
}

//from Util.js
function setFocusById(strID)
{
	var o = strID.length > 0 ? document.getElementById(strID) : null;
	if(o != null)
	{
		o.focus();
		try { o.select(); } catch(e) { }
	}
}
