//Funções do AJAX.
function baCreateXMLHTTPRequest()
{
	var xmlhttp = null;
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xmlhttp;
}

var baxmlhttp = baCreateXMLHTTPRequest();
var pid = null;
var cid = null;

function defineBestAnswer(postId, commentId, scriptdir)
{
	pid = postId;
	cid = commentId;
	if (!baxmlhttp)
	{
		baCreateXMLHTTPRequest();
	}
	baxmlhttp.onreadystatechange=baStateChange;
	baxmlhttp.open("GET", scriptdir + "baprocess.php?pid=" + postId + "&cid=" + commentId, true)
	baxmlhttp.send(null);
}

function baStateChange()
{
	if (baxmlhttp.readyState < 4)
	{
	}
	if (baxmlhttp.readyState == 4)
	{
		location.reload(true);
		var resp = baxmlhttp.responseText;
		if (resp == "BA_OK")
		{
			//$("div.bestanswer").fadeOut("slow", function() { innerHtmlTmp = document.getElementById("comment-" + cid).innerHTML; });
			//$("li#comment-" + cid).fadeOut("slow", function() { document.getElementById("ba-main").innerHTML = innerHtmlTmp; });
			//$("#question_1").fadeOut("slow");
			//$("#question_2").fadeOut("slow");
		}
		else if (resp == "BA_ERROR")
		{
		}
	}
}

//Funções de controle das forms.

var lastHide = "";


function ba_validateForm()
{
	if (SR_Clicked == 0)
	{
		$("span.rateLabel").html("&laquo; Voc&ecirc; esqueceu de avaliar a pergunta.");
		return false;
	}
	else
	{
		return true;
	}
}

function ba_showForm(pid,cid,scriptdir)
{
	//Cria o form.
	addBAForm(pid,cid,scriptdir);
	
	//Esconde todos os forms do BA.
	$(".ba_confirm").slideUp("slow");
	$(".ba_button_div").fadeIn("slow");

	//Mostra a form da resposta escolhida.
	$("div#ba_confirm_" + cid).slideToggle("slow");
	
	//Esconde o botão "Melhor resposta".
	$("div#ba_" + cid).fadeOut("slow");
}


function ba_hideForm(cid)
{
	$("div#ba_confirm_" + cid).slideToggle("slow", function() { delBAForm(cid); });	
	$("div#ba_" + cid).fadeIn("slow");
}


function addBAForm(pid,cid,scriptdir)
{
stopRS();
document.getElementById("ba_confirm_" + cid).innerHTML = '\
<div id="considerAnswer"> \
<label>Avaliar melhor resposta:</label><br /> \
<span class="rate s_star_0_0"> \
    <span class="star-off1">1</span> \
    <span class="star-off2">2</span> \
    <span class="star-off3">3</span> \
    <span class="star-off4">4</span> \
    <span class="star-off5">5</span> \
</span> \
<span class="rateLabel"></span> \
<form method="post" class="considerForm" action="' + scriptdir + 'baprocess.php" onsubmit="return ba_validateForm()"> \
<br /><label>Suas considera&ccedil;&otilde;es:</label> \
<textarea name="rating_consider" class="rating_consider"></textarea> \
 \
<button value="submit" type="submit">Confirmar</button> \
<button value="Cancelar" type="button" onClick="javascript:ba_hideForm(' + cid + ');">Cancelar</button> \
</p> \
<input type="hidden" name="pid" value="' + pid + '" /> \
<input type="hidden" name="cid" value="' + cid + '" /> \
<input type="hidden" class="SR_clicked" name="ba_rating" value="' + SR_Clicked + '" /> \
<input type="hidden" name="currenturl" value="' + window.location +'" /> \
</form> \
</div>\
';
startRS();
}

function delBAForm(cid)
{
	document.getElementById("ba_confirm_" + cid).innerHTML = "";
}
