var MessageMax = 64000;

var B_open = 0;
var I_open = 0;
var U_open = 0;
var QUOTE_open = 0;
var CODE_open = 0;

var bbtags = new Array();

var myAgent = navigator.userAgent.toLowerCase();
var myVersion = parseInt(navigator.appVersion);

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

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

var smallEngLettersReg = new Array(/e'/g, /ch/g, /sh/g, /yo/g, /jo/g, /zh/g, /yu/g, /ju/g, /ya/g, /ja/g, /a/g, /b/g, /v/g, /g/g, /d/g, /e/g, /z/g, /i/g, /j/g, /k/g, /l/g, /m/g, /n/g, /o/g, /p/g, /r/g, /s/g, /t/g, /u/g, /f/g, /h/g, /c/g, /w/g, /~/g, /y/g, /'/g);
var capitEngLettersReg = new Array(/E'/g, /CH/g, /SH/g, /YO/g, /JO/g, /ZH/g, /YU/g, /JU/g, /YA/g, /JA/g, /A/g, /B/g, /V/g, /G/g, /D/g, /E/g, /Z/g, /I/g, /J/g, /K/g, /L/g, /M/g, /N/g, /O/g, /P/g, /R/g, /S/g, /T/g, /U/g, /F/g, /H/g, /C/g, /W/g, /~/g, /Y/g, /'/g);
var smallRusLetters = new Array("ý", "÷", "ø", "¸", "¸", "æ", "þ", "þ", "ÿ", "ÿ", "à", "á", "â", "ã", "ä", "å", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ù", "ú", "û", "ü");
var capitRusLetters = new Array( "Ý", "×", "Ø", "¨", "¨", "Æ", "Þ", "Þ", "\ß", "\ß", "À", "Á", "Â", "Ã", "Ä", "Å", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ù", "Ú", "Û", "Ü");

var smallEngLetters = new Array("e", "ch", "sh", "yo", "jo", "zh", "yu", "ju", "ya", "ja", "a", "b", "v", "g", "d", "e", "z", "i", "j", "k", "l", "m", "n", "o", "p", "r", "s", "t", "u", "f", "h", "c", "w", "~", "y", "'");
var capitEngLetters = new Array("E", "CH", "SH", "YO", "JO", "ZH", "YU", "JU", "YA", "JA", "A", "B", "V", "G", "D", "E", "Z", "I", "J", "K", "L", "M", "N", "O", "P", "R", "S", "T", "U", "F", "H", "C", "W", "~", "Y", "'");
var smallRusLettersReg = new Array(/ý/g, /÷/g, /ø/g, /¸/g, /¸/g,/æ/g, /þ/g, /þ/g, /ÿ/g, /ÿ/g, /à/g, /á/g, /â/g, /ã/g, /ä/g, /å/g, /ç/g, /è/g, /é/g, /ê/g, /ë/g, /ì/g, /í/g, /î/g, /ï/g, /ð/g, /ñ/g, /ò/g, /ó/g, /ô/g, /õ/g, /ö/g, /ù/g, /ú/g, /û/g, /ü/ );
var capitRusLettersReg = new Array(/Ý/g, /×/g, /Ø/g, /¨/g, /¨/g, /Æ/g, /Þ/g, /Þ/g, /ß/g, /ß/g, /À/g, /Á/g, /Â/g, /Ã/g, /Ä/g, /Å/g, /Ç/g, /È/g, /É/g, /Ê/g, /Ë/g, /Ì/g, /Í/g, /Î/g, /Ï/g, /Ð/g, /Ñ/g, /Ò/g, /Ó/g, /Ô/g, /Õ/g, /Ö/g, /Ù/g, /Ú/g, /Û/g, /Ü/);

// ý, ÷, ø, ¸, ¸,æ, þ, þ, ÿ, ÿ, à, á, â, ã, ä, å, ç, è, é, ê, ë, ì, í, î, ï, ð, ñ, ò, ó, ô, õ, ö, ù, ú, û, ü
// e, ch, sh, yo, jo, zh, yu, ju, ya, ja, a, b, v, g, d, e, z, i, j, k, l, m, n, o, p, r, s, t, u, f, h, c, w, ~, y, '

var translitStatus = 0;
var translitTitle = "";

function quoteMessageEx(theAuthor, textMessage)
{
	var selection = "";
	var message_id = 0;
	if (document.getSelection)
	{
		selection = document.getSelection();
		selection = selection.replace(/\r\n\r\n/gi, "_newstringhere_");
		selection = selection.replace(/\r\n/gi, " ");
		selection = selection.replace(/  /gi, "");
		selection = selection.replace(/_newstringhere_/gi, "\r\n\r\n");
	}
	else
	{
		selection = document.selection.createRange().text;
	}
	
	if (selection == "")
	{
		message = textMessage.replace(/message_text_/gi, "");
		if ((message + 0) > 0)
		{
			message = document.getElementById(textMessage);
			if (typeof(message) == "object")
			{
				selection = message.innerHTML;
				selection = selection.replace(/\<br(\s)*(\/)*\>/gi, "\n");
				selection = selection.replace(/\<[^\>]+\>/gi, "");
				selection = selection.replace(/&lt;/gi, "<");
				selection = selection.replace(/&gt;/gi, ">");
				selection = selection.replace(/&quot;/gi, "\"");
			}
			else
			{
				selection = 'is not object';
			}
		}
		else if (textMessage.Length() > 0)
		{
			selection = textMessage;
		}
	}
	
	if (selection != "")
	{
		selection = selection.replace(/\&shy;/gi, "");
		selection = theAuthor + oText['author'] + selection;
		doInsert("[QUOTE]", "[/QUOTE]", false, selection);
	}
}

function emoticon(theSmilie)
{
	doInsert(" " + theSmilie + " ", "", false);
}

// Insert simple tags: B, I, U, CODE, QUOTE
function simpletag(thetag)
{
	var tagOpen = eval(thetag + "_open");

	if (tagOpen == 0)
	{
		if (doInsert("[" + thetag + "]", "[/" + thetag + "]", true))
		{
			eval(thetag + "_open = 1");
			// Change the button status
			eval("document.REPLIER." + thetag + ".value += '*'");

			pushstack(bbtags, thetag);
			cstat();
			show_hints('click_close');
		}
	}
	else
	{
		// Find the last occurance of the opened tag
		lastindex = 0;

		for (i = 0 ; i < bbtags.length; i++ )
		{
			if ( bbtags[i] == thetag )
			{
				lastindex = i;
			}
		}

		// Close all tags opened up to that tag was opened
		while (bbtags[lastindex])
		{
			tagRemove = popstack(bbtags);
			doInsert("[/" + tagRemove + "]", "", false);

			// Change the button status
			eval("document.REPLIER." + tagRemove + ".value = ' " + tagRemove + " '");
			eval(tagRemove + "_open = 0");
		}

		cstat();
	}
}

// Insert font tag
function alterfont(theval, thetag)
{
	if (theval == 0)
		return;

	if (doInsert("[" + thetag + "=" + theval + "]", "[/" + thetag + "]", true))
		pushstack(bbtags, thetag);

	document.REPLIER.ffont.selectedIndex  = 0;
	document.REPLIER.fcolor.selectedIndex = 0;
	cstat();
	document.getElementById("POST_MESSAGE").focus();
}

// Insert url tag
function tag_url()
{
	var FoundErrors = '';
	var enterURL   = prompt(oText['enter_url'], "http://");
	var enterTITLE = prompt(oText['enter_url_name'], "My Webpage");

	if (!enterURL)
	{
		FoundErrors += " " + oErrors['no_url'];
	}
	if (!enterTITLE)
	{
		FoundErrors += " " + oErrors['no_title'];
	}

	if (FoundErrors)
	{
		alert("Error! " + FoundErrors);
		return;
	}

	doInsert("[URL="+enterURL+"]"+enterTITLE+"[/URL]", "", false);
}

// Insert image tag
function tag_image()
{
	var FoundErrors = '';
	var enterURL = prompt(oText['enter_image'], "http://");

	if (!enterURL)
	{
		FoundErrors += " " + oErrors['no_url'];
	}

	if (FoundErrors)
	{
		alert("Error! "+FoundErrors);
		return;
	}

	doInsert("[IMG]"+enterURL+"[/IMG]", "", false);
}

// Insert list tag
function tag_list()
{
	var listvalue = "init";
	var thelist = "[LIST]\n";

	while ( (listvalue != "") && (listvalue != null) ) 
	{
		listvalue = prompt(oText['list_prompt'], "");
		if ( (listvalue != "") && (listvalue != null) ) 
		{
			thelist = thelist+"[*]"+listvalue+"\n";
		}
	}

	doInsert(thelist + "[/LIST]\n", "", false);
}

// Close all tags
function closeall()
{
	if (bbtags[0]) 
	{
		while (bbtags[0]) 
		{
			tagRemove = popstack(bbtags);
			document.REPLIER.POST_MESSAGE.value += "[/" + tagRemove + "]";

			if ( (tagRemove != 'FONT') && (tagRemove != 'SIZE') && (tagRemove != 'COLOR') )
			{
				eval("document.REPLIER." + tagRemove + ".value = ' " + tagRemove + " '");
				eval(tagRemove + "_open = 0");
			}
		}
	}

	document.getElementById('tagcount').innerHTML = 0;
	bbtags = new Array();
	document.REPLIER.POST_MESSAGE.focus();
}

// Stack functions
function pushstack(thearray, newval)
{
	arraysize = stacksize(thearray);
	thearray[arraysize] = newval;
}

function popstack(thearray)
{
	arraysize = stacksize(thearray);
	theval = thearray[arraysize - 1];
	delete thearray[arraysize - 1];
	return theval;
}

function stacksize(thearray)
{
	for (i = 0 ; i < thearray.length; i++ )
	{
		if ( (thearray[i] == "") || (thearray[i] == null) || (thearray == 'undefined') ) 
		{
			return i;
		}
	}

	return thearray.length;
}

// Show statistic
function cstat()
{
	var c = stacksize(bbtags);

	if ( (c < 1) || (c == null) )
	{
		c = 0;
	}

	if ( ! bbtags[0] )
	{
		c = 0;
	}

	document.getElementById('tagcount').innerHTML = c;
}

function mozillaWr(textarea, open, close)
{
	var selLength = textarea.textLength;
	var selStart = textarea.selectionStart;
	var selEnd = textarea.selectionEnd;
	
	if (selEnd == 1 || selEnd == 2)
	selEnd = selLength;

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

	textarea.selectionEnd = selEnd + open.length + close.length;
	return;
}

// Main insert tag
// ibTag: opening tag
// ibClsTag: closing tag, used if we have selected text
// isSingle: true if we do not close the tag right now
// return value: true if the tag needs to be closed later
function doInsert(ibTag, ibClsTag, isSingle, postText)
{
	var isClose = false;
	var textarea = document.REPLIER.POST_MESSAGE;
	if (postText == null)
		postText = "";
	
	if (isSingle)
		isClose = true;
	if (is_ie || is_opera)
	{
		textarea.focus();
		var sel = document.selection;
		var rng = sel.createRange();
		rng.collapse;
		if ((sel.type == "Text" || sel.type == "None") && rng != null)
		{
			if (ibClsTag != "" && rng.text.length > 0)
			{
				ibTag += rng.text + ibClsTag;
				isClose = false;
			}
			else if (postText.length > 0)
			{
				ibTag += postText + ibClsTag;
				isClose = false;
			}
			rng.text = ibTag;
		}
	}
	else if (is_nav && document.getElementById)
	{
		if (ibClsTag != "" && textarea.selectionEnd > textarea.selectionStart)
		{
			mozillaWr(textarea, ibTag, ibClsTag);
			isClose = false;
		}
		else if (postText.length > 0)
		{
			mozillaWr(textarea, ibTag + postText + ibClsTag, '');
			isClose = false;
		}
		else
		{
			mozillaWr(textarea, ibTag, '');
		}
	} 
	else
	{
		textarea.value += ibTag;
	}

	textarea.focus();
	return isClose;
}

function quoteMessage()
{
	var selection = "";
	if (document.getSelection)
	{
		selection = document.getSelection();
		selection = selection.replace(/\r\n\r\n/gi, "_newstringhere_");
		selection = selection.replace(/\r\n/gi, " ");
		selection = selection.replace(/  /gi, "");
		selection = selection.replace(/_newstringhere_/gi, "\r\n\r\n");
	}
	else
	{
		selection = document.selection.createRange().text;
	}
	
	if (selection != "")
	{
		doInsert("[QUOTE]", "[/QUOTE]", false, selection);
	}
}

function show_hints(msg)
{
	document.getElementById('helpbox').innerHTML = oHelp[msg];
}

function storeCaret(textEl)
{
	if (textEl.createTextRange) 
		textEl.caretPos = document.selection.createRange().duplicate();
}
function ValidateForm(form)
{
	var errors = "";
	MessageLength = form.POST_MESSAGE.value.length;

	if ((form.MESSAGE_TYPE.value != "REPLY") || (form.MESSAGE_TYPE.value != "VIEW"))
	{
		if (form.TITLE && (form.TITLE.value.length < 2))
		{
			errors += oErrors['no_topic_name'];
		}
	}

	if (MessageLength < 2)
	{
		errors += oErrors['no_message'];
    }
    else if ((MessageMax != 0) && (MessageLength > MessageMax))
	{
		errors += oErrors['max_len1'] + MessageMax + oErrors['max_len2'] + MessageLength;
	}

	if (errors != "")
	{
		alert(errors);
	}
	else
	{
		bSendForm = true;
		var arr = form.getElementsByTagName("button")
		for (var butt in arr)
			butt.disabled = true;
		form.submit();
	}
	return bSendForm;
}
function View(form)
{
	form.MESSAGE_MODE.value = 'VIEW';
	return ValidateForm(form);
}
function translit()
{
	var i;
	var objTextarea = document.REPLIER['POST_MESSAGE'];
	var textbody = objTextarea.value;
	var selected = false;
	
	if ((is_ie || is_opera) && (objTextarea.isTextEdit))
	{
		objTextarea.focus();
		var sel = document.selection;
		var rng = sel.createRange();
		rng.colapse;
		if (sel.type=="Text" && rng != null)
		{
			textbody = rng.text;
			selected = true;
		}
	}
	else if (is_nav && document.getElementById)
	{
		if (objTextarea.selectionEnd > objTextarea.selectionStart)
		{
			var selEnd = objTextarea.selectionEnd;
			if (selEnd == 0)
				selEnd = objTextarea.textLength;
			var startText = (objTextarea.value).substring(0, objTextarea.selectionStart);
			textbody = (objTextarea.value).substring(objTextarea.selectionStart, selEnd);
			var endText = (objTextarea.value).substring(selEnd, objTextarea.textLength);
			selected = true;
		}
	} 
	
	if (textbody)
	{
		if (translitStatus==0) // ëàòèíèöà -> êèðèëëèöà
		{
			for (i=0; i<smallEngLettersReg.length; i++) textbody = textbody.replace(smallEngLettersReg[i], smallRusLetters[i]);
			for (i=0; i<capitEngLettersReg.length; i++) textbody = textbody.replace(capitEngLettersReg[i], capitRusLetters[i]);
		}
		else // êèðèëëèöà -> ëàòèíèöà
		{
			for (i=0; i<smallRusLetters.length; i++) textbody = textbody.replace(smallRusLettersReg[i], smallEngLetters[i]);
			for (i=0; i<capitRusLetters.length; i++) textbody = textbody.replace(capitRusLettersReg[i], capitEngLetters[i]);
		}
		if (!selected) 
		{
			objTextarea.value = textbody;
		}
		else 
		{
			if ((is_ie || is_opera) && (objTextarea.isTextEdit))
			{
				rng.text = textbody;
			}
			else
			{
				objTextarea.value = startText + textbody + endText;
				objTextarea.selectionEnd = startText.length + textbody.length;
			}
		}
	}
	if (translitStatus==0) // ëàòèíèöà -> êèðèëëèöà
	{
		translitTile = document.REPLIER['TRANSLIT'].value;
		document.REPLIER['TRANSLIT'].value = oText['translit_en'];
		translitStatus = 1;
	}
	else // êèðèëëèöà -> ëàòèíèöà
	{
		document.REPLIER['TRANSLIT'].value = translitTile;
		translitStatus = 0;
	}
	objTextarea.focus();	
}
