// procedures  for working in a textarea
// Note: for "moveable box" (display only) functions  see dMoveBox.JS instead

/* ===
Functions:

  clickInTextarea(this,anid)   

     When area is clicked in, current cursor location is displayed (in the anid element)
     Meant to be used with an onClick in  <textarea> element. 

    this:  REQUIRED.

    anid: optional. Id of element to write resutls (line number clicked on) to.
          If not specified, use 'editableBoxStatus'
   
   Example:
        <textarea rows="10" cols="80" onClick="clickInTextarea(this)" >
              This is what I am editing.
              ...
              End of it all.
        </textarea>
        <textarea rows="10" cols="80" onClick="clickInTextarea(this,'showLineNumber')" >
              More stuff
              And more
              End of stuff
        </textarea>
        

  searchInTextarea(avalue,textareaId,statusId)   

       avalue is  what to search for.
         If no arguments, use the value of an "editableBoxSearch" element 
             For example:  
                  <a href="#" onClick="searchInTextarea()">Find: </a>
                  <input id="editableBoxSearch" type="text" />
  
        Hint: if searchInTextarea is used as an onChange (or onBlur), use 'this.value' (without quotes)
              For example: <input title="search for..." type="text" onChange=searchInTextarea(this.value)" />
  
       textareaId is the id of the <textarea> to search in.  
          If not specified, use "editableBox"

       statusId is the id of an element to write the "line found at" 
          If not specified, use "editableBoxStatus"



Example:
  Search for: <input type="text" value="[find]" onClick="this.value=''"  onChange="searchInTextarea(this.value)"  />
    <span id="editableBoxStatus" style="color:blue" title="last find at">... </span>

 <textarea onClick="clickInTextArea(this)" id="editableBox" rows="30" cols="140"> 
 </textarea>

Note: an element with id="currentLineInEditableBox" will contain the last line clicked on.

== */

// ================ 
// Work in text box functions

//======
// report where click is at in textarea. clickInTextArea should be an onClick in a <textarea>

function clickInTextarea(athis,anid) {
 var vv,bigvalue,nowat,ccount,blines,m,ifoo;
  if (arguments.length<2) anid='editableBoxStatus';
  vv=athis ;
  if (!vv) return 0 ;
  bigvalue=vv.value ;

  nowat= doGetCaretPosition(vv);
  if (nowat>=bigvalue.length)  nowat=0;
// count # lines in vv.value
   ccount=0; 
   blines=vv.value.split('\n');
   for (var m=0;m<blines.length;m++) {                  // find what line this iz2 character is on
     ccount=ccount+blines[m].length+1 ;  // add 1 for lf
     if (ccount>nowat) {
        ifoo=document.getElementById(anid);
        ifoo.innerHTML='<em>click at </em> <b><span id="currentLineInEditableBox">'+m+'</span></b>';
        return 0;
     }
   }
   ifoo=document.getElementById(anid);
   ifoo.innerHTML='<em>ERROR: could not find clicked-line!</em>';
   return 0;

}

//======
// search in textarea: 
// avalue is what to look for (i.e.; a this.value), 
// textareaId is id of textArea to look in (if not specified: editableBox
// statusId is id of where to write resuls (if not specified: editableBoxStatus)

function searchInTextarea(avalue,textareaId,statusId) {

var vv,bigvalue,iz,nowat,iz2,zoob;
var m,vz2,ccount,blines,nlines;
var ifoo,tmp,tmp1;

  if (arguments.length<1) {
     tmp=document.getElementById('editableBoxSearch');
     if (!tmp) {
        alert('Error in search: no editableBoxSearch element ');
        return 0;
     }
    avalue=tmp.value;
  }
  if (arguments.length<2) textareaId='editableBox';
  if (arguments.length<3) statusId='editableBoxStatus';

  if (strip(avalue)=='') return 1 ;

  vv=document.getElementById(textareaId); 
  if (!vv) return 0 ;
  nowat= doGetCaretPosition(vv);
  if (nowat>=vv.value.length) {
         nowat=0;
  } else {
        nowat=nowat+avalue.length;
  }

  bigvalue=vv.value.substr(nowat);
  rr=new RegExp(avalue,'i'); 
  iz=bigvalue.search(rr);
  if (iz==-1) {
     ifoo=document.getElementById(statusId);
     ifoo.innerHTML='<em>not found</em>';
     return 0;
  }

  iz2=iz+nowat;                // spot in text. 

// count # lines in vv.value
   ccount=0; 
   blines=vv.value.split('\n');
   vz2=1;
   for (var m=0;m<blines.length;m++) {                  // find what line this iz2 character is on
     ccount=ccount+blines[m].length+1 ;  // add 1 for crl
     if (ccount>iz2) {
        ifoo=document.getElementById(statusId);
        ifoo.innerHTML='<em> found on </em><b>'+m+'</b>';
        vz2=m/blines.length;
        break;
     }
   }
   setSelRange(vv,iz2,iz2+avalue.length);
   vv.scrollTop=parseInt(vz2*vv.scrollHeight);

 return 1;
}



//======
// position of cursor in the ctrl textarea 
function doGetCaretPosition (ctrl) {
	var CaretPos = 0;	// IE Support
	if (document.selection) {
	ctrl.focus ();
		var Sel = document.selection.createRange ();
		Sel.moveStart ('character', -ctrl.value.length);
		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;
	return (CaretPos);
}

// set selection area in elem textarea to be caretPos
function setSelRange(inputEa, selStart, selEnd) { 
 if (inputEa.setSelectionRange) {  
  inputEa.focus(); 
  inputEa.setSelectionRange(selStart, selEnd); 
 } else if (inputEa.createTextRange) { 
  var range = inputEa.createTextRange(); 
  range.collapse(true); 
  range.moveEnd('character', selEnd); 
  range.moveStart('character', selStart); 
  range.select(); 
 } 
}

// set position in text box
function setCaretPosition(elem, caretPos) {
        if(elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {
            if(elem.selectionStart) {
                elem.focus();
                elem.setSelectionRange(caretPos, caretPos);
            }
            else
                elem.focus();
        }
    return 1;
}


