// feddback. подставляет готовые темы для письма
function copyS(subj)
{
	document.forms['feedback'].elements['subject'].value = subj;
	return(false);
}

// Цитировать выделенный текст
function postQuote(commentarea,alertmsg) {
	var posttext = '';
	if (window.getSelection){
		posttext = window.getSelection();
	}
	else if (document.getSelection){
		posttext = document.getSelection();
	}
	else if (document.selection){
		posttext = document.selection.createRange().text;
	}
	else {
		return true;
	}
	
	if (posttext==''){
		alert(alertmsg);
		return true;
	} else {
		var quote='[quote]'+posttext+'[/quote]\n';
		var comment=document.getElementById(commentarea);
		addQuote(comment,quote);
	}
	
	return false;
}

// Добавить выделенный текст в форму
function addQuote(comment,quote){
	if (document.selection) {
		comment.focus();
		sel = document.selection.createRange();
		sel.text = quote;
		comment.focus();
	}
	else if (comment.selectionStart || comment.selectionStart == '0') {
		var startPos = comment.selectionStart;
		var endPos = comment.selectionEnd;
		var cursorPos = endPos;
		var scrollTop = comment.scrollTop;
		if (startPos != endPos) {
			comment.value = comment.value.substring(0, startPos)
			              + quote
			              + comment.value.substring(endPos, comment.value.length);
			cursorPos = startPos + quote.length
		}
		else {
			comment.value = comment.value.substring(0, startPos) 
				              + quote
				              + comment.value.substring(endPos, comment.value.length);
			cursorPos = startPos + quote.length;
		}
		comment.focus();
		comment.selectionStart = cursorPos;
		comment.selectionEnd = cursorPos;
		comment.scrollTop = scrollTop;
	}
	else {
		comment.value += quote;
	}
}

// добавить в избранное
function addBookmark(title,url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	} else if( window.opera && window.print ) {
		var t = document.createElement('a');
		t.setAttribute('rel', 'sidebar');
		t.setAttribute('href', url);
		t.setAttribute('title', title);
		t.click();
	} else if( document.all ) {  
		window.external.AddFavorite( url, title);  
	}
}

// счётчик символов
function countLength(elem,max,min)
{
	var length = max - elem.value.length;

	length--;
	if (length < 0 ) {length =0;}
	
	document.getElementById(elem.name+'_status').innerHTML = length;
}

// Показать и скрыть блок
function show_and_hidden_block(show_block,hidden_block)
{
	document.getElementById(show_block).style.display = 'block';
	document.getElementById(hidden_block).style.display = 'none';
}

// Скрыть / Показать Секцию
function SectionClick(id)
{
	var div = document.getElementById(id);
	div.style.display = (div.style.display != 'none' ? 'none' : 'block');
}

// счётчик символов в комментариях
function countLengthComment(elem,max,min,bSubmit)
{
	var length = max - elem.value.length;
	var length_per = (length-1)/max * 535;
	var bSubmit = document.getElementById(bSubmit);

	length--;
	if (length < 0 ) { length=0; }

	if (length < (max-min)) { 
		image = '/images/dot.gif';
		bSubmit.disabled = false;
	} else { 
		image = '/images/dot_red.gif';
		bSubmit.disabled = true;
	}

	document.getElementById(elem.name+'_status').innerHTML = '('+length+') <img src="'+image+'" height=8 width='+length_per+'>';
}

// Голосование за комментарий
function VoteComment(id,mark,vote)
{
	var voteClass = document.getElementById('vote'+id);
	var markSpan = document.getElementById('mark'+id);

	if (voteClass.className == 'vote')
	{
		if (vote == 'plus') {
			if (mark < -1) {
				txt = mark + 1;
			} else {
				txt = '+'+(mark + 1);
			}
		}
		if (vote == 'minus') {
			txt = mark - 1;
		}
	
		document.getElementById('mark'+id).innerHTML = txt;
		voteClass.className = 'novote';
	}
}

// Вставить имя в ответ
function nameQuote(name)
{
	var el=document.getElementById('comment');
	var ln = '';
	var length = el.value.length;

	if (length > 0) {
		ln = ' ';
	}

	el.focus();

    if (el.selectionStart==null) {
    	var rng=document.selection.createRange();
    	rng.text=rng.text+ln+name+': '
	} else {
		el.value=el.value.substring(0,el.selectionStart)+el.value.substring(el.selectionStart,el.selectionEnd)+ln+name+': '+el.value.substring(el.selectionEnd);
	}
}