//HtmlTable.js
//manages the display of a Table object
//needs: table.js, dynapi.js, cookiesArray.js
//(c)2001 A.M.C. SoftWorks

function HtmlTable(tableClass,drawObjId,cookieName)
{
	this.tableClass=tableClass
	this.tableInit()
	this.tableArray=tableClass.array||[]
	this.drawObjId=drawObjId
	this.cookieName=cookieName
	this.style=[]
	this.buttons=[]
	this.modified=false
	this.preload=[]
}
HtmlTable.prototype.tableInit=function()
{
	if(this.tableClass.initializer)
		this.table=new this.tableClass(this.tableClass.initializer)
	else
		this.table=new this.tableClass()
}
HtmlTable.prototype.toHTML=function()
{
	var sHtml="<table border=0 cellspacing=1 cellpadding=0 bgcolor=\"#DDDDDD\">\n"
	sHtml+="<tr>\n"
	for(var j=0;j<this.style.length;j++)
		sHtml+="<td class="+this.style[j].css_h+"H width="+this.style[j].width+">"+this.style[j].get_h+"</td>\n"
	sHtml+="</tr>\n"
	for(var i=0;i<this.table.data.length;i++){
		var valore=this.table.data[i]
		sHtml+="<tr>\n"
		for(var j=0;j<this.style.length;j++){
			var v=eval("valore."+this.style[j].get_r)
			if (v==null) v="-"
			sHtml+="<td class="+this.style[j].css_r+(i%2?"O":"E")+">"+v+"</td>\n"
		}
		sHtml+="</tr>\n"
	}
	sHtml+="</table>\n"
	return sHtml
}
HtmlTable.prototype.redraw=function()
{
	document.getElementById(this.drawObjId).innerHTML=this.toHTML()
	for(var i=0;i<this.preload.length;i++){
		var p=this.preload[i]
		document.getElementById(p.imgId+p.rowId).src=HtmlTable.imgs[p.imgId].src
	}
	this.preload=[]
}
HtmlTable.prototype.addRow=function(obj)
{
	this.modified=true
	this.table.ins(obj)
	this.redraw()
}
HtmlTable.prototype.delRow=function(id)
{
	this.modified=true
	this.table.del(id)
	this.redraw()
}
HtmlTable.prototype.save=function()
{
	//Elimina cookie
	DynAPI.cookies.deleteCookie(this.cookieName+"."+this.table.getKeyValue())
	//azioni da eseguire prima del salvataggio
	this.table.prepareSave()
	//Creazione HTML per la finestra di salvataggio
	var sHtml=""
	sHtml+="<html>\n"
	sHtml+="<head>\n"
	sHtml+="<title>Salvataggio in corso...</title>\n"
	sHtml+="</head>\n"
	sHtml+="<body>\n"
	sHtml+="<form action=\""+this.saveAction+"\" method=POST>\n"
	sHtml+="<input type=hidden name=\"K_UTENTE\" value="+visitor.K_UTENTE+">\n"
	for(var i=0;i<this.table.keyArray.length;i++){
		sHtml+="<input type=hidden name=\""+this.table.keyArray[i]+"\" value=\""+this.table[this.table.keyArray[i]]+"\">\n"
	}
	sHtml+="<input type=hidden name=\"length\" value=\""+this.table.data.length+"\">\n"
	//se la tabella è vuota, il salvataggio ne causerà l'eliminazione
	for(var i=0;i<this.table.data.length;i++){
		for(var j=0;j<this.table.pivotArray.length;j++){
			sHtml+="<input type=hidden name=\""+this.table.pivotArray[j]+"_"+i+"\" value=\""+this.table.data[i][this.table.pivotArray[j]]+"\">\n"
		}
	}
	sHtml+="</form>\n"
	sHtml+="</body>\n"
	sHtml+="</html>\n"
	//Apertura finestra di salvataggio
	var saveWin=window.open("","saveWin","dependent=yes,directories=no,height=50,width=250,hotkeys=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no")
	saveWin.document.open()
	saveWin.document.write(sHtml)
	saveWin.document.close()
	//invio del form con i dati
	saveWin.document.forms[0].submit()
}
HtmlTable.prototype.load=function(keyValue)
{
	this.tableInit();
	var sCookie=DynAPI.cookies.readCookie(this.cookieName+"."+keyValue);
	if(sCookie){
		this.table.fromString(sCookie,this.table.dataClass);
		this.modified=true;
	}
	else{
		for(var i=0;i<this.tableArray.length;i++){
			if(this.tableArray[i].getKeyValue()==keyValue){
				this.table=this.tableArray[i];
				this.modified=false;
				break;
			}
		}
	}
	this.redraw();
}
HtmlTable.prototype.saveCookie=function()
{
	if(this.modified){
		var iCookie=this.table.getKeyValue()
		if(this.table.data.length>0){
			var sCookie=this.table.toString(this.table.keyArray,this.table.pivotArray)
			DynAPI.cookies.saveCookie(this.cookieName+"."+iCookie,sCookie,1)
		}
		else{
			DynAPI.cookies.deleteCookie(this.cookieName+"."+iCookie)
		}
	}
}
HtmlTable.prototype.deleteCookie=function()
{
	DynAPI.cookies.deleteCookie(this.cookieName+"."+this.table.getKeyValue())
}
HtmlTable.imgs={
	sel:new Image(8,8),
	piu:new Image(8,8),
	meno:new Image(8,8),
	dettagli:new Image(8,8)
}
HtmlTable.imgs.sel.src="../img/sel.gif"
HtmlTable.imgs.piu.src="../img/piu.gif"
HtmlTable.imgs.meno.src="../img/meno.gif"
HtmlTable.imgs.dettagli.src="../img/dettagli.gif"
function ColumnStyle(get_h,get_r,css_h,css_r,width)
{
	this.get_h=get_h
	this.get_r=get_r
	this.css_h=css_h
	this.css_r=css_r
	this.width=width
}
function RowButton(table,name,imgId,callback)
{
	this.table=table
	this.name=name
	this.imgId=imgId
	this.callback=callback
}
RowButton.prototype.display=function(iRowID)
{
	this.table.preload[this.table.preload.length]={rowId:iRowID,imgId:this.imgId}
	return "<a onClick=\"javascript:"+this.callback+"('"+iRowID+"')\"><img id="+this.imgId+iRowID+" width="+HtmlTable.imgs[this.imgId].width+" height="+HtmlTable.imgs[this.imgId].height+" alt=\""+this.name+"\" border=0></a>\n"
}

