var req = null;
var timer = null;

var PERIOD = 30000;//30 sec
var PING_URL_POSTFIX = "ping/";
var pingURL;
var READY_STATE_COMPLETE = 4;

function sendRequest(httpMethod, url, callbackFunction)
{
    req = initXMLHTTPRequest();
    if (req)
    {
        req.onreadystatechange = function()
        {
            var ready = req.readyState;
            if (ready == READY_STATE_COMPLETE)
            {
                callbackFunction();
            }
        };
        req.open(httpMethod, url, true);
        req.send(null);
    }
}
function sendHeadRequest(url)
{
    req = initXMLHTTPRequest();
    if (req)
    {
        req.onreadystatechange = onReadyState;
        req.open("HEAD", url, true);
        req.send("");
    }
}
function initXMLHTTPRequest()
{
    var xRequest = null;
    if (window.XMLHttpRequest)
    {
        xRequest = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        xRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xRequest;
}

function onReadyState()
{
    var ready = req.readyState;
    if (ready == READY_STATE_COMPLETE)
    {
        //alert("Response is coming");
    }
}
function pingInternal()
{
    sendHeadRequest(pingURL);
}
function ping(interval, appPath)
{
    pingURL = appPath + "/" + PING_URL_POSTFIX;
    stopPing();
    timer = setInterval("pingInternal()", interval);
}

function stopPing()
{
    if (timer)
    {
        clearInterval(timer);
        timer = null;
    }
}
window.onload = function()
{
    ping(PERIOD, APP_HOME);
};
