var limit = 500;

function check() {
   if(document.getElementById('f').comment.value.length > limit) {
     alert('Too much data!');
     document.getElementById('f').comment.focus();
     return false;}
   else
     return true;
}

function writeImgTag(code) {
	var obj = document.getElementById('f').comment;
	var text = code;
	insertAtCaret(obj, text);
}

function update() {
   var old = document.getElementById('f').counter.value;
   document.getElementById('f').counter.value = document.getElementById('f').comment.value.length;
   if(document.getElementById('f').counter.value > limit && old <= limit) {
     alert('Too much data in the text box!');
     if(document.styleSheets) {
       document.getElementById('f').counter.style.fontWeight = 'bold';
       document.getElementById('f').counter.style.color = '#ff0000'; } }
   else if(document.getElementById('f').counter.value <= limit && old > limit
	   && document.styleSheets ) {
       document.getElementById('f').counter.style.fontWeight = 'normal';
       document.getElementById('f').counter.style.color = '#166F07'; }
}

/***********************************
/*
/* This function is where all the magic happens to
/* allow the client to add an element to the textarea
/* and have the cursor positioned after that element
/*
/***********************************/

function insertAtCaret(obj, text) {
//	var obj = document.getElementById('f').article;
//	var text = "This is the stuff that goes into the textarea";
    if(document.selection) {
        // Go the IE way
        /* First of all, focus the object, we want to work with
		   If we do not do so, it is possible, that the selection
		   is not, where we expect it to be
		*/
		obj.focus();

		/* Create a TextRange based on the document.selection
		   This TextRanged can be used to replace the selected
		   Text with the new one
		*/
		var range = document.selection.createRange();

		/* If the range is not part of our Object (remember the
		   textarea or input field), stop processing here
		*/
		if(range.parentElement() != obj) {
		    return false;
		}

		/* Save the current value. We will need this value later
		   to find out, where the text has been changed
		*/
		var orig = obj.value.replace(/rn/g, "n");

		/* Replace the Text */
		range.text = text;

		/* Now get the new content and save it into
		   a temporary variable
		*/
		var actual = tmp = obj.value.replace(/rn/g, "n");

		/* Find the first occurance, where the original differs
		   from the actual content. This could be the startposition
		   of our text selection, but it has not to be. Think of the
		   selection "ab" and replacing it with "ac". The first
		   difference would be the "c", while the start position
		   is the "a"
		*/
		for(var diff = 0; diff < orig.length; diff++) {
		    if(orig.charAt(diff) != actual.charAt(diff)) break;
		}

		/* To get the real start position, we iterate through
		   the string searching for the whole replacement
		   text - "abc", as long as the first difference is not
		   reached. If you do not understand that logic - no
		   blame to you, just copy & paste it ;)
		*/
		for(var index = 0, start = 0;
		    tmp.match(text)
		        && (tmp = tmp.replace(text, ""))
		        && index <= diff;
		    index = start + text.length
		) {
		    start = actual.indexOf(text, index);
		}
    } else if(obj.selectionStart) {
        // Go the Gecko way
        /* Find the Start and End Position */
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;

		/* Remember obj is a textarea or input field */
		obj.value = obj.value.substr(0, start)
		    + text
		    + obj.value.substr(end, obj.value.length);
    }

	if(start != null) {
		setCaretTo(obj, start + text.length);
	} else {
		obj.value += text;
	}

	function setCaretTo(obj, pos) {
		if(obj.createTextRange) {
			var range = obj.createTextRange();
			range.move('character', pos);
			range.select();
		} else if(obj.selectionStart) {
			obj.focus();
			obj.setSelectionRange(pos, pos);
		}
	}
}

/***********************************
/*
/* This function writes one of a number of
/* smilies to the textarea... nice and simple too
/*
/***********************************/

/*function writeImgTag(code) {
	var obj = document.getElementById('f').article;
	var text = code;
	insertAtCaret(obj, text);
}*/

function writeImgTag(code) {
	var obj = document.getElementById('f').comment;
	var text = code;
	insertAtCaret(obj, text);
}