var requestedArray = new Array();
var statCache = new Array();
var statCacheEmpty = new Array();

function loadAjaxStatByStatNo(_No)
{
    if(requestedArray[_No] == 'undefined')
    {
        requestedArray[_No] = false;
    }

    var statNameS = getStatNameById(_No);    
    
    if(statNameS != '')
    {
        var ajaxRequest;  // The variable that makes Ajax possible!
        
        try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
        
    	// Create a function that will receive data sent from the server
    	ajaxRequest.onreadystatechange = function(){
    		if(ajaxRequest.readyState == 4){
                var ajaxDisplay = document.getElementById(statNameS);
                statCacheEmpty[_No] = ajaxDisplay.innerHTML;
                ajaxDisplay.innerHTML = ajaxRequest.responseText;
                statCache[_No] = ajaxRequest.responseText;
                requestedArray[_No] = !requestedArray[_No];
    		}
    	}
        
        if(requestedArray[_No] != 'undefined'
            && requestedArray[_No] != null)
        {
            if(requestedArray[_No])
            {
                var ajaxDisplay = document.getElementById(statNameS);
                ajaxDisplay.innerHTML = statCacheEmpty[_No];
                requestedArray[_No] = !requestedArray[_No];
                return;
            }
            
            requestedArray[_No] = !requestedArray[_No];
            
            if(statCache[_No] != null)
            {
                var ajaxDisplay = document.getElementById(statNameS);
        			ajaxDisplay.innerHTML = statCache[_No];
                return;
            }    	
        }
    	
        ajaxRequest.open("GET", statsFile + "?stat_id=" + _No, true);
        
    	ajaxRequest.send(null);               
    }
}

function getStatNameById(id)
{
    switch(id){
    case 0:
        return 'ajaxGetLatestTopics';
        break;
    case 1:
        return 'ajaxGetLastActiveUsers';
        break;
    case 2:
        return 'ajaxGetTopPosters';
        break;
    case 3:
        return 'ajaxGetMostViewedTopics';
        break;
    case 4:
        return 'ajaxGetMostActiveTopics';
        break;
    case 5:
        return 'ajaxGetPostsByMonth';
        break;
    case 6:
        return 'ajaxGetTopicsByMonth';
        break;
    case 7:
        return 'ajaxGetUsersByMonth';
        break;
    default: return '';
    }    
}