// One character letters
var t_table1 = "ABVGDEZIJKLMNOPRSTUFHXCYabvgdezijklmnoprstufhxcy'";
var w_table1 = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÕÖÛàáâãäåçèéêëìíîïðñòóôõõöûü";

// Two character letters
var t_table2 = "E'YOJOZHCHSHYUJUYAJAe'yojozhchshyujuyajaE'YoYoZhChShYuJuYaJa";
var w_table2 = "Ý¨¨Æ×ØÞÞßßý¸¸æ÷øþþÿÿÝ¨¨Æ×ØÞÞßß";

// Four character letters
var t_table4 = "shchShchSHCH";
var w_table4 = "ùÙÙ";


function translit2win(str) {
  var quoteOpen = 0;
  var engOpen = 0;
  var len = str.length;
  var new_str="";

  for(i=0; i<len; i++) {
    // [QUOTE] Tags
    if (i<=len-7 && str.substr(i,7).toUpperCase()=="[QUOTE]") {
      quoteOpen++;
      new_str += str.substr(i,7);
      i+=6;	 
      continue;
    }
    if (i<=len-8 && str.substr(i,8).toUpperCase()=="[/QUOTE]") {
      quoteOpen--;
      new_str += str.substr(i,8);
      i+=7;	
      continue;
    }
    // [ENG] Tags
    if (i<=len-5 && str.substr(i,5).toUpperCase()=="[ENG]") {
      engOpen++;
      i+=4;	 
      continue;
    }
    if (i<=len-6 && str.substr(i,6).toUpperCase()=="[/ENG]") {
      engOpen--;
      i+=5;	
      continue;
    }
    // vB [b],[i] and [u] tags
    if (i<=len-3 && (str.substr(i,3).toUpperCase()=="[B]" || str.substr(i,3).toUpperCase()=="[I]" || str.substr(i,3).toUpperCase()=="[U]")) {
      new_str += str.substr(i,3);
      i+=2;	
      continue;
    }
    if (i<=len-4 && (str.substr(i,4).toUpperCase()=="[/B]" || str.substr(i,4).toUpperCase()=="[/I]" || str.substr(i,3).toUpperCase()=="[/U]")) {
      new_str += str.substr(i,4);
      i+=3;	
      continue;
    }
    if (!quoteOpen && !engOpen) {
      var is2char = false;
      var is4char = false;
      if (i<len-3) {
        // Check for 4-character letters
        for (j=0; j<w_table4.length; j++) {
          if (str.substr(i,4)==t_table4.substr(j*4,4)) {
            new_str+= w_table4.charAt(j);
            i=i+3;
            is4char=true;
            break;
          }
        }
      }
      if ((i<len-1) && (is4char==false)) {
        // Check for 2-character letters
        for (j=0; j<w_table2.length; j++) {
          if (str.substr(i,2)==t_table2.substr(j*2,2)) {
            new_str+= w_table2.charAt(j);
            i++;
            is2char=true;
            break;
          }
        }
      }  
      if ((is2char==false) && (is4char==false)) {
        // Convert one-character letter
        var c = str.substr(i,1);
        var pos = t_table1.indexOf(c);
        if (pos<0) {
          new_str += c;
        } else {
          new_str += w_table1.charAt(pos);
        }
      }
    } else {
      new_str += str.charAt(i);
    }
  }
  //alert(new_str);
  return new_str;
}  

function tra2win(theform) {
  with(theform.message) {
    value=translit2win(value); 
    focus();
  }
}
