
	var ns6 = document.getElementById && ! document.all;

	function setSelectionRange(input, selectionStart, selectionEnd) {
		if (input.setSelectionRange) { // NETSCAPE
			input.focus();
			input.setSelectionRange(selectionStart, selectionEnd);
		} else if (input.createTextRange) {
			var range = input.createTextRange();
			range.collapse(true);
			range.moveEnd('character', selectionEnd);
			range.moveStart('character', selectionStart);
			range.select();
		}
	}
			
	function getSelectedText( input ) {
		var selectedText = "";
		if ( input.setSelectionRange ) { // NETSCAPE
			var selectionStart = input.selectionStart;
			var selectionEnd = input.selectionEnd;
			if ( selectionStart != selectionEnd ) {
				selectedText = input.value.substring(selectionStart,selectionEnd);
			} 
		} else if ( document.selection ) { // EXPLORER
			selectedText = document.selection.createRange( ).text;
		}
		return selectedText;
	}
			
	function replaceSelection (input, replaceString) {
		if (input.setSelectionRange) { //NETSCAPE
			var selectionStart = input.selectionStart;
			var selectionEnd = input.selectionEnd;
			input.value = input.value.substring(0, selectionStart) + replaceString + input.value.substring(selectionEnd);
			if (selectionStart != selectionEnd) { // has there been a selection
				setSelectionRange(input, selectionStart, selectionStart + replaceString.length);
			} else { // set caret
				setCaretToPos(input, selectionStart + replaceString.length);
			}
		} else if (document.selection) { //EXPLORER
			var range = document.selection.createRange();
			if (range.parentElement() == input) {
				var isCollapsed = range.text == '';
				range.text = replaceString;
				if (! isCollapsed) { 
					/*****************************************************/
					// there has been a selection
					//it appears range.select() should select the newly
					//inserted text but that fails with IE
					/*****************************************************/
					range.moveStart('character', -replaceString.length);
					range.select();
				}
			}
		}
	}		
			
	function setCaretToPos (input, pos) {
		setSelectionRange(input, pos, pos);
	}						

	/*****************************************************************************/
	/* ricordare di richiamare questa funzione negli eventi onclick e onkeyup    */
	/* della textarea per cui attivare le funzionalitą di inserimento e          */
	/* selezione testo e tag personalizzati.                                     */
	/*****************************************************************************/
	function storeCaret (textEl) {
		if (textEl.createTextRange) {
			textEl.caretPos = document.selection.createRange().duplicate();
		}
	} 

			
	function insertAtCaret (textEl, text) {
		
		if (textEl.createTextRange && textEl.caretPos) {
			var caretPos = textEl.caretPos;
			caretPos.text =
				caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
				text + ' ' : text;
		} else {
			textEl.value  = textEl.value + text; // for non MSIE browsers just append it
		}
		
		return true;
	}
			
			
	function insertMarkup(input,markup) {
		var text = getSelectedText(input);
		if ( text != "" ) {
			addMarkupToSelection( input, markup, text );
		} else {
			switch(markup) {
				case "B" : // bold
					text = "[b] [/b]";
					break;
				case "I" : // italic
					text = "[i] [/i]";
					break;
				case "U" : // underlined
					text = "[u] [/u]";
					break;
				case "A" : // tag a
					text = "<a href=\"\" target=\"_new\" title=\"\"></a>";
					break;
			}
			insertAtCaret(input,text);
		}
	}
			
	function addMarkupToSelection( input, markup, text ) {
		if ( typeof( text ) == "undefined" ) {
			text = getSelectedText(input);
		}
		switch(markup) {
			case "B" : // bold
				text = "[b]" + text + "[/b]";
				break;
			case "I" : // italic
				text = "[i]" + text + "[/i]";
				break;
			case "U" : // underlined
				text = "[u]" + text + "[/u]";
				break;
			case "A" : // tag a
				text = "<a href=\"\" target=\"_new\" title=\"\">" + text + "</a>";
				break;
		}
		replaceSelection(input,text);
	}			
	
	function restrictinput(maxlength,e,placeholder){
		if (window.event && event.srcElement.value.length >= maxlength) {
			return false;
		} else if (e.target && e.target == eval(placeholder) && e.target.value.length >= maxlength) {
			var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
			if ( pressedkey.test(String.fromCharCode(e.which)) ) {
				e.stopPropagation( )
			}
		}
	}

	function countlimit(maxlength,e,placeholder){
		var theform=eval(placeholder)
		var lengthleft=maxlength-theform.value.length;
		var placeholderobj = document.all ? document.all[placeholder] : document.getElementById(placeholder);
		if ( window.event || e.target && e.target==eval(placeholder) ) {
			if (lengthleft<0) {
				theform.value=theform.value.substring(0,maxlength);
			}
			placeholderobj.innerHTML=lengthleft;
		}
	}


	function displaylimit(theform,thelimit) {
		var limit_text='<b><span id="'+theform.toString()+'">'+thelimit+'</span></b> caratteri rimanenti'
		if (document.all || ns6) {
			document.write(limit_text);
		}
		if (document.all) {
			eval(theform).onkeypress = function(){ return restrictinput(thelimit,event,theform) }
			eval(theform).onkeyup = function() { countlimit(thelimit,event,theform) }
		}
		else if (ns6) {
			document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
			document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
		}
	}
	
	function countChars(name,maxchar)
	{
		var textarea = eval(name);
		if ( textarea.value.length > maxchar ) {
			textarea.value = textarea.value.substring(0,maxchar);
			return false;
		} 
		return true;
	}	
	
	
	function checkTextLength( text, length ) {
		if ( typeof(length) == "undefined" ) {
			length = 20;
		}
		var re = /\s/gi;
		var vector = text.split(re);
		for( var i = 0; i < vector.length; i++ ) {
			if ( vector[i].length > length ) {
				return false;
			}
		}
		if ( i == 0 && text > length ) {
			return false;
		}
		return true;
	}

		
	function displaylimit2(theform,thelimit) {
		var limit_text='<p><span id="'+theform.toString()+'"></span></p>'
		if (document.all || ns6) {
			document.write(limit_text);
		}
		if (document.all) {
			eval(theform).onkeypress = function(){ return restrictinput(thelimit,event,theform) }
			eval(theform).onkeyup = function() { countlimit2(thelimit,event,theform) }
		}
		else if (ns6) {
			document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
			document.body.addEventListener('keyup', function(event) { countlimit2(thelimit,event,theform) }, true); 
		}
	}
	
	function countlimit2(maxlength,e,placeholder){
		var theform = eval(placeholder);
		var usedchars = theform.value.length;
		var lengthleft = maxlength - theform.value.length;
		var placeholderobj = document.all ? document.all[placeholder] : document.getElementById(placeholder);
		if ( window.event || e.target && e.target==eval(placeholder) ) {
			if ( lengthleft < 0 ) {
				theform.value = theform.value.substring(0,maxlength);
			}
			placeholderobj.innerHTML = "Caratteri utilizzati : <b>" + usedchars + "</b> - Caratteri rimanenti : <b>" + lengthleft + "</b>";
		}
	}
	
	
	


