﻿var language = 0;
var prelanguage = 0;

var processhtmltags = 0;
var processbbcodetags = 0;
var tra = new Array();
var abc2 = new Array();
var abc1 = new Array();
tra[String.fromCharCode(97)] = new Array ('ы+','Й+','Ы+','й+','Ы','й','ы','Й','','');
abc2[String.fromCharCode(97)] = new Array ('ыа','Йа','Ыа','йа','Я','я','я','Я','а','a');

tra[String.fromCharCode(98)] = new Array ('','');
abc2[String.fromCharCode(98)] = new Array ('б','b');

tra[String.fromCharCode(118)] = new Array ('','');
abc2[String.fromCharCode(118)] = new Array ('в','v');

tra[String.fromCharCode(103)] = new Array ('','');
abc2[String.fromCharCode(103)] = new Array ('г','g');

tra[String.fromCharCode(100)] = new Array ('','');
abc2[String.fromCharCode(100)] = new Array ('д','d');

tra[String.fromCharCode(101)] = new Array ('Й+','й+','Й','й','','');
abc2[String.fromCharCode(101)] = new Array ('Йе','йе','Э','э','е','e');

tra[String.fromCharCode(111)] = new Array ('ы+','Й+','Ы+','й+','Ы','ы','Й','й','','');
abc2[String.fromCharCode(111)] = new Array ('ыо','Йо','Ыо','йо','Ё','ё','Ё','ё','о','o');

tra[String.fromCharCode(246)] = new Array ('','');
abc2[String.fromCharCode(246)] = new Array ('ё','ц');

tra[String.fromCharCode(104)] = new Array ('сх+','Сх+','з+','Сх','с+','ш+','Ц+','Ш+','С+','сх','ц+','З+','Ш','с','ц','ш','З','С','Ц','з','','');
abc2[String.fromCharCode(104)] = new Array ('схх','Схх','зх','Щ','сх','шх','Цх','Шх','Сх','щ','цх','Зх','Щ','ш','ч','щ','Ж','Ш','Ч','ж','х','h');

tra[String.fromCharCode(122)] = new Array ('','');
abc2[String.fromCharCode(122)] = new Array ('з','z');

tra[String.fromCharCode(105)] = new Array ('','');
abc2[String.fromCharCode(105)] = new Array ('и','i');

tra[String.fromCharCode(106)] = new Array ('','');
abc2[String.fromCharCode(106)] = new Array ('й','j');

tra[String.fromCharCode(107)] = new Array ('','');
abc2[String.fromCharCode(107)] = new Array ('к','k');

tra[String.fromCharCode(108)] = new Array ('','');
abc2[String.fromCharCode(108)] = new Array ('л','l');

tra[String.fromCharCode(109)] = new Array ('','');
abc2[String.fromCharCode(109)] = new Array ('м','m');

tra[String.fromCharCode(110)] = new Array ('','');
abc2[String.fromCharCode(110)] = new Array ('н','n');

tra[String.fromCharCode(112)] = new Array ('','');
abc2[String.fromCharCode(112)] = new Array ('п','p');

tra[String.fromCharCode(114)] = new Array ('','');
abc2[String.fromCharCode(114)] = new Array ('р','r');

tra[String.fromCharCode(115)] = new Array ('','');
abc2[String.fromCharCode(115)] = new Array ('с','s');

tra[String.fromCharCode(116)] = new Array ('','');
abc2[String.fromCharCode(116)] = new Array ('т','t');

tra[String.fromCharCode(117)] = new Array ('ы+','Й+','Ы+','й+','Ы','й','ы','Й','','');
abc2[String.fromCharCode(117)] = new Array ('ыу','Йу','Ыу','йу','Ю','ю','ю','Ю','у','u');

tra[String.fromCharCode(102)] = new Array ('','');
abc2[String.fromCharCode(102)] = new Array ('ф','f');

tra[String.fromCharCode(120)] = new Array ('','');
abc2[String.fromCharCode(120)] = new Array ('х','x');

tra[String.fromCharCode(99)] = new Array ('','');
abc2[String.fromCharCode(99)] = new Array ('ц','c');

tra[String.fromCharCode(119)] = new Array ('','');
abc2[String.fromCharCode(119)] = new Array ('щ','w');

tra[String.fromCharCode(35)] = new Array ('ъ+','ъ','','');
abc2[String.fromCharCode(35)] = new Array ('ъъ','Ъ','ъ','#');

tra[String.fromCharCode(121)] = new Array ('','');
abc2[String.fromCharCode(121)] = new Array ('ы','y');

tra[String.fromCharCode(39)] = new Array ('ь+','ь','','');
abc2[String.fromCharCode(39)] = new Array ('ьь','Ь','ь','\'');

tra[String.fromCharCode(228)] = new Array ('','');
abc2[String.fromCharCode(228)] = new Array ('э','д');

tra[String.fromCharCode(252)] = new Array ('','');
abc2[String.fromCharCode(252)] = new Array ('ю','ь');

tra[String.fromCharCode(65)] = new Array ('Ы+','Й+','Ы','Й','','');
abc2[String.fromCharCode(65)] = new Array ('ЫА','ЙА','Я','Я','А','A');

tra[String.fromCharCode(66)] = new Array ('','');
abc2[String.fromCharCode(66)] = new Array ('Б','B');

tra[String.fromCharCode(86)] = new Array ('','');
abc2[String.fromCharCode(86)] = new Array ('В','V');

tra[String.fromCharCode(71)] = new Array ('','');
abc2[String.fromCharCode(71)] = new Array ('Г','G');

tra[String.fromCharCode(68)] = new Array ('','');
abc2[String.fromCharCode(68)] = new Array ('Д','D');

tra[String.fromCharCode(69)] = new Array ('Й+','Й','','');
abc2[String.fromCharCode(69)] = new Array ('ЙЕ','Э','Е','E');

tra[String.fromCharCode(79)] = new Array ('Ы+','Й+','Ы','Й','','');
abc2[String.fromCharCode(79)] = new Array ('ЫО','ЙО','Ё','Ё','О','O');

tra[String.fromCharCode(214)] = new Array ('','');
abc2[String.fromCharCode(214)] = new Array ('Ё','Ц');

tra[String.fromCharCode(72)] = new Array ('СХ+','Ц+','СХ','С+','З+','Ш+','Ш','Ц','С','З','','');
abc2[String.fromCharCode(72)] = new Array ('СХХ','ЦХ','Щ','СХ','ЗХ','ШХ','Щ','Ч','Ш','Ж','Х','H');

tra[String.fromCharCode(90)] = new Array ('','');
abc2[String.fromCharCode(90)] = new Array ('З','Z');

tra[String.fromCharCode(73)] = new Array ('','');
abc2[String.fromCharCode(73)] = new Array ('И','I');

tra[String.fromCharCode(74)] = new Array ('','');
abc2[String.fromCharCode(74)] = new Array ('Й','J');

tra[String.fromCharCode(75)] = new Array ('','');
abc2[String.fromCharCode(75)] = new Array ('К','K');

tra[String.fromCharCode(76)] = new Array ('','');
abc2[String.fromCharCode(76)] = new Array ('Л','L');

tra[String.fromCharCode(77)] = new Array ('','');
abc2[String.fromCharCode(77)] = new Array ('М','M');

tra[String.fromCharCode(78)] = new Array ('','');
abc2[String.fromCharCode(78)] = new Array ('Н','N');

tra[String.fromCharCode(80)] = new Array ('','');
abc2[String.fromCharCode(80)] = new Array ('П','P');

tra[String.fromCharCode(82)] = new Array ('','');
abc2[String.fromCharCode(82)] = new Array ('Р','R');

tra[String.fromCharCode(83)] = new Array ('','');
abc2[String.fromCharCode(83)] = new Array ('С','S');

tra[String.fromCharCode(84)] = new Array ('','');
abc2[String.fromCharCode(84)] = new Array ('Т','T');

tra[String.fromCharCode(85)] = new Array ('Ы+','Й+','Ы','Й','','');
abc2[String.fromCharCode(85)] = new Array ('ЫУ','ЙУ','Ю','Ю','У','U');

tra[String.fromCharCode(70)] = new Array ('','');
abc2[String.fromCharCode(70)] = new Array ('Ф','F');

tra[String.fromCharCode(88)] = new Array ('','');
abc2[String.fromCharCode(88)] = new Array ('Х','X');

tra[String.fromCharCode(67)] = new Array ('','');
abc2[String.fromCharCode(67)] = new Array ('Ц','C');

tra[String.fromCharCode(87)] = new Array ('','');
abc2[String.fromCharCode(87)] = new Array ('Щ','W');

tra[String.fromCharCode(89)] = new Array ('','');
abc2[String.fromCharCode(89)] = new Array ('Ы','Y');

tra[String.fromCharCode(196)] = new Array ('','');
abc2[String.fromCharCode(196)] = new Array ('Э','Д');

tra[String.fromCharCode(220)] = new Array ('','');
abc2[String.fromCharCode(220)] = new Array ('Ю','Ь');

abc1['а'] = 'a';
abc1['б'] = 'b';
abc1['в'] = 'v';
abc1['г'] = 'g';
abc1['д'] = 'd';
abc1['е'] = 'e';
abc1['ё'] = 'jo';
abc1['ж'] = 'zh';
abc1['з'] = 'z';
abc1['и'] = 'i';
abc1['й'] = 'j';
abc1['к'] = 'k';
abc1['л'] = 'l';
abc1['м'] = 'm';
abc1['н'] = 'n';
abc1['о'] = 'o';
abc1['п'] = 'p';
abc1['р'] = 'r';
abc1['с'] = 's';
abc1['т'] = 't';
abc1['у'] = 'u';
abc1['ф'] = 'f';
abc1['х'] = 'h';
abc1['ц'] = 'c';
abc1['ч'] = 'ch';
abc1['ш'] = 'sh';
abc1['щ'] = 'w';
abc1['ъ'] = '#';
abc1['ы'] = 'y';
abc1['ь'] = '\'';
abc1['э'] = 'je';
abc1['ю'] = 'ju';
abc1['я'] = 'ja';
abc1['А'] = 'A';
abc1['Б'] = 'B';
abc1['В'] = 'V';
abc1['Г'] = 'G';
abc1['Д'] = 'D';
abc1['Е'] = 'E';
abc1['Ё'] = 'JO';
abc1['Ж'] = 'ZH';
abc1['З'] = 'Z';
abc1['И'] = 'I';
abc1['Й'] = 'J';
abc1['К'] = 'K';
abc1['Л'] = 'L';
abc1['М'] = 'M';
abc1['Н'] = 'N';
abc1['О'] = 'O';
abc1['П'] = 'P';
abc1['Р'] = 'R';
abc1['С'] = 'S';
abc1['Т'] = 'T';
abc1['У'] = 'U';
abc1['Ф'] = 'F';
abc1['Х'] = 'H';
abc1['Ц'] = 'C';
abc1['Ч'] = 'CH';
abc1['Ш'] = 'SH';
abc1['Щ'] = 'W';
abc1['Ъ'] = '##';
abc1['Ы'] = 'Y';
abc1['Ь'] = '\'\'';
abc1['Э'] = 'JE';
abc1['Ю'] = 'JU';
abc1['Я'] = 'JA';
var textareafontsize = 14; // warning: the value is also defined in the css-file !!!
var textreafontwidth = 7; // the width of the text is supposed to be 7 !!!

var pXpix = 0;
var pYpix = 0;
function get_texatrea_scroll_position()
{
  pXpix = window.document.comment_form.comment.scrollTop;
  pYpix = window.document.comment_form.comment.scrollLeft;
  return false;
}
function translateAlltoCyrillic()
{
  var inloop=1;
  get_texatrea_scroll_position()
  var tt = window.document.comment_form.comment; 
  var p1 = tt.selectionStart; 
  var p2 = tt.selectionEnd;
  var preval = ""; 
  var postval="";
  if (p1==p2) {txt = tt.value;}
  else
  {
    preval = tt.value.substring(0, p1);
    txt = tt.value.substring(p1,p2);
    postval = tt.value.substring(p2);
  }
  var txtnew = "";
  if ((!processhtmltags)&&(!processbbcodetags))
  {
    txtnew = translateStringtoCyrillic(txt);  
  }
  else
  {
    var htt1,pbb1,t1,t2,txt1,txt2,tag_open,tag_close;
    var noinputtag=0;
    if (processhtmltags) {tag_open="<";tag_close=">";}
    if (processbbcodetags) {tag_open="[";tag_close="]";}
    while(inloop)
    {
      if (processhtmltags&&processbbcodetags)
      {
        htt1 = txt.indexOf("<");
        pbb1 = txt.indexOf("[");
        if (pbb1==htt1) {noinputtag=1}
        if (pbb1==-1) {pbb1=txt.length;}
        if (htt1==-1) {htt1=txt.length;}    
        if (htt1<pbb1) {t1=htt1; tag_close=">";} else {t1=pbb1; tag_close="]";}
      }
      else
      {
        t1 = txt.indexOf(tag_open);
        if (t1==-1) noinputtag=1;
      }
      if (noinputtag) {inloop=0;t1=txt.length;t2=txt.length;}
      else
      {
        txt2=txt.substring(t1,txt.length);
        t2=txt2.indexOf(tag_close);
        //if (t2==-1) {t2=txt.length; inloop=0;} else {t2=t2+t1+1};
        if (t2==-1) {t2=t1+1} else {t2=t2+t1+1};
      }
      txt1 = txt.substring(0,t1);
      txt2 = txt.substring(t1,t2);
      txt = txt.substring(t2,txt.length);
      txtnew = txtnew+translateStringtoCyrillic(txt1)+txt2;     
    }
  }
  tt.value = preval+txtnew+postval;
  if (p1!=p2) {tt.setSelectionRange(p1+txtnew.length,p1+txtnew.length);}
  set_texatrea_scroll_position();
  setfoc();
  return false;
}
function setfoc() 
{
  document.comment_form.comment.focus();
  return false;
}
function set_texatrea_scroll_position() 
{
  var txtarea = document.comment_form.comment;
  var therest = txtarea.value.substr(txtarea.selectionEnd);
  var fbeg = txtarea.value.substring(0, txtarea.selectionStart);
  var vv = therest.search(/[\n\s]/);
  var r = laststringlength(fbeg)*(textreafontwidth+1)-pYpix-txtarea.clientWidth/2;
  var dd = Math.abs(2*r)<txtarea.clientWidth?0:r-txtarea.clientWidth/2*(r>0?1:-1);
  txtarea.scrollLeft = pYpix+dd+(dd==0?0:(dd>0)?2:-textreafontwidth-1);
  r = (nofstrings(fbeg+(vv==-1?therest:therest.substring(0, vv)),txtarea.clientWidth, textreafontwidth)-0.5)*(textareafontsize+3)-pXpix-txtarea.clientHeight/2;
  txtarea.scrollTop = pXpix+(Math.abs(2*r)<(txtarea.clientHeight-textareafontsize-3)?0:r-(txtarea.clientHeight-textareafontsize-3)/2*(r>0?1:-1));
  return false;
}

function nofstrings(thetext, txtareawidthpix, symbolwidth)
{
  var maxstrlengthallowed = Math.floor(txtareawidthpix/(symbolwidth+1))+1;
  var tt, pp, ppp, tuntil, ii;
  var t2 = thetext.split("\n");
  var s = t2.length;
  for (ii=0; ii<t2.length; ii++)
  {
    tt=t2[ii]+" ";
    pp = 0;
    tuntil = maxstrlengthallowed;
    while(tt.indexOf(" ",pp)!=-1)
    {
      ppp = pp;
      pp = tt.indexOf(" ",pp)+1;
      if (pp>tuntil&&pp-ppp-1)
      {
        tuntil = ppp+maxstrlengthallowed;
        if (pp<tuntil) 
        {
          pp = ppp;
        }
        s++;
      } 
    }   
  }
  return s;
}

function laststringlength(thetext)
{
  var t = thetext.replace(/\n/g," ");
  return thetext.replace(/\n/g," ").length-thetext.replace(/\n/g," ").lastIndexOf(" ")-1;
}

function translateStringtoCyrillic(thestringlat)
{
  var symbb,fromm,howmuch,thestringcyr="";
  for (kk=0;kk<thestringlat.length;kk++)
  {
    thestringcyr = translatesymboltocyrillic(thestringcyr+thestringlat.substr(kk,1))    
  }
  return thestringcyr;
}
function translatesymboltocyrillic(txt)
{
  var pretxt = txt.substr(0,txt.length-1);
  var last = txt.substr(txt.length-1,1);
  var lat = tra[last];
  var rus = abc2[last];
  if (lat)
  {
    for (var ii=0; ii<lat.length; ii++)
    {
      var pos = pretxt.length>lat[ii].length?(pretxt.length-lat[ii].length):0;
      if (lat[ii]==pretxt.substr(pos,pretxt.length-pos)) 
      {
        return pretxt.substr(0,pretxt.length-lat[ii].length)+rus[ii];
      }
    }
  }
  return txt;
}
