/*
=====================================================
 DLE Forum
-----------------------------------------------------
 http://dle-files.ru/
-----------------------------------------------------
 Copyright (c) 2008 DLE Files Group
=====================================================
 Данный код защищен авторскими правами
=====================================================
 Файл: dle_forum.js
=====================================================
*/

// ********************************************************************************
// POST EDIT
// ********************************************************************************	
function whenCompletedPostEdit(){

	var post_main_obj = document.getElementById( 'post-id-' + post_id );
	var post_box_top  = _get_obj_toppos( post_main_obj );

			if ( post_box_top )
			{
				scroll( 0, post_box_top - 70 );
			}

};

function ajax_post_edit( p_id )
{
	if ( ! c_cache[ p_id ] || c_cache[ p_id ] == '' )
	{
		c_cache[ p_id ] = document.getElementById( 'post-id-'+p_id ).innerHTML;
	}

	var ajax = new dle_ajax();
	post_id = p_id;
	ajax.onShow ('');
	var varsString = "";
	ajax.setVar("id", p_id);
	ajax.setVar("action", "edit");
	ajax.requestFile = dle_root + "ajax/editpost.php";
	ajax.method = 'GET';
	ajax.element = 'post-id-'+p_id;
	ajax.onCompletion = whenCompletedPostEdit;
	ajax.execute = true;
	ajax.sendAJAX(varsString);	
	return false;
};

function ajax_cancel_post_edit( p_id )
{
	if ( n_cache[ p_id ] != "" )
	{
		document.getElementById( 'post-id-'+p_id ).innerHTML = c_cache[ p_id ];
	}
	
	return false;
};

// ********************************************************************************
// POST SAVE
// ********************************************************************************	
function whenCompletedSavePost(){
c_cache[ comm_edit_id ] = '';
};

function ajax_save_post_edit( c_id, forum_wysiwyg )
{
	var ajax = new dle_ajax();
	var post_txt = '';
	
	comm_edit_id = c_id;
	ajax.onShow ('');
	
	if (forum_wysiwyg == "1")
	{
		post_txt = ajax.encodeVAR( tinyMCE.get('forum_post_'+c_id).getContent() );
	}
	else
	{
		post_txt = ajax.encodeVAR( document.getElementById('forum_post_'+c_id).value );
	}
	
	var varsString = "post_text=" + post_txt;
	ajax.setVar("id", c_id);
	ajax.setVar("action", "save");
	ajax.setVar("wysiwyg", forum_wysiwyg);
	ajax.requestFile = dle_root + "ajax/editpost.php";
	ajax.method = 'POST';
	ajax.element = 'post-id-'+c_id;
	ajax.onCompletion = whenCompletedSavePost;
	ajax.execute = true;
	ajax.sendAJAX(varsString);

	return false;
};

// ********************************************************************************
// POST ADD
// ********************************************************************************	
function doAddPost(){

	var form = document.getElementById('forum-post-form');
    var post_ajax = new dle_ajax();
    
    if (forum_wysiwyg == "1")
	{
		form.post_text.value = tinyMCE.get('post_text').getContent();
		post_ajax.setVar("editor_mode", 'wysiwyg');
	}
	else
	{
		closeall();
	}
	
	if (form.post_text.value == '' || form.post_text.value == '')
	{
		alert ( dle_req_field );
		return false;
	}

	post_ajax.onShow ('');
	var varsString = "topic_id=" + form.topic_id.value;
	
	post_ajax.setVar("forum_id", post_ajax.encodeVAR(form.forum_id.value));
	post_ajax.setVar("topic_title", post_ajax.encodeVAR(form.topic_title.value));
	post_ajax.setVar("post_id", post_ajax.encodeVAR(form.post_id.value));
	
	post_ajax.setVar("post_text", post_ajax.encodeVAR(form.post_text.value));
	post_ajax.setVar("name", post_ajax.encodeVAR(form.name.value));
	post_ajax.setVar("mail", post_ajax.encodeVAR(form.mail.value));
	post_ajax.setVar("skin", dle_skin);

	if ( form.sec_code ) {

	   post_ajax.setVar("sec_code", form.sec_code.value);

    }

	post_ajax.requestFile = dle_root + "ajax/addpost.php";
	post_ajax.method = 'POST';
	post_ajax.add_html = true;
	post_ajax.execute = true;
	post_ajax.element = 'ajax-post';
	post_ajax.sendAJAX(varsString);
};

// ********************************************************************************
// POST DEL
// ********************************************************************************	
function postDelete(url){

    var agree=confirm( 'Вы действительно хотите удалить это сообщение?' );

    if (agree)
    document.location=url;
};

// ********************************************************************************
// TOPIC DEL
// ********************************************************************************	
function topicDelete(url){

    var agree=confirm( 'Вы действительно хотите удалить эту тему?' );

    if (agree)
    document.location=url;
};

function rowDelete(url){

    var agree=confirm( 'Вы действительно хотите удалить эту запись?' );

    if (agree)
    document.location=url;
};

// ********************************************************************************
// TOPIC MENU
// ********************************************************************************	
function TopicMenu( tid, forum_url ){

var menu=new Array();

menu[0]='<a href="' + forum_url + 'act=subscription&code=add&tid=' + tid + '">Подписка на тему</a>';

menu[1]='<a href="' + forum_url + 'act=_topic&code=forward&tid=' + tid + '">Сообщить другу</a>';

menu[2]='<a href="' + forum_url + 'act=_topic&code=print&tid=' + tid + '">Версия для печати</a>';

return menu;
};

// ********************************************************************************
// FORUM MENU
// ********************************************************************************	
function ForumMenu( fid, moderation, forum_url ){

var menu=new Array();

if (moderation){

menu[0]='<a href="' + forum_url + 'showforum=' + fid + '&code=hidden">Показать все скрытые темы</a>';

menu[1]='<a href="' + forum_url + 'act=getforum&code=calc&fid=' + fid + '">Пересчитать все в форуме</a>';

}

menu[3]='<a href="' + forum_url + 'showforum=' + fid + '&code=today">Активные темы</a>';

menu[4]='<a href="' + forum_url + 'showforum=' + fid + '&code=noreply">Темы без ответов</a>';

return menu;
};

// ********************************************************************************
// POST EDIT MENU
// ********************************************************************************	
function PostEditMenu( pid, forum_url, page, post_n ){

var menu=new Array();

menu[0]='<a onclick="ajax_post_edit(\'' + pid + '\'); return false;" href="#">' + menu_short + '</a>';
menu[1]='<a href="' + forum_url + 'act=post&code=02&pid=' + pid + '&p=' + page + '&pn=' + post_n + '">' + menu_full + '</a>';

return menu;
};

// ********************************************************************************
// USER MENU
// ********************************************************************************	
function FUserMenu( url, m_id, group, forum_url ){

var menu=new Array();

	menu[0]='<a href="' + dle_root + 'index.php?subaction=userinfo&user=' + url + '">' + menu_profile + '</a>';

	menu[1]='<a href="' + dle_root + 'index.php?do=pm&doaction=newpm&user=' + m_id + '">' + menu_send + '</a>';
	menu[2]='<a href="' + forum_url + 'act=getforum&code=user&mname=' + url + '">Найти темы пользователя</a>';

	if (group == '1') {
		menu[3]='<a onclick="window.open(\'' + dle_root + dle_admin + '?mod=editusers&action=edituser&id=' + m_id + '\', \'User\',\'toolbar=0,location=0,status=0, left=0, top=0, menubar=0,scrollbars=yes,resizable=0,width=540,height=500\'); return false;" href="#">' + menu_uedit + '</a>';
	}

return menu;
};

// ********************************************************************************
// WARN ADD
// ********************************************************************************	
function FWarn(action, user_id, post_id, forum_id)
{
	var cause = prompt("Введите причину", "");
	
	if (cause)
	{
		var ajax = new dle_ajax();
		ajax.onShow ('');
		var varsString = "user_id=" + user_id;
		ajax.setVar("skin", dle_skin);
		ajax.setVar("action", action);
		ajax.setVar("forum_id", forum_id);
		ajax.setVar("post_id", post_id);
		ajax.setVar("cause", ajax.encodeVAR(cause));
		ajax.requestFile = dle_root + "ajax/warn.php";
		ajax.method = 'POST';
		ajax.element = 'warn-' + post_id;
		ajax.sendAJAX(varsString);
	}

	return false;
};

// ********************************************************************************
// REPUTATION
// ********************************************************************************	
function FRep(action, user_id, post_id, forum_id)
{
	var cause = prompt("Введите причину", "");
	
	if (cause)
	{
		var ajax = new dle_ajax();
		ajax.onShow ('');
		var varsString = "user_id=" + user_id;
		ajax.setVar("skin", dle_skin);
		ajax.setVar("action", action);
		ajax.setVar("forum_id", forum_id);
		ajax.setVar("post_id", post_id);
		ajax.setVar("cause", ajax.encodeVAR(cause));
		ajax.requestFile = dle_root + "ajax/reputation.php";
		ajax.method = 'POST';
		ajax.element = 'rep-' + post_id;
		ajax.sendAJAX(varsString);
	}

	return false;
};

// ********************************************************************************
// NAVIGATION
// ********************************************************************************	
function navigation(pages_count, url)
{
	var page = prompt("Перейти к странице", "");
	
	if (page)
	{
		if (pages_count >= page)
		{
			window.location.href = url + page;
		}
	}
};

function PostLink(link)
{
	url = window.location;
	var enterCause = prompt("Копирование прямой ссылки", url + "#post-" + link);
};

// ********************************************************************************
// SELECT ID
// ********************************************************************************	
function select_id( sid )
{	
	var saved = new Array();
	var clean = new Array();
	var add   = 1;
	
	tmp = document.modform.selected_id.value;
	
	if( tmp != "" )
	{
		saved = tmp.split(",");
	}
	
	for( i = 0 ; i < saved.length; i++ )
	{
		if ( saved[i] != "" )
		{
			if ( saved[i] == sid )
			{
				 add = 0;
			}
			else
			{
				clean[clean.length] = saved[i];
			}
		}
	}
	
	if ( add )
	{
		clean[ clean.length ] = sid;
	}
	
	newvalue = clean.join(',');
	
	document.modform.selected_id.value = newvalue;	
};

// ********************************************************************************
// SHOW HIDE
// ********************************************************************************	
function ShowHide( name, open ){
	
	if (document.getElementById( name ).style.display != "none")
	{
		document.getElementById( name ).style.display = "none";
	}
	else
	{
		document.getElementById( name ).style.display = "";
	}
	
	if (open)
	{
		document.getElementById( name ).style.display = "";
	}
};

// ********************************************************************************
// POST PREVIEW
// ********************************************************************************	
function PostPreviewCompleted()
{
	var post_main_obj = document.getElementById( 'post-preview' );
	var post_box_top  = _get_obj_toppos( post_main_obj );
	
	if ( post_box_top )
	{
		scroll( 0, post_box_top - 70 );
	}
};

function PostPreview()
{
	var ajax = new dle_ajax();
	var varsString = "";
	
	var post_text = "";
	
	if (forum_wysiwyg == "1")
	{
		post_text = tinyMCE.get('post_text').getContent();
		
		ajax.setVar("editor_mode", 'wysiwyg');
	}
	else
	{
		post_text = document.getElementById('forum-post-form').post_text.value;
	}
	
	if (post_text == '')
	{
		alert('Заполните все необходимые поля');return false
	}
	
	ajax.onShow ('');
	ajax.setVar("post_text", ajax.encodeVAR(post_text));
	ajax.setVar("wysiwyg", forum_wysiwyg);
	ajax.setVar("skin", dle_skin);
	ajax.requestFile = dle_root + "ajax/post.preview.php";
	ajax.method = 'POST';
	ajax.element = 'post-preview';
	ajax.onCompletion = PostPreviewCompleted;
	ajax.sendAJAX(varsString);

};

// ********************************************************************************
// FORUM INS
// ********************************************************************************	
function forum_ins(name)
{
	var input=document.getElementById('forum-post-form').post_text;
	var finalhtml = "";
	
	if (forum_wysiwyg !== "1")
	{
		if (dle_txt!= "")
		{
			input.value += dle_txt;
		}
		
		else
		{
			input.value += "[b]"+name+"[/b],"+"\n";
		}
	}
	
	else
	{
		if (dle_txt!= "")
		{
			finalhtml = dle_txt;
		}
		
		else
		{
			finalhtml = "<b>"+name+"</b>,"+"<br />";
		}
		
		tinyMCE.execInstanceCommand('post_text', 'mceInsertContent', false, finalhtml, true);
	}
};

// ********************************************************************************
// Copyright (c) 2008 DLE Files Group
// ********************************************************************************	