function ajaxFunction() {
	var ajax;
	try {
		ajax = new XMLHttpRequest();
	} catch (e) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return ajax;
}

function thumbsRateGame(rating, id) {
	ajaxRate = ajaxFunction();
	ajaxRate.onreadystatechange = function () { thumbsRateGame_stateChanged(); };
	var url = baseDir+"php/rategame.php?r=" +rating+ "&id=" +id;
	
	ajaxRate.open("GET", url, true);
	ajaxRate.send(null);
}

function thumbsRateGame_stateChanged() { 
	if (ajaxRate.readyState == 4) {
		if (ajaxRate.responseText == 1) {
			alert("There was an error submitting your rating. Please try again.");
		} else if (ajaxRate.responseText == 2) {
			alert("An improper rating was sumitted. Please try again.");
		} else if (ajaxRate.responseText == 3) {
			alert("You must be logged in to rate games.");
		} else if (ajaxRate.responseText == 4) {
			alert("You have already rated this game.");
		} else {
			document.getElementById('gameRatingsBox').innerHTML = ajaxRate.responseText;
		}
	}
}

function favGame(id, uid, action, tag) {
	ajaxRate = ajaxFunction();
	ajaxRate.onreadystatechange = function () { favGame_stateChanged(id, uid, action, tag); };
	var url = baseDir+"php/favgame.php?id=" +id+ "&uid=" +uid+ "&a=" +action;
	
	ajaxRate.open("GET", url, true);
	ajaxRate.send(null);
}

function favGame_stateChanged(id, uid, action, tag) { 
	if (ajaxRate.readyState == 4) {
		if (ajaxRate.responseText == 1) {
			if (action == 1) {
				tag.onclick = function () { favGame(id, uid, 0, tag); };
				tag.innerHTML = "Remove from Favorites?";
			} else {
				if (isNaN(tag)) {
					tag.onclick = function () { favGame(id, uid, 1, tag); };
					tag.innerHTML = "Add to Favorites?";
				} else {
					//$("#profileFavGame_"+ tag).children().css("background-color", "#FF0000");
					$("#profileFavGame_"+ tag).fadeOut(800);
				}
			}	
		} else {
			alert(ajaxRate.responseText);
		}
	}
}
