function saveCaret(text)
{
  if ( text.isTextEdit ) 
    text.caretPos = document.selection.createRange();
}
function cursorPosition(text)
{
  if ( text.isTextEdit && text.caretPos )
  {
    var bookmark = "~~";
    var orig = text.value;
    text.caretPos.text = bookmark;
    var i = text.value.search( bookmark );
    text.value = orig;
	setCursor(text,i);    
	return i;
  }
}


function setCursor(text,pos)
{
	if ( text.isTextEdit ) 
		text.caretPos = document.selection.createRange();
	text.caretPos.moveStart("character",-text.value.length),text.caretPos.moveStart("character",pos);
	text.caretPos.collapse(), text.caretPos.select();
}

function getSel()
{
	var txt = '';
	if (window.getSelection)
		txt = window.getSelection();
	else if (document.getSelection)
		txt = document.getSelection();
	else if (document.selection)
		txt = document.selection.createRange().text;
	else return;
	return txt;
}
