
var showCatRequestInProgress = 0;

function showCat(sid, inType)
{
	if (showCatRequestInProgress == 0)
	{
		showCatRequestInProgress = 1;
		sendHTTPRequest(catAjaxResponse, "ajax/show_categories.php?s=" + sid + "&type=" + inType);
		var elem = document.getElementById('ajax_tag_data');
		elem.innerHTML = "<div style='padding:20px; text-align:center;'>Loading...</div>";
	}
}

function catAjaxResponse(request)
{
	var resp = request.responseText;

	var elem = document.getElementById('ajax_tag_data');

	elem.innerHTML = resp;

	showCatRequestInProgress = 0;
}



var catRequests = new Array();

function changeCatRating(sid, cat, num, vote)
{
	var val2 = generateValueToSend(cat);
	if (catRequests[num] !== undefined && catRequests[num].active == 1)
	{
		return;
	}
	catRequests[num] = { active: 1, sid: sid, cat: cat };
	sendHTTPRequest(catRatingResponse, "ajax/update_series_categories.php?s=" + sid + "&c=" + val2 + "&v=" + vote + "&id=" + num);
	var elem = document.getElementById('cat_row_cancel_id_' + num);
	elem.innerHTML = "Please Wait...";
}

function catRatingResponse(request)
{
	var resp = request.responseText;

	var colonPos1 = resp.indexOf(':');

	if (!colonPos1 || colonPos1 == -1)
	{
		return;
	}

	var idString = resp.substring(0,colonPos1);
	var respString = resp.substring(colonPos1 + 1);

	var respInt = parseInt(respString);

	var myRequest = catRequests[idString];

	var elem = document.getElementById('cat_row_cancel_id_' + idString);

	myRequest.cat = myRequest.cat.replace("'", "\\'");

	if (respInt == 0)
	{
		elem.innerHTML = "&nbsp;&nbsp;You <u>agreed</u> with this.&nbsp;<a href=\"javascript:changeCatRating("+myRequest.sid+", '"+myRequest.cat+"',"+idString+",2)\"><img src=\"images/cat_disagree.gif\" border=\"\" alt=\"Undo\"/> Undo</a>";
	}
	else if (respInt == 1)
	{
		elem.innerHTML = "&nbsp;&nbsp;You <b>disagreed</b> with this.&nbsp;<a href=\"javascript:changeCatRating("+myRequest.sid+", '"+myRequest.cat+"',"+idString+",2)\"><img src=\"images/cat_disagree.gif\" border=\"\" alt=\"Undo\"/> Undo</a>";

	}
	else
	{
		elem.innerHTML = "&nbsp;&nbsp;<a href=\"javascript:changeCatRating("+myRequest.sid+", '"+myRequest.cat+"',"+idString+",0)\"><img src=\"images/cat_agree.gif\" border=\"\" alt=\"Agree\"/> Agree</a>&nbsp;<a href=\"javascript:changeCatRating("+myRequest.sid+", '"+myRequest.cat+"',"+idString+",1)\"><img src=\"images/cat_disagree.gif\" border=\"\" alt=\"Disagree\"/> Disagree</a>";
	}
	
	catRequests[idString].active = 0;

}

function generateValueToSend(stringToSend)
{
	var val1 = escape(stringToSend);
	var val2 = "";
	var pos = 0;
	var loc = 0;
	while ((loc = val1.indexOf('+', pos)) != -1)
	{
		val2 += val1.substring(pos, loc) + "%2B";
		pos = loc + 1;
	}
	val2 += val1.substring(pos);
	return val2;
}
