//funzioni javascript

var ms_out = true;


//-------------------------------------------------------------------------------------------
//	visualizza il codice html dell' RTE
//-------------------------------------------------------------------------------------------

function rte_code ()  {

	// display html
	if( post.rte_text.style.display != "" )
	{
		updateRTE();
		oMessageFrame.style.display = "none";
		post.rte_text.style.display = "";
		post.rte_text.value = post.message.value;
		post.sel_template.disabled = true;
		tab_btn1.style.display = "none";
		tab_btn2.style.display = "none";
	}
	// display editor
	else
	{
		post.rte_text.style.display = "none";
		oMessageFrame.style.display = "";
		oMessageFrame.contentWindow.document.body.innerHTML = post.rte_text.value;
		updateRTE();
		post.sel_template.disabled = false;
		tab_btn1.style.display = "";
		tab_btn2.style.display = "";
	}
}


//-------------------------------------------------------------------------------------------
//	carica un modello nell'editor
//-------------------------------------------------------------------------------------------

function rte_template( sel ) {
	n = eval("post.template"+sel.options[sel.selectedIndex].value+".value");
	oMessageFrame.contentWindow.document.body.innerHTML = n
}

//-------------------------------------------------------------------------------------------
//	visualizza un messaggio in cui l'utente può rispondere si o no
//-------------------------------------------------------------------------------------------

function question(txt)	{
	return window.confirm(txt);
}


//-------------------------------------------------------------------------------------------
//	apre finestra inserimento files/immagini
//-------------------------------------------------------------------------------------------

function rte_insert_file(type)	{
	javascript:openWin('rt_file.asp?type='+type,'smile_window','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,width=515,height=310');
}


//-------------------------------------------------------------------------------------------
//	inserisci smile
//-------------------------------------------------------------------------------------------

function rte_smile(img)	{
	img = "images/smiley/" + img;
	window.opener.frames.message.focus();								
	window.opener.frames.message.document.execCommand('InsertImage', false, img);
	window.close();
}


//-------------------------------------------------------------------------------------------
//	inserisci linea
//-------------------------------------------------------------------------------------------

function rte_line()	{
	message.focus();
	var htmlLink = message.document.selection.createRange()
	htmlLink.pasteHTML('<hr>');
	message.document.execCommand('paste', false, '');
}


//-------------------------------------------------------------------------------------------
//	stili bottoni rte over
//-------------------------------------------------------------------------------------------

function rte_btn_over (btn)	{
	// border
	btn.style.borderColor="#3264D2";
	// background
	btn.style.background="#E6F0FF";
}


//-------------------------------------------------------------------------------------------
//	stili bottoni rte out
//-------------------------------------------------------------------------------------------

function rte_btn_out (btn)	{
	// border
	btn.style.borderColor="#798494";
	// background
	btn.style.background="#FFFFFF";
}


//-------------------------------------------------------------------------------------------
//	stili bottoni rte down
//-------------------------------------------------------------------------------------------

function rte_btn_down (btn)	{
	// border
	btn.style.borderColor="#003399";
	// background
	btn.style.background="#AAC8FF";
}


//-------------------------------------------------------------------------------------------
//	sposta file o cartella
//-------------------------------------------------------------------------------------------

function move_element(nome,ext,folder)	{
	if( nome.length <= 0)
	{
		alert("Selezionare un elemento...");
		return;
	}
	if( ext == 'dir' )	{
		title = "Sposta Cartella"
	}
	else	{
		title = "Sposta File"
	}

	openWin('components/copy_move_file.asp?title='+escape(title)+'&ext='+ext+'&file='+escape(nome)+'&dir='+escape(folder),'copy_move_window','toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=480,height=280');
}


//-------------------------------------------------------------------------------------------
//	copia file o cartella
//-------------------------------------------------------------------------------------------

function copy_element(nome,ext,folder)	{
	if( nome.length <= 0)
	{
		alert("Selezionare un elemento...");
		return;
	}
	if( ext == 'dir' )	{
		title = "Copia Cartella"
	}
	else	{
		title = "Copia File"
	}

	openWin('components/copy_move_file.asp?title='+escape(title)+'&ext='+ext+'&file='+escape(nome)+'&dir='+escape(folder),'copy_move_window','toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=480,height=280');
}


//-------------------------------------------------------------------------------------------
//	scarica un file
//-------------------------------------------------------------------------------------------

function download(nome,ext,folder)	{
	if( nome.length <= 0 | ext == "dir" )
	{
		alert("Selezionare un file...");
		return;
	}
	window.open("."+folder+nome);
}


//-------------------------------------------------------------------------------------------
//	evidenzia il file selezionato nel pannello gestione file
//-------------------------------------------------------------------------------------------

function hilite(form,items,table,id)	{
	// hilite this tab
	table.borderColor="0066FF";
	table.background="images/explorer_bg.gif";

	// unhilite all others tables (items)
	for (i=1; i<=items; i++)
	{
		if(i!=id)
		{
			obj = eval("tab_" + i)
			//alert(obj.borderColor)
			obj.borderColor="white"
			obj.background="";
		}
	}
}


//-------------------------------------------------------------------------------------------
//	evidenzia il file selezionato nel pannello gestione file
//-------------------------------------------------------------------------------------------

function unselect(form,items)	{
	
	if ( ms_out == false)
		return;

	// unhilite all tables (items)
	for (i=1; i<=items; i++)
	{
		obj = eval("tab_" + i)
		obj.borderColor="white";
		obj.background="";
	}

	//inserisci dati nel form
	parent.explorer_task.form_info.nome.value = "";
	//inserisci dati nel form
	parent.explorer_task.form_info.ext.value = "";
	//inserisci dati nel form
	parent.explorer_task.form_info.folder.value = form.folder.value;

	parent.explorer_task.menu3.innerHTML = "<br>&nbsp;Selezionare un elemento...<br><br>";
}

//-------------------------------------------------------------------------------------------
//	controlla se il sito è all'interno di un altro frame
//-------------------------------------------------------------------------------------------

function check_location()
{
	if ( top.location.href != self.location.href && self.location.href.search("export")==-1 )
		top.location = self.location
}


//-------------------------------------------------------------------------------------------
//	show details about file / folder
//-------------------------------------------------------------------------------------------

function show_details(name,size,mdate,cdate,type,ext,attrib,link)
{
	//inserisci dati nel form
	parent.explorer_task.form_info.nome.value = name
	//inserisci dati nel form
	parent.explorer_task.form_info.ext.value = ext
	//inserisci dati nel form
	parent.explorer_task.form_info.folder.value = form_folder.folder.value

	//visualizza informazioni sull'elemento selezionato
	if(name.length>20)
		name = name.substring(0,18) + "..."

	msg = "<font size=1><b>" + name + "</b><br> " + type + " <br><br>"
	msg = msg + "Data ultima modifica: "+ mdate + " <br>"
	msg = msg + "Data creazione: "+ cdate + " <br><br>"
	msg = msg + "Attributi: " + attrib + " "//<br>"
	
	//se non è una cartella
	if(ext != "dir")
	{
	   msg = msg + "Estensione: " + ext + " <br>"
	   msg = msg + "<br>Dimensione: " + size //+ "<br>"
	}

	//se è un'immagine
	if(ext == "gif" || ext == "jpg" || ext == "png" )
	{
	  //msg = msg + "<br>Anteprima:<br>"
	  msg = msg + "<p style=\"margin-top: 0\" align=\"center\"><img border=\"0\" src=\"" + link + "\" width=\"128\" height=\"128\" vspace=\"5\"><br>"
	}

	//mostra
	parent.explorer_task.menu3.innerHTML=msg + "</font>"
}


//-------------------------------------------------------------------------------------------
//	basic image swap
//-------------------------------------------------------------------------------------------

function swap(name,file)
{
	name.src = file;
}


//-------------------------------------------------------------------------------------------
//	invert menu
//-------------------------------------------------------------------------------------------

function menu(objRef,imgRef){
    objRef.style.display = (objRef.style.display=="none") ? "" : "none";
    if (objRef.style.display == "none")
    {
    	imgRef.src=img2.src;
	//display = " "
    }
    else
    {
    	imgRef.src=img1.src;
	//display = "none"
    }

    //salva cookie
    document.cookie = objRef.id + " = " + objRef.style.display	
}


//-------------------------------------------------------------------------------------------
// funzione che rinomina un file o una cartella
//-------------------------------------------------------------------------------------------

function rename_item (base,nome,ext)
{
	if( nome.length <= 0 )
	{
		alert("Selezionare un file...");
		return;
	}
		

	if(ext !="dir")
	{
		var new_name = window.prompt('Specificare il nuovo nome del file '+ nome,nome);
		if(new_name)
		parent.location='explorer.asp?dir=' + escape(base) + '&action=rename_file&new_name=' + escape(new_name) + '&old_name=' + escape(nome);
	}
	else
	{
		var new_name = window.prompt('Specificare il nuovo nome della cartella '+ nome,nome);
		if(new_name)
		parent.location='explorer.asp?dir=' + escape(base) + '&action=rename_folder&new_name=' + escape(new_name) + '&old_name=' + escape(nome);
	}
}


//-------------------------------------------------------------------------------------------
// funzione che chiede all'utente il nome della nuova cartella e lo spedisce come parametro a explorer.asp
//-------------------------------------------------------------------------------------------

function delete_item (base,nome,ext)
{
	if( nome.length <= 0 )
	{
		alert("Selezionare un file...");
		return;
	}

	if(ext !="dir")
	{
		if(window.confirm("Eliminare il file "+nome+"?"))
		{
		//alert(base+"\n"+nome+"\n"+ext)
		parent.location='explorer.asp?dir=' + escape(base) + '&action=delete_file&file_name=' + escape(nome);
		}
	}
	else
	{
		if(window.confirm("Eliminare la cartella "+nome+" e tutto il suo contenuto?"))
		{
		//alert(base+"\n"+nome+"\n"+ext)
		parent.location='explorer.asp?dir=' + escape(base) + '&action=delete_folder&folder_name=' + escape(nome);
		}
	}
}


//-------------------------------------------------------------------------------------------
// funzione che chiede all'utente il nome della nuova cartella e lo spedisce come parametro a explorer.asp
//-------------------------------------------------------------------------------------------

function create_folder (base)
{
	var new_folder = window.prompt('Specificare il nome della nuova cartella...','');
	if(new_folder)
		parent.location='explorer.asp?dir=' + escape(base) + '&action=new_folder&folder_name=' + escape(new_folder);
}


//-------------------------------------------------------------------------------------------
// controlla il form per la registrazione
//-------------------------------------------------------------------------------------------

function check_form_profilo(form)	{
	errors = 0;
	msg = "";

	// nome utente
	if ( form.txt_insert_user.value.length < 4 )	{
		errors++;
		msg += "\n\tnome utente\t- il nome utente deve essere di almeno 4 caratteri";
	}
	// password
	if ( form.txt_insert_password.value.length < 4 )	{
		errors++;
		msg += "\n\tpassword\t\t- la password deve essere di almeno 4 caratteri";
	}
	// nome
	if ( form.txt_insert_nome.value.length < 4 )	{
		errors++;
		msg += "\n\tnome\t\t- il nome deve essere di almeno 4 caratteri";
	}
	// mail
	if ( !check_mail(form.txt_insert_mail.value) )	{
		errors++;
		msg += "\n\te-mail\t\t- l'indirizzo e-mail non è valido";
	}
	// mostra messaggio
	if ( errors)	{
		msg_header = "_________________________________________________________________\n\n";
		msg_header += "I dati immessi non sono stati inviati poiché si sono verificati i seguenti problemi:\n";
		msg_header += "_________________________________________________________________\n";
		msg_footer = "\n_________________________________________________________________\n";
		msg_footer += "\nper poter continuare è necessario correggere i problemi e reinviare il form...\n";
		msg = msg_header + msg + msg_footer;
		alert ( msg );
		return false;
	}
	else
		return true;
}


//-------------------------------------------------------------------------------------------
// controlla se spuntato
//-------------------------------------------------------------------------------------------

function cbk_check( cbk, tf, msg )	{
	if ( cbk.checked == tf )
		alert (msg);
}


//-------------------------------------------------------------------------------------------
// controlla la validità di un indirizzo e-mail
//-------------------------------------------------------------------------------------------

function check_mail(mail)   {
	Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if (Filtro.test(mail))
		return true;
	else
		return false;
}

//-------------------------------------------------------------------------------------------
// controlla il form per la registrazione
//-------------------------------------------------------------------------------------------

function check_forum_registrazione(form)	{
	errors = 0;
	msg = "";

	// nome utente
	if ( form.txt_user.value.length < 4 )	{
		errors++;
		msg += "\n\tnome utente\t- il nome utente deve essere di almeno 4 caratteri";
	}
	// password
	if ( form.txt_password.value.length < 4 )	{
		errors++;
		msg += "\n\tpassword\t\t- la password deve essere di almeno 4 caratteri";
	}
	// nome
	if ( form.txt_nome.value.length < 4 )	{
		errors++;
		msg += "\n\tnome\t\t- il nome deve essere di almeno 4 caratteri";
	}
	// mail
	if ( !check_mail(form.txt_mail.value) )	{
		errors++;
		msg += "\n\te-mail\t\t- l'indirizzo e-mail non è valido";
	}
	// privacy
	if ( form.cbk_privacy.checked == false)	{
		errors++;
		msg += "\n\tprivacy\t\t- dare il consenso per il trattamento dei propri dati";
	}
	// mostra messaggio
	if ( errors)	{
		msg_header = "_________________________________________________________________\n\n";
		msg_header += "I dati immessi non sono stati inviati poiché si sono verificati i seguenti problemi:\n";
		msg_header += "_________________________________________________________________\n";
		msg_footer = "\n_________________________________________________________________\n";
		msg_footer += "\nper poter continuare è necessario correggere i problemi e reinviare il form...\n";
		msg = msg_header + msg + msg_footer;
		alert ( msg );
		return false;
	}
	else
		return true;
}

//-------------------------------------------------------------------------------------------
// compila campi testo,select,testo area admin ( categoria [ area | sql ] )
//-------------------------------------------------------------------------------------------

function fill_txt_and_select_and_txt( txt1, select, txt2, string )
{
	txt1.value = string.substring(0,string.lastIndexOf(" ["));
	match = string.substring(string.lastIndexOf("[ ")+2,string.lastIndexOf(" | "));
	for ( i=0; i<select.length; i++ )
	{
		if ( select.options[i].value == match )
		{
			select.options[i].selected = true;
		}
	}
	txt2.value = string.substring(string.lastIndexOf(" | ")+3,string.lastIndexOf(" ]"));
}


//-------------------------------------------------------------------------------------------
// compila campi testo,testo,testo area admin ( image [ link | pos ] )
//-------------------------------------------------------------------------------------------

function fill_txt_and_txt_and_txt( txt1, txt2, txt3, string )
{
	txt1.value = string.substring(0,string.lastIndexOf(" ["));
	txt2.value = string.substring(string.lastIndexOf("[ ")+2,string.lastIndexOf(" | "));
	txt3.value = string.substring(string.lastIndexOf(" | ")+3,string.lastIndexOf(" ]"));
}


//-------------------------------------------------------------------------------------------
// compila campi testo e select area admin
//-------------------------------------------------------------------------------------------

function fill_txt_and_select( txt, select, string )
{
	txt.value = string.substring(0,string.lastIndexOf(" ["));
	match = string.substring(string.lastIndexOf("[ ")+2,string.lastIndexOf(" ]"));
	
	for ( i=0; i<select.length; i++ )
	{
		if ( select.options[i].value == match )
		{
			select.options[i].selected = true;
		}
	}
}


//-------------------------------------------------------------------------------------------
// compila 2 campi testo area admin
//-------------------------------------------------------------------------------------------

function fill_txt_and_txt( txt1,txt2, string )
{
	txt1.value = string.substring(0,string.lastIndexOf(" ["));
	txt2.value = string.substring(string.lastIndexOf("[ ")+2,string.lastIndexOf(" ]"));
}


//-------------------------------------------------------------------------------------------
// popup file function
//-------------------------------------------------------------------------------------------

function popup(file)
{
  window.open(file,"","height=300,width=600,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes");
}


//-------------------------------------------------------------------------------------------
// Function to open pop up window
//-------------------------------------------------------------------------------------------

function openWin(theURL,winName,features) {
  	window.open(theURL,winName,features);
}


//-------------------------------------------------------------------------------------------
// Function to open pop up image album
//-------------------------------------------------------------------------------------------

function popup_image(file)
{
	window.open(file,"","height=600,width=600,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes");
}


//-------------------------------------------------------------------------------------------
// Function to open preview post window
//-------------------------------------------------------------------------------------------

function OpenPreviewWindow(targetPage,formName){
	
	//Open the window first 	
   	openWin('','preview_window','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width=680,height=400');
   		
   	//save form settings
   	action = formName.action;
	target = formName.target;
   	
   	//Now submit form to the new window
   	formName.action = targetPage;
	formName.target = "preview_window";
	formName.submit();
	
	//restore form settings
   	formName.action = action;
	formName.target = target;
}


//-------------------------------------------------------------------------------------------
// startup function
//-------------------------------------------------------------------------------------------

function init(){
    // hide login menu
    //menu_login.style.display = "none";
}


//-------------------------------------------------------------------------------------------
// invert menu
//-------------------------------------------------------------------------------------------

function invert_menu(objRef){
    objRef.style.display = (objRef.style.display=="none") ? "" : "none";
}


//-------------------------------------------------------------------------------------------
// funzione che inserisce una stringa all'interno di un input se questo è vuoto o se overwrite è true
//-------------------------------------------------------------------------------------------

function fill_input (obj,str,overwrite){
if(obj.value=="")
obj.value = str;
if(overwrite)
obj.value = str;
}


//-------------------------------------------------------------------------------------------
// funzione che cancella il valore di un input. se overwrite è true cancella anche se diverso dal valore predefinito
//-------------------------------------------------------------------------------------------

function clear_input (obj,str,overwrite){
if(obj.value==str)
obj.value = "";
if(overwrite)
obj.value = "";
}


//-------------------------------------------------------------------------------------------
// Function to correct for 2.x Mac date bug. Call this function to fix a date object prior to passing it to SetCookie.
// IMPORTANT: This function should only be called *once* for any given date object! See example at the end of this document.
//-------------------------------------------------------------------------------------------

function FixCookieDate (date) {
var base = new Date(0);
var skew = base.getTime(); // dawn of (Unix) time - should be 0
if (skew > 0) // Except on the Mac - ahead of its time
date.setTime (date.getTime() - skew);
}


//-------------------------------------------------------------------------------------------
// "Internal" function to return the decoded value of a cookie
//-------------------------------------------------------------------------------------------

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}


//-------------------------------------------------------------------------------------------
// Function to return the value of the cookie specified by "name". return null if the cookie does not exist or is empty.
//-------------------------------------------------------------------------------------------

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
{
str = getCookieVal (j);
if(str.length>0)
return str;
else
return null;
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}


//-------------------------------------------------------------------------------------------
// SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//-------------------------------------------------------------------------------------------

function SetCookie (name,value,expires,path,domain,secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}


//-------------------------------------------------------------------------------------------
// Function to delete a cookie. (Sets expiration date to start of epoch)
// name - String object containing the cookie name
// path - String object containing the path of the cookie to delete (can be null/omitted).
// domain - String object containing the domain of the cookie to delete (can be null/omitted)
//-------------------------------------------------------------------------------------------

function DeleteCookie (name,path,domain){
if (GetCookie(name))
	{
	document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}


//-------------------------------------------------------------------------------------------
//	general message box
//-------------------------------------------------------------------------------------------

function msg(msg)
{
	alert(msg);
}



function ControllaMail(mail)
   {
   Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
   if (Filtro.test(mail))
	   return true;
   else
      {
	         alert("Controlla l'indirizzo e-mail inserito");
			 //document.form.mail.focus();
			 return false;
      }
}


//-------------------------------------------------------------------------------------------
//	controlla form forum modifica messaggio
//-------------------------------------------------------------------------------------------

function check_forum_form_edit_message(form) {

	updateRTE();
	formErrors = false;
	if (form.message.value.length < 1 || form.message.value == "<P>&nbsp;</P>") {
		formErrors = "Non hai scritto niente nel messaggio!\n";
	}

	if (form.title.value.length < 1) {
		formErrors = "Il messaggio deve avere un titolo!\n";
	}

	if (formErrors) {
		alert(formErrors);
		return false;
	} else {
		ystyle(-1);
		return true;
	}
}


//-------------------------------------------------------------------------------------------
//	controlla form forum inserimento messaggio
//-------------------------------------------------------------------------------------------

function check_forum_form_add_message(form) {

	updateRTE();
	formErrors = false;

	if (form.message.value.length < 1 || form.message.value == "<P>&nbsp;</P>") {
		formErrors = "Non hai scritto niente!";
	}

	if (formErrors) {
		alert(formErrors);
		return false;
	} else {
		ystyle(-1);
		return true;
	}
}


//-------------------------------------------------------------------------------------------
//	controlla form forum inserimento argomento
//-------------------------------------------------------------------------------------------

function check_forum_form_add_topic(form) {

	updateRTE();
	formErrors = false;

	if (form.message.value.length < 1 || form.message.value == "<P>&nbsp;</P>") {
		formErrors = "Non hai scritto niente nel messaggio!\n";
	}

	if (form.title.value.length < 1) {
		formErrors = "Il messaggio deve avere un titolo!\n";
	}

	if (formErrors) {
		alert(formErrors);
		return false;
	} else {
		ystyle(-1);
		return true;
	}
}

// aggiunge un immagine
function add_image(src) {
	str = document.post.message.value + "[img]"+src+"[/img]";
	document.post.message.value = str;
	if (document.post.message.type != "hidden") document.post.message.focus();
}



// Startup variables
var imageTag = false;
var theSelection = false;

// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav  = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));

var is_win   = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac    = (clientPC.indexOf("mac")!=-1);


// Helpline messages
g_help = "Grassetto: [g]testo[/g]  (alt+g)";
c_help = "Corsivo: [c]testo[/c]  (alt+c)";
i_help = "Sottolineato: [s]testo[/s]  (alt+i)";
l_help = "Linea: [line] (alt+l)";
t_help = "Lista: [list]testo[/list] (alt+t)";
p_help = "Immagine: [img]http://immagine_url[/img]  (alt+p)";
w_help = "Link: [url=http://url]text[/url] (alt+w)";
a_help = "Chiude tutti i tag aperti (alt+a)";
r_help = "Colore: [color=red]text[/color] anche color=#FF0000 (alt+r)";
d_help = "Dimensione: [dim=n]testo piccolo[/dim] (alt+d)";
o_help = "Elemento Lista: [o]testo elemento[/o] (alt+o)";
s_help = "Un click per vedere l'anteprima e doppio click per inserire l'immagine";
k_help = "Conferma le modifiche";
j_help = "Ripristina";
b_help = "Visualizza un'anteprima del messaggio";

// Define the ycode tags
ycode = new Array();
bbtags = new Array('[g]','[/g]','[c]','[/c]','[s]','[/s]','[line]','','[list]','[/list]','[img]','[/img]','[url=http://]','[/url]','[o]','[/o]');
imageTag = false;

// Shows the help messages in the helpline window
function helpline(help) {
	//document.post.helpbox.value = eval(help + "_help");
	help_field.innerHTML = eval(help + "_help");
}


// Replacement for arrayname.length property
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

// Replacement for arrayname.push(value) not implemented in IE until version 5.5
// Appends element to the array
function arraypush(thearray,value) {
	thearray[ getarraysize(thearray) ] = value;
}

// Replacement for arrayname.pop() not implemented in IE until version 5.5
// Removes and returns the last element of an array
function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}


function emoticon(text) {
	text = ' ' + text + ' ';
	if (document.post.message.createTextRange && document.post.message.caretPos) {
		var caretPos = document.post.message.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		if (document.post.message.type != "hidden") document.post.message.focus();
	} else {
	document.post.message.value  += text;
	if (document.post.message.type != "hidden") document.post.message.focus();
	}
}

function yfontstyle(yopen, yclose) {
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			document.post.message.value += yopen + yclose;
			if (document.post.message.type != "hidden") document.post.message.focus();
			return;
		}
		document.selection.createRange().text = yopen + theSelection + yclose;
		if (document.post.message.type != "hidden") document.post.message.focus();
		return;
	} else {
		document.post.message.value += yopen + yclose;
		if (document.post.message.type != "hidden") document.post.message.focus();
		return;
	}
	storeCaret(document.post.message);
}


function ystyle(bbnumber) {

	donotinsert = false;
	theSelection = false;
	bblast = 0;

	if (bbnumber == -1 ) { // Close all open tags & default button names
		while (ycode[0]) {
			butnumber = arraypop(ycode) - 1;
			document.post.message.value += bbtags[butnumber + 1];
			buttext = eval('document.post.addycode' + butnumber + '.value');
			eval('document.post.addycode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		imageTag = false; // All tags are closed including image tags :D
		if (document.post.message.type != "hidden") document.post.message.focus();
		return;
	}

	if ((clientVer >= 4) && is_ie && is_win)
		theSelection = document.selection.createRange().text; // Get text selection

	if (theSelection) {
		// Add tags around selection
		document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
		if (document.post.message.type != "hidden") document.post.message.focus();
		theSelection = '';
		return;
	}

	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < ycode.length; i++) {
		if (ycode[i] == bbnumber+1) {
			bblast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (ycode[bblast]) {
				butnumber = arraypop(ycode) - 1;
				document.post.message.value += bbtags[butnumber + 1];
				buttext = eval('document.post.addycode' + butnumber + '.value');
				eval('document.post.addycode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			}
			if (document.post.message.type != "hidden") document.post.message.focus();
			return;
	} else { // Open tags

		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another
			document.post.message.value += bbtags[15];
			lastValue = arraypop(ycode) - 1;	// Remove the close image tag from the list
			document.post.addycode14.value = "Img";	// Return button back to normal state
			imageTag = false;
		}

		// Open tag
		document.post.message.value += bbtags[bbnumber];
		if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(ycode,bbnumber+1);
		eval('document.post.addycode'+bbnumber+'.value += "*"');
		if (document.post.message.type != "hidden") document.post.message.focus();
		return;
	}
	storeCaret(document.post.message);
}

// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}
