// CaretPosition object
function CaretPosition()
{
    var start = null;
    var end = null;
}

function getCaretPosition(oField)
{
    // Initialise the CaretPosition object
    var oCaretPos = new CaretPosition();

    // IE support
    if(document.selection) {
	// Focus on the text box
	oField.focus();

	// This returns us an object containing
	// information about the currently selected text
	var oSel = document.selection.createRange();

	// Find out the length of the selected text
	// (you'll see why below)
	var selectionLength = oSel.text.length;

	// Move the selection start to 0 position.
	//
	// This is where it gets interesting, and this is why
	// some have claimed you can't get the caret positions
	// in IE.
	//
	// IE has no 'selectionStart' or 'selectionEnd' property,
	// so we can not get or set this value directly. We can
	// only move the caret positions relative to where they
	// currently are (this should make more sense when you read
	// the next line of code).
	//
	// Note, that even though we have moved the start
	// position on our object in memory, this is not reflected
	// in the browser until we call oSel.select() (which we're
	// not going to do here).
	//
	// Also note, the start position will never be a negative
	// number, no matter how far we try to move it back.
	oSel.moveStart ('character', -oField.value.length);

	// This is where it should start to make sense. We now know
	// our start caret position is the length of the currently
	// selected text minus the original selection length
	// (think about it).
	oCaretPos.start = oSel.text.length - selectionLength;

	// Since the start of the selection is at the start of the
	// text, we know that the length of the selection is also
	// the index of the end caret position.
	oCaretPos.end = oSel.text.length;
    }
    // Firefox support
    else if(oField.selectionStart || oField.selectionStart == '0') {
	// This is a whole lot easier in Firefox
	oCaretPos.start = oField.selectionStart;
	oCaretPos.end = oField.selectionEnd;
    }

    // Return results
    return (oCaretPos);
}

function setCaretPosition(oField, iCaretStart, iCaretEnd)
{
    // IE Support
    if (document.selection) {
	// Focus on the text box
	oField.focus();

	// This returns us an object containing
	// information about the currently selected text
	var oSel = document.selection.createRange();

	// Since we don't know where the caret positions
	// currently are (see comments in getCaretPosition() for
	// further information on this), move them to position 0.
	//
	// Note, the caret positions will never be a negative
	// number, no matter how far we try to move them back.
	oSel.moveStart ('character', -oField.value.length);
	oSel.moveEnd ('character', -oField.value.length);

	// Now we know the caret positions are at index 0, move
	// them forward to the desired position (move end caret
	// position first - actually not sure if moving start
	// caret position first affects the end caret position -
	// it might).
	//
	// Note, we allow for a null end caret position and just
	// default it to the same as the start caret position.
	if(iCaretEnd != null)
	    oSel.moveEnd ('character', iCaretEnd);
	else
	    oSel.moveEnd ('character', iCaretStart);

	oSel.moveStart ('character', iCaretStart);

	// Everything thus far has just been on our object in
	// memory - this line actually updates the browser
	oSel.select();
    }
    // Firefox support
    else if(oField.selectionStart || oField.selectionStart == '0') {
	oField.selectionStart = iCaretStart;

	if(iCaretEnd != null)
	    oField.selectionEnd = iCaretEnd;
	else
	    oField.selectionEnd = iCaretStart;

	oField.focus();
    }
}

function selectName(oField)
{
    // Use our previous method to get the current caret positions
    var caretPos = getCaretPosition(oField);

    // Use a handful of string functions to find out where the last
    // and next '[' and ']' characters are. Obviously, this can be
    // any character you like that suits your purpose.
    var text = oField.value;
    var prevText = oField.value.substring(0, caretPos.start);
    var nextText = oField.value.substring(caretPos.end);
    var prevCloseBracket = prevText.lastIndexOf("]");
    var prevOpenBracket = prevText.lastIndexOf("[");
    var nextCloseBracket = nextText.indexOf("]");

    // This basically asks - is our caret currently positioned in
    // the middle of a cell name?
    if(prevOpenBracket > prevCloseBracket && nextCloseBracket != -1) {
	// IE Support
	if (document.selection) {
	    // This returns us an object containing
	    // information about the currently selected text
	    var oSel = document.selection.createRange();

	    // Since we don't know where the start caret position
	    // currently is (see comments in getCaretPosition() for
	    // further information on this), move it to position 0.
	    //
	    // Note, the caret position will never be a negative
	    // number, no matter how far we try to move it back.
	    oSel.moveStart('character', -oField.value.length);

	    // Move selection start and end to desired position
	    oSel.moveStart('character', prevOpenBracket);
	    oSel.moveEnd('character', nextCloseBracket+1);

	    // Apply the selection
	    oSel.select();
	}
	// Firefox support
	else if(oField.selectionStart || oField.selectionStart == '0') {
	    oField.selectionStart = prevOpenBracket;
	    oField.selectionEnd = caretPos.end + nextCloseBracket+1;
	    oField.focus();
	}
    }
}
