function Insert(nick, text){
if (text!="") paste("[QUOTE="+nick+"]"+text+"[/QUOTE]\n", 0);
}

function paste(text, flag){
if ((document.selection)&&(flag)) {
	document.REPLIER.Post.focus();
	document.REPLIER.document.selection.createRange().text = text;
} else document.REPLIER.Post.value += text;
}

function get_selection() {
   if (document.getSelection){
	selection = document.getSelection();
	selection = selection.replace(/\r\n\r\n/gi, "_doublecaret_");
	selection = selection.replace(/\r\n/gi, " ");
       while (selection.indexOf("  ") !=-1) selection = selection.replace(/  /gi, "");
	selection = selection.replace(/_doublecaret_/gi, "\r\n\r\n");
  } else
      selection = document.selection.createRange().text;
}

function ins(name){
    var input=document.REPLIER.Post;
    input.value=input.value+"[B]"+name+"[/B]"+" \n";
}

var shift=0;
var caps=0;
function ping(capa) {
    switch(capa) {
        case "BACK" : {
            var text_ch=this.document.REPLIER.Post.value;
            this.document.REPLIER.Post.value = text_ch.substr(0, (text_ch.length-1));
            return;
        };
        case "ENTER" : {
            var text_ch=this.document.REPLIER.Post.value;
            this.document.REPLIER.Post.value = this.document.REPLIER.Post.value+"\n";
            return;
        };
        case "CAPS" : caps= (caps==0) ? 1 : 0;
        case "SHIFT" : {
            shift= (shift==1) ? 0 : 1;
            return;
        };
        case "CLEAR_ALL" : {
            this.document.REPLIER.reset();
            return;
        };
        default : {
            if(shift==0) {
                this.document.REPLIER.Post.value=this.document.REPLIER.Post.value+capa;
            } else {
                var text_ch =""
                switch(capa) {
                    case "`" : text_ch="~"; break;
                    case "1" : text_ch="!"; break;
                    case "2" : text_ch="@"; break;
                    case "3" : text_ch="#"; break;
                    case "4" : text_ch="$"; break;
                    case "5" : text_ch="%"; break;
                    case "6" : text_ch="^"; break;
                    case "7" : text_ch="&"; break;
                    case "8" : text_ch="*"; break;
                    case "9" : text_ch="("; break;
                    case "0" : text_ch=")"; break;
                    case "-" : text_ch="_"; break;
                    case "=" : text_ch="+"; break;
                    case ";" : text_ch=":"; break;
                    case "é" : text_ch="É"; break;
                    case "ö" : text_ch="Ö"; break;
                    case "ó" : text_ch="Ó"; break;
                    case "ê" : text_ch="Ê"; break;
                    case "å" : text_ch="Å"; break;
                    case "í" : text_ch="Í"; break;
                    case "ã" : text_ch="Ã"; break;
                    case "ø" : text_ch="Ø"; break;
                    case "ù" : text_ch="Ù"; break;
                    case "ç" : text_ch="Ç"; break;
                    case "õ" : text_ch="Õ"; break;
                    case "ú" : text_ch="Ú"; break;
                    case "{" : text_ch="}"; break;
                    case "ô" : text_ch="Ô"; break;
                    case "û" : text_ch="Û"; break;
                    case "â" : text_ch="Â"; break;
                    case "à" : text_ch="À"; break;
                    case "ï" : text_ch="Ï"; break;
                    case "ð" : text_ch="Ð"; break;
                    case "î" : text_ch="Î"; break;
                    case "ë" : text_ch="Ë"; break;
                    case "ä" : text_ch="Ä"; break;
                    case "æ" : text_ch="Æ"; break;
                    case "ý" : text_ch="Ý"; break;
                    case "ÿ" : text_ch="ß"; break;
                    case "÷" : text_ch="×"; break;
                    case "ñ" : text_ch="Ñ"; break;
                    case "ì" : text_ch="Ì"; break;
                    case "è" : text_ch="È"; break;
                    case "ò" : text_ch="Ò"; break;
                    case "ü" : text_ch="Ü"; break;
                    case "á" : text_ch="Á"; break;
                    case "þ" : text_ch="Þ"; break;
                    case "," : text_ch="."; break;
                    case "<" : text_ch=">"; break;
                    case "'" : text_ch='"'; break;
                    case "]" : text_ch="["; break;
                    case "/" : text_ch="?"; break;
                    case "\\" : text_ch="|"; break;
                    default : text_ch=capa
                }
                this.document.REPLIER.Post.value=this.document.REPLIER.Post.value+text_ch
            }
            shift = (caps==1) ? 1 : 0;
        }
    };
}


function DoTableWin(str) {
    var aTable1 = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ\ÿ¨¸";    // Windows-1251
    var aTable6 = "ABVGDE~ZIJKLMNOPRSTUFHC~~~'Y'~~~abvgde~zijklmnoprstufhc~~~'y'~~~~~";     // ëàòèíèöà

    var strTable1, strTable2;

    strTable1 = aTable6;
    strTable2 = aTable1;

    var strNew = "";

    var i, len = str.length;
    for(i = 0; i < len; i++) {
        if("'" == str.substr(i, 1)) {
            strNew = strNew+"ü";
            continue;
        }

        if(i < (len-11)) {
            var c = str.substr(i, 12);
            if(("[URL=http://" == c) || ("[url=http://" == c)) {
                strNew = strNew+"[URL=http://";
                i += 11;
                continue;
            }
        }

        if(i < (len-6)) {
            var c = str.substr(i, 7);
            if(("[/CODE]" == c) || ("[/code]" == c)) {
                strNew = strNew+"[/CODE]";
                i += 6;
                continue;
            }
            else if(("[/LIST]" == c) || ("[/list]" == c)) {
                strNew = strNew+"[/LIST]";
                i += 6;
                continue;
            }
        }

        if(i < (len-5)) {
            var c = str.substr(i, 6);
            if(("[/OFF]" == c) || ("[/off]" == c)) {
                strNew = strNew+"[/OFF]";
                i += 6;
                continue;
            }
            else if(("[/PHP]" == c) || ("[/PHP]" == c)) {
                strNew = strNew+"[/PHP]";
                i += 6;
                continue;
            }
            else if(("[/URL]" == c) || ("[/URL]" == c)) {
                strNew = strNew+"[/URL]";
                i += 6;
                continue;
            }
            else if(("[/IMG]" == c) || ("[/img]" == c)) {
                strNew = strNew+"[/IMG]";
                i += 6;
                continue;
            }
            else if(("[CODE]" == c) || ("[code]" == c)) {
                strNew = strNew+"[CODE]";
                i += 6;
                continue;
            }
            else if(("[LIST]" == c) || ("[list]" == c)) {
                strNew = strNew+"[LIST]";
                i += 6;
                continue;
            }
        }

        if(i < (len-4)) {
            var c = str.substr(i, 5);
            if(("[OFF]" == c) || ("[off]" == c)) {
                strNew = strNew+"[OFF]";
                i += 4;
                continue;
            }
            else if(("[PHP]" == c) || ("[php]" == c)) {
                strNew = strNew+"[PHP]";
                i += 4;
                continue;
            }
            else if(("[IMG]" == c) || ("[img]" == c)) {
                strNew = strNew+"[IMG]";
                i += 4;
                continue;
            }
            else if(("[URL]" == c) || ("[url]" == c)) {
                strNew = strNew+"[URL]";
                i += 4;
                continue;
            }
        }

        if(i < (len-3)) {
            var c = str.substr(i, 4);
            if(("[/b]" == c) || ("[/B]" == c)) {
                strNew = strNew+"[/B";
                i += 2;
                continue;
            }
            else if(("[/i]" == c) || ("[/I]" == c)) {
                strNew = strNew+"[/I";
                i += 2;
                continue;
            }
        }

        if(i < (len-2)) {
            var c = str.substr(i, 3);
            if("Sch" == c) {
                strNew = strNew+"Ù";
                i += 2;
                continue;
            }
            else if("sch" == c) {
                strNew = strNew+"ù";
                i += 2;
                continue;
            }
            else if(("[b]" == c) || ("[B]" == c)) {
                strNew = strNew+"[B]";
                i += 2;
                continue;
            }
            else if(("[i]" == c) || ("[I]" == c)) {
                strNew = strNew+"[I]";
                i += 2;
                continue;
            }
            else if(("[u]" == c) || ("[U]" == c)) {
                strNew = strNew+"[U]";
                i += 2;
                continue;
            }
        }

        if(i < (len-1)) {
            var c = str.substr(i, 2);
            if(("Zh" == c) || ("ZH" == c)) {
                strNew = strNew+"Æ";
                i++;
                continue;
            }
            else if(("Ch" == c) || ("CH" == c)) {
                strNew = strNew+"×";
                i++;
                continue;
            }
            else if(("Sh" == c) || ("SH" == c)) {
                strNew = strNew+"Ø";
                i++;
                continue;
            }
            else if(("Ju" == c) || ("JU" == c)) {
                strNew = strNew+"Þ";
                i++;
                continue;
            }
            else if(("Yu" == c) || ("YU" == c)) {
                strNew = strNew+"Þ";
                i++;
                continue;
            }
            else if(("Ja" == c) || ("JA" == c)) {
                strNew = strNew+"ß";
                i++;
                continue;
            }
            else if(("Ya" == c) || ("YA" == c)) {
                strNew = strNew+"ß";
                i++;
                continue;
            }
            if("zh" == c) {
                strNew = strNew+"æ";
                i++;
                continue;
            }
            else if("ch" == c) {
                strNew = strNew+"÷";
                i++;
                continue;
            }
            else if("sh" == c) {
                strNew = strNew+"ø";
                i++;
                continue;
            }
            else if("ju" == c) {
                strNew = strNew+"þ";
                i++;
                continue;
            }
            else if("yu" == c) {
                strNew = strNew+"þ";
                i++;
                continue;
            }
            else if("ja" == c) {
                strNew = strNew+"\ÿ";
                i++;
                continue;
            }
            else if("ya" == c) {
                strNew = strNew+"\ÿ";
                i++;
                continue;
            }
            else if(("Jo" == c) || ("JO" == c)) {
                strNew = strNew+"¨";
                i++;
                continue;
            }
            else if("jo" == c) {
                strNew = strNew+"¸";
                i++;
                continue;
            }
            else if(("Yo" == c) || ("YO" == c)) {
                strNew = strNew+"¨";
                i++;
                continue;
            }
            else if("yo" == c) {
                strNew = strNew+"¸";
                i++;
                continue;
            }
            else if("E'" == c) {
                strNew = strNew+"Ý";
                i++;
                continue;
            }
            else if("e'" == c) {
                strNew = strNew+"ý";
                i++;
                continue;
            }
            else if("Q" == c) {
                strNew = strNew+"Ý";
                i++;
                continue;
            }
            else if("q" == c) {
                strNew = strNew + "ý";
                i++;
                continue;
            }
        }

        var c = str.substr(i, 1);
        var pos = strTable1.indexOf(c);
        if(pos < 0) {
            strNew = strNew+c;
        } else {
            var d = strTable2.substr(pos, 1);
            strNew = strNew+d;
        }
    }

    return strNew;
}

function Decoder() {
    var strText = document.forms.REPLIER.Post.value;

    if(0 == strText.length) {
        alert("Ââåäèòå òåêñò äë\ÿ ïðåîáðàçîâàíè\ÿ!");
        document.forms.REPLIER.Post.focus();
        return strText;
    }

    strText = DoTableWin(strText);

    document.forms.REPLIER.Post.value = strText;

    return strText;
}
