// Ajax for torrent info

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		//Display your error message here. 
		//and inform the user they might want to upgrade
		//their browser.
		//alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
	}
}


//Get our browser specific XmlHttpRequest object.
var receiveReq = null;

//Initiate the asyncronous request.
function getTorrentInfo(torrent,span_id) {
	//If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
	if(receiveReq == null)
		receiveReq = getXmlHttpRequestObject();
	if(receiveReq == null)
		return; // failed to create object

	// show activity
	document.getElementById(span_id).innerHTML = '<img style="padding-left:7px" src="/images/indicator_tiny_red.gif" alt="" title="Fetching torrent info..." />';

	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		//Setup the connection as a GET call
		//True explicity sets the request to asyncronous (default).
		receiveReq.open("GET", '/torrent-info-ajax.php?torrent='+torrent, true);
		//Set the function that will be called when the XmlHttpRequest objects state changes.
		receiveReq.onreadystatechange = function () {
			if (receiveReq.readyState == 4) {
				showTorrentInfo(span_id);
			}
		};		
		//Make the actual request.
		receiveReq.send(null);
	}
	else
	{
		// try again later
		// NOTE: other version is to create the receivereq locally and pass it to showTorrentInfo
		setTimeout("getTorrentInfo('"+torrent+"','"+span_id+"')",500);
	}
}

function showTorrentInfo(span_id)
{
	document.getElementById(span_id).innerHTML = receiveReq.responseText;
}
