function toggle_block(block, icon) {
	var varblock = $(block);
        if (varblock) {
        		if (varblock.style.display == 'block') {
                        varblock.style.display = 'none';
                        icon.src = 'images/plus.gif'; 
                        addCookie(block, true);                   
                } else {
                        varblock.style.display = 'block';
                        icon.src = 'images/minus.gif';
                        removeCookie(block);                          
                }
        }
}

function addCookie(szName,szValue) {
var dtExpires = new Date();
var dtExpiryDate = "";

dtExpires.setTime(dtExpires.getTime() + (1000 * 86400 * 365));
dtExpiryDate = dtExpires.toGMTString();
document.cookie = szName + "=" + szValue + "; expires=" + dtExpiryDate;
}

function findCookie(szName) {
var i = 0;
var nStartPosition = 0;
var nEndPosition = 0; 
var szCookieString = document.cookie; 

while(i <= szCookieString.length) {
nStartPosition = i;
nEndPosition = nStartPosition + szName.length;
if(szCookieString.substring(nStartPosition,nEndPosition) == szName) {
nStartPosition = nEndPosition + 1;
nEndPosition = document.cookie.indexOf(";",nStartPosition);
if(nEndPosition < nStartPosition) nEndPosition = document.cookie.length;
return document.cookie.substring(nStartPosition,nEndPosition); 
break; 
}
i++; 
}
return "";
}

function removeCookie(szName) {
var dtExpires = new Date();

dtExpires.setTime(dtExpires.getTime() - 1);
var szValue = findCookie(szName);
document.cookie = szName + "=" + szValue + "; expires=" + dtExpires.toGMTString();
}

function pm(name) {
var txtarea = $("message");
text = '[b]' + name + '[/b] писал:' + '\r\n';
if (txtarea.createTextRange && txtarea.caretPos) {
var caretPos = txtarea.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
txtarea.focus();
} else {
txtarea.value += text;
txtarea.focus();
}
return 0;
}

function pm2(name) {
var txtarea = $("message");
text = '[b]' + name + '[/b],' + ' ';
if (txtarea.createTextRange && txtarea.caretPos) {
var caretPos = txtarea.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
txtarea.focus();
} else {
txtarea.value += text;
txtarea.focus();
}
return 0;
}

var copy_txt = '';
function copy() {
    if (document.getSelection) copy_txt = document.getSelection();
    else if (document.selection) copy_txt = document.selection.createRange().text;
    else return;
}

function ins_in (tex_to) {
    var txtarea = $("message");
    if (txtarea.createTextRange && txtarea.caretPos) {
        var caretPos = txtarea.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? tex_to + ' ' : tex_to;
  } else txtarea.value += tex_to;
    copy_txt = '';
}

function ins_to(by_who) {
    pm(by_who);
    if (copy_txt) {
    ins_in ("[quote]" + copy_txt + "[/quote]" + "\r\n");
    } else return;
}

function selectMsg(id, clascol) {
 var post = $('id_' + id);	
 if (post.className == clascol) {
	post.className = 'frmselect';
	cnt_m = eval($('mess_count').value) + 1;
	$('mess_count').value = '(' + cnt_m + ')';
 } else if (post.className == 'frmselect') {
	post.className = clascol;
	cnt_m = eval($('mess_count').value) - 1;
	$('mess_count').value = '(' + cnt_m + ')';	
 } 
}

function formValidate(formId, subjectId, textareaId, submitId) {
var maxchars    = 65535;
var subjectDom  = $(subjectId);
var textareaDom = $(textareaId);
var submitDom   = $(submitId);
var formDom     = $(formId);

if (subjectDom.value == "") {
        alert( lng_subj_empty );
        subjectDom.focus();
        return false;
}
if (textareaDom.value == "") {
        alert( lng_mess_empty );
        textareaDom.focus();
        return false;
}

if (textareaDom.value.length > maxchars) {
   alert( lng_maxchars_1 + "\n\n" + lng_maxchars_2 + maxchars + "\n" + lng_maxchars_3 + textareaDom.value.length + "");
   textareaDom.focus();
   return false;
} else {
    submitDom.disabled = true;
    //return true;
    formDom.submit();
}
}

function show_post(f,t,id,m){	
   var url = hpDir + "/showpost.php";
   var pars = {forum: f, topic: t, post: id, mode: m};  
   var myAjax = new Ajax.Updater({success: 'post_'+id}, url, {method: 'post', parameters: pars, onFailure: reportError});
 
 function reportError(request) {
 alert ('Sorry. Error: not connect.');
 }
}

function resize_textarea(to, id) {
	if (to == 'down') {
		var rows = -5;
		var cols = -5;
	} else if (to == 'up') {
		var rows = 5;
		var cols = 5;
	}
	var textarea = $(id);
	if (typeof orig_rows == 'undefined') {
		orig_rows = textarea.rows;
		orig_cols = textarea.cols;
	}	
	var newrows = textarea.rows + rows;
	var newcols = textarea.cols + cols;
	if (newrows >= orig_rows && newcols >= orig_cols) {
		textarea.rows = newrows;
		textarea.cols = newcols;
	}
	return;
}
