
  /////////////
 // CTable.js 
//
function CTable(id, cls)
{
try
{	
	if ( IsUndefined(cls) ) cls = "CTable";

	this.id = id;
	this.cls = cls; 

	// Gestione selezione
	this.lastSelected = [];

	// Gestori eventi
	this.onclick = function() {};
	this.ondblclick = function() {};
	this.onchange = function() {};
	this.onsubmit = function() {};
	this.onexternal = function() {};
	this.onquery = function() {};
}
catch (e)
{
	window.alert("CTable():\nErrore in fase di inizializzazione");
}
};


//
// CTable_writeHTML
//
function CTable_writeHTML()
{
	document.write('<DIV ID="' + this.id + '_DIV" CLASS="' + this.cls + '_clsDIV" ondblclick="return false;"></DIV>');
}
CTable.prototype.writeHTML = CTable_writeHTML;


//
// CTable_setHTML
//
function CTable_setHTML(innerHTML)
{
	document.getElementById(this.id + "_DIV").innerHTML = this.replaceMacro(innerHTML);
	this.lastSelected = [];
	this.onquery();
}
CTable.prototype.setHTML = CTable_setHTML;


//
// CTable_replaceMacro
//
function CTable_replaceMacro(str)
{
//window.alert("prima: " + str);

	str = str.replace(/MACROid/g, this.id);
	str = str.replace(/MACROcls/g, this.cls);

//window.alert("dopo: " + str);
	return str;
}
CTable.prototype.replaceMacro = CTable_replaceMacro;


//
// CTable_select
//
function CTable_select(arraySelected)
{
	var i; 

	for (i=0; i<this.lastSelected.length; i++)
		document.getElementById(this.lastSelected[i]).className = this.cls + "-clsTR";
	for (i=0; i<arraySelected.length; i++)
		document.getElementById(arraySelected[i]).className = this.cls + "-clsTR-selected";
	this.lastSelected = arraySelected;
}
CTable.prototype.select = CTable_select;


  //////////
 // CField
//
function CField(name)
{
	this.name = name;
	this.value = null;
}


  ///////////
 // CRecord
//
function CRecord(fieldArray)
{
	this.field = [];
	for (var i=0; i<fieldArray.length; i++)
		this.field[i] = new CField(fieldArray[i]);
}

//
//	CRecord.reset
//
function CRecord_reset()
{
	for (var i=0; i<this.field.length; i++)
		this.field[i].value = null;
}
CRecord.prototype.reset = CRecord_reset;

//
//	CRecord.getValue
//
function CRecord_getValue(name)
{
	try
	{
		for (var i=0; i<this.field.length; i++)
			if ( this.field[i].name == Trim(name))
				return this.field[i].value;
	}
	catch(e)
	{}
	return null;
}
CRecord.prototype.getValue = CRecord_getValue;

//
//	CRecord.setValue
//
function CRecord_setValue(name, value)
{
	try
	{
		for (var i=0; i<this.field.length; i++)
		{
			if ( this.field[i].name == Trim(name))
			{
				this.field[i].value = value;
				return true;
			}
		}
	}
	catch(e)
	{}
	return false;
}
CRecord.prototype.setValue = CRecord_setValue;

//
// CRecord.bindInnerHTML
//
function CRecord_bindInnerHTML(idRow, targetDocument)
{
	if ( IsEmpty(targetDocument) ) targetDocument = document;
	
	var result = false;
	var idBase = idRow.replace(/_idTR/, "_idTD") + "_";
	for (var i=0; i<this.field.length; i++)
	{
		try
		{
			this.field[i].value = Trim(targetDocument.getElementById(idBase + this.field[i].name).innerHTML);
			this.field[i].value = this.field[i].value.replace(/&nbsp;$/, "");
		}
		catch(e)
		{
			this.field[i].value = null;
			result = false;
		}
	}
	return result;
}
CRecord.prototype.bindInnerHTML = CRecord_bindInnerHTML;

//
// CRecord.getNext
//
function CRecord_getNext(row, idTable, targetDocument)
{
	if ( IsEmpty(targetDocument) ) targetDocument = document;
	
	var id = idTable + "_idTR_" + row;
	try	
	{
		if ( targetDocument.getElementById(id) == null )
			return false;
		this.bindInnerHTML(id, targetDocument);
		return true;
	}
	catch(e)
	{
		return false;
	}
}
CRecord.prototype.getNext = CRecord_getNext;

//
// CRecord.alert
//
function CRecord_alert()
{
	var str = "";
	for (var i=0; i<this.field.length; i++)
		str += this.field[i].name + " = " + this.field[i].value + "\n";
	window.alert(str);
}
CRecord.prototype.alert = CRecord_alert;
