<!--
//--------------------------- calculate sybmols left
function CalcRest()
{
	window.onerror=null;
	var maxnum;
	var rest;
	var str1;
	maxnum = 2048;
	if (document.postForm.message.value != null)
	{	str1=document.postForm.message.value;
		rest =maxnum - str1.length;
		if (rest < 0 )
		{	document.postForm.message.value=str1.substring(0,maxnum);
			rest = 0;
		}
		document.postForm.rest.value=rest;
	}
	return (rest>0)
}

function MsgOnKeyPress()
{
  event.returnValue=CalcRest();
}


// bbCode control some time by subBlue design (www.subBlue.com)
// some new strings added by Mercuree (mercuree@moldova.cc)
// this file is a part of CHAT.MD project

// map for smilies row
document.writeln('<map name="smilies">');
document.writeln('<area shape="rect" coords="465,0,490,24" onclick="emoticon(\':super:\');" alt="Круто!">');
document.writeln('<area shape="rect" coords="438,0,463,24" onclick="emoticon(\':beer:\');" alt="Друзья">');
document.writeln('<area shape="rect" coords="415,0,436,24" onclick="emoticon(\':kettle:\');" alt="Чайник">');
document.writeln('<area shape="rect" coords="392,0,413,24" onclick="emoticon(\':help:\');" alt="Помогите">');
document.writeln('<area shape="rect" coords="369,0,390,24" onclick="emoticon(\':cranky:\');" alt="Тронулся">');
document.writeln('<area shape="rect" coords="346,0,367,24" onclick="emoticon(\':cry:\');" alt="Весьма расстроен">');
document.writeln('<area shape="rect" coords="306,0,327,24" onclick="emoticon(\':mad:\');" alt="Обозлён">');
document.writeln('<area shape="rect" coords="329,0,344,24" onclick="emoticon(\':devious:\');" alt="Злобный">');
document.writeln('<area shape="rect" coords="289,0,304,24" onclick="emoticon(\':evil:\');" alt="Очень зол">');
document.writeln('<area shape="rect" coords="272,0,287,24" onclick="emoticon(\':oops:\');" alt="Ой">');
document.writeln('<area shape="rect" coords="255,0,270,24" onclick="emoticon(\':spy:\');" alt="Подозрение">');
document.writeln('<area shape="rect" coords="238,0,253,24" onclick="emoticon(\':shy:\');" alt="Смущение">');
document.writeln('<area shape="rect" coords="221,0,236,24" onclick="emoticon(\':insane:\');" alt="Ничё не понимаю">');
document.writeln('<area shape="rect" coords="204,0,219,24" onclick="emoticon(\':roll:\');" alt="Закатывать глаза">');
document.writeln('<area shape="rect" coords="187,0,202,24" onclick="emoticon(\':o\');" alt="Удивление">');
document.writeln('<area shape="rect" coords="170,0,185,24" onclick="emoticon(\':?\');" alt="Непонятка">');
document.writeln('<area shape="rect" coords="153,0,168,24" onclick="emoticon(\'8)\');" alt="Крут">');
document.writeln('<area shape="rect" coords="136,0,151,24" onclick="emoticon(\':laugh:\');" alt="Смех">');
document.writeln('<area shape="rect" coords="119,0,134,24" onclick="emoticon(\':lol:\');" alt="Умираю со смеху">');
document.writeln('<area shape="rect" coords="102,0,117,24" onclick="emoticon(\':gg:\');" alt="Гы">');
document.writeln('<area shape="rect" coords="85,0,100,24" onclick="emoticon(\':D\');" alt="Улыбка до ушей">');
document.writeln('<area shape="rect" coords="68,0,83,24" onclick="emoticon(\':P\');" alt="Дразнить">');
document.writeln('<area shape="rect" coords="51,0,66,24" onclick="emoticon(\':ermm:\');" alt="Ухмылка">');
document.writeln('<area shape="rect" coords="34,0,49,24" onclick="emoticon(\';)\');" alt="Подмигивание">');
document.writeln('<area shape="rect" coords="17,0,32,24" onclick="emoticon(\':(\');" alt="Огорчение">');
document.writeln('<area shape="rect" coords="0,0,15,24" onclick="emoticon(\':)\');" alt="Улыбка">');
document.writeln('</map>');

// Startup variables
var imageTag = false;
var theSelection = false;

// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_moz = 0;

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);

// Define the bbCode tags
bbcode = new Array();
bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[list]','[/list]','[mod]','[/mod]','[img]','[/img]','[url]','[/url]');
imageTag = false;


// Replacement for arrayname.length property
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

// Replacement for arrayname.push(value) not implemented in IE until version 5.5
// Appends element to the array
function arraypush(thearray,value) {
	thearray[ getarraysize(thearray) ] = value;
}

// Replacement for arrayname.pop() not implemented in IE until version 5.5
// Removes and returns the last element of an array
function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

function checkForm() {

	formErrors = false;

	if (document.postForm.message.value.length < 2) {
		formErrors = 'Пустое сообщение';
	}

	if (formErrors) {
		alert(formErrors);
		return false;
	} else {
		bbstyle(-1);
		//formObj.preview.disabled = true;
		//formObj.submit.disabled = true;
		document.postForm.value = l_submitting;
		return true;
		}
}

// pasting smilies
function emoticon(text) {
	var txtarea = document.postForm.message;
	text = ' ' + text + ' ';
	txtarea.focus();
	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();
	}
}

// pasting plain text
function pastext(text) {
	var txtarea = document.postForm.message;
	text = text + '\n';
	txtarea.focus();
	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();
	}
}

// talking to person
function sayto(text) {
	var txtarea = document.postForm.message;
	text = '[b]' + text + '[/b],\n';
	txtarea.focus();
	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();
	}
CalcRest();
}

function bbfontstyle(bbopen, bbclose) {
	var txtarea = document.postForm.message;

	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			txtarea.value += bbopen + bbclose;
			txtarea.focus();
			return;
		}
		document.selection.createRange().text = bbopen + theSelection + bbclose;
		txtarea.focus();
		return;
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, bbopen, bbclose);
		return;
	}
	else
	{
		txtarea.value += bbopen + bbclose;
		txtarea.focus();
	}
	storeCaret(txtarea);
}


function bbstyle(bbnumber) {
	var txtarea = document.postForm.message;

	txtarea.focus();
	donotinsert = false;
	theSelection = false;
	bblast = 0;

	if (bbnumber == -1) { // Close all open tags & default button names
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			txtarea.value += bbtags[butnumber + 1];
		}
		imageTag = false; // All tags are closed including image tags :D
		txtarea.focus();
		return;
	}

	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			// Add tags around selection
			document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
			txtarea.focus();
			theSelection = '';
			return;
		}
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, bbtags[bbnumber], bbtags[bbnumber+1]);
		return;
	}

	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumber+1) {
			bblast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (bbcode[bblast]) {
				butnumber = arraypop(bbcode) - 1;
				txtarea.value += bbtags[butnumber + 1];
				imageTag = false;
			}
			txtarea.focus();
			return;
	} else { // Open tags

		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another
			txtarea.value += bbtags[15];
			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list
			document.postForm.addbbcode14.value = "Img";	// Return button back to normal state
			imageTag = false;
		}

		// Open tag
		txtarea.value += bbtags[bbnumber];
		if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(bbcode,bbnumber+1);
		txtarea.focus();
		return;
	}
	storeCaret(txtarea);
}

// From http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2)
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

// Quote selected text on the page
function quoteSelection() {

	theSelection = false;
	theSelection = document.selection.createRange().text; // Get text selection

	if (theSelection) {
		// Add tags around selection
//		if (quoteUsername == '') { pastext( '[quote]' + theSelection + '[/quote]'); }
//		else { pastext( '[quote="' + quoteUsername + '"]' + theSelection + '[/quote]'); }
		pastext( '[quote]' + theSelection + '[/quote]');
		document.postForm.message.focus();
		theSelection = '';
		quoteUsername = '';
		return;
	}else{
		alert('Ничего не выделено');
	}
}

// copy to clipboard
function highlightmetasearch() {
document.postForm.message.select(); document.postForm.message.focus();
}
function copymetasearch() {
highlightmetasearch();
textRange = document.postForm.message.createTextRange();
textRange.execCommand("RemoveFormat");
textRange.execCommand("Copy");
}

function copy_post_link(post_id){
 if (window.clipboardData){
	postlink = "[url=" + forum_path + post_id + "#" + post_id + "]" + topic_title + "#" + post_id + "[/url]";
	window.clipboardData.setData("Text", postlink);
}
else if (window.netscape) 
{   
	netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
	
	var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
	if (!clip) return;
	
	var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
	if (!trans) return;
	
	trans.addDataFlavor('text/unicode');
	
	var str = new Object();
	var len = new Object();
	
	var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
	
	var copytext=postlink;
	
	str.data=copytext;
	
	trans.setTransferData("text/unicode",str,copytext.length*2);
	
	var clipid=Components.interfaces.nsIClipboard;
	
	if (!clip) return false;
	
	clip.setData(trans,null,clipid.kGlobalClipboard);
}
alert("Link on post #" + post_id + " copied to clipboard:\n\n" + postlink);
}



//-->
