var currentEditorMode = "TESTO";

/*-------------------------------
	PAINT FROM COLOR SELECTION
-------------------------------*/
function PaintFromColorSelection(textbox) 
{
	var BrClr;
	BrClr=GetColorSelection();
	if (BrClr != null) 
	{ 	
   		textbox.value=BrClr; 			
   		if (textbox.style!=null) textbox.style.backgroundColor=""+BrClr; 
	}   	 
	return;
}//PaintFromColorSelection


/*-------------------------------
	GET COLOR SELECTION
-------------------------------*/
function GetColorSelection() 
{
	//return window.showModalDialog("../ecm5/colori.htm", "", "dialogWidth:13em; dialogHeight:18em; status:NO; scroll=NO; resizable=NO; help=NO;");
	return window.showModalDialog("/central/servizi/ecm5/colori.htm", 
		"", 
		"dialogWidth:13em; dialogHeight:18em; status:NO; scroll=NO; resizable=NO; help=NO;");
}//GetColorSelection


/*-------------------------------
	CHECK INT VALUE
-------------------------------*/
function CheckIntValue(textbox, alternativeValue) 
{
	if((textbox.value=="")||(isNaN(textbox.value))) 
	{
		textbox.value=alternativeValue;
		textbox.focus();
		return;
	}
}//CheckIntValue

/*-------------------------------
	IS INT
-------------------------------*/
function isInt() 
{
    return ((event.keyCode >= 48) && (event.keyCode <= 57))
}



/*-------------------------------
	IS INT BETWEEN
-------------------------------*/
function isIntBetween(textbox, int1, int2) 
{
	if (textbox == null) return;
	
	if(Math.abs(textbox.value) < int1 || Math.abs(textbox.value) > int2) 
	{
		textbox.focus();
		return false;				
	}
	
	return true;				
}//isIntBetween

/*-----------------------------------------------------------
-------------------------------------------------------------
-------------------------------------------------------------
		FUNZIONI PER L'EDITOR
-------------------------------------------------------------
-------------------------------------------------------------
-----------------------------------------------------------*/

/*-------------------------------
	EDITOR OPERATIVO
-------------------------------*/
function EditorOperativo(editor) 
{
	if (currentEditorMode == "HTML") return false;
	if (editor == null) return false;
	if (editor.document == null) return false;

	return true;
}//EditorOperativo

/*-------------------------------
	BASE COMMAND
-------------------------------*/
function BaseCommand(editor, cmd, startag, endtag) 
{
	if (!EditorOperativo(editor)) return;
	
	var tr = editor.document.selection.createRange();
	if (!EditorSelezionato(editor.document.selection)) return;
	tr.execCommand(cmd, startag, endtag);
	tr.select();
	editor.focus();
}


/*-------------------------------
	TAG FOR OBJECT
-------------------------------*/
function TagForObject(editor, tagName, cmd) 
{
	if (!EditorOperativo(editor)) return;
	
	var tr = editor.document.selection.createRange();
	if (EditorSelezionato(editor.document.selection, true))
	{
		tr.execCommand(cmd);
		tr.select();
	}
	else if (editor.document.selection.type == 'Control') 
	{
//		if (tr.item(0).tagName.toLowerCase() == 'label')
//		{
			var myTag = getParentTag(editor, tagName.toLowerCase());
			if (myTag == null)
				tr.item(0).outerHTML = "<"+tagName+">"+tr.item(0).outerHTML+"</"+tagName+">"
			else 
				myTag.removeNode(false);
				
	//		tr.item(0).select();
//		}
//		else return;	
	}
	else return;	
	
	editor.focus();

}//TagForObject


/*-------------------------------
	COMMAND FOR OBJECT
-------------------------------*/
function CommandForObject(editor, cmd) 
{
	if (!EditorOperativo(editor)) return;
	if (!EditorSelezionato(editor.document.selection)) return;
	
	var tr = editor.document.selection.createRange();
	tr.execCommand(cmd);
//	if (editor.document.selection.type == 'Text') tr.select();
//	else return;	
	
//	editor.focus();

}//CommandForObject


/*-------------------------------
	ADD TAGS HEAD
-------------------------------*/
function addTagsHead(editor, startag, endtag) 
{
	if (!EditorOperativo(editor)) return;
	if (!EditorSelezionato(editor.document.selection)) return;
	if (editor.document.selection.type != "Text") return;

	BaseCommand(editor, "formatBlock",false,startag);
} 


/*-------------------------------
	ADD TAGS
-------------------------------*/
function addTags(editor, startag, endtag) 
{
	if (!EditorOperativo(editor)) return;
	if (!EditorSelezionato(editor.document.selection)) return;

	if (editor.document.selection.type != "Text" && editor.document.selection.type != "None") return;
	
	var tr = editor.document.selection.createRange();
	
	tr.pasteHTML(startag + tr.text + endtag+'\n');
	tr.select();
	editor.focus();
} 


/*-------------------------------
	SELEZIONA TESTO
-------------------------------*/
/*function SelezionaTesto(editor) 
{
	if (!EditorSelezionato(editor.document.selection)) return;

	var r = editor.document.body.createTextRange();
	var ricerca = window.showModalDialog("../ecm5/find.htm", "","dialogWidth:350px; dialogHeight:150px;help:0;status:no;");
//	alert("ricerca['testo']="+ricerca["testo"]+" ricerca['direzione']="+ricerca["direzione"]);
	if (ricerca == null) return;
	
	if (Math.abs(ricerca["direzione"]) < 0) r.moveStart("character", -1);
	
	alert(r.findText(ricerca["testo"], Math.abs(ricerca["direzione"]), ricerca["flags"]));
	r.scrollIntoView();
	r.select();
	
	
}//SelezionaTesto

*/
function SelezionaTesto(editor, stringa, direzione, falgs)
{
	if (!EditorOperativo(editor)) return;
	if (!EditorSelezionato(editor.document.selection)) return;
	
	var ricerca = new Array();
	if (stringa == null || direzione == null)
	{
		alert("stringa="+stringa+" direzione="+direzione+" falgs="+falgs);
		ricerca = window.showModalDialog("../ecm5/find.htm", "","dialogWidth:350px; dialogHeight:150px;help:0;status:no;");
		if (ricerca == null) return;
	}
	else
	{
		ricerca["testo"] = stringa;
		ricerca["direzione"] = direzione;
		ricerca["falgs"] = falgs;
	}

	var searchval = ricerca["testo"];
	var rng = editor.document.body.createTextRange();
	rng.collapse(false);
	if (rng.findText(searchval, Math.abs(ricerca["direzione"]), Math.abs(ricerca["flags"])) ) 
	{
		rng.select();
	} 
	else
	{
		var startfromtop = confirm("Your word was not found.\nWould you like to start again from the top?");
		if (startfromtop) 
		{
			rng.expand("textedit"); // selects everything
			rng.collapse(); // collapse at the beginning
			rng.select(); // create the selection
			SelezionaTesto(editor, ricerca["testo"], ricerca["direzione"], ricerca["falgs"]); // start again 
		}
	}
	
}//SelezionaTesto


/*-------------------------------
	REMOVE LINK
-------------------------------*/
function RemoveLink(editor) 
{
	if (!EditorOperativo(editor)) return;

	var myLink = getParentTag(editor, 'a');
	if (myLink != null) myLink.removeNode(false);
} 


/*-------------------------------
	GET PARENT TAG
(mi ritorna la selezione del tag specificato
se è selezionato un suo elemento figlio)
-------------------------------*/
function getParentTag(editor, tagName) 
{
//	if (editor.document.selection.type == "None") return null;

	if (!EditorOperativo(editor)) return;

	var currentElement;
	var tr = editor.document.selection.createRange();
	if (editor.document.selection.type == "Text" || editor.document.selection.type == "None") 
	{
		currentElement = tr.parentElement();
	} 
	else 
	{
		currentElement = tr.item(0).parentElement;  
	}
	
	while (currentElement != null && currentElement.tagName.toLowerCase() != "body") 
	{
		if (currentElement.tagName.toLowerCase() == tagName) return currentElement;
		currentElement = currentElement.parentElement;
	}  
	return null;
}


/*-------------------------------
	ELENCO PUNTATO
-------------------------------*/
function ElencoPuntato(editor) 
{
 	BaseCommand(editor, "InsertUnorderedList");
}


/*-------------------------------
	ELENCO NUMERATO
-------------------------------*/
function ElencoNumerato(editor) 
{
 	BaseCommand(editor, "InsertOrderedList");
}


/*-------------------------------
	INDENT
-------------------------------*/
function Indent(editor) 
{
 	BaseCommand(editor, "Indent");
}


/*-------------------------------
	BACK COLOR
-------------------------------*/
function BackColor(editor) 
{
	if (!EditorOperativo(editor)) return;
	if (editor.document.selection.type != "Text") return;

	var color = GetColorSelection();
	if (color == null || color == '') 
	{
		var myFont = getParentTag(editor, "font");
		if (myFont != null) myFont.removeAttribute("color");
	}
	else BaseCommand(editor, "BackColor", false, color);
}

/*-------------------------------
	FORE COLOR
-------------------------------*/
function ForeColor(editor) 
{
	if (!EditorOperativo(editor)) return;
	if (editor.document.selection.type != "Text") return;

	var color = GetColorSelection();
	if (color == null || color == '')
	{
		var myFont = getParentTag(editor, "font");
		if (myFont != null) myFont.removeAttribute("color");
	}
	else
//		TagForObject(editor, "FONT", "ForeColor");	//questo rimuoverà tutto il tag, però, invece che solo l'attributo FONT
		BaseCommand(editor, "ForeColor", false, color);
}

/*-------------------------------
	HR
-------------------------------*/
function Hr(editor) 
{
 	addTags(editor, '<hr>','');	
}


/*-------------------------------
	BR
-------------------------------*/
function Br(editor) 
{
 	addTags(editor, '<br>','');	
}

/*-------------------------------
	OUTDENT
-------------------------------*/
function Outdent(editor) 
{
 	BaseCommand(editor, "Outdent");
}


/*-------------------------------
	UNDO
-------------------------------*/
function Undo(editor) 
{
 	BaseCommand(editor, "Undo");
}

/*-------------------------------
	REDO
-------------------------------*/
function Redo(editor) 
{
 	BaseCommand(editor, "Redo");
}


/*-------------------------------
	DELETE
-------------------------------*/
function Delete(editor) 
{
 	CommandForObject(editor, "Delete");
}

/*-------------------------------
	CUT
-------------------------------*/
function Cut(editor) 
{
 	CommandForObject(editor, "Cut");
}

/*-------------------------------
	COPY
-------------------------------*/
function Copy(editor) 
{
 	CommandForObject(editor, "Copy");
}


/*-------------------------------
	PASTE
-------------------------------*/
function Paste(editor) 
{
 	CommandForObject(editor, "Paste");
}


/*-------------------------------
	ALIGN JUSTIFY
-------------------------------*/
function AlignJustify(editor) 
{
 	BaseCommand(editor, "JustifyFull");
}

/*-------------------------------
	ALIGN LEFT
-------------------------------*/
function AlignLeft(editor) 
{
 	BaseCommand(editor, "JustifyLeft");
}

/*-------------------------------
	ALIGN RIGHT
-------------------------------*/
function AlignRight(editor)
{
 	BaseCommand(editor, "JustifyRight");
}

/*-------------------------------
	ALIGN CENTER
-------------------------------*/
function AlignCenter(editor) 
{
 	BaseCommand(editor, "JustifyCenter");
}


/*-------------------------------
	BOLD
-------------------------------*/
function Bold(editor) 
{
	//alert("BOLD!");
	TagForObject(editor, "STRONG", "Bold");
}


/*-------------------------------
	ITALICS
-------------------------------*/
function Italic(editor) 
{
	TagForObject(editor, "EM", "Italic");
}


/*-------------------------------
	UNDERLINE
-------------------------------*/
function Underline(editor) 
{
 	TagForObject(editor, "U", "Underline");
}


/*-------------------------------
	CAMBIA TIPO EDITOR
-------------------------------*/
function CambiaTipoEditor(editor, mode, editorMode)
{
	if (mode=="HTML") {
		editor.document.body.innerText = editor.document.body.innerHTML;
		currentEditorMode = "HTML";
		editorMode.value = "HTML";
	}
	else {
		editor.document.body.innerHTML = editor.document.body.innerText;
		currentEditorMode = "TESTO";
		editorMode.value = "TESTO";
	}
	
	editor.focus();
	
	var tr1 = editor.document.selection.createRange();
	tr1.collapse(false);
	tr1.select();
	
	editor.focus();
}


/*-------------------------------
	LINK
-------------------------------*/
function Link(editor, ancora) 
{
	if (!EditorOperativo(editor)) return;
		
	var attributi = new Array();
	var linkAdImmagine = null;
	var LinkSelezionato = null;
	
	var elementoSelezionato = editor.document.selection.createRange();
	if (!EditorSelezionato(editor.document.selection)) return;

	//se è selezionato un elemento ed è un immagine procedo
	if (editor.document.selection.type == "Control") 
	{
		if (elementoSelezionato.item(0).tagName.toLowerCase() == "img") 
		{
			linkAdImmagine = elementoSelezionato.item(0);
			//se l'immagine selezionata ha già un link lo modificherò, altrimenti lo inserirò

			if (elementoSelezionato.item(0).parentElement.tagName.toLowerCase() == "a") 
			{
				LinkSelezionato = elementoSelezionato.item(0).parentElement;
			}
		} 
		else return;
	} 
	else 
	{ 
		LinkSelezionato = getParentTag(editor, "a");
	}

	
	/*..................................................
		seleziono le sue caratteristiche attuali
	..................................................*/
	if(LinkSelezionato)
	{
		attributi["title"] = LinkSelezionato.getAttribute("title");
		//ANCORA
		if (LinkSelezionato.getAttribute("name").length > 0) 
		{
			attributi["name"] = LinkSelezionato.getAttribute("name");
		}
		//LINK
		else 
		{
			attributi["href"] = LinkSelezionato.getAttribute("href",2);
			attributi["target"] = LinkSelezionato.target;
			attributi["accessKey"] = LinkSelezionato.accessKey;
			for ( var i = 0; i < editor.document.anchors.length; i++) 
			{
				attributi[editor.document.anchors[i].name] = editor.document.anchors[i].name;
			}
		}
	} 
	else 
	{
		//defaults
		if (ancora)
			attributi["name"] = "";
		else 
		{
			attributi["href"] = "";
			for ( var i = 0; i < editor.document.anchors.length; i++) 
			{
				attributi[editor.document.anchors[i].name] = editor.document.anchors[i].name;
			}
		}   
	}
	
	/*..................................................
		ricavo le sue caratteristiche inserite
	..................................................*/
	var attributiInseriti;
	//var finestra = "../ecm5/link.htm";
	// var finestra = "/central/servizi/ecm5/link.htm";
	var finestra = "/central/servizi/pdi/link.htm";
	var altezzaFinestra = 19;
	
	// non dovremmo avere anchor
	/*
	if (LinkSelezionato)
	{
		if (LinkSelezionato.getAttribute("name").length > 0) 
		{
			// finestra = "../ecm5/anchor.htm";
			finestra = "/central/servizi/ecm5/anchor.htm";
			altezzaFinestra = 10;
		}
	}
	else
	{
		if (ancora) 
		{
			// finestra = "../ecm5/anchor.htm";
			finestra = "/central/servizi/ecm5/anchor.htm";
			altezzaFinestra = 10;
		}
	}
	*/	

	//alert("apro " + finestra);
	
	attributiInseriti = window.showModalDialog(finestra, attributi, 
		"dialogWidth:16em; dialogHeight:"+altezzaFinestra+"em; status:NO; scroll=NO; resizable=NO; help=NO;");

	
	if (attributiInseriti != null) 
	{  
		//assign the existing link its new properties
		if (LinkSelezionato) 
		{
			if (attributiInseriti["accessKey"] != null && attributiInseriti["accessKey"] != '') 
				LinkSelezionato.accessKey = attributiInseriti["accessKey"];
			else
				LinkSelezionato.removeAttribute("accessKey");
			LinkSelezionato.title = attributiInseriti["title"];
			if (LinkSelezionato.getAttribute("name").length > 0) 
			{
				LinkSelezionato.name = attributiInseriti["name"];
			}
			else 
			{
				LinkSelezionato.href = attributiInseriti["href"];    
				LinkSelezionato.target = attributiInseriti["target"];
			}     
		} 
		else 
		{
			//CREA UN LINK INTORNO ALL'IMMAGINE
			var accessKey = "";
			if (attributiInseriti["accessKey"] != null && attributiInseriti["accessKey"] != '') 
				accessKey = " accessKey=\"" + attributiInseriti["accessKey"] + "\"";
			if (linkAdImmagine) 
			{
				if (ancora)
					linkAdImmagine.outerHTML = "<a " + accessKey + " name=\"" + attributiInseriti["name"] + "\">" + linkAdImmagine.outerHTML + "</a>";
				else
					linkAdImmagine.outerHTML = "<a " + accessKey + " href=\"" + attributiInseriti["href"] + "\" target=\"" + attributiInseriti["target"] + "\">" + linkAdImmagine.outerHTML + "</a>";
				} 
			//ALTRIMENTI CREA UN LINK E BASTA
			else 
			{
				if (ancora)
					elementoSelezionato.pasteHTML("<a " + accessKey + " name=\"" + attributiInseriti["name"] + "\">" + elementoSelezionato.htmlText + "</a>");
				else
					elementoSelezionato.pasteHTML("<a " + accessKey + " href=\"" + attributiInseriti["href"] + "\" target=\"" + attributiInseriti["target"] + "\" title=\"" + attributiInseriti["title"] + "\">" + elementoSelezionato.htmlText + "</a>"); 
			}
		}
	}
	
	editor.document.focus();  
}//Link

/*--------------------------------------
	EDITOR SELEZIONATO
--------------------------------------*/
function EditorSelezionato(sel) 
{
	if (sel.type == 'Text' || sel.type == 'None') 
	{
		var selezione = sel.createRange();
		return selezione.parentElement().isContentEditable;
	}
	else if (sel.type == 'Control') 
	{
		return sel.createRange().item(0).isContentEditable;
	}
	
	return false;
}//EditorSelezionato

/*--------------------------------------
	GESTIONE TABELLA
--------------------------------------*/
function GestioneTabella(editor) 
{
	if (!EditorOperativo(editor)) return;

	editor.document.focus();
	if (!EditorSelezionato(editor.document.selection)) return;
	
	var modifica = false;
	if (editor.document.selection.type == 'Control') modifica = true;

	var attributiTabella = null;
	if (modifica)
	{
		attributiTabella = new Array();
		var oggettoSelezionato = editor.document.selection.createRange().item(0);
		if (oggettoSelezionato.tagName.toLowerCase() != 'table') return;
		
		var idTabella = oggettoSelezionato.getAttribute("id");
		if (idTabella != "" && idTabella != null) return;
		
		attributiTabella["modifica"] = true;
		attributiTabella["width"] = oggettoSelezionato.getAttribute("width");
//		attributiTabella["align"] = oggettoSelezionato.getAttribute("align");
		attributiTabella["border"] = oggettoSelezionato.getAttribute("border");
		attributiTabella["cellspacing"] = oggettoSelezionato.getAttribute("cellspacing");
		attributiTabella["cellpadding"] = oggettoSelezionato.getAttribute("cellpadding");
		attributiTabella["bordercolor"] = oggettoSelezionato.getAttribute("bordercolor");
		attributiTabella["bgcolor"] = oggettoSelezionato.getAttribute("bgcolor");
	}
		
	var selezione = editor.document.selection.createRange();
	
	//var attributiInseriti = window.showModalDialog("../ecm5/tabella_gestione.aspx", attributiTabella,"dialogWidth:400px; dialogHeight:250px;help:0;status:no;");
	//var attributiInseriti = window.showModalDialog("/central/servizi/ecm5/tabella_gestione.aspx", attributiTabella, "dialogWidth:400px; dialogHeight:250px;help:0;status:no;");

	var attributiInseriti = window.showModalDialog("/central/servizi/pdi/tabella_gestione.aspx", 
		attributiTabella, "dialogWidth:400px; dialogHeight:250px;help:0;status:no;");

	if (attributiInseriti != null) 
	{  
		//INSERISCO LA TABELLA NUOVA
		if (!modifica)
		{
			//se sto inserendo una tabella in uan cella vuota gli devo togliere il '&nbsp;' che contiene
/*			var parentTag = getParentTag(editor, 'td');
			if (parentTag != null)
			{
				alert(parentTag.innerText);
				if ( parentTag.innerText == '')  parentTag.innerText = null;
			}
*/			
			//INSERISCO LA TABELLA
			var tag = "<table ";
			if (attributiInseriti["width"] != null) tag = tag + "width=\""+attributiInseriti["width"]+"%\" ";
//			if (attributiInseriti["align"] != null) tag = tag + "align=\""+attributiInseriti["align"]+"\" ";
			if (attributiInseriti["border"] != null) tag = tag + "border=\""+attributiInseriti["border"]+"\" ";
			if (attributiInseriti["cellspacing"] != null) tag = tag + "cellspacing=\""+attributiInseriti["cellspacing"]+"\" ";
			if (attributiInseriti["cellpadding"] != null) tag = tag + "cellpadding=\""+attributiInseriti["cellpadding"]+"\" ";
			if (attributiInseriti["bordercolor"] != null) tag = tag + "bordercolor=\""+attributiInseriti["bordercolor"]+"\" ";
			if (attributiInseriti["bgcolor"] != null) tag = tag + "bgcolor=\""+attributiInseriti["bgcolor"]+"\" ";
			tag = tag + ">";
			
			//ROWS AND COLS
			var bgColRighePari = "";
			if (attributiInseriti["coloreRighePari"] != null) bgColRighePari = " bgcolor=\""+attributiInseriti["coloreRighePari"]+"\"";
			var bgColRigheDispari = "";
			if (attributiInseriti["coloreRigheDispari"] != null) bgColRigheDispari = " bgcolor=\""+attributiInseriti["coloreRigheDispari"]+"\"";
			for (var r=0; r < attributiInseriti["rows"]; r=r+1)
			{
				var bgColor = bgColRigheDispari;
				if ((r+1)%2 == 0) bgColor = bgColRighePari;

				tag = tag + "<tr>";
				for (var c=0; c < attributiInseriti["cols"]; c=c+1)
				{
					if (r==0) tag = tag + "<th"+bgColor+" align=\"left\"></th>";
					else tag = tag + "<td"+bgColor+" align=\"left\"></td>";					
				}
				tag = tag + "</tr>";
			}
			
			tag = tag + "</table>";
	
			selezione.pasteHTML(tag);
		}
		//MODIFICO LA TABELLA ESISTENTE
		else
		{
			oggettoSelezionato.width = attributiInseriti["width"]+'%';
//			oggettoSelezionato.align = attributiInseriti["align"];
			oggettoSelezionato.border = attributiInseriti["border"];
			oggettoSelezionato.cellSpacing = attributiInseriti["cellspacing"];
			oggettoSelezionato.cellPadding = attributiInseriti["cellpadding"];
			
			if (attributiInseriti["bordercolor"] == null || attributiInseriti["bordercolor"] == '') oggettoSelezionato.borderColor = attributiInseriti["bordercolor"];
				else oggettoSelezionato.borderColor = attributiInseriti["bordercolor"];
			
			if (attributiInseriti["bgcolor"] == null || attributiInseriti["bgcolor"] == '') oggettoSelezionato.bgColor = attributiInseriti["bgcolor"];
				else oggettoSelezionato.bgColor = attributiInseriti["bgcolor"];

			var bgColRighePari = "";
			if (attributiInseriti["coloreRighePari"] != null) bgColRighePari = attributiInseriti["coloreRighePari"];
			var bgColRigheDispari = "";
			if (attributiInseriti["coloreRigheDispari"] != null) bgColRigheDispari = attributiInseriti["coloreRigheDispari"];

			for (var i=0; i < oggettoSelezionato.rows.length; i=i+1)
			{
				var bgColor = attributiInseriti["coloreRigheDispari"];
				if ((i+1)%2 == 0) bgColor = attributiInseriti["coloreRighePari"];
				
				for (var c=0; c < oggettoSelezionato.rows(i).cells.length; c=c+1)
				{
					if (bgColor != "" && bgColor != null)
						oggettoSelezionato.rows(i).cells(c).bgColor = bgColor;

					var allineamento = oggettoSelezionato.rows(i).cells(c).align;
					if (allineamento == '' || allineamento == null)
						oggettoSelezionato.rows(i).cells(c).align = "left";
				}
			}
		}
	}
	
	selezione.select();
	editor.document.focus();  
}//GestioneTabella

/*--------------------------------------
	INSERISCI RIGA
--------------------------------------*/
function InserisciRiga(editor)
{
	if (!EditorOperativo(editor)) return;

	editor.document.focus();
	if (!EditorSelezionato(editor.document.selection)) return;
	
//	var indiceInserimento = -1;
	var indiceInserimento = 0;
	var tabella;
	if (editor.document.selection.type == 'Text' || editor.document.selection.type == 'None')
	{
		var tr = getParentTag(editor, 'tr');
		if (tr == null) return;
//		indiceInserimento = tr.rowIndex;
		indiceInserimento = tr.rowIndex+1;
		tabella = getParentTag(editor, 'table');
	}
	else if (editor.document.selection.type == 'Control')
	{
		tabella = editor.document.selection.createRange().item(0);
		if (tabella.tagName.toLowerCase() != 'table') return;
	}
	else return;
	
	var idProcedura = getParentTag(editor, 'table').getAttribute("id");
	if (idProcedura != null && idProcedura != "") return;
	
	var TR = tabella.insertRow(indiceInserimento);
	
	var indiceRigaCopiata = indiceInserimento-1;
	var indiceCellaInserita = 0;
	for (var i=0; i < tabella.rows(indiceRigaCopiata).cells.length; i=i+1) 
	{
		TR.insertCell();
		TR.cells(indiceCellaInserita).bgColor = tabella.rows(indiceRigaCopiata).cells(indiceCellaInserita).bgColor;
		TR.cells(indiceCellaInserita).borderColor = tabella.rows(indiceRigaCopiata).cells(indiceCellaInserita).borderColor;

		indiceCellaInserita=indiceCellaInserita+1;
	}

}//InserisciRiga


/*--------------------------------------
	ELIMINA RIGA
--------------------------------------*/
function EliminaRiga(editor)
{
	if (!EditorOperativo(editor)) return;

	editor.document.focus();
	if (!EditorSelezionato(editor.document.selection)) return;
	
	var idProcedura = getParentTag(editor, 'table').getAttribute("id");
	if (idProcedura != null && idProcedura != "") return;
	
	var indice = -1
	var tabella;
	if (editor.document.selection.type == 'Text' || editor.document.selection.type == 'None')
	{
		var tr = getParentTag(editor, 'tr');
		if (tr == null) return;
		indice = tr.rowIndex;
		tabella = getParentTag(editor, 'table');
	}
	else return;

	var cancella = true;
	if (tabella.rows.length == 1)
		cancella = confirm("ATTENZIONE: Eliminando questa riga eliminerai l'intera tabella.\nConfermi l'operazione?");
	if (!cancella) return;


	var TR = tabella.deleteRow(indice);
		
}//EliminaRiga


/*--------------------------------------
	ELIMINA COLONNA
--------------------------------------*/
function EliminaColonna(editor)
{
	if (!EditorOperativo(editor)) return;

	editor.document.focus();
	if (!EditorSelezionato(editor.document.selection)) return;

	var indice = -1
	var tabella;
	if (editor.document.selection.type == 'Text' || editor.document.selection.type == 'None')
	{
		var td = getParentTag(editor, 'th');
		if (td == null) td = getParentTag(editor, 'td');
		if (td == null) return;
		indice = td.cellIndex;
		tabella = getParentTag(editor, 'table');
	}
	else if (editor.document.selection.type == 'Control')
	{
		tabella = editor.document.selection.createRange().item(0);
		if (tabella.tagName.toLowerCase() != 'table') return;
	}
	else return;

	var idProcedura = getParentTag(editor, 'table').getAttribute("id");
	if (idProcedura != null && idProcedura != "") return;

	var cancella = true;
	//TROVO IL NUMERO MASSIMO DI CELLE PRESENTI IN UNA RIGA IN QUESTA TABELLA
	//(se c'è una colonna sola avviso che si cancellerà tutta la tabelle)
	var maxCelle = 0;
	for (var i=0; i < tabella.rows.length; i=i+1)
	{
		if (maxCelle < tabella.rows(i).cells.length) maxCelle = tabella.rows(i).cells.length;
	}
	if (maxCelle == 1)
	{
			cancella = confirm("ATTENZIONE: Eliminando questa colonna eliminerai l'intera tabella.\nConfermi l'operazione?");
	}

	if (!cancella) return;

	for (var i=0; i < tabella.rows.length; i=i+1)
	{
		tabella.rows(i).cells(indice).innerHTML = '';
		tabella.rows(i).cells(indice).removeNode();
	}
	
}//EliminaColonna

/*--------------------------------------
	INSERISCI COLONNA
--------------------------------------*/
function InserisciColonna(editor)
{
	if (!EditorOperativo(editor)) return;

	editor.document.focus();
	if (!EditorSelezionato(editor.document.selection)) return;
	
	var idProcedura = getParentTag(editor, 'table').getAttribute("id");
	if (idProcedura != null && idProcedura != "") return;
	
	var indice = -1
	var tabella;
	if (editor.document.selection.type == 'Text' || editor.document.selection.type == 'None')
	{
		var myTd = getParentTag(editor, 'th');
		if (myTd == null) myTd = getParentTag(editor, 'td');
		if (myTd == null) return;
		indice = myTd.cellIndex;
		tabella = getParentTag(editor, 'table');
	}
	else if (editor.document.selection.type == 'Control')
	{
		tabella = editor.document.selection.createRange().item(0);
		if (tabella.tagName.toLowerCase() != 'table') return;
	}
	else return;

	if ((indice > 0 && tabella.rows(0).cells(indice-1).tagName.toLowerCase() == 'td')
		|| tabella.rows(0).cells(indice).tagName.toLowerCase() == 'td')
	{
		alert("Impossibile inserire una nuova colonna in questo punto. E' necessario salvare prima il documento.");
		return;
	}

	var indiceColonnaCopiata = indice+1;

	/********************************************
	deve essere inserito l'header come prima cella
	ma da javascript non è possibile, quindi deve
	essere fatto da programmazione lato server
	********************************************/
	for (var i=0; i < tabella.rows.length; i=i+1)
	{
		var td;

		if (indice >= 0) td = tabella.rows(i).insertCell(indice);
			else td = tabella.rows(i).insertCell();
		
		
		td.bgColor = tabella.rows(i).cells(indiceColonnaCopiata).bgColor;
		td.borderColor = tabella.rows(i).cells(indiceColonnaCopiata).borderColor;
	}
		
}//InserisciColonna


/*--------------------------------------
	PROPRIETA CELLA
--------------------------------------*/
function ProprietaCella(editor) 
{
	if (!EditorOperativo(editor)) return;

	editor.document.focus();
	if (!EditorSelezionato(editor.document.selection)) return;
	
	if (editor.document.selection.type != 'Text' && editor.document.selection.type != 'None') return;
	
	var attributiTabella = new Array();	
	var myTd = getParentTag(editor, 'th');
	if (myTd == null) myTd = getParentTag(editor, 'td');
	if (myTd == null) return;
	
	idProcedura = getParentTag(editor, 'table').getAttribute("id");
	if (idProcedura != null && idProcedura != "") return;
	
	attributiTabella["width"] = myTd.getAttribute("width");
	attributiTabella["height"] = myTd.getAttribute("height");
	attributiTabella["align"] = myTd.getAttribute("align");
	attributiTabella["valign"] = myTd.getAttribute("valign");
	attributiTabella["bordercolor"] = myTd.getAttribute("bordercolor");
	attributiTabella["bgcolor"] = myTd.getAttribute("bgcolor");
		
	var selezione = editor.document.selection.createRange();
	
	//var attributiInseriti = window.showModalDialog("../ecm5/tabella_cella.htm", attributiTabella,"dialogWidth:420px; dialogHeight:200px;help:0;status:no;");
	var attributiInseriti = window.showModalDialog("/central/servizi/ecm5/tabella_cella.htm", 
		attributiTabella, "dialogWidth:420px; dialogHeight:200px;help:0;status:no;");

	if (attributiInseriti != null) 
	{  
		if (attributiInseriti["width"] == null || attributiInseriti["width"] == '') myTd.removeAttribute("width");
			else myTd.width = attributiInseriti["width"]+'%';
			
		if (attributiInseriti["height"] == null || attributiInseriti["height"] == '') myTd.height = attributiInseriti["height"];
			else myTd.height = attributiInseriti["height"];
			
		myTd.align = attributiInseriti["align"];
		myTd.vAlign = attributiInseriti["valign"];
		
		if (attributiInseriti["bordercolor"] == null || attributiInseriti["bordercolor"] == '') myTd.borderColor = attributiInseriti["bordercolor"];
			else myTd.borderColor = attributiInseriti["bordercolor"];
			
		if (attributiInseriti["bgcolor"] == null || attributiInseriti["bgcolor"] == '') myTd.bgColor = attributiInseriti["bgcolor"];
			else myTd.bgColor = attributiInseriti["bgcolor"];
	}
	
	selezione.select();
	editor.document.focus();  
}//ProprietaCella



/*--------------------------------------
	EVIDENZIA LE TABELLE
--------------------------------------*/
function EvidenziaLeTabelle(editor, coloreEvidenziatore, giaEvidenziate) 
{
	if (!EditorOperativo(editor)) return;

	var tagInseriti = editor.document.all;
	if (tagInseriti == null) return;
	
	for (var i=0;i < tagInseriti.length; i++)
	{
		//CERCO LE VARIABILI E LE PROCEDURE
		if (tagInseriti(i).tagName.toLowerCase() == "table")
		{
			if (giaEvidenziate)
			{
				tagInseriti(i).style.borderColor = coloreEvidenziatore;
				tagInseriti(i).style.borderWidth = 2;
			}
			else 
				tagInseriti(i).removeAttribute("style");

			tagInseriti(i).focus();
		}
	}

	editor.document.body.innerText = editor.document.body.innerHTML;
	editor.document.body.innerHTML = editor.document.body.innerText;
	
	editor.focus();

	return !giaEvidenziate;

}//EvidenziaLeTabelle



/*--------------------------------------
	PROPRIETA IMMAGINE
--------------------------------------*/
function ProprietaImmagine(editor) 
{
	if (!EditorOperativo(editor)) return;

	editor.document.focus();
	if (!EditorSelezionato(editor.document.selection)) return;
	
	if (editor.document.selection.type != 'Control') return;
	
	var selezione = editor.document.selection.createRange();
	var immagine = selezione.item(0);
	if (immagine.tagName.toLowerCase() != 'img') return;
	
	var attributi = new Array();	
	attributi["width"] = immagine.getAttribute("width");
	attributi["height"] = immagine.getAttribute("height");
	attributi["align"] = immagine.getAttribute("align");
	attributi["border"] = immagine.getAttribute("border");
	attributi["alt"] = immagine.getAttribute("alt");
	attributi["src"] = immagine.getAttribute("src");
	
	// var attributiInseriti = window.showModalDialog("../ecm5/image.htm", attributi, "dialogWidth:330px; dialogHeight:230px;help:0;status:no;");

	var attributiInseriti = window.showModalDialog("/central/ecm5/image.htm", 
			attributi, "dialogWidth:330px; dialogHeight:230px;help:0;status:no;");

	if (attributiInseriti != null) 
	{  
		if (attributiInseriti["width"] == null || attributiInseriti["width"] == '') immagine.removeAttribute("width");
			else immagine.width = attributiInseriti["width"];
			
		if (attributiInseriti["height"] == null || attributiInseriti["height"] == '') immagine.removeAttribute("height");
			else immagine.height = attributiInseriti["height"];
		
		if (attributiInseriti["align"] == null || attributiInseriti["align"] == '') immagine.removeAttribute("align");
			else immagine.align = attributiInseriti["align"];
			
		immagine.border = attributiInseriti["border"];
		
		if (attributiInseriti["alt"] == null || attributiInseriti["alt"] == '') immagine.removeAttribute("alt");
			else immagine.alt = attributiInseriti["alt"];
			
		if (attributiInseriti["src"] == null || attributiInseriti["src"] == '') immagine.removeAttribute("src");
			else immagine.src = attributiInseriti["src"];
	}
	
	selezione.select();
	editor.document.focus();  
}//ProprietaImmagine


/*-------------------------------
	ANCHOR
-------------------------------*/
function Anchor(editor, args) 
{
	if (!EditorOperativo(editor)) return;

	return window.showModalDialog("./ecm5/anchor.htm", args, "dialogWidth:13em; dialogHeight:13em; status:NO; scroll:NO; resizable=NO; help=NO;");
}