function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

// Get the HTTP Object
function getHTTPObject(){
   if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
   else if (window.XMLHttpRequest) return new XMLHttpRequest();
   else {
      alert("Your browser does not support AJAX.");
      return null;
   }
}   
 
// Change the value of the outputText field
function setOutput(){
 	if(httpObject.readyState == 4){
 		var parsed = httpObject.responseText.split('|');
        document.getElementById('outputText1').value = addCommas(parsed[0])+" right now | "+addCommas(parsed[1])+" today | "+addCommas(parsed[2])+" this year";
    }
 
}

function setOutputTopicRefresh(){
 	if(httpObjectTopic.readyState == 4){
 	 	var cookie_topicid = Get_Cookie('14erscom_homelastpostid');
        if (cookie_topicid != httpObjectTopic.responseText) {
         document.getElementById('topicrefresh_icon').innerHTML = '<img src="../images/icon_refresh_tiny2.gif" style="width:12px;height:12px;border:0px;">';
         $("#topicrefresh_icon").effect( "pulsate", {times:500}, 3000 );
		}
    }
 
}

function checkLastTopic(){    
 	var randomnumber1=Math.floor(Math.random()*1000001)
	var randomnumber2=Math.floor(Math.random()*1000001)
	var randomnumber3=Math.floor(Math.random()*1000001)

    httpObjectTopic = getHTTPObject();
    if (httpObjectTopic != null) {
     	var cookie_currenttab1 = Get_Cookie('14erscom_hometab1');
        httpObjectTopic.open("GET", "./php14ers/ajax_homelasttopic.php?listtype="+cookie_currenttab1+"&nocash="+randomnumber1+randomnumber2+randomnumber3, true);
        httpObjectTopic.send(null); 
        httpObjectTopic.onreadystatechange = setOutputTopicRefresh;
    }
} 

// Implement business logic    
function updateStats(){    
 	var randomnumber1=Math.floor(Math.random()*1000001)
	var randomnumber2=Math.floor(Math.random()*1000001)
	var randomnumber3=Math.floor(Math.random()*1000001)

    httpObject = getHTTPObject();
    if (httpObject != null) {
        httpObject.open("GET", "./php14ers/visitors_return.php"+"?nocash="+randomnumber1+randomnumber2+randomnumber3, true);
        httpObject.send(null); 
        httpObject.onreadystatechange = setOutput;
    }
    setTimeout ( "updateStats()", 30000 );
    setTimeout ( "checkLastTopic()", 30000 );
}
 
var httpObject = null;
var httpObjectTopic = null;
