function Tabella(variabile,tag,titoli,dimTab,stileTabella,stileTitolo) {
	this.inizializza(variabile,tag,titoli,dimTab,stileTabella,stileTitolo);
}

Tabella.prototype.inizializza=function (variabile,tag,titoli,dimTab,stileTabella,stileTitolo) {
	this.variabile=variabile;
	this.tag=tag;
	this.stilePiePag="pieTabella";
	this.stileTabella=stileTabella;
	this.stileTitolo=stileTitolo;
	this.stileFiltro="stileFiltro";
	this.stileOrdD="stileFiltro";
	this.stileOrdC="stileFiltro";
	this.rTit=0;
	this.col=0;
	this.tipoCol=new Array();
	this.decCol=new Array();
	this.origCol=new Array();
	if (titoli){
		this.titoli=[new Riga()];
		this.rTit=1;
	}
	else
		this.titoli=new Array();
	this.stile=new Stile('class',this.stileTabella);
	this.dati=new Array();
	this.dimTab=dimTab;
	this.intestazione="";
	this.widthMin=170;
	this.widthMax=700;
	this.valTitoli=new Array();
};

Tabella.fuoco=null;

Tabella.prototype.setColonne=function (n){
	this.tipoCol=new Array(n);
};

Tabella.prototype.setStile=function (st){
	this.stilePiePag=st;
};

Tabella.prototype.setStileFiltro=function (st,tipo){
	if (arguments.length==1 || tipo=='f')
		this.stileFiltro=st;
	else if (tipo=='c')
		this.stileOrdC=st;
	else if (tipo=='d')
		this.stileOrdD=st;
};

Tabella.prototype.setCella=function (valore,col,riga){
	this.dati[riga].celle[col].setValore(valore);
	this.tabe.rows[riga+this.rTit].cells[col].innerHTML=this.dati[riga].celle[col].valore;
};

Tabella.prototype.getCella=function (col,riga){
	return this.dati[riga].celle[col].getValore();
};

Tabella.prototype.setIntestazione=function (intestazione){
	this.intestazione=intestazione;
};

Tabella.prototype.addRigaTitolo=function (){
	this.titoli[this.titoli.length]=new Riga();
	this.rTit++;
};

Tabella.prototype.addTitolo=function (valore,col,tipo,filtro,decimali){
	var indice;
	if (col==null)
		indice=this.titoli[this.titoli.length-1].celle.length;
	else
		indice=col;
	this.tipoCol[indice]=tipo;
	if(arguments.length<5)
		this.decCol[indice]=(tipo=="b")?"true":0;
	else
		this.decCol[indice]=decimali;
	this.valTitoli[this.valTitoli.length]=[valore,filtro,indice];
	var cella=this.titoli[this.titoli.length-1].aggiungiCella("" ,"s",0);
	cella.setId("__tit" + (this.valTitoli.length-1) + this.tag);
	cella.aggiungiStile(new Stile('class',this.stileTitolo));
	return cella;
}

Tabella.prototype.vuota=function (){
	return this.dati.length==0;
}

Tabella.prototype.aggiungiCella=function (riga){
	var tipo='s';
	var decimali=0;
	if (this.rTit>0){
		var pos=this.dati[riga].celle.length;
		tipo=this.tipoCol[pos];
		decimali=this.decCol[pos];
	}
	return this.dati[riga].aggiungiCellaN(this.tag,riga,tipo,this.fr,decimali);
}

Tabella.prototype.aggiungiRiga=function (){
	var altri=(new getfObj(this.fr,this.tag + "infoi" + this.dati.length)).obj.innerHTML;
	var evento=(new getfObj(this.fr,this.tag + "infoe" + this.dati.length)).obj.innerHTML;
	//if (altri!="")
		altri=altri.split(",");
	//else
	//	altri=null;
	var riga=this.dati[this.dati.length]=new Riga(altri);
//	if (evento!=""){
		var eventi=evento.split("§");
		for(var i=0; i<eventi.length; i++){
			evento=eventi[i].split("@");
			riga.aggiungiEvento(new Evento(evento[0],evento[1]));
		}
//	}
	return riga;
}

Tabella.prototype.riga=function (indice){
	return this.dati[indice];
}

Tabella.prototype.gestTasti=function (e){
}

Tabella.prototype.nRighe=function (){
	return this.dati.length;
}

Tabella.prototype.toString=function (){
	var testo="<table id="+ this.tag + this.stile + " border=0 cellpadding=-1 cellspacing=-1 onFocus='Tabella.fuoco=" + this.variabile + ".variabile' onkeydown='" + this.variabile + ".gestTasti(event)'>"
	for ( var i in this.titoli)
		testo+= this.titoli[i];
	for ( var i in this.dati)
		testo+= this.dati[i];
	testo += "</table>";
	return testo;
}

Tabella.prototype.creaTabella=Tabella.prototype.crea=function (contenitore,frame){
	this.menu=new Array(this.tipoCol.length);
	for(var c=0,i=0; c<this.valTitoli.length; c++){
		if (this.valTitoli[c][1]){
			submenu=new SubMenu("__submenu"+ i + this.tag);
			submenu.setStile("submenuTab");
			submenu.addVoceClick('Filtro in base a selezione',this.variabile + ".filtroSelezione(" + this.valTitoli[c][2] + ")");
			submenu.addVoceClick('Filtro ad esclusione selezione',this.variabile + ".filtroEsclusione(" + this.valTitoli[c][2] + ")");
			submenu.addVoceClick('Filtro',this.variabile + ".attivaFiltro(" + this.valTitoli[c][2] + ")");
			submenu.addVoceClick('Ordinamento crescente',this.variabile + ".ordina(" + this.valTitoli[c][2] + ",'c')");
			submenu.addVoceClick('Ordinamento decrescente',this.variabile + ".ordina(" + this.valTitoli[c][2] + ",'d')");
			submenu.addVoceClick('Annulla filtro,ordinamento',this.variabile + ".annullaFiltro()");
			submenu.addVoceClick('Esporta dati',"window.open('/varie/CaricaDatiTabella.asp?tabella=" + this.variabile + "')");
			submenu.width=170;
			this.menu[i]=new Menu("__menu" + c + this.tag , this.variabile + ".menu[" + i + "]","menuTab","menuTabDis");
			this.menu[i].addSub(this.valTitoli[c][0],submenu);
			this.menu[i].setAssoluta(true);
			i++;
		}
	}
	try{
		this.fr=docFrame(frame);
		for(var c=0; c<this.tipoCol.length; c++){
			this.origCol[c]=(new getfObj(this.fr,this.tag + "m" + c).obj).innerHTML.replace(/&amp;/g,"&");
		}
		this.dati=new Array();
		this.paginaCorrente=parseInt((new getfObj(this.fr,this.tag + "pc")).obj.innerHTML);
		this.pagineTotali=parseInt((new getfObj(this.fr,this.tag + "pt")).obj.innerHTML);
		this.primoRecord=parseInt((new getfObj(this.fr,this.tag + "pr")).obj.innerHTML);
		this.totRecord=parseInt((new getfObj(this.fr,this.tag + "ptr")).obj.innerHTML);
		var righe=parseInt((new getfObj(this.fr,this.tag + "TotRighe")).obj.innerHTML);
		for(var r=0; r<righe; r++){
			this.aggiungiRiga();
			for(var c=0; c<this.tipoCol.length; c++){
				this.aggiungiCella(r);
			}
		}
	}
  catch(e){};
  	
	this.contenitore=contenitore;
	this.contenitore.innerHTML=this.intestazione + "<div id='div" + this.tag + "' class='" + this.dimTab + "'>" + this + "</div>";
	if(this.paginaCorrente!=-1){
		var selPagina="<Select class='selPagina' value='" + this.paginaCorrente + "' onChange='" + this.variabile + ".spostaPagina(this)'>";
		for (i=1; i<=this.pagineTotali; i++)
			selPagina+="<option value='" + i + "'" + ((i==this.paginaCorrente)?" SELECTED ":"") + ">" + i + "</option>";
		selPagina+="</select>";
		this.contenitore.innerHTML+="<div id='__pieTabella' class='" + this.stilePiePag + "'>"
			+"<span class='numRec'>" + this.primoRecord + "-" + (this.primoRecord+this.dati.length -1) + " (di " +  this.totRecord + ")   " + "</span>"
			+"<span class='titPiePagina'>&nbsp;righe per pagina</span>"
			+"<input class='recPerPagina' type='text' value='" + this.dati.length + "' text-align='2' size='3' maxLength='3' onchange='" + this.variabile + ".cambiatotRighe(this)'>"
			+ "<span class='titPiePagina'>vai a pagina</span>" +  selPagina
			+"<span class='sposta' onClick='" + this.variabile + ".spostaPagina(\"p\")' title='prima pagina'>«</span>"
			+"<span class='sposta' title='pagina precedente' onClick='" + this.variabile + ".spostaPagina(\"i\")'>&lt;</span>"
			+"<span class='sposta' title='pagina successiva' onClick='" + this.variabile + ".spostaPagina(\"a\")'>&gt;</span>"
			+"<span class='sposta' title='ultima pagina' onClick='" + this.variabile + ".spostaPagina(\"u\")'>»</span>"
			+ "</div>";
	}
	this.tabe=(new getObj(this.tag)).obj;
	for ( var r=0; r<this.tabe.rows.length; r++)
		for ( var c=0; c<this.tabe.rows[r].cells.length; c++)
			this.tabe.rows[r].cells[c].style.emptyCells="show";
	for(var c=0,i=0; c<this.valTitoli.length; c++)
		if (this.valTitoli[c][1]){
			if (this.origCol[c]!=""){
				this.menu[i].crea("__tit" + c + this.tag);
				i++;
			}
			else
				(new getObj("__tit" + c + this.tag)).obj.innerHTML=this.valTitoli[c][0];
		}
		else
			(new getObj("__tit" + c + this.tag)).obj.innerHTML=this.valTitoli[c][0];
	var div=(new getObj("div" + this.tag)).obj;
	var larg=0;
	if(Tabella.fuoco==this.variabile)
		this.tabe.focus();
	if (this.tabe.offsetWidth<this.widthMin){
		this.tabe.style.width=String(this.widthMin) + "px";
		larg=this.widthMin;
	}
	else if (this.tabe.offsetWidth>this.widthMax){
		this.tabe.style.width=String(this.widthMax) + "px";
		larg=this.widthMax;
	}
	if (this.tabe.style.width==""){
		this.contenitore.style.width=(this.tabe.offsetWidth ) + "px";
			larg=this.tabe.offsetWidth;
		//div.style.width=this.tabe.offsetWidth + "px";
	}
	else{
		this.contenitore.style.width=(larg) + "px";
		div.style.width=(larg) + "px";
	}
}

Tabella.prototype.cambiatotRighe=function (campo){
	try{
		var form=(new getfObj(this.fr,"filtro")).obj;
		if (campo.value!='' && !isNaN(parseInt(campo.value)))
			form.sizePagina.value=campo.value;
		else
			return;
		form.filtro.value="undefined";
		form.submit();
	}
	catch(e){}
}

Tabella.prototype.spostaPagina=function (tipo){
	try{
		var form=(new getfObj(this.fr,"filtro")).obj;
		if (typeof tipo=='object')
			form.pagina.value=tipo.options[tipo.selectedIndex].value;
		else if (tipo=='p' && this.paginaCorrente>1)
			form.pagina.value=1;
		else if (tipo=='a' && this.paginaCorrente<this.pagineTotali)
			form.pagina.value=this.paginaCorrente + 1;
		else if (tipo=='i' && this.paginaCorrente>1)
			form.pagina.value=this.paginaCorrente - 1;
		else if (tipo=='u' && this.paginaCorrente<this.pagineTotali)
			form.pagina.value=this.pagineTotali;
		else
			return;
		if (form.pagina.value!="undefined")
			form.filtro.value="undefined";
		form.submit();
	}
	catch(e){}
}

Tabella.prototype.info=function (riga,indice){
	return eval(this.dati[riga].info(indice));
}

Tabella.prototype.getStileCella=function (pos,col){
	return this.dati[pos].celle[col].getNomeStile();
}

Tabella.prototype.cambiaStileCella=function (pos,col,stile){
	this.tabe.rows[pos+this.rTit].cells[col].className=stile;
	this.dati[pos].celle[col].setStile(new Stile(this.dati[pos].celle[col].getTipoStile(),stile));
}

Tabella.prototype.cambiaStileCelleRiga=function (pos,stile){
	for ( var i=0; i<this.dati[pos].celle.length; i++)
		this.tabe.rows[pos+this.rTit].cells[i].className=stile;
}

Tabella.prototype.getCol=function (){
	return this.dati[0].nCelle();
}

Tabella.prototype.scarica=function (){
	var dati="";
	for ( var r=0; r<this.dati.length; r++){
		var datiRiga="";
		for ( var c=0; c<this.dati[r].celle.length; c++)
			datiRiga+= "§" + this.dati[r].celle[c].getValore();
		dati += "¶" + datiRiga.substring(1);
	}
	return dati.substring(1);
}
