
function appinfo_getXmlHttp()
{
	var xmlhttp;
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Your browser does not support XMLHTTP!");
	}
	
	return xmlhttp;
}

/*
function appinfo_getByStub(stub, format, testing)
{
	var xmlhttp = appinfo_getXmlHttp();
	
	document.write("<div id=\"appinfo-" + stub + "\"></div>");
	
	xmlhttp.onreadystatechange =	function()
									{
										if (xmlhttp.readyState == 4)
										{
											var elem = document.getElementById("appinfo-" + stub);
											elem.innerHTML = xmlhttp.responseText;
										}
									};
	
	if (testing)
		xmlhttp.open("GET","http://forums.toucharcade.com/appinfotest_server.php?stub=" + stub + "&format=" + format,true);
	else
		xmlhttp.open("GET","http://forums.toucharcade.com/appinfo_server.php?stub=" + stub + "&format=" + format,true);
	
	xmlhttp.send(null);
}
*/

function appinfo_getById(id, format, testing)
{
	var xmlhttp = appinfo_getXmlHttp();
	
	document.write("<div id=\"appinfo-" + id + "\"></div>");

	xmlhttp.onreadystatechange =	function()
									{
										if (xmlhttp.readyState == 4)
										{
											var elem = document.getElementById("appinfo-" + id);
											elem.innerHTML = xmlhttp.responseText;
										}
									};
	if (testing)
		xmlhttp.open("GET","http://forums.toucharcade.com/appinfotest_server.php?id=" + id + "&format=" + format,true);
	else
		xmlhttp.open("GET","http://forums.toucharcade.com/appinfo_server.php?id=" + id + "&format=" + format,true);
	
	xmlhttp.send(null);
}

function appinfo_getByUrl(url, format, testing)
{
	var match = url.match(/id(\d{9})/);

	if (!match)
	{
		var match = url.match(/id=(\d{9})/);
		id = match[1];
	}

	id = match[1];

	appinfo_getById(id, format, testing);
}

function appinfo_appshotshow(appleid, index)
{
	for (var i = 0; i < 100; i++)
	{
		var elem = document.getElementById("appshot_" + appleid + "_" + i);
		if (elem == null) break;
		
		if (i == index) elem.style.display = "inline";
		else elem.style.display = "none";
	}
	
	var caption = "Screenshot " + (index + 1) + " of " + i;
	elemcap = document.getElementById("appshotcaption_" + appleid);
	elemcap.innerHTML = caption;
}

function appinfo_appshotnext(appleid)
{
	var index = 0;
	
	for (var i = 0; i < 100; i++)
	{
		var elem = document.getElementById("appshot_" + appleid + "_" + i);
		if (elem == null) break;
		
		if (elem.style.display == "inline") index = i;
	}
	
	if (++index == i) index = 0;

	appinfo_appshotshow(appleid,index);
}

function appinfo_appshotprev(appleid)
{
	var index = 0;
	
	for (var i = 0; i < 100; i++)
	{
		var elem = document.getElementById("appshot_" + appleid + "_" + i);
		if (elem == null) break;
		
		if (elem.style.display == "inline") index = i;
	}
	
	if (--index == -1) index = i - 1;
	
	appinfo_appshotshow(appleid,index);
}

function appinfo_voter_mouseover(appleid, rating)
{
	var elem = document.getElementById("app_" + appleid + "_button_rating");
	elem.originalClassname = elem.className;

	elem.className = "score s" + rating;
}

function appinfo_voter_mouseout(appleid)
{
	var elem = document.getElementById("app_" + appleid + "_button_rating");
	elem.className = elem.originalClassname;
}

function appinfo_appRate(appleid, rating)
{
	var elemButtonRate = document.getElementById("app_" + appleid + "_button_rating");
	
	var newClassName = "score s" + rating;
	
	if (newClassName == elemButtonRate.originalClassname)
	{
		elemButtonRate.originalClassname = "score s0";
		rating = "";
	}
	else
		elemButtonRate.originalClassname = newClassName;
	
	// record in database
	var xmlHttp = appinfo_getXmlHttp();
	xmlHttp.open("GET","/appinfo_appRateAjax.php?appleid=" + appleid + "&rating=" + rating, true);
	xmlHttp.send(null);
}

function showRatingLog(appleid, show)
{
	var elem = document.getElementById("app_" + appleid + "_ratingLog");
	elem.style.display = (show) ? "block" : "none";
}
