﻿    var mobj_HttpRequest                    = null;
    var mbol_debugging                      = false;
    var mobj_debugWindow                    = null;
    var mint_lastDebugID                    = 1;
    var mint_executeRetryAttempts           = 1;
    var mint_maxExecuteRetryAttempts        = 10;
    var mobj_callbacks                      = new Array();

    function QueryString(val)
    {
        var str_url = window.location.href;
        var str_a = str_url.split('?');
        if(str_a.length <= 1)
            return null;
        
        var str_queryStrings = str_a[1];
        var str_variables = str_a[1].split('&');
        
        for(i=0;i<str_variables.length;i++)
        {
            var str_fields = str_variables[i].split('=');
            if(str_fields[0] == val)
                return str_fields[1];
        }
    }
    function ParseVariable(text, val)
    {
        var str_a = text.split('?');
        if(str_a.length <= 1)
            return null;
        
        var str_queryStrings = str_a[1];
        var str_variables = str_a[1].split('&');
        
        for(i=0;i<str_variables.length;i++)
        {
            var str_fields = str_variables[i].split('=');
            if(str_fields[0] == val)
                return str_fields[1];
        }    
    }
    function uniqid()
    {
        var newDate = new Date;
        return newDate.getTime();
    }
    function InitAjax()
    {
        if(mbol_debugging && mobj_debugWindow == null)
            mobj_debugWindow = window.open ("", "mywindow", "location=1,status=1,scrollbars=1,width=500,height=300");
        return;
    }
    function debug_print(text)
    {
        if(!mbol_debugging)
            return;
        if(mobj_debugWindow == null)
        {
            alert('no debug window to output to.');
            return;
        }
        mobj_debugWindow.document.write(mint_lastDebugID+':) '+text+'<br>');    
        mint_lastDebugID++;
    }
    function GetXmlHttpObject()
    {
        if (window.XMLHttpRequest)
          {
          // code for IE7+, Firefox, Chrome, Opera, Safari
          return new XMLHttpRequest();
          }
        if (window.ActiveXObject)
          {
          // code for IE6, IE5
          return new ActiveXObject("Microsoft.XMLHTTP");
          }
        return null;
    }
    function ExecuteQuery(callback, query)
    {
        debug_print("ExecuteQuery.Begin - "+query);
        if(mobj_HttpRequest != null && mobj_HttpRequest.readyState != 4)
        {
            mint_executeRetryAttempts++;
            if(mint_executeRetryAttempts > mint_maxExecuteRetryAttempts)
            {
                debug_print('Failed after '+mint_executeRetryAttempts+' to retry query.... Giving up.');
                return;
            }
            else
            {
                self.setTimeout(function() {ExecuteQuery(callback, query);}, 1000);
                debug_print('XMLHttpRequest was not null or may be in use.  Retrying.');
                return;
            }            
        }
        mobj_HttpRequest = GetXmlHttpObject();
            
        if(mobj_HttpRequest == null)
        {
            alert('Failed to initialize XMLHttpRequest.  AJAX will not be used.');
            return;
        }
       
        var id = Math.random();
        var url = query+'&hash='+id;

        // functionid, function, query, id, output
        mobj_callbacks.push([id, callback, query]);

        mobj_HttpRequest.onreadystatechange=ReadyStateChanged;
        mobj_HttpRequest.open("GET",url,true);
        mobj_HttpRequest.send(null);        
        debug_print("ExecuteQuery.End - "+url);
    }
    
    function ReadyStateChanged()
    {
        if(mobj_HttpRequest.readyState == 4)
        {
            var str_output = mobj_HttpRequest.responseText;
            var str_id = mobj_HttpRequest.getResponseHeader('hash');
            
            // find our request we sent and notify the callback method were done
            for(var i=0; i<mobj_callbacks.length; i++)
            {
                if(mobj_callbacks[i][0] == str_id)
                {
                    //mobj_callbacks[i][1](str_query, str_queryid, str_output);
                    //mobj_callbacks[i][1](mobj_callbacks[i][2], str_output);
                    //mobj_callbacks[i][1](mobj_callbacks[i][2], mobj_HttpRequest);
                    mobj_callbacks[i][1](mobj_HttpRequest);
                    mobj_callbacks.slice(i,i);
                    return;
                }
            }
            //debug_print('No callback method was found for query ('+str_query+') with id ('+str_id+')');
            debug_print('output:'+str_output);
            debug_print('headers:'+mobj_HttpRequest.getAllResponseHeaders());
            return;
        }
    }
function UpdateTimeStamp_Callback(httpRequest)
{
    debug_print("UpdateTimeStamp_Callback:Begin - " + httpRequest.responseText);
    //alert("UpdateTimeStamp_Callback:Begin - " + httpRequest.responseText);
}
function UpdateTimeStamp(username)
{
    var url = '/query.php?username=' + username + '&uniqueid=' + uniqid();
    debug_print("UpdateTimeStamp(" + username + ")");
    debug_print(url);
    ExecuteQuery(UpdateTimeStamp_Callback, url);
    //alert(username);
    self.setTimeout(function() {UpdateTimeStamp(username);}, 30000);
}
    InitAjax();