// Copyright © 2005-2006 Water Street USA, LLC.  All rights reserved. --
// ----------------------------------------------------------------------------------
// Water Street USA (WSUSA) software and code [both binary and source 
// (if released)] (Software) is intellectual property owned by WSUSA and is copyright 
// of WSUSA in all countries in the world, and ownership remains with WSWS.
//
// You (Licensee) may use the WSUSA Software and install it only in accordance with 
// use and licensing agreements you have signed with WSWS.  Licenses are distributed 
// only by WSWS. Please send an email to info@waterstreetusa.com for more information.
//
// If you do not have a valid signed license agreement, you may not use, copy or
// distribute the Software for any purpose.
//
// This copyright notice may not be removed or altered or it will constitute a
// violation of any license agreements.
// ----------------------------------------------------------------------------------

/* el is element */
function setCaretToStart (el) { 
  if (el.createTextRange) { 
    var v = el.value; 
    var r = el.createTextRange(); 
    r.moveEnd('character', -v.length); 
    r.select(); 
  }
} 

function setCaretToEnd (el) { 
  if (el.createTextRange) { 
    var v = el.value; 
    var r = el.createTextRange(); 
    r.moveStart('character', v.length); 
    r.select(); 
  } 
} 

/* first line number is 1 */
function setCaretToLine (el, lNo) { 
  if (el.createTextRange) { 
    var v = el.value; 
    var p = -1; 
    var r = el.createTextRange(); 
    r.collapse(); 
    var l = 1; 
    while (l++ < lNo) 
      p = el.value.indexOf('\r\n', p + 1); 
    r.moveStart('character', lNo == 1 ? 0 : p - (lNo * 1) + 3); 
    r.select(); 
  } 
} 
function insertAtEnd (el, txt) { 
  el.value += txt; 
  setCaretToEnd (el); 
} 
function insertAtStart (el, txt) { 
  el.value = txt + el.value; 
  setCaretToStart (el); 

} 
/* insert at current mouse position */
function insertOnClick (el, txt) { 
  var r = document.selection.createRange(); 
  r.text = txt; 
}
function insertTag (el, sel) { 
  var v = sel.options[sel.selectedIndex].value; 
  insertOnClick(el, v);
}
function insertAtCaret (el, txt) { 
  if (el.currRange) 
  { 
    el.currRange.text = 
		el.currRange.text.charAt(el.currRange.text.length - 1) != ' ' ? txt : 
		txt + ' '; 
    el.currRange.select(); 
  } 
  else 
    insertAtEnd(el, txt); 
} 
function storeCaret (editEl) { 
  if (editEl.createTextRange) 
    editEl.currRange = document.selection.createRange().duplicate(); 
} 


/* ---------------------- */
function isTabKey(evt)
{

	//alert('In isTabKey')
	if (evt != null) {
		// grab IE event object
		//alert('Is IE event object')
		evt = window.event
	} else {
		// grab NN4 event info
		//alert('Non IE event object')
		evt.keyCode = evt.which
	}
	//alert('Keycode is ' + evt.keyCode)
	return (evt.keyCode == 9)
}
/* get selected text */
function getSel()
{
	var txt = '';
	var foundIn = '';
	if (window.getSelection)
	{
		txt = window.getSelection();
		foundIn = 'window.getSelection()';
	}
	else if (document.getSelection)
	{
		txt = document.getSelection();
		foundIn = 'document.getSelection()';
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;
		foundIn = 'document.selection.createRange()';
	}

	return txt;	

}
/* 
	If no selection, then inserts preText+text+postText
   at caret.  If there is a selection, then inseart
   preText before and postText after, ignoring text.
*/
function myInsertAtCaret (textEl, preText, text, postText)
{
/*
	if (textEl.createTextRange)
	{
		textEl.focus(textEl.caretPos);
		textEl.caretPos = document.selection.createRange().duplicate();
		var textRangeObject = textEl.caretPos;
		alert(textEl.caretPos.boundingLeft);
		if (textRangeObject.text.length > 0)
		{
			textRangeObject.text = preText + textRangeObject.text + postText;
		}
		else
		{
			textRangeObject.text = preText + text + postText;
		}
	}
	else
		textEl.value += preText + text + postText;
*/
	
//	var textRangeObject = document.selection.createRange();
//	textRangeObject.text = "<a>" + txtRangeObject.text + "</a>";
	
	/*
	if (textEl.createTextRange && textEl.caretPos) 
	{
		var caretPos = textEl.caretPos;
		caretPos.text =
			caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
			text + ' ' : text;
		caretPos.select();
	}
	*/
}

function handleTab(obj, evt) {
   var tabKeyCode = 9;
   if (event.keyCode == tabKeyCode && event.srcElement == obj) {
      obj.selection = document.selection.createRange();
      obj.selection.text = String.fromCharCode(tabKeyCode);
      event.returnValue = false;
   }
}
function doSelection(fld)
{
	fld.focus();
	fld.select();
}

