// Funzioni di costruzione HTML comuni

function GetLink(value) { return value.replace(/^ICAdmin\//i, LOCAL_URL_SITE + "/ICAdmin/"); }

function Category_TextHTML(node, cls)
{
	if ( IsEmpty(node) ) return "";
	if ( IsEmpty(node.item.value) )	return "";
	
	return '\n<P CLASS="' + cls + '-clsP">' + node.item.value + '</P>';
}

function Category_TextLinkHTML(node, cls)
{
	if ( IsEmpty(node) ) return "";
	if ( IsEmpty(node.item.value) )	return "";
	
	var HTML = '<P CLASS="' + cls + '-clsP">' + node.item.value + '</P>';
	var link = node.findChild("Link");
	if ( !IsEmpty(link.item.value) )
		HTML = '\n<A CLASS="' + cls + '-clsP-link" TARGET="'+ link.item.getAttribute("target") + '" HREF="' + GetLink(link.item.value) + '">' + HTML + '</A>';
	return HTML;
}

function Category_ImmagineHTML(node, cls, arg)
{
	if ( arguments.length < 3 ) arg = "";
	if ( IsEmpty(node) ) return "";
	if ( IsEmpty(node.item.value) )	return "";
	
	var scritta = node.findChild("Scritta");
	var titleHTML = ""; 
	if ( !IsEmpty(scritta.item.value) ) 
		titleHTML = 'TITLE="' + scritta.item.value + '"';
	
	var HTML;
	if ( node.item.getAttribute("resize") == 1 )
		HTML = '<IMG '+ arg +' CLASS="'+ cls +'-clsIMG-resize" '+ titleHTML +' SRC="'+ GetLink(node.item.value) +'">';
	else
		HTML = '<IMG '+ arg +' CLASS="'+ cls +'-clsIMG" '+ titleHTML +' SRC="'+ GetLink(node.item.value) +'">';
	
	var link = node.findChild("Link");
	if ( !IsEmpty(link.item.value) )
		HTML = '\n<A CLASS="'+ cls +'-clsA" TARGET="'+ link.item.getAttribute("target") +'" HREF="'+ GetLink(link.item.value) +'">'+ HTML +'</A>';
	return HTML;
}

function Category_FiguraHTML(node, cls)
{
	if ( IsEmpty(node) ) return "";
	
	var target = node.findChild("Immagine");
	if ( IsEmpty(target) ) return "";
	if ( IsEmpty(target.item.value) ) return "";
		
	var HTML = Category_ImmagineHTML(target, cls + "-Immagine");	
	if ( HTML.length == 0 )	return "";
	
	target = node.findChild("Didascalia");
	if ( !IsEmpty(target) )
		HTML += Category_TextLinkHTML(target, cls + "-Didascalia");
	return HTML;
}

function Category_ElencoHTML(node, clsBase)
{	
	var target = node.findChild("Voci");
	var list = target.item.getValueAsList();
	if ( list.length == 0 )
		return "";

	var HTML = '<OL CLASS="'+ clsBase +'-clsOL" TYPE='+ node.item.getAttribute("type") +'>';
	var prefix = '<LI CLASS="'+ clsBase +'-clsLI">';
	var prefixHREF = prefix + '<A CLASS="'+ clsBase +'-link" TARGET="'+ node.item.getAttribute("target") +'" HREF="';
	for (var i=0; i<list.length; i++)
	{
		if ( IsEmpty(list[i][1]) )
			HTML += prefix + list[i][0];
		else
			HTML += prefixHREF + GetLink(list[i][1]) +'">' + list[i][0] + '</A>';
	}
	HTML += '</OL>';
	
	return HTML;
}

function Category_TabellaHTML(node, clsBase)
{	
	var tmp = node.findChild("Celle");
	var table = tmp.item.getValueAsTable();
	if ( table.length == 0 )
		return "";
		
	var HTML = '<BR><TABLE CLASS="'+ clsBase +'-clsTABLE" CELLSPACING=2 CELLPADDING=0>';
	if ( !IsEmpty(node.item.value) )
		HTML +=	'<CAPTION CLASS="'+ clsBase +'-clsCAPTION">'+ node.item.value +'</CAPTION>';
	
	var id, i, j, rowHTML;
	id = tmp.item.getAttribute("id");
	rowHTML = '<THEAD><TR>'
	for (i=0,j=0; j<table[0].length; j++)
	{
		tmp = table[0][j][0];
		if ( IsEmpty(tmp) ) 
			tmp = "&nbsp;"
		else
			i++;
		rowHTML += '<TH CLASS="'+ clsBase +'-clsTH">'+ tmp +'</TH>';
	}
	rowHTML += '</TR></THEAD>';
	if ( i )
		HTML +=	rowHTML;
	var prefixTD = '<TD CLASS="'+ clsBase +'-clsTD" ';
	var prefixA = '<A CLASS="'+ clsBase +'-link" TARGET="'+ node.item.getAttribute("target") +'" HREF="';
	for (i=1; i<table.length; i++)
	{
		rowHTML = '<TR CLASS="'+ clsBase + '-clsTR';
		if ( i%2 == 0 )
			rowHTML += "-pari";
		else
			rowHTML += "-dispari";
		rowHTML += '">'
		for (j=0; j<table[0].length; j++)
		{
			tmp = table[i][j][0];
			if ( IsEmpty(tmp) ) 
				tmp = "&nbsp;"
			if ( IsEmpty(table[i][j][1]) )
				rowHTML += prefixTD + 'ID="Table_'+ id +'_'+ i +'_'+ j +'">' + tmp;
			else
				rowHTML += prefixTD + '>' + prefixA + GetLink(table[i][j][1]) +'" ID="Table_'+ id +'_'+ i +'_'+ j +'">' + tmp + '</A>';			
			rowHTML += '</TD>';
		}
		rowHTML += '</TR>';
		HTML += rowHTML;
	}
	HTML += '</TABLE>';
//window.alert(HTML);	
	return HTML;
}

