// Constants
var CONST_IDENT = '    ';
var CONST_NL = "\n";

function TextEditor(name, maxlength,warningColor){
	this.name=name;
	this.maxlength=maxlength;
	this.TextObj=document.getElementById(name);
	this.ccObj=document.getElementById("counterOf" + name);
	if (typeof(this.ccObj)=='undefined') this.ccObj=null;
	else this.defaultColor=this.ccObj.style.color;
	this.warningColor=warningColor;
	
	this.insertType=function(type){
		switch(type){
			case "bold": this.insertAtCaret('[b]','[/b]'); break;
			case "italic": this.insertAtCaret('[i]','[/i]'); break;
			case "under": this.insertAtCaret('[u]','[/u]'); break;
			case "enum": this.insertAtCaret('[enum]','[/enum]'); break;
			case "num": this.insertAtCaret('[num]','[/num]'); break;
			case "center": this.insertAtCaret('[c]','[/c]'); break;
			case "tab": this.insertAtCaret('[tab]'); break;
			case "url": this.insertAtCaret('[url cim=""]','[/url]'); break;
			case "email": this.insertAtCaret('[email cim=""]','[/email]'); break;
		}
	}
	
	this.insertAtCaret=function(before, after, replace, select){
		return TextEditor.insertAtCaret(this.TextObj,before, after, replace, select);
	}
	
	this.getCaret=function(MSIEReturnNotLogicalPosition){
		return TextEditor.getCaret(this.TextObj,MSIEReturnNotLogicalPosition)
	}
	
	this.setCaret=function(pos, scrollTop){
		return TextEditor.setCaret(this.TextObj, pos, scrollTop)
	}
	
	this.ccRefresh=function(){
		if (this.ccObj==null) return;
		if (this.maxlength>-1){
			var v;
			v=this.maxlength-this.TextObj.value.length;
			this.ccObj.innerHTML=v;
			if (v<0) this.ccObj.style.color=this.warningColor;
			else this.ccObj.style.color=this.defaultColor;
		}
		else{
			this.ccObj.innerHTML=this.TextObj.value.length
		}
	}
	
	this.ccRefresh();
}
TextEditor.insertAtCaret=function(textField, before, after, replace, select) {
	if (arguments.length < 3 || !after) after = '';
	if (arguments.length < 4 || !replace) replace = false;
	if (arguments.length < 5 || select) select = true;

	if (typeof textField.selectionStart != 'undefined') {
		var startPos = textField.selectionStart;
		var endPos = textField.selectionEnd;
		textField.value = textField.value.substr(0, startPos) + before
						+ (replace ? "" : textField.value.substring(startPos, endPos))
						+ after + textField.value.substr(endPos);
		if (select) {
			textField.setSelectionRange(startPos,
				endPos + before.length + after.length - (replace ? endPos-startPos : 0));
		} else {
			startPos = startPos + before.length + after.length;
			textField.setSelectionRange(startPos , startPos);
		}
	} else if (textField.createTextRange) {
		textField.focus();
		var pos = TextEditor.getCaret(textField);
		var range = document.selection.createRange();
		// Adding the before/after text to the textRange
		range.text = before+(replace ? "" : range.text)+after;

		// Selecting the needed text
		if (select) {
			pos.selectionLength = replace ? before.length + after.length :
				pos.selectionLength + before.length + after.length;
		} else {
			pos.position += before.length + after.length;
			pos.selectionLength = 0;
		}
		TextEditor.setCaret(textField, pos);
	} else {
		var position = textField.value.length+before.length;
		textField.value += (before + after);
		setCursorPosition(textField, position);
	}
}

TextEditor.getCaret=function(textField, MSIEReturnNotLogicalPosition)
{
	if (arguments.length < 2) MSIEReturnNotLogicalPosition = false;

	if (typeof textField.selectionStart != "undefined") {
		textField.focus();
		return {
			position: textField.selectionStart,
			selectionLength: textField.selectionEnd-textField.selectionStart
		};
	} else if (textField.createTextRange) {
		var newLineNumAfterCursor = 0;
		match = textField.value.match(/\n/g);
		if (match) {
			newLineNumAfterCursor = match.length;
		}

		textField.focus();
		var position = textField.value.length;
		var selectionLength = document.selection.createRange().text.length;
		var cursor = document.selection.createRange().duplicate();

		while (cursor.parentElement() == textField && cursor.move('character', 1)) {
			if (textField.value.charAt(position - 1) == CONST_NL) {
				position -= 1;
				newLineNumAfterCursor--;
			}
			position--;
		}

		if (MSIEReturnNotLogicalPosition) {
			return {
				position: position + 1,
				selectionLength: selectionLength
			};
		} else {
			return {
				position: position + 1 - (newLineNumAfterCursor),
				selectionLength: selectionLength
			};
		}
	}
}

TextEditor.setCaret=function(textField, pos, scrollTop)
{
	if (typeof pos == "number") {
		pos = {position: pos, selectionLength: 0}
	}
	if (typeof pos.position == "undefined") {
		pos.position = getCaret(textField).position;
	}
	if (pos.selectionLength < 0) {
		pos.position += pos.selectionLength;
		pos.selectionLength = -pos.selectionLength;
	}

	if (typeof textField.selectionStart != 'undefined') {
		textField.focus();
		textField.setSelectionRange(pos.position, pos.position+pos.selectionLength);
	} else if (textField.createTextRange) {
		var textRange = textField.createTextRange();
		textRange.moveStart("character", pos.position);
		textRange.collapse();
		textRange.moveEnd("character", pos.selectionLength);
		textRange.select();
	}
	if (typeof scrollTop != "undefined" && typeof textField.scrollTop != "undefined") {
		textField.scrollTop = scrollTop;
	}
}
