function getActiveText(selectedtext) {
	text = (document.all) ? document.selection.createRange().text : window.getSelection();
	if (selectedtext.createTextRange) {
		selectedtext.caretPos = document.selection.createRange().duplicate();
	}
	return true;
}

function addText(NewCode) {
	var textWindow = document.topic.message;
	if (textWindow.createTextRange && textWindow.caretPos) {
		var caretPos = textWindow.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
	} else {
		textWindow.value += NewCode
	}
	textWindow.focus();
}


var focused = null;

function translate(msgField) {
	var msg = msgField.value;
	var msg_part = new Array();
	var current_part = '';
	var bracket_open = false;
	for (i = 0; i < msg.length; i++) 
	{

		chr = msg.charAt(i);

		if (chr == '[') {

			bracket_open = true;
			// msg_part.push(current_part);
			msg_part[msg_part.length] = current_part;
			current_part = chr;

		} else if (chr == ']' && bracket_open) {

			bracket_open = false;
			current_part += chr;
			//msg_part.push(current_part);
			msg_part[msg_part.length] = current_part;
			current_part = '';

		} else {

			current_part += chr;

		} // if

	} // for

	msg_part.push(current_part);

	
		var search_cap = new Array(/S(HCH|hch)/g, /[YJ][Oo]/g, /C[Hh]/g, /S[Hh]/g, /(E[Hh]|-E)/g, /[YJ][Uu]/g, /[YJ][Aa]/g, /Z[Hh]/g, /(#|'')/g, /A/g, /B/g, /[VW]/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, /[HX]/g, /C/g, /'/g, /Y/g);
		var replace_cap = new Array
		(
		String.fromCharCode(1065),
		String.fromCharCode(1025),
		String.fromCharCode(1063),
		String.fromCharCode(1064),
		String.fromCharCode(1101),
		String.fromCharCode(1070),
		String.fromCharCode(1071),
		String.fromCharCode(1046),
		String.fromCharCode(1066),
		String.fromCharCode(1040),
		String.fromCharCode(1041),
		String.fromCharCode(1042),
		String.fromCharCode(1043),
		String.fromCharCode(1044),
		String.fromCharCode(1045),
		String.fromCharCode(1047),
		String.fromCharCode(1048),
		String.fromCharCode(1049),
		String.fromCharCode(1050),
		String.fromCharCode(1051),
		String.fromCharCode(1052),
		String.fromCharCode(1053),
		String.fromCharCode(1054),
		String.fromCharCode(1055),
		String.fromCharCode(1056),
		String.fromCharCode(1057),
		String.fromCharCode(1058),
		String.fromCharCode(1059),
		String.fromCharCode(1060),
		String.fromCharCode(1061),
		String.fromCharCode(1062),
		String.fromCharCode(1068),
		String.fromCharCode(1067)
		);

		var search_small = new Array(/shch/g, /[yj]o/g, /ch/g, /sh/g, /(eh|-e)/g, /[yj]u/g, /[yj]a/g, /zh/g, /(#|'')/g, /a/g, /b/g, /[vw]/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, /[hx]/g, /c/g, /'/g, /y/g, /#/);
		var replace_small = new Array(
		String.fromCharCode(1097),
		String.fromCharCode(1105),
		String.fromCharCode(1095),
		String.fromCharCode(1096),
		String.fromCharCode(1101),
		String.fromCharCode(1102),
		String.fromCharCode(1103),
		String.fromCharCode(1078),
		String.fromCharCode(1098),
		String.fromCharCode(1072),
		String.fromCharCode(1073),
		String.fromCharCode(1074),
		String.fromCharCode(1075),
		String.fromCharCode(1076),
		String.fromCharCode(1077),
		String.fromCharCode(1079),
		String.fromCharCode(1080),
		String.fromCharCode(1081),
		String.fromCharCode(1082),
		String.fromCharCode(1083),
		String.fromCharCode(1084),
		String.fromCharCode(1085),
		String.fromCharCode(1086),
		String.fromCharCode(1087),
		String.fromCharCode(1088),
		String.fromCharCode(1089),
		String.fromCharCode(1090),
		String.fromCharCode(1091),
		String.fromCharCode(1092),
		String.fromCharCode(1093),
		String.fromCharCode(1094),
		String.fromCharCode(1100),
		String.fromCharCode(1099),
		String.fromCharCode(1098)
		);
	for (j = 0; j < msg_part.length; j++) {

		if (msg_part[j] != '') {

			for (k=0; k < search_cap.length; k++) {

				msg_part[j] = msg_part[j].replace(search_small[k],replace_small[k]);
				msg_part[j] = msg_part[j].replace(search_cap[k],replace_cap[k]);



			}

		}

		j++;

	} // for

	msg = msg_part.join("");

	msgField.value=msg;
	msgField.focus();

}

function setFocused(obj) {
	focused = obj;
}
