<!--

var selezione = false;

//browser type
var browser_type = navigator.userAgent.toLowerCase();
var browser_ver = parseInt(navigator.appVersion);

var browser_ie = ((browser_type.indexOf("msie") != -1) && (browser_type.indexOf("opera") == -1));
var browser_nav  = ((browser_type.indexOf('mozilla')!=-1) && (browser_type.indexOf('spoofer')==-1)
                && (browser_type.indexOf('compatible') == -1) && (browser_type.indexOf('opera')==-1)
                && (browser_type.indexOf('webtv')==-1) && (browser_type.indexOf('hotjava')==-1));

var so_windows   = ((browser_type.indexOf("win")!=-1) || (browser_type.indexOf("16bit") != -1));
var so_mac    = (browser_type.indexOf("mac")!=-1);

bbcode = new Array();
bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[li]','[/li]','[url]','[/url]','[quote]','[/quote]');

function grandezza_array(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "indefinito") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

function arraypush(thearray,value) {
	thearray[ grandezza_array(thearray) ] = value;
}

function arraypop(thearray) {
	thearraysize = grandezza_array(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

function bbstile(bbnumero,nomecampo,button) {

	addbbcode = button;
	
	donotinsert = false;
	selezione = false;
	bblast = 0;
	var campo = nomecampo;
   
	if (bbnumero == -1) {
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			eval('document.formcommento.'+campo+'.value += bbtags[butnumber + 1]');
			buttext = eval('document.formcommento.'+addbbcode+'' + butnumber + '.value');
			eval('document.formcommento.'+addbbcode+'' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		eval('document.formcommento.'+campo+'.focus()');
		return;
	}


	if ((browser_ver >= 4) && browser_ie && so_windows)
		selezione = document.selection.createRange().text;


	if (selezione) {
		document.selection.createRange().text = bbtags[bbnumero] + selezione + bbtags[bbnumero+1];
		eval('document.formcommento.'+campo+'.focus()');
		selezione = '';
		return;
	}

	for (i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumero+1) {
			bblast = i;
			donotinsert = true;
		}
	}


	if (donotinsert) {	
		while (bbcode[bblast]) {
				butnumber = arraypop(bbcode) - 1;
				eval('document.formcommento.'+campo+'.value += bbtags[butnumber + 1]');
				buttext = eval('document.formcommento.'+addbbcode+'' + butnumber + '.value');
		eval('document.formcommento.'+addbbcode+''+ butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
			}
			eval('document.formcommento.'+campo+'.focus()');
			return;
	} else { 
		
		eval('document.formcommento.'+campo+'.value += bbtags[bbnumero]');
		arraypush(bbcode,bbnumero+1);
		eval('document.formcommento.'+addbbcode+''+bbnumero+'.value += "^"');
		eval('document.formcommento.'+campo+'.focus()');
		return;
		
	}
	storeCaret(document.formcommento.campo);
	}
	
	 
	
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}


/* PROMPT per formcommento */
function url_prompt(campo,tipo,divisore) { 
var campo = campo;

if(divisore == 1){
url=prompt("Scrivere l'url del link","http://"); 
testo=prompt("Scrivere il testo del link",""); 
primaparte="[url="+url+"]"+testo+"[/url]*\n";
	} else {
	
	if(tipo == "img"){
url=prompt("Scrivere l'url dell'immagine","http://"); 
primaparte="[img]"+url+"[/img] ";
			}else if(tipo == "email"){
email=prompt("Scrivere l'indirizzo email",""); 
primaparte="[email]"+email+"[/email] ";		
				} else {			
url=prompt("Scrivere l'url del link","http://"); 
testo=prompt("Scrivere il testo del link",""); 
primaparte="[url="+url+"]"+testo+"[/url] ";
				}
			}
valore= eval('document.formcommento.'+campo+'.value'); 
valore=primaparte; 
eval('document.formcommento.'+campo+'.value +=valore'); 
eval('document.formcommento.'+campo+'.focus()');
}

//-->