var receiveReq = getXmlHttpRequestObject();
var receiveTotalBlogPostsReq = getXmlHttpRequestObject();
var displayID = "";
var displayTotalBlogPostsID = "";
var currentTwinglyFunction = 0;

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function getTwinglyData(webAddress, reqDisplayID, evtFunctionName) {
	if (reqDisplayID.length>0) {
		displayID = reqDisplayID;
	}

	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", webAddress, true);
		receiveReq.onreadystatechange = eval(evtFunctionName);
		receiveReq.send(null);
	}
}

function getTwinglyTotalBlogPostsData(webAddress, reqDisplayID, evtFunctionName) {
	if (reqDisplayID.length>0) {
		displayTotalBlogPostsID = reqDisplayID;
	}

	if (receiveTotalBlogPostsReq.readyState == 4 || receiveTotalBlogPostsReq.readyState == 0) {
		receiveTotalBlogPostsReq.open("GET", webAddress, true);
		receiveTotalBlogPostsReq.onreadystatechange = eval(evtFunctionName);
		receiveTotalBlogPostsReq.send(null);
	}
}

function handleReceivedTotalCounterFeed() {
	var totalResponses = 0;
	if (receiveReq.readyState == 4) {
		//Get a reference to the container div for easy access
		var counter_div = document.getElementById(displayID);
		//Get the AJAX response and run the JavaScript evaluation function
		//on it to turn it into a usable object.  Notice since we are passing
		//in the JSON value as a string we need to wrap it in parentheses
		var response = eval("(" + receiveReq.responseText + ")");
		
		if (parseInt(response.totalFound)>5000) {
			totalResponses = 5000;
		}
		else {
			totalResponses = response.totalFound;
		}
		
		if (counter_div) {
			counter_div.innerHTML = totalResponses;
			
			document.getElementById("donated").innerHTML=formatCurrency((totalResponses*20));
		}
	}
}

function handleReceivedTotalCounterFeed_Sergey() {
	var totalResponses = 0;
	if (receiveReq.readyState == 4) {
		//Get a reference to the container div for easy access
		var counter_div = document.getElementById(displayID);
		//Get the AJAX response and run the JavaScript evaluation function
		//on it to turn it into a usable object.  Notice since we are passing
		//in the JSON value as a string we need to wrap it in parentheses
		
		var tmpResponseText="";
		tmpResponseText = receiveReq.responseText;
		tmpResponseText = tmpResponseText.replace("SERGEYCHE.remoteloader.callback('", "");
		tmpResponseText = tmpResponseText.replace("','tw_ubc');", "");
		
		var response = eval("(" + tmpResponseText + ")");
		
		if (parseInt(response.uniqueCount)>5000) {
			totalResponses = 5000;
		}
		else {
			totalResponses = response.uniqueCount;
		}
		
		counter_div.innerHTML = totalResponses;
		
		document.getElementById("donated").innerHTML=formatCurrency((totalResponses*20));
	}
	
	calculateTotalCommentsPosts();
}

function handleReceivedTotalBlogEntriesCounter_Sergey() {
	var totalResponses = 0;
	if (receiveTotalBlogPostsReq.readyState == 4) {
		//Get a reference to the container div for easy access
		var counter_div = document.getElementById(displayTotalBlogPostsID);
		//Get the AJAX response and run the JavaScript evaluation function
		//on it to turn it into a usable object.  Notice since we are passing
		//in the JSON value as a string we need to wrap it in parentheses
		
		var tmpResponseText="";
		tmpResponseText = receiveTotalBlogPostsReq.responseText;
		tmpResponseText = tmpResponseText.replace("SERGEYCHE.remoteloader.callback('", "");
		tmpResponseText = tmpResponseText.replace("','tw_ubc');", "");
		
		var response = eval("(" + tmpResponseText + ")");
		
		if (parseInt(response.uniqueCount)>5000) {
			totalResponses = 5000;
		}
		else {
			totalResponses = response.uniqueCount;
		}
		
		counter_div.innerHTML = totalResponses;
	}
}

function handleReceivedPostCounterFeed() {
	if (receiveReq.readyState == 4) {
		//Get a reference to the container div for easy access
		var counter_div = document.getElementById(displayID);
		//Get the AJAX response and run the JavaScript evaluation function
		//on it to turn it into a usable object.  Notice since we are passing
		//in the JSON value as a string we need to wrap it in parentheses
		var response = eval("(" + receiveReq.responseText + ")");
		counter_div.innerHTML = response.totalFound;
		currentTwinglyFunction++;
		
		if (twinglyFunctionArray!=null) {
			if ((currentTwinglyFunction+1)<=twinglyFunctionArray.length) {
				twinglyFunctionArray[currentTwinglyFunction]();
			}
		}
	}
}

function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + dblValue + '.' + strCents);
}

