function hideCheatReplyForm(form_id) 
{
	var div_id = "div_" + form_id;
	toggleVisibility(div_id, false);
}

function printCheatReplyForm(form_id, cheat_parent_id) 
{
	var div_id = "div_" + form_id;
	var reply_cheat_form = "cheat_form" + form_id;

	var innerHTMLContent = '\
			<form class="cheatform" name="' + reply_cheat_form + '" id="' + reply_cheat_form + '" onSubmit="return false;" >\
			<input type="hidden" name="media_id" value="' + media_id + '">\
			<input type="hidden" name="add_cheat" value="">\
			<input type="hidden" name="form_id" value="' + reply_cheat_form + '">\
			<input type="hidden" name="reply_parent_id" value="' + cheat_parent_id + '">\
			<textarea name="cheat" style="width:450px;height:70px;"></textarea>\
			<br/>\
			<div style="float:left;clear:left">\
<input align="left" type="button" class="mini_more" name="add_cheat_button" value="Submit" onclick="postCheat(\'' +  reply_cheat_form + '\');">\
			</div>\
			</form>';
	setInnerHTML(div_id, innerHTMLContent);
}

var postCheatSuccess = function(t) 
{		
	response_str = t.responseText;
	response_code = response_str.substr(0, response_str.indexOf(" "));
	form_id = response_str.substr(response_str.indexOf(" ")+1);

	var form = document.forms[form_id];
	var dstDiv = form.add_cheat_button;
	var cheatDiv = form.cheat;
	
	if (response_code == "OK") 
	{
		setInnerHTML('div_main_cheat', "<div id='message-results' style='width:698px'>Thank you for posting help!</div>");
		fetchCheats(media_id, '1');
	}
	else
	{
		if(response_code == "NOPERM") 
		{
			alert("You do not have permission to post help!");
			dstDiv.disabled = false;
		}
		else if(response_code == "TOOSHORT") 
		{
			alert("The help you have entered is too short. Please add more and try again");
			dstDiv.disabled = false;
			cheatDiv.disabled = false;
			cheatDiv.focus();
		}
		else 
		{
			dstDiv.disabled = false;
		}
	
		dstDiv.value = "Post Help";
	}
}

function postCheat(cheat_form_id) 
{
	/*if(!is_logged_in)
		return alert("You must login to post a cheat!");*/

	var form = document.forms[cheat_form_id];
		
	if (checkCheat(form, cheat_form_id)) 
	{
		var add_button = form.add_cheat_button;
		add_button.value = "Adding help...";
		form.cheat.disabled = true;
		add_button.disabled = true;
		
		cheat_escaped = encodeURIComponent(document.forms[cheat_form_id].cheat.value);
		
		var url = 'http://www.gamesbutler.com/common/ajax/media_add_cheat.php';
		//var pars = Form.serialize($(cheat_form_id)) + '&cheat=' + cheat_escaped;
		var pars = $(cheat_form_id).serialize() + '&cheat=' + cheat_escaped;
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onSuccess: postCheatSuccess });
	} 
}

function checkCheat(cheat_form, cheat_form_id)
{
	var cheat = cheat_form.cheat;
	var cheat_button = cheat_form.cheat_button;
	
	if (cheat.value.length == 0 || cheat.value == null)
	{
		alert("You must enter help!");
		cheat.disabled = false;
		cheat.focus();
		return false;
	}

	if (cheat.value.length > 5000)
	{
		alert("Help must be shorter than 5,000 characters!");
		cheat.disabled = false;
		cheat.focus();
		return false;
	}
	
	return true;
}

function helpful(cheat_id, user_id) 
{
		var url = 'http://www.gamesbutler.com/common/ajax/cheat_helpful.php';
		var pars = 'cheat_id='+escape(cheat_id);
		var target = 'div_display_cheats';
		var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
		setTimeout("fetchCheats(media_id, '1')", 400);
}

function fetchCheats(file_id, page)
{
	var url = 'http://www.gamesbutler.com/common/ajax/media_load_cheats.php';
	var pars = 'file_id='+escape(file_id)+'&p='+escape(page);
	var target = 'div_display_cheats';
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

