/**
 * Ekushey Bangla keyboard parser version 3.0
 * This script helps to type bangla in webpages 100% according to bijoy keyboard layout
 * It supports every possible Joint Character and Kars
 *
 * Developed By Hasin Hayder at 11th January, 2004
 * First Official Release 11th January, 2004 at 8:20 PM
 *
 * Bug fixed for Ho+Mo [ Brahmon ] and Murdhonno So + Murdhonno No [ushno]
 * At 12th January 2004 at 10:30 AM
 */

/**
 * made compatible to FireFox
 * version 2.0
 * Bugs Removed
 * at 12th Nov, 05 By Hasin Hayder
 * email me at countdraculla@gmail.com
 */

/**
 * made phonetic
 * version 3.0
 * at 5th January, 06 By Hasin Hayder
 * email me at countdraculla@gmail.com
 * 
 * you must take permission to use this script in your website
 */
 
/**
 * fixed jo fola + preceding kar bug
 * at 7th January, 06 By Hasin Hayder
 * email me at countdraculla@gmail.com
 * 
 * you must take permission to use this script in your website
 */ 
 
 /**
 * fixed oi kar and first conjunction bug for IE
 * at 20th January, 06 By Hasin Hayder
 * email me at countdraculla@gmail.com
 * 
 * you must take permission to use this script in your website
 */ 
 
/**
 * 01st February
 * added n+s
 * modified b+b
 * fixed bugs for oi kar in firefox
 */

/**
 * fixed s+r
 * 3rd feb
 */

// Set of Characters
var activeta; //active text area
var phonetic=new Array();

// phonetic bangla equivalents
phonetic['k'] = 'K'; // ko
phonetic['oi']='‰'; // oi kar
phonetic['OI']='H'; // oi
phonetic['i']='w'; // hrossho i kar
phonetic['I']='B'; // hrossho i
phonetic['ii']='x'; // dirgho i kar
phonetic['II']='C'; // dirgho i
phonetic['e']='‡'; // e kar
phonetic['E'] = 'G'; // E
phonetic['U'] = 'D'; // hrossho u
phonetic['u'] = 'y'; // hrossho u kar
phonetic['uu'] = '~'; // dirgho u kar
phonetic['UU'] = 'E'; // dirgho u
phonetic['r']='i'; // ro
phonetic['WR']='F'; // wri
phonetic['a']='v'; // a kar
phonetic['A']='Av'; // shore a
phonetic['ao']='A'; // shore o
phonetic['s']='m'; // dontyo so
phonetic['t']='U'; // to
phonetic['K'] = 'L'; // Kho
phonetic['K+r'] = 'Lª'; // Kho + rofola
phonetic['kh'] = 'L'; // kho
phonetic['kh+r'] = 'Lª'; //kho + ro fola
phonetic['n']='b'; // dontyo no
phonetic['N']='Y'; // murdhonyo no
phonetic['T']='Z'; // tto
phonetic['Th']='_'; // ttho
phonetic['Th+r']='_ª'; // ttho + rofola
phonetic['D']='`'; // ddo
phonetic['Dh']='a'; // ddho
phonetic['Dh+r']='aª'; // ddho + rofola
phonetic['b']='e'; // bo
phonetic['bh']='f'; // bho
phonetic['v']='f'; // bho
//phonetic['rh']='o';	 // doye bindu ro
phonetic['R']='o';	 // doye bindu ro
phonetic['Rh']='p';	 // dhoye bindu ro
phonetic['g']='M';	// go
phonetic['G']='N';	// gho
phonetic['G+r']='Nª';	// gho + rofola
phonetic['gh']='N'; // gho
phonetic['gh+r']='Nª'; // gho 
phonetic['h']='n';	// ho
phonetic['NG']='T';	// yo
phonetic['j']='R';	// borgio jo
phonetic['J']='S'; // jho
phonetic['jh']='S'; // jho
phonetic['c']='P'; //  cho
phonetic['ch']='P'; // cho
phonetic['C']='Q'; // ccho
phonetic['th']='V'; // tho
phonetic['p']='c'; // po
phonetic['f']='d'; // fo
phonetic['ph']='d'; // fo
phonetic['d']='W'; // do
phonetic['dh']='X'; // dho
phonetic['dh+r']='Xª'; // dho + rofola
phonetic['z']='h';// ontoshyo zo
phonetic['y']='q';	// ontostho yo
phonetic['Ng']='O';	// Uma
phonetic['ng']='s';	// uniswor
phonetic['l']='j';	// lo
phonetic['m']='g';	// mo
phonetic['sh']='k';	// talobyo sho
phonetic['S']='l'; // mordhonyo sho
phonetic['O']='I'; // o
phonetic['ou']='Š'; // ou kar
phonetic['OU']='J'; // OU
phonetic['tt']='r'; // tto
phonetic['H']='t'; // bisworgo
phonetic["."] ="|"; // dari
phonetic[".."] = "."; // fullstop
phonetic['HH'] = '&'; // hosonto
phonetic['NN'] = 'u'; // chondrobindu
phonetic['Y'] ='¨'; // jo fola
phonetic['w'] =' '; // wri kar
phonetic['W'] =' '; // wri kar
phonetic['wr'] ='„'; // wri kar
//End Set

//set of Ko
phonetic['k+s']='·';
phonetic['x']='·';
phonetic['k+T']='³';
phonetic['k+t']='±';
//phonetic['k+n']= 'Kè';
phonetic['k+w']='K¡';
phonetic['k+b']='K¡';
phonetic['k+l']='K¬';
phonetic['k+S']='¶';
//phonetic['kS']='¶'; //jukto kho
phonetic['k+r']='µ';
//End Set

//set of Go
phonetic['g+g']='¹';
phonetic['g+D']='º';
phonetic['g+l']='M­';
phonetic['g+n']='Mœ';
phonetic['g+m']='M¥';
phonetic['g+Dh']='»';
phonetic['g+r']='Mª';
//End Set

//set of uma
phonetic['Ng+k']='¼';
phonetic['Ng+K']='•L';
phonetic['Ng+kh']='•L';
phonetic['Ng+g']='½';
phonetic['Ng+G']='•N';
phonetic['Ng+gh']='•N';
phonetic['Ng+k+S']='•¶';
//phonetic['Ng+kS']='•¶';
//phonetic['Ng+G+r']='•NÖ';
//phonetic['Ng+gh+r']='•NÖ';
//end set

//set of cho
phonetic['c+c']='”P';
phonetic['ch+c']='”P';
phonetic['c+ch']='”P';
phonetic['c+C']='”Q';
phonetic['ch+C']='”Q';
phonetic['c+NG']='”T';
phonetic['ch+NG']='”T';
phonetic['c+C+w']='”Q¡';
phonetic['ch+C+w']='”Q¡';
phonetic['c+C+b']='”Q¡';
phonetic['ch+C+b']='”Q¡';
//end set

//set of Borgio Jo
phonetic['j+j']='¾';
//phonetic['j+J']='À';
//phonetic['j+jh']='À';
phonetic['j+N']='..';
//phonetic['j+NG']='Á';
phonetic['j+w']='R¡';
phonetic['j+b']='R¡';
phonetic['j+j+b']='¾¡';
phonetic['j+j+w']='¾¡';
phonetic['j+r']='Rª';
//End set

//Set of Eon
/*
phonetic['NG+c']='Â';
phonetic['NG+ch']='Â';
phonetic['NG+j']='Ä';
phonetic['NG+C']='Ã';
phonetic['NG+J']='Å';
phonetic['NG+jh']='Å';
*/
//End Set

//Set of To
//phonetic['t+t']='Æ';
phonetic['t+w']='U¡';
phonetic['t+b']='U¡';
phonetic['t+m']='U¥';
phonetic['t+r']='Uª';
//End Set

//Set of Do
//phonetic['d+d']='Ç';
phonetic['d+r']='Wª';
//End Set

//Set of Dontonno No
phonetic['n+n']='bœ';
phonetic['n+t']='›U';
//phonetic['n+s']='Ý';
phonetic['n+b']='š^';
phonetic['n+w']='š^';
//phonetic['n+d']='Û';
phonetic['n+T']='š—';
phonetic['n+Th']='š’';
phonetic['n+D']='›`';
//phonetic['n+Dh']='Ü';
phonetic['n+m']='b¥';
phonetic['n+T+r']='š¿';
//phonetic['n+D+w']='›Ø';
//phonetic['n+D+b']='›Ø';
phonetic['n+Dh+r']='Ü«';
//End Set

//Set of  Tto
//phonetic['T+T']='Ë'; //Z¡
//phonetic['T+Th']='Ì';
phonetic['T+n']='Zœ';
phonetic['T+w']='Z¡';
phonetic['T+b']='Z¡';
phonetic['T+m']='Z¥';
//phonetic['T+r']='Î';
phonetic['T+T+w']='Ë¡';
phonetic['T+T+b']='Ë¡';
//phonetic['107.118']='Î';
//End set

//Set of Tho
phonetic['Th+w']='_¡';
phonetic['Th+b']='_¡';
//End Set

//Set of Ddo
phonetic['D+g']='˜M';
//phonetic['D+r']='˜Ö';
phonetic['D+G']='˜N';
phonetic['D+gh']='˜N';
//phonetic['D+D']='Ï'; 
//phonetic['D+Dh']='×';
phonetic['D+v']='™¢';
phonetic['D+bh']='™¢';
//phonetic['D+m']='Ù';
phonetic['D+v+r']='™£';
phonetic['D+bh+r']='™£';
phonetic['D+w'] = '˜¡'; //ddo + bo
phonetic['D+b'] = '˜¡'; //ddo + bo
//End set

//Set of Ddho
phonetic['Dh+w']='aŸ';
phonetic['Dh+b']='aŸ';
phonetic['Dh+m']='a¥';
//phonetic['76.118']='aª';
//End Set

//Set of Murdhono No
//phonetic['N+t']='È';
//phonetic['N+th']='É';
//phonetic['N+d']='Ê';
phonetic['N+n']='Yœ';
phonetic['N+b']='Y^';
phonetic['N+w']='Y^';
//End Set

//Set of Po
//phonetic['p+T']='ß';
//phonetic['p+t']='Þ';
//phonetic['p+p']='à';
phonetic['p+l']='c­';
phonetic['p+n']='cœ';
//phonetic['p+r']='cÖ';
//phonetic['p+s']='á';
//End Set

//Set of Fo
phonetic['f+l']='d¬';
phonetic['ph+l']='d¬';
phonetic['ph+r']='d«';
phonetic['f+r']='d«';
//End Set

//Set of Bo
//phonetic['b+j']='â';
phonetic['b+l']='e­';
//phonetic['b+D']='ã';
//phonetic['b+Dh']='ä';
phonetic['b+w']='eŸ';
phonetic['b+b']='eŸ';
phonetic['b+r']='eª'; //fixed 01-02-06
//phonetic['b+r']='eÖ';
//phonetic['104.118']='';
//End Set

//Set of Vo
phonetic['bh+l']='f­';
phonetic['v+l']='f­';
//phonetic['v+r']='å';
//phonetic['bh+r']='å';
//End Set

//Set of Mo
phonetic['m+n']='gœ';
phonetic['m+l']='¤­';
//phonetic['m+p']='¤ú';
//phonetic['m+f']='ç';
//phonetic['m+ph']='ç';
phonetic['m+b']='¤^';
phonetic['m+w']='¤^';
phonetic['m+v']='¤¢';
phonetic['m+bh']='¤¢';
//phonetic['m+p+l']='¤ú­';
//phonetic['m+p+r']='¤cÖ';
phonetic['m+v+r']='¤£';
phonetic['m+bh+r']='¤£';
phonetic['m+m']='¤§';
phonetic['m+r']='gª';
//End Set

//Set of Lo
/*
phonetic['l+k']='é';
phonetic['l+g']='ê';
phonetic['l+p']='í';
phonetic['l+f']='î';
phonetic['l+ph']='î';
*/
phonetic['l+w']='j¦';
phonetic['l+b']='j¦';
phonetic['l+m']='j¥';
//phonetic['l+l']='j­';
phonetic['l+l']='j¬';
//phonetic['l+f+r']='î«';
//phonetic['l+ph+r']='î«';
//End Set

//Set of Talobyo Sho
phonetic['sh+n']='kœ';
//phonetic['sh+c']='ð';
//phonetic['sh+ch']='ð';
phonetic['sh+l']='k­';
phonetic['sh+b']='k¦';
phonetic['sh+w']='k¦';
phonetic['sh+m']='k¥';
phonetic['sh+r']='kª';
//phonetic['77.109']='k¥';
//End Set

//Set of Mordhonno So
phonetic['S+k']='®‹';
//phonetic['S+t']='ó';
//phonetic['S+th']='ô';
//phonetic['S+p']='®ú';
//phonetic['S+f']='õ';
//phonetic['S+ph']='õ';
phonetic['S+k+r']='®Œ';
phonetic['S+m']='®§';
//phonetic['S+NG']='ò';  //So + Eon
//phonetic['S+t+r']='óª';
//End Set

//Set of Donto So
phonetic['s+k']='¯‹';
//phonetic['s+K']='ö';
//phonetic['s+kh']='ö';
phonetic['s+T']='¯—';
phonetic['s+r'] = 'mª';
phonetic['s+Th']='¯’';
/*
phonetic['s+p']='¯ú';
phonetic['s+f']='ù';
phonetic['s+ph']='ù';
phonetic['s+T+r']='¯¿';
*/
phonetic['s+n']='mœ';
phonetic['s+b']='¯^';
phonetic['s+w']='¯^';
phonetic['s+m']='¯§';
phonetic['s+k+r']='¯Œ';
phonetic['s+l']='¯­';
/*
phonetic['s+t']='÷';
phonetic['s+p+l']='¯ú­';
phonetic['s+p+r']='¯cÖ';
phonetic['s+t+r']='÷ª';
*/
//End Set

//Set of Ho
phonetic['h+w'] = '..';
/*
phonetic['h+wr']='ü';
phonetic['h+N']='ý';
phonetic['h+n']='nè';
phonetic['h+m']='þ';
phonetic['h+l']='n¬';
*/
//phonetic['105.73']='þ';
//End Set

isIE=document.all? 1:0;

var carry = '';  //This variable stores each keystrokes
var old_len =0; //This stores length parsed bangla charcter
var ctrlPressed=false;
var len_to_process_oi_kar=0;
var first_letter = false;

function checkKeyDown(ev)
{
	//just trap the control key
	var e = (window.event) ? event.keyCode : ev.which;
	if (e==17)
	{
			ctrlPressed = true;
	}
}

function checkKeyUp(ev)
{
	//just trap the control key
	var e = (window.event) ? event.keyCode : ev.which;
	if (e==17)
	{
			ctrlPressed = false;
	}
}

//function parseCode(t,am,iscalltex)

function parsePhonetic(evnt)
{
	// main phonetic parser
	var t = document.getElementById(activeta); // the active text area
	var e = (window.event) ? event.keyCode : evnt.which; // get the keycode
	
	if(ctrlPressed)
	{
		// user is pressing control, so leave the parsing
		e=0; 
	}

	var char_e = String.fromCharCode(e); // get the character equivalent to this keycode
	
	if(e==8 || e==32)
	{
		// if space is pressed we have to clear the carry. otherwise there will be some malformed conjunctions
		carry = " ";	
		old_len = 1;
		return;
	}

	lastcarry = carry;
	carry += "" + char_e;	 //append the current character pressed to the carry
	
	bangla = parsePhoneticCarry(carry); // get the combined equivalent
	tempBangla = parsePhoneticCarry(char_e); // get the single equivalent
	//document.getElementById("tb").innerHTML=tempBangla;document.getElementById("cb").innerHTML=bangla;
	//alert(t.value.length);
	if (tempBangla == ".." || bangla == "..") //that means it has next sibling
	{
		return false;
	}
	if (char_e=="+")
	{
		// it may be a conjunction preparator
		if (carry=="++") 
		{
			// if it is a double press of + sign, display one
			insertAtCursor("+");
			old_len = 1;
		}
		return false;
	}
	else if(old_len==0) //first character
	{
		// this is first time someone press a character
		//document.getElementById("stat").innerHTML="first char";
		//t.value = bangla;
		//status = "ok";
		insertJointAtCursor(bangla,1);
		old_len=1;
		return false;
		
	}
	else if(carry=="ao")
	{
		// its a shore o
		insertJointAtCursor(parsePhoneticCarry("ao"),old_len);
		old_len=1;
		return false;
	}
	else if (carry == "ii" || bangla == parsePhoneticCarry("ii"))
	{
		// process dirgho i kar
		//document.getElementById("stat").innerHTML="Dirgho I kar";
		insertKar(bangla, old_len);
		old_len = 1;
		return false;
	}
	else if (carry == "oi" )
	{
		insertKar("‰", len_to_process_oi_kar);
		return false;
	}		
	else if(char_e =="e" || char_e == "i" )
	{
		// all the preceding kars like i kar, e kar 
		if (lastcarry =="Y") old_len +=1; // when the character has jofola, preceding kar must take place before character length + 1 
		insertKar(tempBangla, old_len);
		old_len = 1;
		carry = char_e;
		return false;
	}

	else if (char_e == "o")
	{
		// o kar
		insertKar(parsePhoneticCarry("e"), old_len);
		len_to_process_oi_kar = old_len + 2;
		old_len = 1;
		insertAtCursor(parsePhoneticCarry("a"),old_len);
		carry = "o";
		return false;
	}
	else if (carry == "ou")
	{
		// ou kar
		insertJointAtCursor("Š",old_len);
		old_len = 1;
		return false;
	}	
	
	
	else if(carry.substring(0,2)=="r+")
	{
		// process ref
		// ref is typed before a character but it is shown after a character
		// so we need a special processing
		
		restChar = carry.substring(2);
		tempbangla = parsePhoneticCarry(restChar);
		//alert(tempbangla.length);
		if (tempbangla.length !=0)
		{
		// ref with a single character
		ref = '©';
		insertJointAtCursor(tempbangla,old_len);
		insertAtCursor(ref);
		old_len = 2+bangla.length;
		//carry = restChar;
		}
		else
		{
			// ref with a conjunction
			insertAtCursor(parsePhoneticCarry(char_e));
			carry = char_e;
			old_len = 1;
			return false;
		}
		return false;
	}
	else if((bangla == "" && tempBangla !="")) //that means it has no joint equivalent
	{
		
		// there is no joint equivalent - so show the single equivalent. 
		bangla = tempBangla;
		if (bangla=="")
		{
			// there is no available equivalent - leave as is
			//document.getElementById("stat").innerHTML="No replacement";
			carry ="";
			return;
		}
		
		else
		{
			// found one equivalent
			//document.getElementById("stat").innerHTML="Normal replacement";
			carry = char_e;
			insertAtCursor(bangla);
			old_len = bangla.length;
			return false;
		}
	}
	else if(bangla!="")//joint equivalent found 
	{
		// we have found some joint equivalent process it
		//document.getElementById("stat").innerHTML="Joint Replacement";
		//alert("now joint");
		
		insertJointAtCursor(bangla, old_len);
		old_len = bangla.length;
		return false;
	}
}

    function parsePhoneticCarry(code)
    {
	//this function just returns a bangla equivalent for a given keystroke
	//or a joint one
	//just read the array - if found then return the bangla eq.
	//otherwise return a null value
        if (!phonetic[code])  //Oh my god :-( no bangla equivalent for this keystroke

        {
			return ''; //return a null value
        }
        else
        {
            return ( phonetic[code]);  //voila - we've found bangla equivalent
        }

    }


function insertAtCursor(myValue) {
	/**
	 * this function inserts a character at the current cursor position in a text area
	 * many thanks to alex king and phpMyAdmin for this cool function
	 * 
	 * This function is originally found in phpMyAdmin package and modified by me to meet our need
	 */
	var myField = document.getElementById(activeta);
	if (document.selection) {
		//alert("hello2");
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		sel.collapse(true);
		sel.select();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == 0) {
		
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		startPos = (startPos == -1 ? myField.value.length : startPos );
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
	} else {
		var scrollTop = myField.scrollTop;
		myField.value += myValue;
		myField.focus();
		myField.scrollTop = scrollTop;
	}
}

function insertJointAtCursor(myValue, len) {
	/**
	 * this function inserts a conjunction and removes previous single character at the current cursor position in a text area
	 * 
	 * This function is derived from the original one found in phpMyAdmin package and modified by me to meet our need
	 */
	//alert(len);
	var myField = document.getElementById(activeta);
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		if (myField.value.length >= len){  // here is that first conjunction bug in IE, if you use the > operator
			sel.moveStart('character', -1*(len));   
			//sel.moveEnd('character',-1*(len-1));
		}
		sel.text = myValue;
		sel.collapse(true);
		sel.select();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == 0) {
		myField.focus();
		var startPos = myField.selectionStart-len;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		startPos = (startPos == -1 ? myField.value.length : startPos );
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
	} else {
		var scrollTop = myField.scrollTop;
		myField.value += myValue;
		myField.focus();
		myField.scrollTop = scrollTop;
	}
	//document.getElementById("len").innerHTML = len;
}


function insertKar(myValue, len) {
	/**
	 * this function inserts a preceding kar at the current cursor position in a text area
	 * 
	 * This function is derived from the original one found in phpMyAdmin package and modified by me to meet our need
	 */
	var myField = document.getElementById(activeta);
	if (document.selection) { // internet explorer
		myField.focus();
		sel = document.selection.createRange();
		if (myValue == parsePhoneticCarry("oi"))
		{
			//sel.text = myValue; 
			//alert(len);
			sel.moveStart('character', -1*(len)); //it should be len+2 actually but it happens because there is an extra space with "x"
			sel.moveEnd('character', -1*(len-1)); //it should be len+1 actually
			sel.text = myValue;
			sel.moveStart('character', len-1); //it should be len+1 actually
			sel.moveEnd('character', len-1);//it should be len+1 actually
			insertJointAtCursor("",1);
		}
		else if (myValue != parsePhoneticCarry("ii"))
		{
			// general kar
			sel.moveStart('character', -1*len);
			sel.moveEnd('character', -1*len);
			sel.text = myValue;
			sel.moveStart('character', len);
			sel.moveEnd('character', len);
		}

		else
		{
			//process dirgho i kar
			sel.text = "x"; 
			sel.moveStart('character', -1*(len+2)); //it should be len+2 actually but it happens because there is an extra space with "x"
			sel.moveEnd('character', -1*(len+1)); //it should be len+1 actually
			sel.text = "";
			sel.moveStart('character', len+1); //it should be len+1 actually
			sel.moveEnd('character', len+1);//it should be len+1 actually
			
		}
		sel.collapse(true);
		sel.select();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == 0) {
		
		var startPos = myField.selectionStart-len;
		var endPos = myField.selectionEnd-len;
		var scrollTop = myField.scrollTop;
		startPos = (startPos == -1 ? myField.value.length : startPos );
		if (myValue == parsePhoneticCarry("oi"))
		{
			insertJointAtCursor("",1);
			myField.value = myField.value.substring(0, startPos) + myValue
			+ myField.value.substring(endPos+1, myField.value.length);
			len = len-2;
		}
		else if (myValue != parsePhoneticCarry("ii"))
		{
			// general kar
			myField.value = myField.value.substring(0, startPos)
			+ myValue
			+ myField.value.substring(endPos, myField.value.length);
		}
		else
		{
			// process dirgho i kar
			insertAtCursor("x");
			myField.value = myField.value.substring(0, startPos-1) 
			+ myField.value.substring(endPos, myField.value.length);
			len = len-1;
		}
		
		myField.focus();
		myField.selectionStart = startPos + myValue.length + len;
		myField.selectionEnd = startPos + myValue.length + len;
		myField.scrollTop = scrollTop;
	} else {
		var scrollTop = myField.scrollTop;
		myField.value += myValue;
		myField.focus();
		myField.scrollTop = scrollTop;
	}
	//myField.focus();
}


/*********************** */
var ban=new Array();

ban['38']='u';
ban['106'] = 'K';
ban['106.106']='°';
//ban['110.116.118']='÷ª';
//ban['110.116']='÷';
ban['67']='‰';
ban['.67']='H';
ban['100']='w';
ban['.100']='B';
ban['122']='ª';
ban['68']='x';
ban['.68']='C';
ban['99']='‡';
ban['.99'] = 'G';
ban['.115'] = 'D';
ban['115'] = 'y';
ban['83'] = '~';
ban['.83'] = 'E';
ban['97']='„';
ban['118']='i';
ban['.97']='F';
ban['102']='v';
ban['.102']='Av';
ban['70']='A';
ban['110']='m';
ban['116']='U';
ban['74'] = 'L';
ban['98']='b';
ban['66']='Y';
ban['107']='Z';
ban['75']='_';
ban['108']='`';
ban['76']='a';
ban['104']='e';
ban['72']='f';
ban['65']='©';
ban['112']='o';	
ban['80']='p';	
ban['111']='M';	
ban['79']='N';	
ban['105']='n';	
ban['73']='T';	
ban['117']='R';	
ban['85']='S';
ban['121']='P';
ban['89']='Q';
ban['84']='V';
ban['114']='c';
ban['82']='d';
ban['101']='W';
ban['69']='X';
ban['119']='h';	
ban['87']='q';	
ban['113']='O';	
ban['81']='s';	
ban['86']='j';	
ban['109']='g';	
ban['77']='k';	
ban['78']='l';
ban['120']='I';
ban['88']='Š';
ban['.88']='J';
ban['90']='¨';
ban['92']='r';
ban['124']='t';
//End Set

//set of Ko
ban['106.110']='·';
ban['106.107']='³';
ban['106.116']='±';
//ban['106.98']='Kè';
ban['106.104']='K¡';
ban['106.86']='K¬';
ban['106.78']='¶';
ban['106.118']='µ';
//End Set

//set of Go
ban['111.111']='¹';
ban['111.108']='º';
ban['111.86']='M­';
ban['111.98']='Mœ';
ban['111.109']='M¥';
ban['111.76']='»';
//End Set

//set of uma
ban['113.106']='¼';
ban['113.74']='•L';
ban['113.111']='½';
ban['113.79']='•N';
ban['113.106.78']='•¶';
//ban['113.79.118']='•NÖ';
//end set

//set of cho
ban['121.121']='”P';
ban['121.89']='”Q';
ban['121.73']='”T';
ban['121.89.104']='”Q¡';
//end set

//set of Borgio Jo
ban['117.117']='¾';
//ban['117.85']='À';
//ban['117.73']='Á';
ban['117.104']='R¡';
ban['117.117.104']='¾¡';
//End set

//Set of Eon
/*
ban['73.121']='Â';
ban['73.117']='Ä';
ban['73.89']='Ã';
ban['73.85']='Å';
*/
//End Set

//Set of To
//ban['116.116']='Æ';
ban['116.104']='U¡';
ban['116.109']='U¥';
//End Set

//Set of Do
//ban['101.101']='Ç';
//End Set

//Set of Dontonno No
ban['98.98']='bœ';
ban['98.116']='›U';
ban['98.104']='š^';
//ban['98.101']='Û';
ban['98.107']='š—';
ban['98.75']='š’';
ban['98.108']='›`';
//ban['98.76']='Ü';
ban['98.109']='b¥';
ban['98.107.118']='š¿';
//ban['98.108.104']='›Ø';
ban['98.76.118']='Ü«';
//End Set

//Set of  Tto
//ban['107.107']='Ë'; //Z¡
//ban['107.75']='Ì';
ban['107.98']='Zœ';
ban['107.104']='Z¡';
ban['107.109']='Z¥';
//ban['107.118']='Î';
ban['107.107.104']='Ë¡';
//ban['107.118']='Î';
//End set

//Set of Tho
ban['75.104']='_¡';
//End Set

//Set of Ddo
ban['108.111']='˜M';
ban['108.79']='˜N';
//ban['108.108']='Ï'; 
//ban['108.76']='×';
ban['108.72']='™¢';
//ban['108.109']='Ù';
ban['108.72.118']='™£';
ban['108.104'] = '˜¡'; //ddo + bo
//End set

//Set of Ddho
ban['76.104']='aŸ';
ban['76.109']='a¥';
//ban['76.118']='aª';
//End Set

//Set of Murdhono No
/*
ban['66.116']='È';
ban['66.84']='É';
ban['66.101']='Ê';
*/
ban['66.98']='Yœ';
ban['66.104']='Y^';
//End Set

//Set of Po
/*
ban['114.107']='ß';
ban['114.116']='Þ';
ban['114.66']='à';  */
ban['114.86']='c­';
ban['114.98']='cœ';
//ban['114.118']='cÖ';
//ban['114.110']='á';
//End Set

//Set of Fo
ban['82.86']='d¬';
ban['82.118']='d«';
//End Set

//Set of Bo
//ban['104.117']='â';
ban['104.86']='e­';
//ban['104.108']='ã';
//ban['104.76']='ä';
ban['104.104']='eŸ';
//ban['104.118']='';
//End Set

//Set of Vo
ban['72.86']='f­';
//ban['72.118']='å';
//End Set

//Set of Mo
ban['109.98']='gœ';
ban['109.86']='¤­';
//ban['109.114']='¤ú';
//ban['109.82']='ç';
ban['109.104']='¤^';
ban['109.72']='¤¢';
//ban['109.114.86']='¤ú­';
//ban['109.114.118']='¤cÖ';
ban['109.72.118']='¤£';
ban['109.109']='¤§';
//End Set

//Set of Lo
/*
ban['86.106']='é';
ban['86.111']='ê';
ban['86.114']='í';
ban['86.82']='î';
*/
ban['86.104']='j¦';
ban['86.109']='j¥';
ban['86.86']='j­';
//ban['86.82.118']='î«';
//End Set

//Set of Talobyo Sho
ban['77.98']='kœ';
//ban['77.121']='ð';
ban['77.86']='k­';
ban['77.104']='k¦';
ban['77.109']='k¥';
ban['77.118']='kª';
//ban['77.109']='k¥';
//End Set

//Set of Mordhonno So
ban['78.106']='®‹';
//ban['78.116']='ó';
//ban['78.84']='ô';
//ban['78.114']='®ú';
//ban['78.82']='õ';
ban['78.106']='®‹';
ban['78.106.118']='®Œ';
ban['78.109']='®§';
//ban['78.66']='ò';  //So + Eon
//ban['78.116.118']='óª';
//End Set

//Set of Donto So
ban['110.106']='¯‹';
//ban['110.74']='ö';
ban['110.107']='¯—';
ban['110.75']='¯’';
//ban['110.114']='¯ú';
//ban['110.82']='ù';
ban['110.107.118']='¯¿';
ban['110.98']='mœ';
ban['110.104']='¯^';
ban['110.109']='¯§';
ban['110.106.118']='¯Œ';
ban['110.86']='¯­';
/*
ban['110.116']='÷';
ban['110.114.86']='¯ú­';
ban['110.114.118']='¯cÖ';
ban['110.116.118']='÷ª';
*/
//End Set

//Set of Ho
/*
ban['105.118']='ü';
ban['105.98']='ý';
ban['105.66']='nè';
ban['105.109']='þ';
ban['105.86']='n¬';
*/
//ban['105.73']='þ';
//End Set

isIE=document.all? 1:0;

var carry = '';  //This variable stores each keystrokes
var len =1; //This stores length parsed bangla charcter
var ctrlPressed=false;

function checkKeyDown(ev)
{
	var e = (window.event) ? event.keyCode : ev.which;
	if (e==17)
	{
			ctrlPressed = true;
	}
}

function checkKeyUp(ev)
{
	var e = (window.event) ? event.keyCode : ev.which;
	if (e==17)
	{
			ctrlPressed = false;
	}
}

//function parseCode(t,am,iscalltex)
function parseCode(ev)
{
	//var t = document.getElementById("post_content_input_bng");
	var t = document.getElementById(activeta);
	var iscalltex = 1;
	var e = (window.event) ? event.keyCode : ev.which;
	
	if(ctrlPressed)
	{
		e=0;
	}
	//alert(e);

	//alert(e);
	//alert(e);
/*This function hooks the key strokes in the caller component.
*It calls parseCarry() function to parse single or combination of keystrokes 
*into equivalent Bangla Character
*/
    //e = window.event.keyCode; //Capture the keystrokes
    //e = am; //now
	//alert (iscalltex);
	if (iscalltex=='1') 
	{
		if (isIE)
		window.event.keyCode= '';
	}
	//window.event.keyCode=''; // Otherwise Each character repeats
	c = parseCarry(e); //Now check if this keystroke has bangla equivalent
					   //This is helpful to detect symbols which are not parsible
	if ( e != 103 && c=='' && e!= 71) //These are specially processed character
	// 46 = . 103 = g [joiner] , 71 = | [bangla dari]
	{
		//if the keystroke is not specially processed one but symbol, just leave
		//and display itself
		//alert (iscalltex);
		if(iscalltex == 1) 
		{
			if(isIE)
			window.event.keyCode=e;
		}
		
		return;
	}
    if ( e==103) //Now check if the keystroke is a joiner
    //If it is a joining caharcter, then just add a '.' to carry
	//because it will be processed after joining
	{
        carry += '.';
        return false;
    }
    else  //Not joiner but need to be processed
    {
        if (e==32)  //Space is processed separately
		{
		//when space pressed, just add a blank space to the end of caller component
		//and set the carry to null
		//because space does not join any character
			//alert(e);
			//t.value += ' ';
			//t.focus();
			insertAtCursor(" ");
			carry = ''; // noyto complte shift+kar gulo dheka zay na
			len =0; //noyto space ta shift+character diyye replace hoye zay
			return ;
		}
        else if (e==46)  //Space is processed separately
		{
		//when space pressed, just add a blank space to the end of caller component
		//and set the carry to null
		//because space does not join any character
			//alert(e);
			//t.value += ' ';
			//t.focus();
			insertAtCursor(".");
			carry = '32'; // noyto complte shift+kar gulo dheka zay na
			len =1; //noyto space ta shift+character diyye replace hoye zay
			return ;
		}		
        else if (e==71) //if it is a bangla dari, process same as space
		{
			//t.value += '|';
			//t.focus();
			insertAtCursor("|");
			carry = ''; // noyto complte shift+kar gulo dheka zay na
			len =0; //noyto space ta shift+character diyye replace hoye zay
			return false;
		}
		//Now check for other character		
		carry +=e; //add this keystroke to cary and check if it has a joint bangla equivalent
		l = carry.length;
		//alert(carry);
		if (l>3)  //Now check if the keystroke is for complete kar
		//if a kar is pressed with combination of g
		//then the kar is displayed into it's original form
		{
		if (carry.substr(l-3,3)=='.99' || carry.substr(l-3,3)=='.83' || carry.substr(l-3,3)=='.67' || carry.substr(l-3,3)=='.68' || carry.substr(l-3,3)=='.88' || carry.substr(l-3,3)=='.97')
		{
		/* This part is to  check whether it is complete e kar or a kar or ...
		*/
			carry = carry.substr(l-3,3);
			//t.value = t.value+parseCarry(carry);
			//t.focus();
			insertAtCursor(parseCarry(carry));
			return false;
		}
		else if(carry.substr(l-4,4)=='.115' || carry.substr(l-4,4)=='.102' || carry.substr(l-4,4)=='.100') // short a kar 
		{
		/* This part is to  check whether it is complete e kar or a kar or ...
		*/
			carry = carry.substr(l-4,4);
			//t.value = t.value+parseCarry(carry);
			//t.focus();
			insertAtCursor(parseCarry(carry));
			return false;
		}
		//Complete kar processing end
		}
        charValue = parseCarry(carry);  //Now check for joint bangla equivalent
        if (charValue =='')  //This key has no bangla equivalent
        {
            charValue = parseCarry(e); //check for single bangla equivalent. it must have a bangla eq.
            carry = e; //though this character has no joint bangla equivalen
					   // so clear the carry and set this charater into carry
					   //for further processing - and joint bangla check
            len = charValue.length; //get the length of bangla equivalent
            //t.value = t.value + charValue; //though this word is not a joint one, so just append it 
										   //at the end of the caller component
			insertAtCursor(charValue);
            return false;
        }
        else  //well - we found a joint bangla equivalent.aH Ha
        {
            tx = t.value; //though it is a joint character
			//so delete the previous character - I mean the last character
			//in the component and display the joint bangla character
			textlen = tx.length;
            tx = tx.substr(0,textlen-len);  //last character deleted
            //t.value = tx+charValue; //joint bangla character appended
            //t.focus();
			insertJointAtCursor(charValue, len);
            len = charValue.length; //store this length if this joint character 
			//have further joining possibillity. Because three character may turn into
			//one single joint form. Then we need to delete current joint character
			//and display new one
            return false;
        }
    }
}


function parseCodeKB(ev)
{
	var t = document.getElementById(activeta);
	var iscalltex = 1;
	var e = ev;
	//alert(e);
/*This function hooks the key strokes in the caller component.
*It calls parseCarry() function to parse single or combination of keystrokes 
*into equivalent Bangla Character
*/
    //e = window.event.keyCode; //Capture the keystrokes
    //e = am; //now
	//alert (iscalltex);
	if (iscalltex=='1') 
	{
		//if (isIE)
		//window.event.keyCode= '';
	}
	//window.event.keyCode=''; // Otherwise Each character repeats
	c = parseCarry(e); //Now check if this keystroke has bangla equivalent
					   //This is helpful to detect symbols which are not parsible
	if ( e != 103 && c=='' && e!= 71) //These are specially processed character
	// 46 = . 103 = g [joiner] , 71 = | [bangla dari]
	{
		//if the keystroke is not specially processed one but symbol, just leave
		//and display itself
		//alert (iscalltex);
		if(iscalltex == 1) 
		{
			//if(isIE)
			//window.event.keyCode=e;
		}
		
		//return;
	}
    if ( e==103) //Now check if the keystroke is a joiner
    //If it is a joining caharcter, then just add a '.' to carry
	//because it will be processed after joining
	{
        carry += '.';
        return false;
    }
    else  //Not joiner but need to be processed
    {
        if (e==32)  //Space is processed separately
		{
		//when space pressed, just add a blank space to the end of caller component
		//and set the carry to null
		//because space does not join any character
			//alert(e);
			//t.value += ' ';
			//t.focus();
			insertAtCursor(" ");
			carry = '32'; // noyto complte shift+kar gulo dheka zay na
			len =0; //noyto space ta shift+character diyye replace hoye zay
			return ;
		}
        else if (e==46)  //Space is processed separately
		{
		//when space pressed, just add a blank space to the end of caller component
		//and set the carry to null
		//because space does not join any character
			//alert(e);
			//t.value += ' ';
			//t.focus();
			insertAtCursor(".");
			carry = '32'; // noyto complte shift+kar gulo dheka zay na
			len =1; //noyto space ta shift+character diyye replace hoye zay
			return ;
		}		
        else if (e==71) //if it is a bangla dari, process same as space
		{
			//t.value += '|';
			//t.focus();
			insertAtCursor("|");
			carry = ''; // noyto complte shift+kar gulo dheka zay na
			len =0; //noyto space ta shift+character diyye replace hoye zay
			return false;
		}
		//Now check for other character		
		carry +=e; //add this keystroke to cary and check if it has a joint bangla equivalent
		l = carry.length;
		//alert(carry);
		if (l>3)  //Now check if the keystroke is for complete kar
		//if a kar is pressed with combination of g
		//then the kar is displayed into it's original form
		{
		if (carry.substr(l-3,3)=='.99' || carry.substr(l-3,3)=='.83' || carry.substr(l-3,3)=='.67' || carry.substr(l-3,3)=='.68' || carry.substr(l-3,3)=='.88' || carry.substr(l-3,3)=='.97')
		{
		/* This part is to  check whether it is complete e kar or a kar or ...
		*/
			carry = carry.substr(l-3,3);
			//t.value = t.value+parseCarry(carry);
			//t.focus();
			len=-1;
			insertAtCursor(parseCarry(carry));
			return false;
		}
		else if(carry.substr(l-4,4)=='.115' || carry.substr(l-4,4)=='.102' || carry.substr(l-4,4)=='.100') // short a kar 
		{
		/* This part is to  check whether it is complete e kar or a kar or ...
		*/
			carry = carry.substr(l-4,4);
			//t.value = t.value+parseCarry(carry);
			//t.focus();
			len=-1;
			insertAtCursor(parseCarry(carry));
			return false;
		}
		//Complete kar processing end
		}
        charValue = parseCarry(carry);  //Now check for joint bangla equivalent
        if (charValue =='')  //This key has no bangla equivalent
        {
            charValue = parseCarry(e); //check for single bangla equivalent. it must have a bangla eq.
            carry = e; //though this character has no joint bangla equivalen
					   // so clear the carry and set this charater into carry
					   //for further processing - and joint bangla check
            len = charValue.length; //get the length of bangla equivalent
            //t.value = t.value + charValue; //though this word is not a joint one, so just append it 
			//t.focus();							   //at the end of the caller component
			insertAtCursor(charValue);
            return false;
        }
        else  //well - we found a joint bangla equivalent.aH Ha
        {
            tx = t.value; //though it is a joint character
			//so delete the previous character - I mean the last character
			//in the component and display the joint bangla character
			textlen = tx.length;
            //tx = tx.substr(0,textlen-len);  //last character deleted
            tx = t.value;
            //t.value = tx+charValue; //joint bangla character appended
			oldlen = len;
            len = charValue.length; //store this length if this joint character 
			//have further joining possibillity. Because three character may turn into
			//one single joint form. Then we need to delete current joint character
			//and display new one			
			insertJointAtCursor(charValue, oldlen);
            //t.focus();

            return false;
        }
    }
}


    function parseCarry(code)
    {
	//this function just returns a bangla equivalent for a given keystroke
	//or a joint one
	//just read the array - if found then return the bangla eq.
	//otherwise return a null value
        if (!ban[code])  //Oh my god :-( no bangla equivalent for this keystroke

        {
            
			//if (isIE) window.event.keyCode = '';
			return ''; //return a null value
        }
        else
        {
            return ( ban[code]);  //voila - we've found bangla equivalent
        }

    }



function parseKhio()
{

//alert (event.keyCode);
parseCodeKB("106");
carry ="106.";
parseCodeKB(78);
}

function makeAsciiPhoneticEditor(textAreaId)
{
	activeTextAreaInstance = document.getElementById(textAreaId);
	activeTextAreaInstance.onkeypress = parsePhonetic; 
	activeTextAreaInstance.onkeydown = checkKeyDown; 
	activeTextAreaInstance.onkeyup = checkKeyUp;
	activeTextAreaInstance.onfocus = function(){activeta=textAreaId;};
}

function makeAsciiUnijoyEditor(textAreaId)
{
	activeTextAreaInstance = document.getElementById(textAreaId);
	activeTextAreaInstance.onkeypress = parseCode; 
	activeTextAreaInstance.onkeydown = checkKeyDown; 
	activeTextAreaInstance.onkeyup = checkKeyUp;
	activeTextAreaInstance.onfocus = function(){activeta=textAreaId;};
}