function forum_ask(targetName, targetURL) {
	if (confirm (targetName)) {
		self.location.replace(targetURL);
	}
}

function create_theme_go()
{
	if (document.getElementById('new_theme'))
	{
		if (document.getElementById('new_theme').style.display=='block')
			document.getElementById('new_theme').style.display='none';
		else document.getElementById('new_theme').style.display='block';
			$(document).ready(function() { $("#comment_0").markItUp(mySettings); });	 		
	}
}

function surroundColor(color, id)
{
	surroundText('[color='+color+']', '[/color]', document.getElementById('comment_'+id)); 
	document.getElementById('comment_'+id).focus(document.getElementById('comment_'+id).caretPos);
	document.getElementById('color'+id).style.display='none';
	
}

function edit_form(id, text)
{
	var html = '<div>';
	
	html+='<input type=hidden name="used_st_id" value="" id="used_st_id">';
	html+='<div class="ico">';
	html+= '<a href="javascript:void(0);" onClick="surroundText(\'[b]\', \'[/b]\', document.getElementById(\'comment_'+id+'\')); return false;"><img src="'+forum_path+'/img/mes_ico_font-bold.gif" alt="Жирный" title="Жирный" border="0"/></a>';
	html+='<a href="javascript:void(0);" onclick="surroundText(\'[i]\', \'[/i]\', document.getElementById(\'comment_'+id+'\')); return false;"><img src="'+forum_path+'/img/mes_ico_font-italic.gif" alt="Курсив" title="Курсив" border="0"/></a>';
	html+='<a href="javascript:void(0);" onclick="surroundText(\'[u]\', \'[/u]\', document.getElementById(\'comment_'+id+'\')); return false;"><img src="'+forum_path+'/img/mes_ico_font-underline.gif" alt="Подчеркнутый" title="Подчеркнутый" border="0"/></a>';
	html+='<a href="javascript:void(0);" onclick="surroundText(\'[s]\', \'[/s]\', document.getElementById(\'comment_'+id+'\')); return false;"><img src="'+forum_path+'/img/mes_ico_font-deleted.gif" alt="Зачеркнутый" title="Зачеркнутый" border="0"/></a>';
	html+='<a href="javascript:void(0);" onclick="surroundText(\'[size=3pt]\', \'[/size]\', document.getElementById(\'comment_'+id+'\')); return false;"><img src="'+forum_path+'/img/mes_ico_font-size.gif" alt="Размер шрифта" title="Размер шрифта" border="0"/></a>';
	
	
	html+='<a href="javascript:void(0);" onClick="document.getElementById(\'color'+id+'\').style.display=\'block\';"><img src="'+forum_path+'/img/mes_ico_font-color.gif" alt="Цвет шрифта" title="Цвет шрифта" border="0"/></a>';
	//див с палитрой
	html+='<!-- note -->';
	html+='<div class="dynamic-element" style="display: none;" id="color'+id+'">';
	html+='		<div class="block dynamic-note color">';
	html+='			<div class="shadow">';
	html+='				<div class="note-body">';
	html+='					<div class="title">';
	html+='						<div class="w100">';
	html+='							<div class="text">Цвет шрифта</div>';
	html+='							<div class="note-close"><a href="javascript:void(0);" onClick="document.getElementById(\'color'+id+'\').style.display=\'none\';"><img src="/i/note-close.gif" width="7" height="7" border="0" alt="закрыть" title="закрыть"></a></div>';
	html+='							<div class="clear"></div>';
	html+='						</div>';
	html+='					</div><!-- title -->';
	html+='					<div class="content">';
	html+='						<div class="padding">';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'black\',\''+id+'\')" class="black" title="Черный"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'beige\',\''+id+'\')" class="beige" title="Бежевый"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'pink\',\''+id+'\')" class="pink" title="Розовый"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'red\',\''+id+'\')" class="red" title="Красный"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'maroon\',\''+id+'\')" class="maroon" title="Тёмно-красный"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'orange\',\''+id+'\')" class="orange" title="Ораньжевый"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'yellow\',\''+id+'\')" class="yellow" title="Жёлтый"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'limegreen\',\''+id+'\')" class="limegreen" title="Салатовый"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'green\',\''+id+'\')" class="green" title="Зелёный"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'teal\',\''+id+'\')" class="teal" title="Бирюзовый"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'blue\',\''+id+'\')" class="blue" title="Синий"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'purple\',\''+id+'\')" class="purple" title="Пурпурный"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'navy\',\''+id+'\')" class="navy" title="Фиолетовый"></a>';
	html+='<a href="javascript:void(0);" onClick="surroundColor(\'brown\',\''+id+'\')" class="brown" title="Коричневый"></a>';
	html+='							<div class="clear"></div>';
	html+='						</div><!-- padding -->';
	html+='					</div><!-- content -->';
	html+='				</div><!-- note-body -->';
	html+='			</div><!-- shadow -->';
	html+='		</div><!-- block dynamic-note -->';
	html+='</div><!-- dynamic-element -->';
	// див с палитрой конец
	/*html+='<select onchange="surroundText(\'[color=\' + this.options[this.selectedIndex].value.toLowerCase() + \']\', \'[/color]\', document.getElementById(\'comment_'+id+'\')); this.selectedIndex = 0; document.getElementById(\'comment_'+id+'\').focus(document.getElementById(\'comment_'+id+'\').caretPos);" style="margin-bottom:8px;">';
	html+='						<option value="" selected="selected">Цвет текста';
	html+='						<option value="Black" class="black">Черный</option>';
	html+='						<option value="Beige" class="beige">Бежевый</option>';
	html+='						<option value="Pink" class="pink">Розовый</option>';
	html+='						<option value="Red" class="red">Красный</option>';
	html+='						<option value="Maroon" class="maroon">Темно-красный</option>';
	html+='						<option value="Orange" class="orange">Оранжевый</option>';
	html+='						<option value="Yellow" class="yellow">Желтый</option>';
	html+='						<option value="LimeGreen" class="limegreen">Салатовый</option>';
	html+='						<option value="Green" class="green">Зеленый</option>';
	html+='						<option value="Teal" class="teal">Бирюзовый</option>';
	html+='						<option value="Blue" class="blue">Синий</option>';
	html+='						<option value="Purple" class="purple">Пурпурный</option>';
	html+='						<option value="Navy" class="navy">Фиолетовый</option>';
	html+='						<option value="Brown" class="brown">Коричневый</option>';
	html+='					</select>';
	*/
	
	html+='<a href="javascript:void(0);" onclick="surroundText(\'[img]\', \'[/img]\', document.getElementById(\'comment_'+id+'\')); return false;"><img src="'+forum_path+'/img/mes_ico_picture.gif" alt="Вставить изображение" title="Вставить изображение" border="0"/></a>';
	html+='<a href="javascript:void(0);" onclick="surroundText(\'[url=\' + prompt(\'URL\', \'\') + \']\', \'[/url]\', document.getElementById(\'comment_'+id+'\')); return false;"><img src="'+forum_path+'/img/mes_ico_link.gif" alt="Гиперссылка" title="Гиперссылка" border="0"/></a>';
	html+='<a href="javascript:void(0);" onclick="surroundText(\'[quote]\', \'[/quote]\', document.getElementById(\'comment_'+id+'\')); return false;"><img src="'+forum_path+'/img/mes_ico_quote.gif" alt="Цитата" title="Цитата" border="0"/></a>';
	if (typeof (statements_obj) != 'undefined')
 	{	// утверждения для evolution
 		var statements;
 		statements='<ul>';
 		for (var statement_element in statements_obj)
 		{
 			statements+='<li><a href=# onclick=document.getElementById(\\\'used_st_id\\\').value=document.getElementById(\\\'used_st_id\\\').value+\\\',\\\'+statements_obj['+statement_element+'][0];surroundText(statements_obj['+statement_element+'][1],\\\'_____________\\\',document.getElementById(\\\'comment_'+id+'\\\'))>'+statements_obj[statement_element][2]+'</a></li>';
 		}
 		statements+='</ul>';
		html+='<a href="javascript:void(0);"><img src="'+forum_path+'/img/mes_ico_statement.gif" alt="Добавить утверждение" title="Добавить утверждение" border="0" onClick="show_hint(\'Добавить утверждение\',\''+statements+'\',this,event);"/></a>';
 	}
	html+='</div>';
 	
 	html+='<a href="javascript:void(0);" onClick="document.getElementById(\'smile'+id+'\').style.display=\'block\';"><img src="'+forum_path+'/img/mes_ico_smile.gif" alt="Смайлики" title="Смайлики" border="0"/></a>';
	// див со смайлами
	html+='<div class="dynamic-element" style="display: none;" id="smile'+id+'">';
	html+='		<div class="block dynamic-note smile">';
	html+='			<div class="shadow">';
	html+='				<div class="note-body">';
	html+='					<div class="title">';
	html+='						<div class="w100">';
	html+='							<div class="text">Вставить смайлик</div>';
	html+='							<div class="note-close"><a href="javascript:void(0);" onClick="document.getElementById(\'smile'+id+'\').style.display=\'none\';"><img src="/i/note-close.gif" width="7" height="7" border="0" alt="закрыть" title="закрыть"></a></div>';
	html+='							<div class="clear"></div>';
	html+='						</div>';
	html+='					</div><!-- title -->';
	html+='					<div class="content">';
	html+='						<div class="padding menu-list">';

	html+='<table width="100" border="0" cellspacing="0" cellpadding="5">';
	html+='<tr align="center" valign="middle">';
	var smiles_array = get_smiles();
	var j = 0;

	for (i=0; i<smiles_array.length; i++)
	{
		if (j == 5) html+='</tr>';
		html+='<td><a href="javascript:emoticon(\''+smiles_array[i][0]+'\', \''+id+'\');"><img src="/forum/img/smiles/'+smiles_array[i][1]+'" border="0"></a></td>';
		j++;
		if (j == 5)
		{
			html+='<tr align="center" valign="middle">';
			j=0;
		}
	}
	html+='</table>';
	html+='						</div><!-- padding -->';
	html+='					</div><!-- content -->';
	html+='				</div><!-- note-body -->';
	html+='			</div><!-- shadow -->';
	html+='		</div><!-- block dynamic-note -->';
	html+='</div><!-- dynamic-element -->';
	// див со смайлами конец
	
	html+='					</div>';
	
	html+='					<div class="w100">';
	html+='						<textarea class="input w100" name="comment" id="comment_'+id+'" rows="10">'+text+'</textarea>';
	html+='					</div>';

	return(html);

}

function emoticon(text, id) {
	text = ' ' + text + ' ';
	var mes_area = document.getElementById('comment_'+id);
	if (mes_area.createTextRange && mes_area.caretPos) {
		var caretPos = mes_area.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;		
	} else {
		mes_area.value  += text;
	}
	//mes_area.focus();
	mes_area.focus(mes_area.value.length - 1);
	document.getElementById('smile'+id).style.display='none';
}


function edit_comment(id, action, forum_img_path)
{
	var name_div = document.getElementById('name_'+id);
	var mes_div = document.getElementById('mes_'+id);
	var mes_comment = document.getElementById('mes_comment_'+id);
	var mes_id = document.getElementById('mes_id_'+id);
	var edit_form_html_div = document.getElementById('edit_form_html_'+id);
	var post_text;
	var action_text = 'Ответить';
	if (document.getElementById('post_'+id))
	{
		if(document.getElementById('post_'+id).style.display=='none')
		{
			document.getElementById('post_'+id).style.display='block';
		}
		/*
		else
		{
		document.getElementById('post_'+id).style.display='none';
		}
		*/
	}
	switch (action)
	{
		case 'edit':
		if (mes_id) mes_id.value = id;
		post_text = parse_text(mes_div.innerHTML, forum_img_path);
		action_text = 'Редактировать';
		break;
		case 'quote':
		post_text = '[quote="'+name_div.innerHTML+'"]'+parse_text(mes_div.innerHTML, forum_img_path)+'[/quote]';
		break;
		case 'ans':
		post_text = '';
		break;
	}
	if (document.getElementById('action_text_'+id)) document.getElementById('action_text_'+id).innerHTML = action_text;
	//if (edit_form_html_div) edit_form_html_div.innerHTML = edit_form(id, post_text);
	if (edit_form_html_div) edit_form_html_div.innerHTML = '<textarea name="comment" id="comment_' + id + '">' + post_text + '</textarea>';
	$(document).ready(function() { $("#comment_" + id).markItUp(mySettings); });		
	return false;
}


// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, textarea) {
	
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) {
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

		if (temp_length == 0) {
			caretPos.moveStart("character", -text2.length);
			caretPos.moveEnd("character", -text2.length);
			caretPos.select();
		}
		else
			textarea.focus(caretPos);
	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined") {
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange) {
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else {
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
	
	
}
/*
function surroundText(text1, text2, textarea)
{
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;
		
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
		caretPos.select();

	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange)
		{
			if (selection.length == 0)
			textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
			textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else
	{
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}
*/
function clear_form()
{

	document.getElementById('new_theme').style.display='none';
	document.create_theme.theme_name.value='';
	document.create_theme.comment.value='';
}

function check_form()
{
	if(document.create_theme.theme_name.value=='')
	{
		alert('Введите название темы!');
		return false;
	}
	else if(document.create_theme.comment.value=='')
	{
		alert('Введите текст!');
		return false;
	}
	else
	{
		//alert('+++submit'||document.create_theme1);
		document.create_theme.submit();
		return true;
	}
}

function check_new_pm()
{
	if(document.create_new_pm.user_login.value=='')
	{
		alert('Введите логин пользователя');
		return false;
	}
	else if(document.create_new_pm.comment.value=='')
	{
		alert('Введите текст');
		return false;
	}
	else
	{
		document.create_new_pm.submit();
		return true;
	}
}


function confirmation(mes, del_url)
{
	var answer = confirm(mes)
	if (answer){
		window.location = del_url;
	}
	else return false;
}


function insert_comment_parent(id)
{
	if(eval('document.send_post_parent_'+id).comment.value=='')
	{
		alert('Нужно ввести сообщение!');
	}
	else if(eval('document.send_post_parent_'+id).comment.value.length<2)
	{
		alert('Сообщение должно быть больше 2 символов!');
	}
	else
	{
		var error = false;
		var text = eval('document.send_post_parent_'+id).comment.value;
		var reg_good = /(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)/;
		var reg_bad = /(\[|\])+/gi;
		if(eval('document.send_post_parent_'+id).comment.value.indexOf('[img]') != -1)
		{
			var img_array = text.split('[img]');
			for (i=0; i<img_array.length; i++)
			{
				if (img_array[i].indexOf('[/img]') != -1)
				{
					var url_text_href = img_array[i].substring(0, img_array[i].indexOf('[/img]'));
					if (!reg_good.test(url_text_href) || reg_bad.test(url_text_href))
					{
						error = true;
						alert('Некорректная ссылка на картинку: '+ url_text_href);
					}
				}
			}
		}
		if(eval('document.send_post_parent_'+id).comment.value.indexOf('[url') != -1)
		{
			var url_array = text.split('[url');
			for (i=0; i<url_array.length; i++)
			{
				if (url_array[i].indexOf('[/url]') != -1)
				{
					if (url_array[i].substring(0, 1) == ']')
					{
						url_text_href = url_array[i].substring(1, url_array[i].indexOf('[/url]'));
					}
					if (url_array[i].substring(0, 1) == '=')
					{
						url_text_href = url_array[i].substring(1, url_array[i].indexOf(']'));
					}
					if ((!reg_good.test(url_text_href) || reg_bad.test(url_text_href)) && url_text_href!='')
					{
						error = true;
						alert('Некорректная ссылка: '+ url_text_href);
					}
				}
			}
		}
		if(error == false)
		{
			eval('document.send_post_parent_'+id).submit();
			return true;
		}
	}
}


function cancel_comment(id)
{
	document.getElementById('comment_'+id).value='';
	document.getElementById('preview_'+id).style.display='none';
	if(document.getElementById('post_'+id).style.display=='none')
	{
		document.getElementById('post_'+id).style.display='block';
	}
	else
	{
		document.getElementById('post_'+id).style.display='none';
	}
	return 0;
}

function check_all_theme()
{
	for (var i=0;i<document.themes.elements.length;i++)
	{
		if (document.themes.elements[i].type=='checkbox' && document.themes.elements[i].id=='del_th')
		{
			document.themes.elements[i].checked=chk_all
		}
	};
}


// для управления навигацией при редактировании темы
function closed_theme(block_name)
{
	var closed_theme_div = document.getElementById('closed_theme');
	var closed_access_div = document.getElementById('closed_access');
	var closed_moderators_div = document.getElementById('closed_moderators');
	var theme_span = document.getElementById('span_theme');
	var access_span = document.getElementById('span_access');
	var moderators_span = document.getElementById('span_moderators');

	if (document.getElementById('closed_note')) document.getElementById('closed_note').innerHTML = '';

	switch(block_name)
	{
		case 'closed_theme':
		closed_theme_div.style.display = 'block';
		closed_access_div.style.display = 'none';
		closed_moderators_div.style.display = 'none';

		theme_span.className = 'second-razd selected';
		access_span.className = 'second-razd';
		moderators_span.className = 'second-razd';
		break;

		case 'closed_access':
		closed_theme_div.style.display = 'none';
		closed_access_div.style.display = 'block';
		closed_moderators_div.style.display = 'none';

		theme_span.className = 'second-razd';
		access_span.className = 'second-razd selected';
		moderators_span.className = 'second-razd';
		break;

		case 'closed_moderators':
		closed_theme_div.style.display = 'none';
		closed_access_div.style.display = 'none';
		closed_moderators_div.style.display = 'block';

		theme_span.className = 'second-razd';
		access_span.className = 'second-razd';
		moderators_span.className = 'second-razd selected';
		break;
	}
}

function preview_post(id)
{
	var preview = document.getElementById('preview_'+id);
	var preview_text = document.getElementById('preview_text_'+id);
	var mes = document.getElementById('comment_'+id).value;
	preview_text.innerHTML = '';
	preview_text.innerHTML = tag_to_html(mes);
	preview.style.display='block';

}

function tag_to_html(txt)
{
	txt = txt.replace(/</g, '&lt;');
	txt = txt.replace(/>/g, '&gt;');
	txt = nl2br(txt);
	if (txt.indexOf('[quote') != -1)
	{
		txt = quote2div(txt);
	}
	txt = smiles2img(txt);
	if (txt.indexOf('[b]') != -1) txt = b2bold(txt);
	if (txt.indexOf('[i]') != -1) txt = i2italic(txt);
	if (txt.indexOf('[u]') != -1) txt = u2underline(txt);
	if (txt.indexOf('[s]') != -1) txt = s2striped(txt);

	// сначала size потом color
	if (txt.indexOf('[size=') != -1)
	{
		txt = size2font(txt);
	}
	
	if (txt.indexOf('[img]') != -1)
	{
		txt = img2image(txt);
	}

	if (txt.indexOf('[url') != -1)
	{
		txt = url2href(txt);
	}

	if (txt.indexOf('[color=') != -1)
	{
		txt = color2font(txt);
	}
	return txt;
}


function get_users()
{
  forum_JsHttpRequest.query('/backend_get_users_login.php', 
  {},
  function(result, errors) 
  {	
  	/*
  	alert(result.users_array.length);
  	addressbook = Array(result.users_array.length);
  	
	 for(i=0; i<result.users_array.length; i++)
    {
    	addressbook[i]=result.users_array[i].user_login;    	
    } 
    */  
    },false);  
    
    return addressbook;   
}

