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 (text_El.caretPos) 
  	  {
       // var caretPos = text_El.caretPos;
	  }	

	/*%%%%%%%%%%%%%*/
	//var parent=caretPos.parentElement();
	/*%%%%%%%%%%%%%*/
//if (parent.type=='textarea'|| parent.type=='text' )
// {/*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,'');
  	}
/*********/
/*-----*/

	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)=='' && text_El.value.charAt(len-3)=='')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+len;
		//end=-v.length+leng+(ent.length-1);
		if(tot_line.length>ent.length)
		end =end+(tot_line.length-ent.length);
		/*chandrakala
		if(prev=='')
		{
		leng=leng-0.5;
		end=end+0.5;
		}
		chandrakala*/
		r.moveEnd('character',end );
		if(flag)
		{
		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=='')
	{
	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);
	      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);
		if(tot_line.length>ent.length)
		end =end+(tot_line.length-ent.length);
		r.moveEnd('character',end );
           	r.moveStart('character', leng);//-1);
		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='';
}

