function incrementDownloadCounter(id, member_id) { 
	var xmlHttp = createRequest();
	var params = "video_id=" + id + "&member_id=" + member_id;
	var url="ajax_incrementDownloadCounter.php";
	xmlHttp.open("POST", url, false);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(params);
	results = xmlHttp.responseText.split("|");
	processDownloadCount(results[1], results[0]);
}

function incrementDownloadCounterAsync(id, member_id) { 
	
	var xmlHttp = createRequest();
	var params = "video_id=" + id + "&member_id=" + member_id;
	var url="ajax_incrementDownloadCounter.php";
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange= function() { getResponseFromIncrementDownloadCounter(xmlHttp, id, member_id) };
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(params);
}

function getResponseFromIncrementDownloadCounter(req, id, member_id){
	if(req.readyState == 4) {
		getDownloadCount(id, member_id);
	}
}

function getDownloadCount(id, member_id) {
	var xmlHttp = createRequest();
	var params = "video_id=" + id + "&member_id=" + member_id;
	var url="rpc/ajax/ajax_getDownloadCount.php";
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange= function() { onGetDownloadCount(xmlHttp, id) };
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(params);
}

function onGetDownloadCount(req, id){
	if(req.readyState == 4) {
		var numDownloads = Number(req.responseText);
		processDownloadCount(numDownloads, id)
	}
}

function processDownloadCount(numDownloads, id) {
	if (numDownloads >= 2) {
		tag = 'download_' + id;
		element = document.getElementsByName(tag)[0];
		element.style.display = "none";
	} else if (numDownloads == 1) {
		tag = 'dlicon_' + id;
		element = document.getElementsByName(tag)[0];
		element.src = "images/downloadred.gif";
	}
}

