{include file="$gentemplates/$header_tpl"}
<h2>{$lang.answers.menu.answer}</h2>
{include file="$gentemplates/answ_menu.tpl"}

{if $err}
<p class="error_msg" style="padding-top: 20px;">{$err}</p>
{/if}
	
<p>
	<input type="button" value="{$lang.answers.menu.all_qs}" onclick="location.href='{$file_name}?sel=answer';" value="{$lang.answers.menu.my_a}">
</p>
<p>
	<input type="radio" id="opened_qs" name="filter" {if $form.opened == '1'}checked="checked" {/if} onclick="q_page_num_obj = getById('q_page_num');	q_page_num_obj.value = '1'; refreshQuestions();" />&nbsp;{$lang.answers.opened_qs}&nbsp;
	<input type="radio" id="closed_qs" name="filter" {if $form.closed == '1'}checked="checked" {/if} onclick="q_page_num_obj = getById('q_page_num');	q_page_num_obj.value = '1'; refreshQuestions();" />&nbsp;{$lang.answers.closed_qs}
	<input type="hidden" value="0" name="ans_form" id="ans_form">
</p>
<p>
	{$lang.answers.categories}:
</p>
<p>
	<div id="cats0">
	<select name="cats" size="10" onchange="javascript: getSubCats(0, this.value);" style="width:250px;padding-{$default_left}:3px;">
		{foreach name=category from=$categories item=item}
		<option value="{$item.id}" {if $data.cats == $item.id}selected{/if}>{$item.name}</option>
		{/foreach}
	</select>
	</div>
</p>
<p>
	<div id="cats1">
	</div>
</p>

<div id="questions">
	{include file="$gentemplates/answ_quest_pattern.tpl"}
</div>

<div id='tmp' style="display:none;"></div>
<script type="text/javascript">
file_name = '{$file_name}';
tmp_text = '{$lang.ajax.tmp_text}';
{literal}

questions_obj = getById('questions');
all_qs_obj = getById('all_qs');
opened_qs_obj = getById('opened_qs');
closed_qs_obj = getById('closed_qs');

var show_hide = $("#ans_form").val();
if (show_hide != 0) viewAnswers(show_hide);
	
function getSubCats(parent_level, id_parent){
	str='sel=get_cats&parent_level='+parent_level+'&id_parent='+id_parent;
	destination_odj = getById('cats1');
	anisochronous = false;
	ajaxRequest(file_name, str, destination_odj, tmp_text, anisochronous);
	categs1_obj = getById('categs1');

	if (navigator.appName == 'Microsoft Internet Explorer'){
		categs1_obj.setAttribute("onchange", function() {refreshQuestions()});
	}else{
		categs1_obj.onchange = function(){refreshQuestions()};
	}
}

function refreshQuestions(flag){
	if (flag == '') flag = 0;
	if (opened_qs_obj.checked){
		f = 'opened';
	}
	if (closed_qs_obj.checked){
		f = 'closed';
	}
	if (!opened_qs_obj.checked && !closed_qs_obj.checked){
		f = 'all';
	}
	categs1 = getById('categs1');
	if (categs1 != null && categs1.value){
		id_parent = categs1.value;
	}else{
		id_parent='-1';
	}

	q_page_num_obj = getById('q_page_num');
	if (q_page_num_obj != null){
		page = q_page_num_obj.value;
	}else{
		page = '1';
	}
	str = 'sel=get_q&filter='+f+'&id_parent='+id_parent+'&page='+page;
	destination_odj = questions_obj;
	anisochronous = true;
	ajaxRequest(file_name, str, destination_odj, tmp_text, anisochronous);
	if (!flag){
		show_hide = $("#ans_form").val();
		if (show_hide != 0) viewAnswers(show_hide);
	}
}

function goToPage(page){
	q_page_num_obj = getById('q_page_num');
	q_page_num_obj.value = page;
	refreshQuestions();
}


function showAnswerField(id){
	add_answer_area_obj = getById('add_answer_area'+id);
	add_answer_area_obj.style.display = '';
	$('#text_answer'+id).redactor({
		checkBadWords: BadWords,
		lang: redactor_lang
	});
}

function answer(id_parent){
	text_answer_obj = getById('text_answer'+id_parent);

	value = text_answer_obj.value;
	str = 'sel=add_answer&id_parent='+id_parent+'&value='+value;
	destination_odj = getById('tmp');
	anisochronous = false;
	$.post(file_name, str, function(data){
        ShowAjaxError('', data);
    })

	$("#ans_form").val(id_parent);
	text_answer_obj.value='';
	refreshQuestions(1);
	/*show_hide = $("#ans_form").val();
	if (show_hide != 0) viewAnswers(show_hide);*/
}

function viewAnswers(id){
	refreshAnswers(id);
    $('#show_answers_'+id).hide();
    $('#hide_answers_'+id).show();
	$("#ans_form").val(id);
	/*$('#hide_link_'+id).show();
	$('#show_link_'+id).hide();
	$('#hide_image_'+id).show();
	$('#show_image_'+id).hide();*/
}

function refreshAnswers(id){
	tr_answer_obj = getById('tr_answer'+id);
	tr_answer_obj.style.display = '';
	answers_area_obj = getById('answers_area'+id);	
	answers_area_obj.style.display = '';

	str_adds = '';
	a_page_obj = getById('answer_page_num'+id);
	if(a_page_obj!=null){	
		str_adds +='&ans_page'+id+'='+a_page_obj.value;
	}else{
		str_adds +='&ans_page'+id+'=1';
	}
	
	str = 'sel=get_answers&id_parent='+id+str_adds;
	destination_odj = getById('answers_area'+id);
	anisochronous = true;
	ajaxRequest(file_name, str, destination_odj, tmp_text, anisochronous);
}


function goToAnswerPage(num,id){
	a_page_obj = getById('answer_page_num'+id);
	a_page_obj.value = num;
	refreshAnswers(id);
	/*$('#show_answers_'+id).hide();
    $('#hide_answers_'+id).show();*/
}

function makeBest(id, id_parent){
	str = 'sel=make_best&id='+id+'&id_parent='+id_parent;
	destination_odj = getById('tmp');
	anisochronous = false;
	ajaxRequest(file_name, str, destination_odj, tmp_text, anisochronous);
	goToAnswerPage('1', id_parent);
}

function cropAnswers(id){
    $('#show_answers_'+id).show();
    $('#hide_answers_'+id).hide();
	$("#ans_form").val('0');
	/*$('#hide_link_'+id).hide();
	$('#show_link_'+id).show();
	$('#hide_image_'+id).hide();
	$('#show_image_'+id).show();*/
	answers_area_obj = getById('answers_area'+id);
}

function getById(id){
	return document.getElementById(id);
}

{/literal}
</script>
{include file="$gentemplates/$footer_tpl"}