<!--

//**********************************************//
//                   VBCODE                       
//**********************************************//

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);

bbcode1 = new Array();
bbtags1 = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[li]','[/li]','[url]','[/url]','[center]','[/center]','[size=]','[/size]');

bbcode2 = new Array();
bbtags2 = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[li]','[/li]','[url]','[/url]','[center]','[/center]','[down]','[/down]','[code]','[/code]','[quote]','[/quote]','[size]','[/size]');

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,valore) {

if (valore == 1) {

	bbcode = bbcode1;
	bbtags = bbtags1;
	addbbcode = button;

	} else if (valore == 3) {

fontsize = prompt("Scrivere la grandezza del testo",""); 

	bbcode = bbcode1;
	bbtags = bbtags1;
	addbbcode = button;
	bbtags[bbnumero] = "[size=" + fontsize + "]";

	} else if (valore == 4) {

fontsize = prompt("Scrivere la grandezza del testo",""); 

	bbcode = bbcode2;
	bbtags = bbtags2;
	addbbcode = button;
	bbtags[bbnumero] = "[size=" + fontsize + "]";

	} else {
	
	bbcode = bbcode2;
	bbtags = bbtags2;
	addbbcode = button;

	}
	
	donotinsert = false;
	selezione = false;
	bblast = 0;
	var campo = nomecampo;
   
	if (bbnumero == -1) {
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			eval('document.saibal.'+campo+'.value += bbtags[butnumber + 1]');
			buttext = eval('document.saibal.'+addbbcode+'' + butnumber + '.value');
			eval('document.saibal.'+addbbcode+'' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		eval('document.saibal.'+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.saibal.'+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.saibal.'+campo+'.value += bbtags[butnumber + 1]');
				buttext = eval('document.saibal.'+addbbcode+'' + butnumber + '.value');
		eval('document.saibal.'+addbbcode+''+ butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
			}
			eval('document.saibal.'+campo+'.focus()');
			return;
	} else { 
		
		eval('document.saibal.'+campo+'.value += bbtags[bbnumero]');
		arraypush(bbcode,bbnumero+1);
		eval('document.saibal.'+addbbcode+''+bbnumero+'.value += "^"');
		eval('document.saibal.'+campo+'.focus()');
		return;
		
	}
	storeCaret(document.saibal.campo);
	}
	
	 
	
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}
//**********************************************//


//**********************************************//
// CODICE INSERIMENTO CODICE IN PUNTO SPECIFICO   
//**********************************************//

function insert_caret(modulo,campo,sml,add_space) {
	var obj = eval('document.'+modulo+'.'+campo);
	if (document.all) {
		obj.focus();
	  	var sel = document.selection;
	  	var rng = sel.createRange();
	  	rng.colapse;
		if	(add_space) {
	  	rng.text = sml;
		} else {
			rng.text = ' ' +sml+ ' ';
		}
	} else if(obj.selectionEnd){
		var lng = obj.textLength;
		var from = obj.selectionStart;
		var to = obj.selectionEnd;
		obj.value = obj.value.substring(0,from)+' '+sml+' '+obj.value.substring(to,lng)
	} else obj.value += ' ' +sml+ ' ';
	obj.focus();
}	
//**********************************************//


//**********************************************//
//             PROMPT BY SAIBAL                   
//**********************************************//

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 if (tipo == "colore") {
		color = prompt("Scrivi il codice o il nome del colore","");
		color_testo = prompt("Scrivi il testo da colorare","");
		primaparte = "[color="+color+"]"+color_testo+"[/color]";
	} else {			
		url=prompt("Scrivere l'url del link","http://"); 
		testo=prompt("Scrivere il testo del link",""); 
		primaparte="[url="+url+"]"+testo+"[/url] ";
	}
}
	valore= eval('document.saibal.'+campo+'.value'); 
	valore=primaparte; 
	eval('document.saibal.'+campo+'.value +=valore'); 
	eval('document.saibal.'+campo+'.focus()');
}
//**********************************************//


//**********************************************//
//     FUNZIONE CALCOLA LUNGHEZZA TESTO           
//**********************************************//

function lunghezza (campo) {
	var lunghezza= eval('document.saibal.'+campo+'.value.length');
		alert('Il testo contiene ' +lunghezza+ ' caratteri');
		eval('document.saibal.'+campo+'.focus()');
	}
//**********************************************//


//**********************************************//
//    INSERIRE I LOGHI E ARTICOLI CORRELATI       
//**********************************************//

function insert(nome,campo,setclose) {
		eval('top.opener.document.saibal.'+campo+'.value += nome');
	if (setclose == "yes") {
		eval('top.opener.document.saibal.'+campo+'.focus()');
		top.window.close();
	}
}
//**********************************************//


//**********************************************//
//   INSERIRE LE IMMAGINI PRESENTI SUL SERVER     
//**********************************************//
//ricorda che la var "url_img" si trova in "head_popup_images.php"
function insert_serverimg(nome_img,campo) {

	imgcode="[img]"+url_img+"/"+nome_img+"[/img] ";
	valore= eval('top.opener.document.saibal.'+campo+'.value'); 
	valore=imgcode; 

eval('top.opener.document.saibal.'+campo+'.value +=valore');

}
//**********************************************//


//**********************************************//
//          FUNZIONE ALLARGA TEXTAREA             
//**********************************************//

function input_height(areaid, pixel) {
	var box = eval('document.saibal.'+areaid);
	var boxheight = parseInt(box.style.height);
	var newheight = boxheight + pixel;

	if (newheight > 0) {
		box.style.height = newheight + "px";
	}
//eval('document.saibal.'+areaid+'.focus()');
	return false;
}
//**********************************************//


//**********************************************//
//   FUNZIONE PER USARE I TAB NELLA TEXTAREA     
//**********************************************//
function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    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 replaceSelection (input, replaceString) {
	if (input.setSelectionRange) {
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		input.value = input.value.substring(0, selectionStart)+ replaceString + input.value.substring(selectionEnd);
    
		if (selectionStart != selectionEnd){ 
			setSelectionRange(input, selectionStart, selectionStart + 	replaceString.length);
		}else{
			setSelectionRange(input, selectionStart + replaceString.length, selectionStart + replaceString.length);
		}

	} else if (document.selection) {
		var range = document.selection.createRange();

		if (range.parentElement() == input) {
			var isCollapsed = range.text == '';
			range.text = replaceString;

			 if (!isCollapsed)  {
				range.moveStart('character', -replaceString.length);
				range.select();
			}
		}
	}
}

function catchTab(item,e){
	if(navigator.userAgent.match("Gecko")){
		c=e.which;
	}else{
		c=e.keyCode;
	}
	if(c==9){
		replaceSelection(item,String.fromCharCode(9));
		setTimeout("document.getElementById('"+item.id+"').focus();",0);	
		return false;
	}
		    
}
//-->
