﻿
/////////// Including RTS 2 telugu
// by Suresh Kolichala
// http://eemaata.com/
//Convert RTS to Unicode Telugu
//Usage: mark the 'RTS' text with the mouse, right click, and select the "Convert RTS to Telugu" option.
//The '#' sign is used to switch between RTS and English. Telugu is assumed to be the default.

// Using six associative arrays.
// A pair for vowels, a pair for gunintam and another pair for consonants. 
// Always look for a match in 2 lettered array, before looking for a 1 letter match.
// We don't need a 3-letter match, because the only three letter syllable "ksh" is rendered 
// correctly even if we parse it as "k" + "sh".


// Array for match for 1 lettered accu
var accuEncoding1Lettered    = new Array();
accuEncoding1Lettered["a"]   = "అ";
accuEncoding1Lettered["A"]   = "ఆ"; 
accuEncoding1Lettered["i"]   = "ఇ";
accuEncoding1Lettered["I"]   = "ఈ";
accuEncoding1Lettered["u"]   = "ఉ";
accuEncoding1Lettered["U"]   = "ఊ";
accuEncoding1Lettered["R"]   = "ఋ";
accuEncoding1Lettered["e"]   = "ఎ";
accuEncoding1Lettered["E"]   = "ఏ";
accuEncoding1Lettered["o"]   = "ఒ";
accuEncoding1Lettered["O"]   = "ఓ";
accuEncoding1Lettered["M"]   = "ం";

// Array for matching 2-letter vowel
var accuEncoding2Lettered = new Array();
accuEncoding2Lettered["aa"]  =  "ఆ";
accuEncoding2Lettered["ii"]  =  "ఈ";
accuEncoding2Lettered["ee"]  =  "ఈ";
accuEncoding2Lettered["uu"]  =  "ఊ";
accuEncoding2Lettered["oo"]  =  "ఊ";
accuEncoding2Lettered["ua"]  =  "ఊ";
accuEncoding2Lettered["Ru"]  =  "ౠ";
accuEncoding2Lettered["RU"]  =  "ౠ";
accuEncoding2Lettered["~L"]  =  "ౡ";
accuEncoding2Lettered["ae"]  =  "ఏ";
accuEncoding2Lettered["ai"]  =  "ఐ";
accuEncoding2Lettered["oa"]  =  "ఓ";
accuEncoding2Lettered["oe"]  =  "ఓ";
accuEncoding2Lettered["au"]  =  "ఔ";
accuEncoding2Lettered["ou"]  =  "ఔ";
accuEncoding2Lettered["@H"]  =  "ః";
accuEncoding2Lettered["@h"]  =  "ః";
accuEncoding2Lettered["@M"]  =  "ఁ";
accuEncoding2Lettered["@m"]  =  "ఁ";
accuEncoding2Lettered["@2"]  =  "உ";

// Array for match for 1 lettered gunintam
var gunintamEncoding1Lettered    = new Array();
gunintamEncoding1Lettered["a"]   = "";
gunintamEncoding1Lettered["A"]   = "ా";
gunintamEncoding1Lettered["i"]   = "ి";
gunintamEncoding1Lettered["I"]   = "ీ";
gunintamEncoding1Lettered["u"]   = "ు";
gunintamEncoding1Lettered["U"]   = "ూ";
gunintamEncoding1Lettered["R"]   = "ృ";
gunintamEncoding1Lettered["e"]   = "ె";
gunintamEncoding1Lettered["E"]   = "ే";
gunintamEncoding1Lettered["o"]   = "ొ";
gunintamEncoding1Lettered["O"]   = "ో";
gunintamEncoding1Lettered["M"]   = "ం";

// Array for 2-letter gunintam
var gunintamEncoding2Lettered = new Array ();
gunintamEncoding2Lettered["aa"]  =  "ా";
gunintamEncoding2Lettered["ii"]  =  "ీ";
gunintamEncoding2Lettered["ee"]  =  "ీ";
gunintamEncoding2Lettered["uu"]  =  "ూ";

gunintamEncoding2Lettered["oo"]  =  "ూ";
gunintamEncoding2Lettered["ua"]  =  "ూ";
gunintamEncoding2Lettered["Ru"]  =  "ౄ";
gunintamEncoding2Lettered["RU"]  =  "ౄ";
gunintamEncoding2Lettered["~L"]  =  "";
gunintamEncoding2Lettered["ae"]  =  "ే";
gunintamEncoding2Lettered["ai"]  =  "ై";
gunintamEncoding2Lettered["oa"]  =  "ో";
gunintamEncoding2Lettered["oe"]  =  "ో";
gunintamEncoding2Lettered["au"]  =  "ౌ";
gunintamEncoding2Lettered["ou"]  =  "ౌ";
gunintamEncoding2Lettered["@H"]  =  "ః";
gunintamEncoding2Lettered["@h"]  =  "ః";

// Hallu encoding 1 lettered.
var halluEncoding1Lettered = new Array();
halluEncoding1Lettered["k"]      = "క";
halluEncoding1Lettered["K"]      = "ఖ";
halluEncoding1Lettered["q"]      = "ఖ";
halluEncoding1Lettered["g"]      = "గ";
halluEncoding1Lettered["c"]      = "చ";
halluEncoding1Lettered["C"]      = "ఛ";
halluEncoding1Lettered["G"]      = "ఘ";
halluEncoding1Lettered["j"]      = "జ";
halluEncoding1Lettered["z"]      = "జ";
halluEncoding1Lettered["J"]      = "ఝ";
halluEncoding1Lettered["T"]      = "ట";
halluEncoding1Lettered["D"]      = "డ";
halluEncoding1Lettered["N"]      = "ణ"; 
halluEncoding1Lettered["t"]      = "త";
halluEncoding1Lettered["d"]      = "ద";
halluEncoding1Lettered["n"]      = "న"; 
halluEncoding1Lettered["p"]      = "ప";
halluEncoding1Lettered["f"]      = "ఫ"; 
halluEncoding1Lettered["b"]      = "బ";
halluEncoding1Lettered["B"]      = "భ";
halluEncoding1Lettered["m"]      = "మ";
halluEncoding1Lettered["y"]      = "య"; 
halluEncoding1Lettered["Y"]      = "య"; 
halluEncoding1Lettered["r"]      = "ర";
halluEncoding1Lettered["l"]      = "ల";
halluEncoding1Lettered["L"]      = "ళ"; 
halluEncoding1Lettered["v"]      = "వ";
halluEncoding1Lettered["V"]      = "వ";
halluEncoding1Lettered["w"]      = "వ";
halluEncoding1Lettered["W"]      = "వ";
halluEncoding1Lettered["s"]      = "స";
halluEncoding1Lettered["S"]      = "శ";
halluEncoding1Lettered["h"]      = "హ";
halluEncoding1Lettered["H"]      = "హ";
halluEncoding1Lettered["x"]      = "క్ష";
halluEncoding1Lettered["X"]      = "క్ష";
// Consonant encoding - 2 lettered
var halluEncoding2Lettered = new Array();
halluEncoding2Lettered["kh"]     = "ఖ";
halluEncoding2Lettered["Kh"]     = "ఖ";
halluEncoding2Lettered["gh"]     = "ఘ";
halluEncoding2Lettered["Gh"]     = "ఘ";
halluEncoding2Lettered["~m"]     = "ఙ";
halluEncoding2Lettered["ch"]     = "చ";
halluEncoding2Lettered["Ch"]     = "ఛ";
halluEncoding2Lettered["CH"]     = "ఛ";
halluEncoding2Lettered["jh"]     = "ఝ";
halluEncoding2Lettered["Jh"]     = "ఝ";
halluEncoding2Lettered["~n"]     = "ఞ";
halluEncoding2Lettered["Th"]     = "ఠ";
halluEncoding2Lettered["Dh"]     = "ఢ"; 
halluEncoding2Lettered["th"]     = "థ";
halluEncoding2Lettered["dh"]     = "ధ";
//halluEncoding2Lettered["n&"]   = "న";
//halluEncoding2Lettered["n^"]   = "న";
halluEncoding2Lettered["ph"]     = "ఫ";
halluEncoding2Lettered["Ph"]     = "ఫ";
halluEncoding2Lettered["bh"]     = "భ";
halluEncoding2Lettered["Bh"]     = "భ";
//halluEncoding2Lettered["m&"]   = "మ";
//halluEncoding2Lettered["m^"]   = "మ";
halluEncoding2Lettered["~r"]     = "ఱ";
halluEncoding2Lettered["sh"]     = "ష";
halluEncoding2Lettered["Sh"]     = "ష";

//convert the selection to telugu.
function RTStext(text)
{
	this.rtsText = text;
	this.curPos = 0;
	this.length = text.length;
	this.currentMatchCount = 0;
}

function rts2unicode(myField)
{
	//IE support
	if (document.selection) {
		myField.focus();
	    	sel = document.selection.createRange();
		if (sel.text.length > 0) {
			var selectionText = new RTStext(sel.text);
			sel.text = rts2telugu(selectionText);
		}
		else {
			alert("Select RTS text and then click \"RTS to Telugu\"");
		}
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		var selectionText = new RTStext(myField.value.substring(startPos, endPos));
		var teluguText = rts2telugu(selectionText);
		myField.value = myField.value.substring(0, startPos)
		              +  teluguText
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + teluguText.length;
		myField.selectionEnd = startPos + teluguText.length;
		myField.scrollTop = scrollTop;
	} else {
		myField.value += myValue;
		myField.focus();
	}
}

function rts2telugu(sSelectedText)
{
	//alert(sSelectedText.rtsText);
	var sTeluguText = "";
	var  isTelugu = true;
      var isPrevTokenHallu = false;
      
	var len = sSelectedText.rtsText.length;
	//alert(len);
	while (/\s/.test(sSelectedText.rtsText.charAt(sSelectedText.curPos)))
	{
		sSelectedText.curPos++;
	}
	// Keep the language telugu, if they choose the first char as #.
	if (isNextTokenLanguageChanger(sSelectedText) == 1)
	{
		isTelugu = true;
		sSelectedText.curPos++;
	}
	while (sSelectedText.curPos < len)
	{         
		var matchCount = isNextTokenLanguageChanger(sSelectedText);
		if (matchCount)
		{
			sSelectedText.curPos += matchCount;
			if (matchCount == 1)
			{
				// toggle the isTelugu flag.
				if (isTelugu )
				{
					isTelugu = false;
					if (isPrevTokenHallu)
						sTeluguText = sTeluguText + "్";
				}
				else
				{
					isTelugu = true;
				}
			}
			else if (matchCount == 2) // ##
			{
				sTeluguText = sTeluguText + "#";
			}
			continue;
		}

                if (isTelugu)
		{	
			//Check for HTTP://
			//alert("rts text=" + sSelectedText.rtsText + " curPos=" + sSelectedText.curPos);
			matchCount = isNextTokenHttp(sSelectedText);
			if (matchCount)
			{
				 sTeluguText = sTeluguText +  sSelectedText.rtsText.substring(sSelectedText.curPos, sSelectedText.curPos+matchCount); 
				sSelectedText.curPos += matchCount;
				isPrevTokenHallu = false;
				continue;
			}
                                                                                          // Check if it is accu
			matchCount = isNextTokenAccu(sSelectedText);
			if (matchCount)
			{
				//alert("accu " + matchCount);
				if (isPrevTokenHallu)
					sTeluguText = sTeluguText + encodeGunintam(sSelectedText, matchCount);
				else
					sTeluguText = sTeluguText + encodeAccu(sSelectedText, matchCount);
				sSelectedText.curPos += matchCount;
				isPrevTokenHallu = false;
				//alert("sTeluguText");
				continue;
			}

			// Check if it is sunna
			if (isPrevTokenHallu == false && isNextTokenSunna(sSelectedText))
                  {
			//alert("sunna " + sSelectedText.curPos);
			sTeluguText = sTeluguText + "ం"; // Add sunna.
			sSelectedText.curPos++;
			isPrevTokenHallu = false;
			continue;
                  }
			
			// Check if the next token is special character like ^ or &.
			if (sSelectedText.rtsText.charAt(sSelectedText.curPos) == '^' ||
			    sSelectedText.rtsText.charAt(sSelectedText.curPos) == '&')
			{
				sSelectedText.curPos++;
				if (isPrevTokenHallu)
				{
					sTeluguText = sTeluguText + "్";
					isPrevTokenHallu = false;
				}
			}

			else
			{
				// Add pollu if previous was a hallu.
				
				if (isPrevTokenHallu)
				{
					sTeluguText = sTeluguText + "్";
					isPrevTokenHallu = false;
				}	
				
				matchCount = isNextTokenHallu(sSelectedText);
				if (matchCount)
				{
					//alert("hallu " + matchCount);
					sTeluguText = sTeluguText + encodeHallu(sSelectedText, matchCount);
					sSelectedText.curPos += matchCount;
					isPrevTokenHallu = true;
				}
				else
				{
					//Copy the text as is.
					sTeluguText = sTeluguText+sSelectedText.rtsText.charAt(sSelectedText.curPos);
					sSelectedText.curPos++;
				}
			}	
		}
		else 
		{
			// we are in English mode. We took care of "#" already.
			// just copy the text.
			sTeluguText = sTeluguText+sSelectedText.rtsText.charAt(sSelectedText.curPos);
			sSelectedText.curPos++;
		}
	}
	if (isTelugu && isPrevTokenHallu)
	{
		sTeluguText = sTeluguText + "్";
	}
	return sTeluguText;
}


function isNextTokenAccu(sSelectedText)
{
	//alert("Accu rts text=" + sSelectedText.rtsText + " curPos=" + sSelectedText.curPos);
      var mCount = 0;
	// First check if there is any possibility of a vowel.
	if(/[aAeEiIoOuURLlHhM~@]/.test(sSelectedText.rtsText.charAt(sSelectedText.curPos)))
	{
		if (accuEncoding2Lettered[sSelectedText.rtsText.substring(sSelectedText.curPos, sSelectedText.curPos+2)] != null)
      	{
			mCount = 2;
	     	}
		else if (accuEncoding1Lettered[sSelectedText.rtsText.charAt(sSelectedText.curPos)] != null)
		{
			mCount = 1;
		}
	}
	return mCount;
}
	
function isNextTokenSunna(sSelectedText)
{  
	//alert("Sunna rts text=" + sSelectedText.rtsText + " curPos=" + sSelectedText.curPos);
	if (sSelectedText.rtsText.charAt(sSelectedText.curPos)=='n')
	{
            var x = sSelectedText.rtsText.charAt(sSelectedText.curPos+1);
            return(/[kgcjtdpb]/i.test(x) );
      }
      else if (sSelectedText.rtsText.charAt(sSelectedText.curPos) == 'm')
      {
		return (/[^aeiou~nNmyrl^&]/i.test(sSelectedText.rtsText.charAt(sSelectedText.curPos+1)));
      }
      return false;
}
function isNextTokenHttp(sSelectedText)
{  
	var mCount = 0;
	//alert("HTTP rts text=" + sSelectedText.rtsText + " curPos=" + sSelectedText.curPos);
	var httpRegEx=/^http:\/\//i;
	if(httpRegEx.test(sSelectedText.rtsText.substring(sSelectedText.curPos,sSelectedText.curPos+7))) {
		mCount=7;
		while (/[\w\./#]/.test(sSelectedText.rtsText.charAt(sSelectedText.curPos+mCount))) {
			mCount ++;
		}
	}
	return mCount;
}

function isNextTokenHallu(sSelectedText)
{
	var mCount = 0;	
	//alert("Hallu rts text=" + sSelectedText.rtsText + " curPos=" + sSelectedText.curPos);
	// First check if there is any possibility of a vowel.
	if (halluEncoding2Lettered[sSelectedText.rtsText.substring(sSelectedText.curPos, sSelectedText.curPos+2)] != null)
      {
		mCount = 2;
	}
	else if (halluEncoding1Lettered[sSelectedText.rtsText.charAt(sSelectedText.curPos)] != null)
	{
		mCount = 1;
	}
	return mCount;
}

function isNextTokenLanguageChanger(sSelectedText)
{
	var mCount = 0;
	if (sSelectedText.rtsText.charAt(sSelectedText.curPos) == '#')
	{
		if (sSelectedText.rtsText.charAt(sSelectedText.curPos+1) == '#')
		{
			mCount = 2;
		}
            else
            {
                mCount = 1;
		}
	}
	return mCount;
}

function encodeAccu(sSelectedText, mCount)
{
	var sTeluguText = "";
	if (mCount == 2)
			sTeluguText = accuEncoding2Lettered[sSelectedText.rtsText.substring(sSelectedText.curPos,sSelectedText.curPos+2)];
	else if (mCount == 1)
			sTeluguText = accuEncoding1Lettered[sSelectedText.rtsText.charAt(sSelectedText.curPos)];
	return sTeluguText;
}

function encodeHallu(sSelectedText, mCount)
{
	var sTeluguText = "";
	if (mCount == 2)
	{
		sTeluguText = halluEncoding2Lettered[sSelectedText.rtsText.substring(sSelectedText.curPos,sSelectedText.curPos+2)];
	}
	else if (mCount == 1)
	{
		sTeluguText = halluEncoding1Lettered[sSelectedText.rtsText.charAt(sSelectedText.curPos)];
	}
	return sTeluguText;
}

function encodeGunintam(sSelectedText, mCount)
{
	var sTeluguText = "";
	if (mCount == 2)
			sTeluguText = gunintamEncoding2Lettered[sSelectedText.rtsText.substring(sSelectedText.curPos,sSelectedText.curPos+2)]; 
	else if (mCount == 1)
			sTeluguText = gunintamEncoding1Lettered[sSelectedText.rtsText.charAt(sSelectedText.curPos)]; 
	return sTeluguText;
}

