var ajaxReceive;
var ajaxAction = "AJAXTableListRefresh";
var ajaxRefreshPeriod = 1000;
var isReceiving = false;
var isProcessing = false;

var gameType = 1;
var limitType = 1;

function getAjaxRequestObject()
{
    var req = false;
    if (window.XMLHttpRequest)
        req = new XMLHttpRequest();
    else if (window.ActiveXObject)
        req = new ActiveXObject("Microsoft.XMLHTTP");
    return req;
}

function initPokerTableList(game, limit)
{
    gameType = game;
    limitType = limit;
}

function tableRefresh()
{
    if (ajaxReceive && (ajaxReceive.readyState == 0 || ajaxReceive.readyState == 4))
    {
        isReceiving = true;
        ajaxReceive.open('get', ajaxAction + "?tp=" + gameType + "&lity=" + limitType);
        ajaxReceive.onreadystatechange = handleTableReceive;
        ajaxReceive.send(null);
    }
}

function initTableRefresh(interval)
{
    if (document.getElementById("poker-table-list") && interval > 0)
        setInterval('tableRefresh()', interval);
}

function handleTableReceive()
{
    if (ajaxReceive.readyState == 4)
    {
        if (ajaxReceive.status == 200)
        {
            var response = ajaxReceive.responseText;
            if (response && !isProcessing)
                processNewResponse(response);
            isReceiving = false;
        }
    }
}

function processNewResponse(response)
{
    isProcessing = true;
    var tableList = document.getElementById("poker-table-list");
    if (tableList)
        tableList.innerHTML = response;
    isProcessing = false;
}

function afterPageLoad()
{
    var tableList = document.getElementById("poker-table-list");
    if (tableList)
    {
        ajaxReceive = getAjaxRequestObject();
        initTableRefresh(ajaxRefreshPeriod);
    }
}
