//// TRANSLIT
/* Skip these strings */
var undoeng = new Array();
var t_skip = new Array( "html", "", "code", "", "img", "", "php", "", "quote", "( [\"']?[^" + String.fromCharCode(92,93) + "]+)?", "email", "(=[\"']?[a-zA-Z0-9_.-]+@?[a-zA-Z0-9_.-]+[\"']?)?",	"url", "(=[\"']?[^ \"'" + String.fromCharCode(92,93) + "]*[\"']?)?" );

/* Two character letters */
var t_table2 = "KHYOJOZHCHSHYUJUYAJAkhyojozhchshyujuyajaKhYoJoZhChShYuJuYaJa";
var w_table2 = "Õ ¨ ¨ Æ × Ø Þ Þ ß ß õ ¸ ¸ æ ÷ ø þ þ ÿ ÿ Õ ¨ ¨ Æ × Ø Þ Þ ß ß ";

/* One character letters */
var t_table1 = "ABVGDEZIJKLMNOPRSTUFHCWYXQabvgdezijklmnoprstufhxcwy'`~q";
var w_table1 = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÛÚÝàáâãäåçèéêëìíîïðñòóôõõöùûüüúý";

/* HTML Special characters */
spec_table=new Array("&trade;","&amp;","&lt;","&gt;","&nbsp;","&copy;","&reg;");

function translit2win(str) 
{
  len = str.length;
  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 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;
			}
	  }
  }
  
    if (str.substr(i).indexOf("^") == 0)
    {
      end_len = str.substr(i + 1).indexOf("^") + 2;
      if (end_len > 1)
      {
        new_str += str.substr(i, end_len);
        i += end_len - 1;
        continue;
      }
    }

    if (str.substr(i).indexOf(":") == 0)
    {
      end_len = str.substr(i + 1).indexOf(":") + 2;
      if (end_len > 1 && str.substr(i, end_len).match("^:[a-zA-Z0-9]+:$"))
      {
        new_str += str.substr(i, end_len);
        i += end_len - 1;
        continue;
      }
    }

    /* Check for valid ed2k:/.../ links */
    re = new RegExp("^(ed2k:" + String.fromCharCode(92,47,92,47,91,94,92,47,93,43,41), "i");
    if (newArr = str.substr(i).match(re))
    {
      new_str += newArr[1];
      i += newArr[1].length - 1;
      continue;
    }

    /* Check for valid http|new|ftp:/.../ links */
    re = new RegExp("^((http|https|news|ftp):" + String.fromCharCode(92,47,92,47,91,92,47) + "a-zA-Z0-9%_?.:+=@-]+)", "i");
    if (newArr = str.substr(i).match(re))
    {
      new_str += newArr[1];
      i += newArr[1].length - 1;
      continue;
    }

    /* Check for simple tags */
    re = new RegExp(String.fromCharCode(94,40,92,91,92,47,63) + "(b|i|u|s|highlight|left|center|right|indent|list(=[a-z0-9]+)?|font(=[a-z0-9]+)?|size(=[0-9]+)?|thread(=[0-9]+)?|post(=[0-9]+)?|color(=#?[a-z0-9]+)?|quote|attachmentid(=[0-9]+)?)" + String.fromCharCode(92,93,41), "i");
    if (newArr = str.substr(i).match(re))
    {
      new_str += newArr[1];
      i += newArr[1].length - 1;
      continue;
    }

    /* Check for tags, described in t_skip[] array */
    tag_skip=false;
    for(j = 0; j < t_skip.length; j+=2)
    {
      re = new RegExp(String.fromCharCode(94,40,92,91) + t_skip[j] + t_skip[j+1] + String.fromCharCode(92,93,41), "i");
      if (newArr = str.substr(i).match(re))
      {
        re = new RegExp(String.fromCharCode(92,91,92,47) + t_skip[j] + String.fromCharCode(92,93), "i");
        if (end_pos = str.substr(i + newArr[1].length + 2).search(re))
        {
          end_len = end_pos + newArr[1].length + 1 + t_skip[j].length + 3;
          new_str += str.substr(i, end_len);
          i += end_len - 1;
          tag_skip = true;
        }
      }
      if (tag_skip)
        break;
    }
    if (tag_skip) continue;

    /* Check for valid (mailto:)email@host.com links */
    re = new RegExp("^((mailto:)?[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+)", "i");
    if (newArr = str.substr(i).match(re))
    {
      new_str += newArr[1];
      i += newArr[1].length - 1;
      continue;
    }

    /* Check for 2-character letters */
    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*2, 1);
          i++;
          tag_skip = true;
          break;
        }
      }
    }
    if (tag_skip) continue;

    /* Convert one-character letter */
    c = str.substr(i, 1);
    pos = t_table1.indexOf(c);
    if (pos < 0)
    {
      new_str += c;
    }
    else
    {
      new_str += w_table1.substr(pos, 1);
    }
  }
  if (new_str != str)
  {
    undoeng[undoeng.length] = str;
  }
  return new_str;
}

function untranslit(old_str) 
{
  str = "";
  if(undoeng.length)
  {
    str = undoeng[undoeng.length-1];
    undoeng.length--;
  }
	if(str != "")
		return str;
	else
		return old_str; 
}
