/*//File id :Aksharam.js
//Version No:1.0
//Project code:HDC 021
//Project name:Kairali
//Author:Priya.S.Nair
//Date of last updation:14/1/2003
//Description:scripting for the renderings included in this  js file
//****************************************************************************/

var len=0;var leng=0; var iflag=true; var end=0;
var text_El='';
function storeCaret(textEl)/*store the caret position in a text field or text area. */
 
{
       if (textEl.createTextRange)
         textEl.caretPos = document.selection.createRange().duplicate();
		text_El=textEl;


 }


 function insertAtCaret (ch)//Insert char at the cursor position
 {//if(document.selection.type='text')
	//alert(text_El.value);
	if (text_El.caretPos) 
  	  {
        var caretPos = text_El.caretPos;
		
	  }	
//if (document.selection.type=="None" && text_El.caretPos && caretPos.text.length <43)
 //{/*avoid printing icdre when we select document (outside textbox)*/
  var prev='';var prev1=''; var flag=false;var ch1='';
  var txt='';

  /*********/
  	var txt1='icdre';
  	if ( text_El.caretPos) /*get the index of the cursor position in the textbox*/
  	{
        //var caretPos = text_El.caretPos;
        caretPos.text =caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?txt1 +' ' : txt1;
        var s=new String(text_El.value);
	  len=s.indexOf(txt1);  
	  text_El.value=s.replace(txt1,'');
      //alert(len);
  	}
/*********/
/*-----*/

	if(len-2>=0)
	{
		prev=text_El.value.charAt(len-1);
		ch1=text_El.value.charAt(len-2);
	}
	if(prev=='' && ch=='')//nRa
		{ch='';}
	
	if(prev=='')//chandrakala
	{	
		flag=true;//for koottaksharam
		if(ch1=='' && ch=='')
			{txt='';}//kka
		else if(ch1=='' && ch=='')
			{txt='';}//kla
		else if(ch1=='' && ch=='')
			{txt='';}//ksha
		else if(ch1=='' && ch=='')
			{txt='';}//gga
		else if(ch1=='' && ch=='')
			{txt='';}//gla
		else if(ch1=='' && ch=='')
			{txt='';}//nka
		else if(ch1=='' && ch=='')
			{txt='';}//nga
		else if(ch1=='' && ch=='')
			{txt='';}//chcha
		else if(ch1=='' && ch=='')
			{txt='';}//ncha
		else if(ch1=='' && ch=='')
			{txt='';}//njha
		else if(ch1=='' && ch=='')
			{txt='';}//tta
		else if(ch1=='' && ch=='')
			{txt='';}//nda
		else if(ch1=='' && ch=='')
			{txt='';}//NNa
		else if(ch1=='' && ch=='')
			{txt='';}//ththa
		else if(ch1=='' && ch=='')
			{txt='';}//dda
		else if(ch1=='' && ch=='')
			{txt='';}//ddha
		else if(ch1=='' && ch=='')
			{txt='';}//ntha
		else if(ch1=='' && ch=='')
			{txt='';}//nda
		else if(ch1=='' && ch=='')
			{txt='';}//nna
		else if(ch1=='' && ch=='')
			{txt='';}//nma
		else if(ch1=='' && ch=='')
			{txt='';}//ppa
		else if(ch1=='' && ch=='')
			{txt='';}//pla
		else if(ch1=='' && ch=='')
			{txt='';}//bba
		else if(ch1=='' && ch=='')
			{txt='';}//bla
		else if(ch1=='' && ch=='')
			{txt='';}//npa
		else if(ch1=='' && ch=='')
			{txt='';}//mma
		else if(ch1=='' && ch=='')
			{txt='';}//mla
		else if(ch1=='' && ch=='')
			{txt='';}//yya
		else if(ch1=='' && ch=='')
			{txt='';
			 if(text_El.value.charAt(len-4)=='')txt='';//sta
			}//rara
		
		else if(ch1=='' && ch=='')
			{txt='';}//lla
		else if(ch1=='' && ch=='')
			{txt='';}//LLa
		else if(ch1=='' && ch=='')
			{txt='';}//vva
		else if(ch1=='' && ch=='')
			{txt='';}//shla
		else if(ch1=='' && ch=='')
			{txt='';}//shsha
		else if(ch1=='' && ch=='')
			{txt='';}//sla
		else if(ch1=='' && ch=='')
			{txt='';}//ssa
		else if(ch1=='' && ch=='')
			{txt='';}//hla
		else if(ch1=='' && ch=='')
			{txt='';}//DDa
		else if(ch1=='' && ch=='')
			{txt='';}//kTa
		else if(ch1=='' && ch=='')
			{txt='';}//bdha
		else if(ch1=='' && ch=='')
			{txt='';}//bda
		else if(ch1=='' && ch=='')
			{txt='';}//chchha
		else if(ch1=='' && ch=='')
			{txt='';}//jja
		else if(ch1=='' && ch=='')
			{txt='';}//Nma
		else if(ch1=='' && ch=='')
			{txt='';}//stha
		else if(ch1=='' && ch=='')
			{txt='';}//ntha
		else if(ch1=='' && ch=='')
			{txt='';}//jNja
		else if(ch1=='' && ch=='')
			{txt='';}//Tbha
		else if(ch1=='' && ch=='')
			{txt='';}//gma
		else if(ch1=='' && ch=='')
			{txt='';}//shcha
		else if(ch1=='' && ch=='')
			{txt='';}//NDa
		else if(ch1=='' && ch=='')
			{txt='';}//Tma
		else if(ch1=='' && ch=='')
			{txt='';}//kTa
		else if(ch1=='' && ch=='')
			{txt='';}//gna
		else if(ch1=='' && ch=='')
			{txt='';}//nRa
		else if(ch1=='' && ch=='')
			{txt='';}//ShTa
		
		else
			{txt=ch;flag=false;}/*flag=false,if chandrakala is there,not delete previous two char */
	}	
	else
	{
	txt=ch;	
	}	
/*@@@@@@@@@@*/

/*@@@@@@@@@@*/

/***********/
	      //place the cursor at the correct position in the text box to insert the selected char
		var r = text_El.createTextRange();  
		var v = new String(text_El.value); 
		var tot_line=v.split('\n');

		var temp=new String(v.substring(0,len));
		var ent=temp.split('\n');
		if(ent.length>1)
		{
			
		 leng=len-ent.length+1;
		}
		else
		{
		leng=len;				
		}
		
		end=-v.length+leng+(ent.length-1);
		//alert(len+' '+ent.length+' '+v.length+'  '+end +' '+tot_line.length);

		if(tot_line.length>ent.length)
		end =end+(tot_line.length-ent.length);
		
		r.moveEnd('character',end );
	
		if(flag)
		{
		//alert(-v.length+' '+leng);
		
			//r.moveEnd('character',leng+1);	

		if(txt=='')
          		r.moveStart('character', leng-4);//if sTa then delete left four char
		else
			r.moveStart('character', leng-2);//chandrakala then delete left two char
		}
		else
		{
                if (ch=='')//nRa
		        r.moveStart('character', leng-1);
		    else
		        r.moveStart('character', leng);
		}
		

		r.select();
 		text_El.caretPos =document.selection.createRange().duplicate();
/*********/

/*-------*/
if(flag || ch=='')
{//document.selection.empty(); 

	text='icdre';
	if ( text_El.caretPos)
		{
         	var caretPos = text_El.caretPos;
         	caretPos.text =caretPos.text.charAt(caretPos.text.length -1) == ' ' ?text +' ' : text;
		}
		/#######/
		 var s=new String(text_El.value);
	       len=s.indexOf(text);

		 //alert(len);  
	       text_El.value=s.replace(text,'');
		/#########/
		
		var r = text_El.createTextRange(); 
		var v = new String(text_El.value); 
		var temp=new String(v.substring(0,len));
		var ent=temp.split('\n');
		var tot_line=v.split('\n');
		
		if(ent.length>1)
		{
		 leng=len-ent.length+1;
		}
		else
		{
		leng=len;
		}
		end=-v.length+leng+(ent.length-1);
		//alert(len+' '+ent.length+' '+v.length+'  '+end +' '+tot_line.length);

		if(tot_line.length>ent.length)
		end =end+(tot_line.length-ent.length);
		r.moveEnd('character',end );
           // if(txt=='')
		//{
 		//r.moveEnd('character', -v.length+leng-1);
		r.moveStart('character', leng);//-1);
		//}
		//else
		//{
 		//r.moveEnd('character', -v.length+leng-2);
		//r.moveStart('character', leng);//-2);
		//}
		r.select();
		text_El.caretPos =document.selection.createRange().duplicate();
		//}
}
/*------*/

		text=txt;
             if ( text_El.caretPos)
		 {
         		var caretPos = text_El.caretPos;
         		caretPos.text =caretPos.text.charAt(caretPos.text.length -1) == ' ' ?text +' ' : text;
		}
     	  	else
         text_El.value  = text;

       
flag=false;
//}//if document.selection
}//fun insertAt

function here(str)
{
text_El=str;
}

