function paste_smilie(e, s)
{
	//IE support
	if (document.selection)
	{
		e.focus();
		sel = document.selection.createRange();
		sel.text = s;
		sel.select();
	}
	//MOZILLA/NETSCAPE support
	else
	if (e.selectionStart || e.selectionStart == '0')
	{
		var startPos = e.selectionStart;
		var endPos = e.selectionEnd;
		e.value = e.value.substring(0, startPos) + s + e.value.substring(endPos, e.value.length);
		e.selectionStart = startPos + s.length;
		e.selectionEnd = e.selectionStart;
	}
	else
	{
		e.value += s;
	}
	e.focus();
}

function paste_string(e, s)
{
	//IE support
	if (document.selection)
	{
		e.focus();
		sel = document.selection.createRange();
		sel.text = s;
		sel.moveStart('character', s.indexOf(']') + 1 - s.length);
		sel.moveEnd('character', s.lastIndexOf('[')-s.length);
		sel.select();
	}
	//MOZILLA/NETSCAPE support
	else
	if (e.selectionStart || e.selectionStart == '0')
	{
		var startPos = e.selectionStart;
		var endPos = e.selectionEnd;
		e.value = e.value.substring(0, startPos) + s + e.value.substring(endPos, e.value.length);
		e.selectionStart = startPos + s.indexOf("]")+1;
		e.selectionEnd = startPos + s.lastIndexOf('[');
	}
	else
	{
		e.value += s;
	}
	e.focus();
}

function paste_url(e,p1,p2){
	var u1 = prompt(p1,'http://');
	var u2 = prompt(p2,'');
	if(u1 && u2)paste_string(e, '[url='+strip_tags(u1)+']'+strip_tags(u2)+'[/url]');
}
function paste_email(e,p1,p2){
	var u1 = prompt(p1,'http://');
	var u2 = prompt(p2,'');
	if(u1 && u2)paste_string(e, '[email='+strip_tags(u1)+']'+strip_tags(u2)+'[/email]');
}

function get_sel()
{

	selected='';
	if      (window.getSelection)   selected = window.getSelection().toString();
	else if (document.getSelection) selected = document.getSelection();
	else if (document.selection)   selected = document.selection.createRange().text;
	return selected;
}
function quote_sel(e)
{
	sel = get_sel();
	paste_string(e, '[quote]'+strip_tags(sel)+'[/quote]');
}

function insert_tag(e, open, close)
{
	sel = get_sel();
	paste_string(e, open+strip_tags(sel)+close);
}

function show_add_form(){
	$("table.formtable").show('fast');

	var auth=$.cookie('author');
	if (auth)
	$("#topic_author").val(auth);

	var em=$.cookie('email');

	if (em)
	{
		em=em.replace("%40","@");
		$("#topic_email").val(em);
	}
}

function check_topic_form(mode){
	if ($("#topic_author").val()==""){
		alert ("Укажите имя!");
		return false;
	}

	if ($("#message").val()==""){
		alert ("Введите сообщение!");
		return false;
	}
	if ($("#topic_check_code").val()==""){
		alert ("Укажите секретный код!");
		return false;
	}

	if (mode=="topic" && $("#topic_title").val()==''){
		alert ("Укажите название темы!");
		return false;
	}

	if (mode=="topic" && $("#topic_description").val()==''){
		alert ("Укажите описание темы!");
		return false;
	}


	if ($("#topic_email").val()!="" && !$("#topic_email").val().match(/^[A-Za-z0-9\.\_]{1,32}\@[A-Za-z0-9\.\_]{1,32}\.[a-zA-Z]{2,6}$/))
	{
		alert ("Неверный формат email!");
		return false;
	}
	return true;
}

function strip_tags(str){
	str = new String(str);
	//alert (str);
	str = str.replace('/[\[\]]/g', '');
	str = str.replace(/\'/g, '&#39;');
	str = str.replace(/\"/g, '&amp;');
	return str.replace(/<\/?[^>]+>/gi, '');
}
