//
// Costanti
//
var CEntry_SEP1_SAVE = " #1# "
var CEntry_SEP2_SAVE = " #2# "
var CEntry_SEP3_SAVE = " #3# "
var CEntry_SEP1_LOAD = "#1#";
var CEntry_SEP2_LOAD = "#2#";
var CEntry_SEP3_LOAD = "#3#";

var CEntry_TARGET_RADIO = [["_self", "_self"], ["_blank", "_blank"], ["_parent", "_parent"], ["_top", "_top"]];
var CEntry_LIST_TYPE_RADIO = [["1", "1, 2, .."], ["a", "a, b, .."], ["A", "A, B, .."], ["i", "i, ii, .."], ["I", "I, II, ..<BR>"], ["circle", "Cerchio"], ["disc", "Disco"], ["square", "Quadrato"]];
var CEntry_MAX_ITEMS = 100;
var CEntry_MAX_ROWS = 50;
var CEntry_MAX_COLS = 8;

var CEntry_TableNotify = function(idTable, row, col){};


  //////////////
 // CEntryItem
//
function CEntryItem(entry, id)
{
	this.entry = entry;
	this.parent = null;
	this.id = id;
	this.name = null;
	this.value = "";
	this.type = null;
	this.attrValue = [];
	this.subItem = [];
}

//
// CEntryItem.setParent
//
function CEntryItem_setParent(parentId)
{
	// Se il padre non c'é o e' già impostato non c'é altro da fare
	if ( parentId == 0 )
		return;
	if ( this.parent != null )
	{
		if ( this.parent.id != parentId ) 
		{
			window.alert("CEntryItem.setParent: multiple parent not allowed!");
			return;
		}
	}
	
	// Si imposta il padre
	var i;
	var entry = this.entry;
	var item;
	
	for (i=0; i<entry.item.length; i++)
	{		
		item = entry.item[i];
		if ( item.id == parentId )
		{
			this.parent = item;
			break;
		}
	}
	if ( this.parent == null )
	{
		window.alert("CEntryItem.setParent: parent not found!");
		return;
	}
	
	// e si aggancia il figlio
	this.parent.subItem[this.parent.subItem.length] = this;
}
CEntryItem.prototype.setParent = CEntryItem_setParent;

//
// CEntryItem.setAttribute
//
function CEntryItem_setAttribute(attr, value)
{
	// Se l'attributo c'é già si usa
	for (var i=0; i<this.attrValue.length; i++)
	{
		if ( this.attrValue[i][0] == attr )
		{
			this.attrValue[i][1] = value;
			return;
		}
	}
	
	// altrimenti se ne crea uno con value ripetuto come default di valore iniziale
	this.attrValue[this.attrValue.length] = [attr, value, value];
}
CEntryItem.prototype.setAttribute = CEntryItem_setAttribute;

//
// CEntryItem.reset
//
function CEntryItem_reset()
{
	this.value = ""; 
	for (var i=0; i<this.attrValue.length; i++)
		this.attrValue[i][1] = this.attrValue[i][2];
}
CEntryItem.prototype.reset = CEntryItem_reset;

//
// CEntryItem.getAttribute
//
function CEntryItem_getAttribute(attr)
{
	for (var i=0; i<this.attrValue.length; i++)
	{
		if ( this.attrValue[i][0] == attr )
			return this.attrValue[i][1];
	}
	return null;	
}
CEntryItem.prototype.getAttribute = CEntryItem_getAttribute;

//
// CEntryItem.getValueAsList
//
function CEntryItem_getValueAsList()
{
	var result = [];
	try
	{
		result = this.value.split(CEntry_SEP1_LOAD);
		for (var i=0; i<result.length; i++)
		{
			result[i] = result[i].split(CEntry_SEP2_LOAD);
			result[i][0] = Trim(result[i][0]);
			result[i][1] = Trim(result[i][1]);
		}
	}
	catch(e)
	{
		result = [];
	}
	return result;
}
CEntryItem.prototype.getValueAsList = CEntryItem_getValueAsList;

//
// CEntryItem.getValueAsTable
//
function CEntryItem_getValueAsTable()
{
	var result = [];
	try
	{
		result = this.value.split(CEntry_SEP1_LOAD);
		for (var i=0; i<result.length; i++)
		{
			result[i] = result[i].split(CEntry_SEP2_LOAD);
			for (var j=0; j<result[0].length; j++)
			{
				result[i][j] = result[i][j].split(CEntry_SEP3_LOAD);
				result[i][j][0] = Trim(result[i][j][0]);
				result[i][j][1] = Trim(result[i][j][1]);
			}
		}
	}
	catch(e)
	{
		result = [];
	}
	return result;
}
CEntryItem.prototype.getValueAsTable = CEntryItem_getValueAsTable;


//
// CEntryItem.getAttributeString
//
function CEntryItem_getAttributeString()
{
	var str = "";
	
	str += this.id + ":" + this.name + ":" + this.type;
	for (var i=0; i<this.attrValue.length; i++)
		str += ";" + this.attrValue[i][0] + ":" + this.attrValue[i][1];
	return str;
}
CEntryItem.prototype.getAttributeString = CEntryItem_getAttributeString;

//
// CEntryItem.parse
//
function CEntryItem_parse(value, attributes)
{
	var i;
	var name_value_list = [];
	var attr_list = attributes.split(";");
	
	for (i=0; i<attr_list.length; i++)
		name_value_list[i] = attr_list[i].split(":");
	
	// La prima coppia contiene parent_id:item_name:object_type;
	this.setParent(name_value_list[0][0]);
	this.name = name_value_list[0][1];
	this.type = name_value_list[0][2];
	
	// Valore
	this.value = value;
	
	// Attributi
	for (i=1; i<name_value_list.length; i++)
		this.setAttribute(name_value_list[i][0], name_value_list[i][1]); 
}
CEntryItem.prototype.parse = CEntryItem_parse;
  

  //////////
 // CEntry
//
function CEntry(id)
{
	this.id = id;
	this.item = [];
}

//
// CEntry_setItem
//
function CEntry_setItem(item, value, attributes)
{
	var i;
	var target = null;
	
	// Se l'item c'é già si usa
	for (i=0; i<this.item.length; i++)
	{
		if ( this.item[i].id == item )
		{
			target = this.item[i];
			break;
		}
	}
	
	// altrimenti se ne crea uno
	if ( target == null )
	{
		target = new CEntryItem(this, item);
		this.item[this.item.length] = target;
	}
	
	// Ricavato l'item lo si compila
	target.parse(value, attributes);
}
CEntry.prototype.setItem = CEntry_setItem;

//
// CEntry.getItem
//
function CEntry_getItem(item)
{
	for (var i=0; i<this.item.length; i++)
		if ( this.item[i].id == item )
			return this.item[i];						
	return null;
}
CEntry.prototype.getItem = CEntry_getItem;

//
// CEntry.update
//
function CEntry_update(entry)
{
	var i, j, a, attr, value;
	
	for (i=0; i<this.item.length; i++)
	{
		// L'item della entry argomento va ad aggiornare l'item corrente
		for (j=0; j<entry.item.length; j++)
		{
			if ( this.item[i].id == entry.item[j].id )
			{
				// Valore
				this.item[i].value = entry.item[j].value;
				
				// Attributi
				for (a=0; a<this.item[i].attrValue.length; a++)
				{
					attr = this.item[i].attrValue[a][0];
					value = entry.item[j].getAttribute(attr);
					if ( value != null )
						this.item[i].setAttribute(attr, value);
				}
			}
		}
	}
}
CEntry.prototype.update = CEntry_update;

//
// CEntry_bindItemTreeView
//
function CEntry_bindItemTreeView(node, tree)
{
	var i;
	var item;
	var sub;	
	
	for (i=0; i<node.item.subItem.length; i++)
	{		
		item = node.item.subItem[i];
		if ( item.subItem.length > 0 )
		{
			sub = tree.addNode(node, item.name);		
			sub.item = item;
			CEntry_bindItemTreeView(sub, tree);
		}
		else
		{
			sub = tree.addNode(node, item.name);		
			sub.item = item;
		}
	}
}

//
//	CEntry.bindTreeView
//
function CEntry_bindTreeView(tree)
{
	var i;
	var item;
	var node;
	
	tree.clear();
	
	for (i=0; i<this.item.length; i++)
	{
		item = this.item[i];
		if ( item.parent == null )
		{
			if ( item.subItem.length > 0 )
			{
				node = tree.addNode(null, item.name);
				node.item = item;
				CEntry_bindItemTreeView(node, tree);
			}
			else
			{
				node = tree.addNode(null, item.name);
				node.item = item;
			}
		}
	}	
}
CEntry.prototype.bindTreeView = CEntry_bindTreeView;



  ///////////////
 // CEntryTable
//
function CEntryTable()
{
	this.entry = [];
}

//
// CEntryTable.clear
//
function CEntryTable_clear()
{
	delete this.entry;
	this.entry = [];
}
CEntryTable.prototype.clear = CEntryTable_clear;

//
// CEntryTable.setEntry
//
function CEntryTable_setEntry(id, item, value, attributes)
{
	var i;
	var target = null;
	
	// Se l'entry c'é già si usa
	for (i=0; i<this.entry.length; i++)
	{
		if ( this.entry[i].id == id )
		{
			target = this.entry[i];
			break;
		}		
	}
	// atrimenti se ne crea una
	if ( target == null )
	{
		target = new CEntry(id);
		this.entry[this.entry.length] = target;
	}
	
	// Ricavata l'entry si inserisce l'item
	target.setItem(item, value, attributes);
}
CEntryTable.prototype.setEntry = CEntryTable_setEntry;

//
// CEntryTable.parseDocument
//
function CEntryTable_parseDocument(targetDocument, idBase)
{
	if ( IsEmpty(targetDocument) )
		return;
	if ( IsEmpty(idBase) )
		return;
		
	var base = idBase + "_idTD_";
	var value, attributes;
	
	var rec = new CRecord(["IdCategoryEntry", "IdItem", "EntryValue", "Attributes"]);
	var i=1;

	for (i=1; rec.getNext(i, idBase, targetDocument); i++)
	{
		value = rec.getValue("EntryValue");
		if ( value != null )
		{
			value = value.replace(/&gt;/g, ">");
			value = value.replace(/&lt;/g, "<");
			this.setEntry(rec.getValue("IdCategoryEntry"), rec.getValue("IdItem"), value, rec.getValue("Attributes"));
		}
	}
}
CEntryTable.prototype.parseDocument = CEntryTable_parseDocument;

//
// CEntryTable.load
//
function CEntryTable_load(entryArray)
{
	for (var i=0; i<entryArray.length; i++)
		this.setEntry(entryArray[i][0], entryArray[i][1], entryArray[i][2], entryArray[i][3]);
}
CEntryTable.prototype.load = CEntryTable_load;


  ///////////////////////
 // CEntryTableTemplate
//
function CEntryTableTemplate()
{
	this.table = [];
}

//
// CEntryTableTemplate.addText
//
function CEntryTableTemplate_addText(entry, base, parent, name, rows)
{
	if ( IsEmpty(rows) ) rows = 0;
	this.table[this.table.length] = [entry, base, "", parent+":" + name + ":text;rows:"+rows];	
	return base + 1;
}
CEntryTableTemplate.prototype.addText = CEntryTableTemplate_addText;

//
// CEntryTableTemplate.addLink
//
function CEntryTableTemplate_addLink(entry, base, parent)
{
	this.table[this.table.length] = [entry, base, "", parent+":Link:link;target:_self"];	
	return base + 1;
}
CEntryTableTemplate.prototype.addLink = CEntryTableTemplate_addLink;

//
// CEntryTableTemplate.addTextLink
//
function CEntryTableTemplate_addTextLink(entry, base, parent, name, rows)
{
	var next = this.addText(entry, base, parent, name, rows);
	return this.addLink(entry, next, base);
}
CEntryTableTemplate.prototype.addTextLink = CEntryTableTemplate_addTextLink;

//
// CEntryTableTemplate.addImg
//
function CEntryTableTemplate_addImg(entry, base, parent, name, rows)
{
	if ( IsEmpty(rows) ) rows = 2;
	this.table[this.table.length] = [entry, base, "", parent+":" + name + ":img;resize:1"];	
	var next = this.addLink(entry, base+1, base);
	return this.addText(entry, next, base, "Scritta", rows);
}
CEntryTableTemplate.prototype.addImg = CEntryTableTemplate_addImg;

//
// CEntryTableTemplate.addPicture
//
function CEntryTableTemplate_addPicture(entry, base, parent, name)
{
	this.table[this.table.length] = [entry, base, "", parent+":" + name + ":picture"];	
	var next = this.addImg(entry, base+1, base, "Immagine");
	return this.addTextLink(entry, next, base, "Didascalia", 4);
}
CEntryTableTemplate.prototype.addPicture = CEntryTableTemplate_addPicture;

//
// CEntryTableTemplate.addItems
//
function CEntryTableTemplate_addItems(entry, base, parent, name)
{
	this.table[this.table.length] = [entry, base, "", parent+":" + name + ":items"];	
	return base + 1;
}
CEntryTableTemplate.prototype.addItems = CEntryTableTemplate_addItems;

//
// CEntryTableTemplate.addRows
//
function CEntryTableTemplate_addRows(entry, base, parent, name, id)
{
	this.table[this.table.length] = [entry, base, "", parent+":" + name + ":rows;id:"+id];
	return base + 1;
}
CEntryTableTemplate.prototype.addRows = CEntryTableTemplate_addRows;

//
// CEntryTableTemplate.addList
//
function CEntryTableTemplate_addList(entry, base, parent, name)
{
	this.table[this.table.length] = [entry, base, "", parent+":" + name + ":list;type:disc;target:_self"];	
	return this.addItems(entry, base+1, base, "Voci");
}
CEntryTableTemplate.prototype.addList = CEntryTableTemplate_addList;

//
// CEntryTableTemplate.addTable
//
function CEntryTableTemplate_addTable(entry, base, parent, name, id)
{
	this.table[this.table.length] = [entry, base, "", parent+":"+ name +":table;id:"+ id +";target:_self"];
	return this.addRows(entry, base+1, base, "Celle", id);
}
CEntryTableTemplate.prototype.addTable = CEntryTableTemplate_addTable;

//
// CEntryTableTemplate.addBlock
//
function CEntryTableTemplate_addBlock(entry, base, parent, name)
{
	this.table[this.table.length] = [entry, base, "", parent+":" + name + ":block"];
	return base + 1;
}
CEntryTableTemplate.prototype.addBlock = CEntryTableTemplate_addBlock;


  //////////////
 // CEntryEdit
//
function CEntryEdit(id)
{
	this.id = id;	
	this.cls = "CEntryEdit";
	this.pageUrl = "";
	this.pageCaption = "Link alla pagina corrente";
	this.lock = false;
		
	this.node = null;
	this.target = null;
	this.table = [];
	
	this.onsave = function(idItem, value, attributes){};
	this.ondelete = function(idItem){};
	this.onupload = function(idItem, idTarget){};
	this.onexit = function(){};
}

//
// CEntryEdit.updateHTML
//
function CEntryEdit_updateHTML(node, target)
{
	this.node = node;
	if ( !IsEmpty(target) ) this.target = target;
	
	this.target.innerHTML = this.buildHTML();
//window.alert(this.target.innerHTML);
	this.bindHTML();
}
CEntryEdit.prototype.updateHTML = CEntryEdit_updateHTML;

//
// CEntryEdit.buildHTML
//
function CEntryEdit_buildHTML()
{
	if ( IsEmpty(this.node) ) return "&nbsp;";
	
	var id = this.id;
	var cls = this.cls;	
	var HTML = '\n<DIV CLASS='+ cls +'-clsDIV><TABLE CLASS='+ cls +'-clsTABLE><TBODY>';
	switch ( this.node.item.type )
	{
		case "text":
			HTML += '\n<TR><TD CLASS='+ cls +'-clsTD-text>';
			HTML += '<TEXTAREA CLASS='+ cls +'-clsTEXTAREA-text ID='+ id + '_idValue COLS=2';
			var rows = this.node.item.getAttribute("rows");
			if ( rows > 0 )
				HTML += ' ROWS=' + rows;
			HTML += '></TEXTAREA></TD></TR>';
			break;

		case "link":
			HTML += '\n<TR><TD CLASS='+ cls +'-clsTD><B>HREF:</B></TD></TR><TR><TD CLASS=' + cls + '-clsTD><TABLE CELLPADDING=0 CELLSPACING=0><TR>';
			HTML += '<TD CLASS='+ cls +'-clsTD-url><INPUT TYPE=text CLASS='+ cls +'-clsINPUT ID='+ id +'_idValue VALUE="' + this.node.item.value + '"></TD>';
			HTML += '<TD CLASS='+ cls +'-clsTD-upload><INPUT TYPE=button CLASS="' + cls + '-clsImgBtn clsBkBtnUpload" onclick="'+ id + '.action(\'upload-start\',\''+ id +'_idValue\');"></TD>';
			HTML += '</TR></TABLE></TD></TR>';
			// target:
			HTML += '\n<TR><TD CLASS=' + cls +'-clsTD>';
			HTML += this.radioHTML("target", CEntry_TARGET_RADIO, "TARGET:");
			HTML += '</TD></TR>';
			// pagina corrente
			if ( IsEmpty(this.pageUrl) )
				break;
			HTML += '\n<TR><TD CLASS=' + cls +'-clsTD>';
			HTML += '<INPUT TYPE=button CLASS='+ cls +'-clsINPUT-top-btn VALUE="' + this.pageCaption +'" STYLE="width: 164;" onclick="' + id +'.action(\'category-page\')">';
			HTML += '</TD></TR>';
			break;
			
		case "img":
			HTML += '\n<TR><TD CLASS=' + cls + '-clsTD><B>SRC:</B></TD></TR><TR><TD CLASS=' + cls + '-clsTD><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH=100%><TR>';
			HTML += '<TD CLASS='+ cls +'-clsTD-url><INPUT TYPE=text CLASS='+ cls +'-clsINPUT ID='+ id +'_idValue VALUE="'+ this.node.item.value +'"></TD>';
			HTML += '<TD CLASS='+ cls +'-clsTD-upload><INPUT TYPE=button CLASS="' + cls + '-clsImgBtn clsBkBtnUpload" onclick="' + id + '.action(\'upload-start\',\''+ id +'_idValue\');"></TD>';
			HTML += '</TR></TABLE></TD></TR>';
			// resize:
			HTML += '\n<TR><TD CLASS=' + cls + '-clsTD>';
			HTML += '<INPUT TYPE=checkbox ID='+ id +'_idINPUT_resize';
			if ( this.node.item.getAttribute("resize") == 1 )
				HTML += ' CHECKED';
			HTML += '>Resize</TD></TR>';
			break;
		
		case "list":
			// type:
			HTML += '\n<TR><TD CLASS=' + cls +'-clsTD>';
			HTML += '<INPUT TYPE=hidden ID='+ id +'_idValue VALUE="">';
			HTML += this.radioHTML("type", CEntry_LIST_TYPE_RADIO, "<BR>Punti Elenco:<BR>");
			HTML += '</TD></TR>';
			// target:
			HTML += '\n<TR><TD CLASS=' + cls +'-clsTD>';
			HTML += this.radioHTML("target", CEntry_TARGET_RADIO, "<BR>Target per i link dell'elenco:<BR>");
			HTML += '</TD></TR>';
			break;
			
		case "items":
			this.loadItems();
			HTML += '\n<TR><TD CLASS='+ cls +'-clsTD><DIV CLASS='+ cls +'-clsDIV-list ID='+ id +'_idDIV_list>';
			HTML += this.itemsHTML();
			HTML += '</DIV></TD></TR>';
			break;
			
		case "table":
			HTML += '\n<TR><TD CLASS=' + cls + '-clsTD><B>Nome tabella:</B></TD></TR>';
			HTML += '\n<TR><TD CLASS=' + cls + '-clsTD>';
			HTML += '<INPUT TYPE=text CLASS='+ cls +'-clsINPUT ID='+ id +'_idValue VALUE="'+ this.node.item.value +'"><BR>';
			HTML += '</TD></TR>';
			// target:
			HTML += '\n<TR><TD CLASS=' + cls +'-clsTD>';
			HTML += this.radioHTML("target", CEntry_TARGET_RADIO, "<BR>Target per i link della tabella:<BR>");
			HTML += '</TD></TR>';
			break;
			
		case "rows":
			this.loadRows();
			HTML += '\n<TR><TD CLASS='+ cls +'-clsTD><DIV CLASS='+ cls +'-clsDIV-table ID='+ id +'_idDIV_table>';
			HTML += this.rowsHTML();
			HTML += '</DIV></TD></TR>';
			break;

		default:
			return "&nbsp;";
			break;
	}
	HTML += '</TBODY></TABLE></DIV>';

	HTML += '<DIV CLASS='+ cls +'-clsDIV-bottom><TABLE CELLSPACING=0 CELLPADDING=0 CLASS='+ cls + '-clsTABLE-bottom><TR>';
	HTML += '<TD CLASS='+ cls + '-clsTD-bottom-btn>'; 	
	HTML += '&nbsp;<INPUT TYPE=button CLASS='+ cls +'-clsINPUT-bottom-btn VALUE=Salva onclick="'+ id +'.action(\'save\')">';
	HTML += '&nbsp;<INPUT TYPE=button CLASS='+ cls +'-clsINPUT-bottom-btn VALUE=Cancella onclick="' + id +'.action(\'delete\')">';
	HTML += '&nbsp;<INPUT TYPE=button CLASS='+ cls +'-clsINPUT-bottom-btn VALUE=Annulla onclick="' + id +'.action(\'cancel\')">';
	HTML += '&nbsp;<INPUT TYPE=button CLASS='+ cls +'-clsINPUT-bottom-btn VALUE=Ricarica onclick="' + id +'.action(\'reload\')">';
	HTML += '&nbsp;<INPUT TYPE=button CLASS='+ cls +'-clsINPUT-bottom-btn VALUE=Esci onclick="' + id +'.action(\'exit\')">';
	HTML += '<TD CLASS='+ cls + '-clsTD-bottom-name>' + this.node.item.name + '</TD>';
	HTML += '</TD></TR></TABLE></DIV>';
	
	return HTML;
}
CEntryEdit.prototype.buildHTML = CEntryEdit_buildHTML;

//
// CEntryEdit.radioHTML
//
function CEntryEdit_radioHTML(attr, radioArray, caption)
{
	var i;
	var action;
	var HTML = "";
	var attrValue = this.node.item.getAttribute(attr);
	
	HTML += '<FORM CLASS='+ this.cls +'-clsFORM-radio onsubmit="return false;"><B>' + caption + '</B>';
	for (i=0; i<radioArray.length;  i++)
	{
		HTML += '\n<INPUT TYPE=radio NAME=radio_' + attr + ' ID="'+ this.id +'_'+ attr + ':' + i + '"';
		if ( radioArray[i][0] == attrValue )
			HTML += ' CHECKED';
		HTML += '>' + radioArray[i][1] + '</INPUT>';
	}
	return HTML + "</FORM>";
}
CEntryEdit.prototype.radioHTML = CEntryEdit_radioHTML;

//
// CEntryEdit.itemsHTML
//
function CEntryEdit_itemsHTML()
{
	if ( this.table.length == 0 )
		this.table = [["", ""]];
		
	var id = this.id;
	var cls = this.cls;
	var HTML = '\n<TABLE CELLSPACING=4 CELLPADDING=0 CLASS='+ cls +'-clsTABLE-item ID='+ id +'_idTABLE_item>';
	var action;
	for (var i=0; i<this.table.length; i++)
	{
		HTML += '\n<TR><TD CLASS="'+ cls +'-clsTD-item-button" NOWRAP>';
		action = id + ".insItem("+ i +")";
		HTML += '<INPUT TYPE="button" CLASS="'+ cls +'-clsImgBtn clsBkBtnInsUp" onclick="'+ action +'"><BR>';
		action = id + ".insItem("+ (i+1) +")";
		HTML += '<INPUT TYPE="button" CLASS="'+ cls +'-clsImgBtn clsBkBtnInsDown" onclick="'+ action +'"><BR>';
		action = id + ".delItem("+ i +")";
		HTML += '<INPUT TYPE="button" CLASS="'+ cls +'-clsImgBtn clsBkBtnDelete" onclick="'+ action +'">';
		HTML += '</TD><TD NOWRAP>';
		
		HTML += '\n<TABLE CLASS="'+ cls +'-clsTABLE-item-cell" CELLSPACING=0 CELLPADDING=0>';
		HTML += '\n<TR><TD COLSPAN=2  CLASS="'+ cls +'-clsTD-item" NOWRAP>';
		HTML += '<TEXTAREA CLASS="'+ cls +'-clsTEXTAREA-item" ID="'+ id +'_idTEXTAREA_item_'+ i +'" COLS=2></TEXTAREA>';
		HTML += '</TD></TR>';
   		HTML += '\n<TR><TD CLASS="'+ cls +'-clsTD-item-url"><INPUT TYPE="text" CLASS="'+ cls +'-clsINPUT-item" ID="'+ id +'_idINPUT_item_'+ i +'"></TD>'
		action = id +".action('upload-start','"+ id +"_idINPUT_item_"+ i +"',' ["+ i +"]')";
   		HTML += '<TD CLASS="'+ cls +'-clsTD-item-upload"><INPUT TYPE="button" CLASS="'+ cls +'-clsImgBtn clsBkBtnUpload" onclick="'+ action +'">';
		HTML += '</TD></TR></TABLE>';

		HTML += '</TD></TR>';
	}
	HTML += '</TABLE>';
	return HTML;
}
CEntryEdit.prototype.itemsHTML = CEntryEdit_itemsHTML;

//
// CEntryEdit.rowsHTML
//
function CEntryEdit_rowsHTML()
{
	var id = this.id;
	var cls = this.cls;
	var action;
	
	if ( this.table.length < 2 || this.table[0].length < 1 )
		this.table = [[["", ""]], [["", ""]]];
	var HTML = '\n<TABLE CLASS='+ cls +'-clsTABLE-row ID='+ id + '_idTABLE_row>';
	var i, j;
	
	// head
	HTML += '<TR><TD CLASS="'+ cls +'-clsTD-head" NOWRAP>&nbsp;</TD>';
	for (i=0; i<this.table[0].length; i++)
	{
		HTML += '<TD CLASS="'+ cls +'-clsTD-head" NOWRAP><TABLE CLASS="'+ cls +'-clsTABLE-cell" CELLSPACING=0 CELLPADDING=0><TR><TD CLASS="'+ cls +'-clsTD-text" NOWRAP>';
		action = id + ".insCol("+ i +")";
		HTML += '\n<INPUT TYPE=button CLASS="'+ cls +'-clsImgBtn clsBkBtnInsLeft" onclick="'+ action +'">';
		action = id + ".insCol("+ (i+1) +")";
		HTML += '\n<INPUT TYPE=button CLASS="'+ cls +'-clsImgBtn clsBkBtnInsRight" onclick="'+ action +'">';
		action = id + ".delCol("+ i +")";
		HTML += '\n<INPUT TYPE=button CLASS="'+ cls +'-clsImgBtn clsBkBtnDelete" onclick="'+ action +'">';
		action = id + ".setUrl("+ i +")";
		HTML += '\n<INPUT TYPE=button CLASS="'+ cls +'-clsImgBtn clsBkBtnNotify" onclick="'+ action +'">';
		HTML += '\n<BR><TEXTAREA CLASS="'+ cls +'-clsTEXTAREA-cell-col" ID="'+ this.id +'_idTEXTAREA_head_'+ i +'" COLS=2></TEXTAREA>';
		HTML += '</TD></TR></TABLE></TD>';
	}
	HTML += '<TR>';
	
	// rows
	for (i=1; i<this.table.length; i++)
	{
		HTML += '\n<TR><TD CLASS="'+ cls +'-clsTD-row-button" NOWRAP>';
		action = id + ".insRow("+ i +")";
		HTML += '<INPUT TYPE="button" CLASS="'+ cls +'-clsImgBtn clsBkBtnInsUp" onclick="'+ action +'"><BR>';
		action = id + ".insRow("+ (i+1) +")";
		HTML += '<INPUT TYPE="button" CLASS="'+ cls +'-clsImgBtn clsBkBtnInsDown" onclick="'+ action +'"><BR>';
		action = id + ".delRow("+ i +")";
		HTML += '<INPUT TYPE="button" CLASS="'+ cls +'-clsImgBtn clsBkBtnDelete" onclick="'+ action +'">';
		HTML += '</TD>';
	
		for (j=0; j<this.table[0].length; j++)
		{
			HTML += '<TD CLASS="'+ cls +'-clsTD-row" NOWRAP><TABLE CLASS="'+ cls +'-clsTABLE-cell" CELLSPACING=0 CELLPADDING=0>';
			HTML += '\n<TR><TD COLSPAN=2 CLASS="'+ cls +'-clsTD-cell" NOWRAP><TEXTAREA CLASS="'+ cls +'-clsTEXTAREA-cell" ID="'+ id +'_idTEXTAREA_cell_'+ i +'_'+ j +'" COLS=2></TEXTAREA></TD></TR>';
			HTML += '\n<TR><TD CLASS="'+ this.cls +'-clsTD-cell-url"><INPUT TYPE="text" CLASS="'+ cls +'-clsINPUT-cell" ID="'+ id +'_idINPUT_cell_'+ i +'_'+ j +'"></TD>'
   			action = this.id + ".action('upload-start','" + this.id +"_idINPUT_cell_"+ i +'_'+ j +"',' ["+ i +","+ j +"]')";
			HTML += '<TD CLASS="'+ cls +'-clsTD-cell-upload"><INPUT TYPE="button" CLASS="' + cls + '-clsImgBtn clsBkBtnUpload" onclick="'+ action +'">';
			HTML += '</TD></TR></TABLE></TD>';
		}
		HTML += '</TR>';
	}
	HTML += '</TABLE>';
//window.alert(HTML);	
	return HTML;
}
CEntryEdit.prototype.rowsHTML = CEntryEdit_rowsHTML;

//
// CEntryEdit.loadItems
//
function CEntryEdit_loadItems()
{
	this.table = this.node.item.getValueAsList();
}
CEntryEdit.prototype.loadItems = CEntryEdit_loadItems;

//
// CEntryEdit.insItem
//
function CEntryEdit_insItem(i)
{
	if ( this.table.length >= CEntry_MAX_ITEMS )
	{
		window.alert("Sono consentite un massimo di " + CEntry_MAX_ITEMS + " voci");
		return;
	}
	if ( IsEmpty(i) || i > this.table.length || i < 0 ) 
		return;
	
	if (this.lock) return; else	this.lock=true;
	try 
	{		
		this.readHTML();
		this.table.splice(i, 0, ["",""]);
		document.getElementById(this.id + "_idDIV_list").innerHTML = this.itemsHTML();
		this.bindHTML();
	}
	catch(e)
	{}
	this.lock = false;
}
CEntryEdit.prototype.insItem = CEntryEdit_insItem;

//
// CEntryEdit.delItem
//
function CEntryEdit_delItem(i)
{
	if ( IsEmpty(i) || i >= this.table.length || i < 0 )
		return;
	if ( !window.confirm("Cancellare la voce in elenco") )
		return; 
		
	if (this.lock) return; else	this.lock=true;
	try 
	{
		this.readHTML();
		this.table.splice(i, 1);
		document.getElementById(this.id + "_idDIV_list").innerHTML = this.itemsHTML();
		this.bindHTML();
	}
	catch(e)
	{}
	this.lock = false;
}
CEntryEdit.prototype.delItem = CEntryEdit_delItem;

//
// CEntryEdit.loadRows
//
function CEntryEdit_loadRows()
{
	this.table = this.node.item.getValueAsTable();
}
CEntryEdit.prototype.loadRows = CEntryEdit_loadRows;

//
// CEntryEdit.insCol
//
function CEntryEdit_insCol(c)
{
	if ( this.table[0].length >= CEntry_MAX_COLS )
	{
		window.alert("Sono consentite un massimo di " + CEntry_MAX_COLS + " colonne");
		return;
	}
	if ( IsEmpty(c) || c > this.table[0].length || c < 0 ) 
		return;
	
	if (this.lock) return; else	this.lock=true;
	try 
	{
		this.readHTML();
		for (var i=0; i<this.table.length; i++)
			this.table[i].splice(c, 0, ["",""]);
		document.getElementById(this.id + "_idDIV_table").innerHTML = this.rowsHTML();
		this.bindHTML();
	}
	catch(e)
	{}
	this.lock = false;
}
CEntryEdit.prototype.insCol = CEntryEdit_insCol;

//
// CEntryEdit.delCol
//
function CEntryEdit_delCol(c)
{
	if ( IsEmpty(c) || c > this.table[0].length || c < 0 ) 
		return;
	if ( !window.confirm("Cancellare la colonna della tabella") )
		return; 
	
	if (this.lock) return; else	this.lock=true;
	try 
	{
		this.readHTML();
		for (var i=0; i<this.table.length; i++)
			this.table[i].splice(c, 1);
		document.getElementById(this.id + "_idDIV_table").innerHTML = this.rowsHTML();
		this.bindHTML();
	}
	catch(e)
	{}
	this.lock = false;
}
CEntryEdit.prototype.delCol = CEntryEdit_delCol;

//
// CEntryEdit.insRow
//
function CEntryEdit_insRow(r)
{
	if ( this.table.length > CEntry_MAX_ROWS )
	{
		window.alert("Sono consentite un massimo di " + CEntry_MAX_ROWS + " righe");
		return;
	}
	if ( IsEmpty(r) || r > this.table.length + 1 || r < 1 ) 
		return;
		
	if (this.lock) return; else	this.lock=true;
	try 
	{
		this.readHTML();
		var emptyRow = [];
		for (var i=0; i<this.table[0].length; i++)
			emptyRow[emptyRow.length] = ["",""];
		this.table.splice(r, 0, emptyRow);
		document.getElementById(this.id + "_idDIV_table").innerHTML = this.rowsHTML();
		this.bindHTML();
	}
	catch(e)
	{}
	this.lock = false;
}
CEntryEdit.prototype.insRow = CEntryEdit_insRow;

//
// CEntryEdit.delRow
//
function CEntryEdit_delRow(r)
{
	if ( IsEmpty(r) || r >= this.table.length || r < 0 )
		return;
	if ( !window.confirm("Cancellare la riga della tabella") )
		return; 
		
	if (this.lock) return; else	this.lock=true;
	try 
	{
		this.readHTML();
		this.table.splice(r, 1);
		document.getElementById(this.id + "_idDIV_table").innerHTML = this.rowsHTML();
		this.bindHTML();
	}
	catch(e)
	{}
	this.lock = false;
}
CEntryEdit.prototype.delRow = CEntryEdit_delRow;

//
// CEntryEdit.setUrl
//
function CEntryEdit_setUrl(c)
{
	if ( IsEmpty(c) || c > this.table[0].length || c < 0 ) 
		return;
	if ( !window.confirm("Impostare i link della colonna con l'azione di notifica?\nSi ricorda che la tabella va salvata prima per non perdere le ultime modifiche.") )
		return; 
	
	this.readHTML();
	for (var i=1; i<this.table.length; i++)
		this.table[i][c][1] = "javascript:CEntry_TableNotify("+ this.node.item.getAttribute("id") +","+ i +","+ c +");"
	document.getElementById(this.id + "_idDIV_table").innerHTML = this.rowsHTML();
	this.bindHTML();
}
CEntryEdit.prototype.setUrl = CEntryEdit_setUrl;

//
// CEntryEdit.readRadioHTML
//
function CEntryEdit_readRadioHTML(attr, radioArray)
{
	for (var i=0; i<radioArray.length; i++)
		if ( document.getElementById(this.id + "_" + attr + ":" + i).checked )
			return radioArray[i][0];
}
CEntryEdit.prototype.readRadioHTML = CEntryEdit_readRadioHTML;

//
// CEntryEdit.replaceBracket
//
function CEntryEdit_replaceBracket(value)
{
	value = value.replace(/&lt;/g, "<");
	value = value.replace(/&gt;/g, ">");
	return value.replace(/&amp;/g, "&");
}
CEntryEdit.prototype.replaceBracket = CEntryEdit_replaceBracket;

//
// CEntryEdit.bindHTML
//
function CEntryEdit_bindHTML()
{
	if ( this.node == null )
		return;
	
	switch ( this.node.item.type )	
	{
		case "text":		
			document.getElementById(this.id+"_idValue").value = this.replaceBracket(this.node.item.value);
			break;
		
		case "items":
			for (var i=0; i<this.table.length; i++)
			{
				document.getElementById(this.id + "_idTEXTAREA_item_" + i).value = this.replaceBracket(this.table[i][0]);
				document.getElementById(this.id + "_idINPUT_item_" + i).value = this.table[i][1];
			}
			break;
			
		case "rows":
			var r, c;
			for (c=0; c<this.table[0].length; c++)
				document.getElementById(this.id + "_idTEXTAREA_head_" + c).value = this.replaceBracket(this.table[0][c][0]);
			for (r=1; r<this.table.length; r++)
			{
				for (c=0; c<this.table[0].length; c++)
				{
					document.getElementById(this.id + "_idTEXTAREA_cell_" + r + "_" + c).value = this.replaceBracket(this.table[r][c][0]);
					document.getElementById(this.id + "_idINPUT_cell_" + r + "_" + c).value = this.table[r][c][1];
				}
			}
			break;
	}
}
CEntryEdit.prototype.bindHTML = CEntryEdit_bindHTML;

//
// CEntryEdit.readHTML
//
function CEntryEdit_readHTML()
{
	if ( IsEmpty(this.node) )
		return;
		
	// Attributes
	var id = this.id;
	switch ( this.node.item.type )
	{
		case "text":
			this.node.item.value = document.getElementById(id +"_idValue").value;
			break;
			
		case "link":
			this.node.item.value = document.getElementById(id +"_idValue").value;
			this.node.item.setAttribute("target", this.readRadioHTML("target", CEntry_TARGET_RADIO));
			break;
			
		case "img":
			this.node.item.value = document.getElementById(id +"_idValue").value;
			var attrValue = "0";
			if ( document.getElementById(id +"_idINPUT_resize").checked )
				attrValue = "1";
			this.node.item.setAttribute("resize", attrValue);
			break;

		case "list":
			this.node.item.setAttribute("type", this.readRadioHTML("type", CEntry_LIST_TYPE_RADIO));
			this.node.item.setAttribute("target", this.readRadioHTML("target", CEntry_TARGET_RADIO));
			break;
			
		case "items":
			this.node.item.value = "";
			var i, notEmpty, text, url;
			notEmpty = 0;
			for (i=0; i<this.table.length; i++)
			{
				text = document.getElementById(id +"_idTEXTAREA_item_"+ i).value;
				url = document.getElementById(id + "_idINPUT_item_" + i).value;
				if ( text.length + url.length > 0 ) 
					notEmpty++;
				if ( i )
					this.node.item.value += CEntry_SEP1_SAVE;
				this.node.item.value += text + CEntry_SEP2_SAVE + url;
				this.table[i][0] = text;
				this.table[i][1] = url;
			}
			if ( notEmpty == 0 )
				this.node.item.value = "";
			break;
		
		case "table":
			this.node.item.value = document.getElementById(id +"_idValue").value;
			this.node.item.setAttribute("target", this.readRadioHTML("target", CEntry_TARGET_RADIO));
			break;
			
		case "rows":
			this.node.item.value = "";
			var r, c, notEmpty, text, url;
			notEmpty = 0;
			for (c=0; c<this.table[0].length; c++)
			{
				text = document.getElementById(id +"_idTEXTAREA_head_" + c).value;
				if ( text.length > 0 ) 
					notEmpty++;
				if ( c )
					this.node.item.value += CEntry_SEP2_SAVE; 
				this.node.item.value += text + CEntry_SEP3_SAVE;
				this.table[0][c][0] = text;
			}
			for (r=1; r<this.table.length; r++)
			{
				this.node.item.value += CEntry_SEP1_SAVE;
				for (c=0; c<this.table[0].length; c++)
				{
					text = document.getElementById(id +"_idTEXTAREA_cell_" + r + "_" + c).value;
					url = document.getElementById(id +"_idINPUT_cell_" + r + "_" + c).value;
					if ( text.length + url.length > 0 ) 
						notEmpty++;
					if ( c )
						this.node.item.value += CEntry_SEP2_SAVE; 
					this.node.item.value += text + CEntry_SEP3_SAVE + url;
					this.table[r][c][0] = text;
					this.table[r][c][1] = url;
				}
			}
			if ( notEmpty == 0 )
				this.node.item.value = "";
			break;
	}	
}
CEntryEdit.prototype.readHTML = CEntryEdit_readHTML;

//
// CEntryEdit.action
//
function CEntryEdit_action(name, value, target)
{
	switch ( name )
	{
		case "reload":
			window.location.reload(true);
			break;
		
		case "exit":
			this.onexit();
			break;
			
		case "save":
			this.readHTML();
			this.onsave(this.node.item.id, this.node.item.value, this.node.item.getAttributeString());
			break;
			
		case "category-page":
			this.readHTML();
			if ( !IsEmpty(Trim(this.node.item.value)) )
				if ( !window.confirm("Sostituire il link corrente con il collegamento alla pagina?") )
					return;
			this.node.item.value = this.pageUrl;
			this.updateHTML(this.node);
			this.onsave(this.node.item.id, this.node.item.value, this.node.item.getAttributeString());
			break;
		
		case "delete":
			var msg;
			switch ( this.node.item.type )
			{
				case "rows":
					msg = "Cancellare tutte le celle della tabella?";
					break;
				
				case "items":
					msg = "Cancellare tutte le voci dell'elenco?";
					break;
					
				default:
					msg = "Cancellare il contenuto dell'elemento corrente?";
			}
			if ( !window.confirm(msg) )
					return;
			this.node.item.reset();
			this.updateHTML(this.node);
			this.ondelete(this.node.item.id);
			break;
			
		case "cancel":	
			this.updateHTML(this.node);
			break;
			
		case "upload-start":
			if ( IsEmpty(target) ) 
				target = "";
			this.onupload(this.node.item.id + target, value);
			break;
		
		case "upload-end":
//window.alert(target);
			document.getElementById(target).value = value;
			// autosave
			this.readHTML();
			this.onsave(this.node.item.id, this.node.item.value, this.node.item.getAttributeString());
			break;
	}
}
CEntryEdit.prototype.action = CEntryEdit_action;

