
function TextboxHandler( _sTextboxId )
{
	
	this.sTextboxId = _sTextboxId;
	this.oTextbox = document.getElementById( this.sTextboxId );
	this.iCarPos = 0;
	this.sChr = "a";
	
}

TextboxHandler.prototype.setTextboxId = function( _sTextboxId )
{
	
	this.sTextboxId = _sTextboxId;
	this.oTextbox = document.getElementById( this.sTextboxId );
	
}

TextboxHandler.prototype.getTextboxId = function()
{
	
	return this.sTextboxId;
	
}

TextboxHandler.prototype.setTextbox = function( _oTextbox )
{
	
	this.oTextbox = _oTextbox;
	this.sTextboxId = this.oTextbox.id;
	
}

TextboxHandler.prototype.getTextbox = function()
{
	
	return this.oTextbox;
	
}

TextboxHandler.prototype.setCarPos = function( _iCarPos )
{
	
	if (this.oTextbox.createTextRange)
	{
	
		// IE, opera
		
		this.focusOnTextbox();
		var txtRange = this.oTextbox.createTextRange();
	    txtRange.move( "character", _iCarPos );
	    txtRange.select();
		
		this.getCarPos();
		
	}
	
	else if ( this.oTextbox.setSelectionRange )
	{
		
		// firefox, safari
		
		this.focusOnTextbox();
		this.oTextbox.setSelectionRange( _iCarPos, _iCarPos );
		
		this.getCarPos();
		
	}
	
}

TextboxHandler.prototype.getCarPos = function()
{
	
	this.focusOnTextbox();
	
	if ( this.oTextbox.selectionStart )
	{
		
		// firefox, opera
		
		this.iCarPos = this.oTextbox.selectionStart;
		return this.iCarPos;

	}
	
	else if (document.selection && 
			document.selection.createRange && 
			document.activeElement && 
			document.activeElement.createTextRange &&
			document.selection.createRange().compareEndPoints)
	{
		
		// IE
		
		var obj = document.activeElement;
		var cur = document.selection.createRange();
		var carPos = 0;
		if ( obj && cur ) 
		{
			var tr = obj.createTextRange();
			
			if (tr)
			{
				
				while ( cur.compareEndPoints( "StartToStart", tr ) > 0 )
				{
					tr.moveStart( "character", 1 );
					carPos++;
				}
				
				this.iCarPos = carPos;
				return this.iCarPos;
				
			}
		}
		
	}
	
	else
	{
		
		 // firefox2.0win and opera9.20win: 
		 // will jump to this, when caret is at the beginning of the texbox. 
		 // (how come the textBox.selectionStart will not work in this case?)
		
		this.iCarPos = 0;
		return this.iCarPos;
		
	}
	
}

TextboxHandler.prototype.setChr = function( _sChr )
{
	
	this.sChr = _sChr;
	
}

TextboxHandler.prototype.getChr = function()
{
	
	return this.sChr;
	
}

TextboxHandler.prototype.insAtCursor = function()
{
	
	this.focusOnTextbox();
		
	if ( document.selection && document.selection.createRange )
	{
		
		// IE, opera
		
		// the following line is needed, because the textbox loses caret position information, when it loses focus.
		// in firefox for example the caret position is stored even after the textbox loses focus.
		this.setCarPos( this.iCarPos );
		
		var range = document.selection.createRange();
		range.text = this.sChr;
		
		this.focusOnTextbox();

	}
	
	else
	{
		
		// firefox
		
		var currentPos = this.getCarPos();
		var currentValue = this.oTextbox.value;
		var currentLength = this.oTextbox.value.length;
		var currentBegin = currentValue.substring( 0, currentPos );
		var currentEnd = currentValue.substring( currentPos, currentLength );
		
		var textNew = currentBegin + this.sChr + currentEnd;
		
		this.oTextbox.value = textNew;
		this.setCarPos( currentPos+1 );
		this.focusOnTextbox();
		
	}
	
	this.storeCursorPos();
	
}

TextboxHandler.prototype.focusOnTextbox = function()
{
	
	if ( this.oTextbox.focus )
	{
		
		this.oTextbox.focus();
		
	}
	
}

TextboxHandler.prototype.storeCursorPos = function()
{
	
	this.iCarPos = this.getCarPos();
	
}


