/*
 *  Cyrillic-Latin Converter
 *  converts cyrillic letters to latin ones and vice versa
 *
 *  Copyright (c) 2003 Borislav Manolov
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  About the author:
 *      e-mail: b.manolov@web.de
 *   home page: http://purl.oclc.org/NET/manolov/
 */

function CyrLatConvertor() {
  this.COUNT = 60;
  this.actions = new Array("lat-cyr-neo",
                           "cyr-lat-neo",
                           "cyr-lat-classic",
                           "cyr-lat-custom");
  this.encodings = new Array("Windows-1251", "KOI8-R");
  this.action = this.actions[0];
  this.encoding = this.encodings[0];
  this.latArr = new Array(
              'A','B','V','G','D','E','J','Z','I','Y',
              'K','L','M','N','O','P','R','S','T','U',
              'F','H','C','5','7','W','3','8','X','Q',
              'a','b','v','g','d','e','j','z','i','y',
              'k','l','m','n','o','p','r','s','t','u',
              'f','h','c','4','6','w','2','1','x','q');
  this.cyrArr_windows_1251 = new Array(
              'À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É',
              'Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó',
              'Ô','Õ','Ö','×','Ø','Ù','Ú','Ü','Þ','ß',
              'à','á','â','ã','ä','å','æ','ç','è','é',
              'ê','ë','ì','í','î','ï','ð','ñ','ò','ó',
              'ô','õ','ö','÷','ø','ù','ú','ü','þ','ÿ');
  this.cyrArr_koi8_r = new Array(
              'þ','à','á','ö','ä','å','ô','ã','õ','è',
              'é','ê','ë','ì','í','î','ï','ÿ','ð','ñ',
              'ò','ó','æ','â','ü','û','ç','ý','÷','ú',
              'Þ','À','Á','Ö','Ä','Å','Ô','Ã','Õ','È',
              'É','Ê','Ë','Ì','Í','Î','Ï','ß','Ð','Ñ',
              'Ò','Ó','Æ','Â','Ü','Û','Ç','Ý','×','Ú');
  this.latArrClassic = new Array(
              'A','B','V','G','D','E','Zh','Z','I','J',
              'K','L','M','N','O','P','R','S','T','U',
              'F','H','C','Ch','Sh','Sht','Y','J','Ju','Ja',
              'a','b','v','g','d','e','zh','z','i','j',
              'k','l','m','n','o','p','r','s','t','u',
              'f','h','c','ch','sh','sht','y','j','ju','ja');
  this.latArrCustom = this.latArrClassic;

  this.showClassic = showClassic;
  this.convert = convert;
  this.replaceCustom = replaceCustom;
}

function showClassic() {
    var msg = "";
    for (i=1; i<= this.COUNT; i++) {
        msg += this.cyrArr_windows_1251[i-1] + "-" +
               this.latArrClassic[i-1];
        if (i < this.COUNT)
            msg += " | ";
    }
    return msg;
}

function convert(input) {
  var cyrArr = new Array();
  var srcArr = new Array();
  var targetArr = new Array();
  var assocArr = new Array();

  if (this.action == this.actions[0]) {
      srcArr = this.latArr;
      if (this.encoding == this.encodings[0]) {
          targetArr = this.cyrArr_windows_1251;
      } else {
          targetArr = this.cyrArr_koi8_r;
      }
  } else {
      srcArr = this.cyrArr_windows_1251;
      if (this.action == this.actions[1])
          targetArr = this.latArr;
      else if (this.action == this.actions[2])
          targetArr = this.latArrClassic;
      else
          targetArr = this.latArrCustom;
  }

  for (var i=0; i < this.COUNT; i++){
      assocArr[srcArr[i]] = targetArr[i]
  }

  var everyChar = '';
  var output = "";
  var inputLen = input.length;
  var isOneForNoTransl = false;
  var isMoreForNoTransl = false;
  var toWork = true

  if (inputLen > 4999){
      var msg = "There are " + inputLen +" signs to convert.\n"
      msg += "If your computer is not very fast your browser can hang up. \n"
      msg += "First <save your data> before you work on it.\n\n"
      msg += "Will you continue?\n\n"

      toWork = confirm(msg)?true:false
  }
  if (toWork){
         status = "Converting, please wait..."
      for (var i=0; i < inputLen; i++) {
          everyChar = input.charAt(i);
          if (isOneForNoTransl) {
              output += everyChar;
              isOneForNoTransl = false;
          }
          else if (isMoreForNoTransl){
               if (everyChar == "$") {
                  isMoreForNoTransl = false;
               } else {
                  output += everyChar;
               }
          } else {
              switch (everyChar) {
                  case "$" :  isMoreForNoTransl = true; break;
                  case "#" :  isOneForNoTransl = true; break;
                  default  :
                      if (assocArr[everyChar] != undefined){
                          output += assocArr[everyChar]
                      } else {
                          output += everyChar
                      }
              }
          }
      }// end for
      status = ""
      return output
  }// end if(toWork)
  return ''
} // end convert()


function replaceCustom(indexForReplace, newChar) {
  this.latArrCustom[indexForReplace] = newChar;
}

