// One character letters
t_table1 = "ABVGDEZIJKLMNOPRSTUQFHXCYabvgdezijklmnoprstuqfhxcy'";
w_table1 = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÝÔÕÕÖÛàáâãäåçèéêëìíîïðñòóýôõõöûü";

// Two character letters
t_table2 = "YOJOZHCHSHXHYUJUYAJAyojozhchshxhyujuyajaYoYoZhChShXhYuJuYaJa";
w_table2 = "¨¨Æ×ØÙÞÞßß¸¸æ÷øùþþÿÿ¨¨Æ×ØÙÞÞßß";

// HTML Special characters
spec_table=new Array("&trade;","&amp;","&lt;","&gt;","&nbsp;","&copy;","&reg;");


function translitTagAware(str) 
{
 var len = str.length;
 var new_str="";

 for (i = 0; i < len; i++)
 {
  // Skip tags

  while ((i<len) && (str.substr(i,1)=="<"))
  {
      while ( (i<len) && ((c=str.substr(i,1))!=">") )
  {
  new_str+=c;
  i++;
  }
  new_str+=">";
  if (i<len-1)
  {
  i++;
  }
  else
  return new_str;
  }
  
  //skip BBCode
  while ((i<len) && (str.substr(i,1)=="["))
  {
      while ((i<len) && ((c=str.substr(i,1))!="]"))
  {
  new_str+=c;
  i++;
  }
  new_str+="]";
  if (i<len-1)
  {
  i++;
  }
  else
  return new_str;
  }
  
  //skip html special characters
  while (str.substr(i,1)=='&')
  {
  for (j=0; j<spec_table.length; j++)
  {
var spec_len=spec_table[j].length;
if ((i<=len-spec_len) && (str.substr(i,spec_len)==spec_table[j]))
{
new_str+=str.substr(i,spec_len);
i+=spec_len;
if (i==len)
{
return new_str;
}
break;
}
  }
  }
  
  // Check for 2-character letters
  is2char=false;
  if (i < len-1) {
   for(j = 0; j < w_table2.length; j++)
   {
    if(str.substr(i, 2) == t_table2.substr(j*2,2)) {
     new_str+= w_table2.substr(j, 1);
     i++;
     is2char=true;
     break;
    }
   }
  }

  if(!is2char) {
   // 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.substr(pos, 1);
  }
 }

 return new_str;
}