function deleteTableRow(url) {
	if (confirm("Sei sicuro di voler cancellare l'articolo ?")) {
		location.assign(url);
	}
}

function Paginator() {
	this.rowPerPage = 25; // Numero di righe per pagina
	this.maxlinks = 10; // Numero massimo di link x pagina
	
	this.totalRecords = 300; // Numero totale di records

	this.offset = 0;
	this.actPage = 1;
	this.totalPage = 20;
	this.thisFunctionName = "";

	this.setPaginatorImagePath = setPaginatorImagePath; // setPaginatorImagePath(imgpath) 
	this.paginatorImagePath = ''; 
	function setPaginatorImagePath(imgpath) {
		this.paginatorImagePath = imgpath; 
	}
	
	this.goPage = goPage;
	function goPage(n) {
		this.actPage = n;
		this.offset = (n-1)*this.rowPerPage;
	}
	
	this.displayPaginator = displayPaginator;
	function displayPaginator() {
	
		// Calcola i vari parametri
		this.totalPage = Math.floor(this.totalRecords / this.rowPerPage);
		if ((this.totalRecords % this.rowPerPage) > 0) this.totalPage++;
		
		html = "";
		if (this.totalRecords > 0) {
			html += '<table border=0 width=100% style="border-top: solid silver 1px; border-bottom: solid silver 1px; ">';
			html += '<tr>';
			if ((this.actPage-1) > 0) {
				html += '		<td width=3% class=base><a href="javascript:' + 
					this.thisFunctionName + '.ShowPage(' + (this.actPage-1) + ');"' +
					'><img alt="Pagina precedente" border=0 src="' + this.paginatorImagePath + '_imgbase/back.gif"></a></td>';
			}
			html += '<td align=center class=base width=29% >';
			
			// calcola quali pagine devono essere visualizzate
			/*lf = this.actPage;
			lt = this.actPage + this.maxlinks;
			if (lt > this.totalPage) {
				lt = this.totalPage;
				lf = lt - this.maxlinks + 1;
				if (lf < 1) lf = 1;
			}*/
			lf = this.actPage - (this.maxlinks / 2);
			if (lf < 1) lf = 1;
			lt = lf + this.maxlinks;
			if (lt > this.totalPage) {
				lt = this.totalPage;
				lf = lt - this.maxlinks + 1;
				if (lf < 1) lf = 1;
			}
			
			cnt=0;
			//for (p=this.actPage; p<=this.totalPage; p++) {
			for (p=lf; p<=lt; p++) {
				if (p == this.actPage) {
					html += '|&nbsp;&nbsp;<b>' + p + '</b>&nbsp;&nbsp;';
				} else {
					html += '|&nbsp;<a class=red href="javascript:' + 
					this.thisFunctionName + '.ShowPage(' + p + ');">' + p + '</a>&nbsp;';
				}
				if (++cnt >= this.maxlinks) break; // massimo 10 link a pagine
			}
			html += '		</td>';
			if ((this.actPage+1) <= this.totalPage) {
				html += '		<td width=3% class=base><a href="javascript:' + 
					this.thisFunctionName + '.ShowPage(' + (this.actPage+1) + ');"><img alt="Pagina successiva" border=0 src="' + this.paginatorImagePath + '_imgbase/next.gif"></a></td>';
			}
			html += '		<td width=26% class=base align=center>Pagina: <b>' + this.actPage + '</b> di <b>' + this.totalPage + '</b></td>';
			html += '		<td width=32% class=base align=right>Records Totali: <b>' + this.totalRecords + '</b></td>';
			html += '	</tr>';
			html += '</Table>';
		}
		return html;
	}
}

function ajaxTable(divname) {

	// Public methods
	this.setJSON = setJSON; // setJSON(json)
	this.displayTable = displayTable; // displayTable()
	this.setOnClick = setOnClick; // setOnClick(fToCall)
	this.AjaxCall = AjaxCall; // AjaxCall(fCalled, dataToSend, handler)
	this.getHTTPReq_readyState = getHTTPReq_readyState; // getHTTPReq_readyState()
	this.displayLoading = displayLoading; // displayLoading();
	this.ShowPage = ShowPage; // ShowPage(n)
	this.setPaginator = setPaginator; // setPaginator(fn, rpp, ml)
	this.setAjaxURL = setAjaxURL; // setAjaxURL(url)
	this.setPaginatorPosition = setPaginatorPosition; // setPaginatorPosition(p)
	this.setTableStyle = setTableStyle; // setTableStyle(s)
	this.setProperty = setProperty; // setProperty(p, v)
	this.setDeleteButton = setDeleteButton; // setDeleteButton(url)
	this.setPaginatorImagePath = setPaginatorImagePath; // setPaginatorImagePath(imgpath) 

	// Private data
	this.AjaxCall_ = AjaxCall_; // usata internamente dal paginatore
	this.AjaxUrl = "_stuff/Ajaxpage.asp";
	this.PaginatorPosition = 2; // 0=nessun paginatore, 1=top, 2=bottom (default)
	this.tableStyle = 1; // 1=tabella normale, 2=elenco
	this.deleteButton = '';
	this.paginatorImagePath = ''; 
	
	// Per la visualizzazione con lo stile elenco
	this.props = new blockProperties();
	this.props.add('RowHeader', '');
	this.props.add('RowFooter', '');
	this.props.add('Cell1Header', '');
	this.props.add('Cell1Footer', '');
	this.props.add('Cell2Header', '');
	this.props.add('Cell2Footer', '');
	this.props.add('Cell3Header', '');
	this.props.add('Cell3Footer', '');
	this.props.add('Cell4Header', '');
	this.props.add('Cell4Footer', '');

	this.paginator = new Paginator();
	this.div = divname;
	this.header = new Array();
	this.jsonStr = "";
	this.jsonData;
	this.oddeven = false;
	this.onClickFunction = "";
	this.thisFunctionName = "";
	this.callURI = "";
	
	// usate dal paginatore per reinoltrare la richiesta
	this.pag_fCalled = "";
	this.pag_dataToSend = "";
	this.pag_handler = null;
	
	function setPaginatorImagePath(imgpath) {
		this.paginatorImagePath = imgpath; 
		this.paginator.setPaginatorImagePath(imgpath);
	}
	
	this.getCallURI = getCallURI;
	function getCallURI() {
		return this.callURI;
	}
	
	this.getProperty = getProperty;
	function getProperty(p) {
		return this.props.get(p);
	}
	
	function setProperty(p, v) {
		return this.props.set(p, v);
	}
	
	function setDeleteButton(s) {
		this.deleteButton = s;
	}
	
	function setTableStyle(s) {
		this.tableStyle = s;
	}
	
	function setPaginatorPosition(p) {
		this.PaginatorPosition = p;
	}
	
	function setAjaxURL(url) {
		this.AjaxUrl = url;
	}
	
	function setPaginator(fn, rpp, ml) {
		this.paginator.thisFunctionName = fn;
		this.paginator.rowPerPage = rpp;
		this.paginator.maxlinks = ml;
	}
	
	function ShowPage(n) {
		this.paginator.goPage(n);
		this.AjaxCall_(this.pag_fCalled, this.pag_dataToSend, this.pag_handler);
	}
	
	this.aThttpreq;
	function AjaxCall(fCalled, dataToSend, handler) {
		this.pag_fCalled = fCalled;
		this.pag_dataToSend = dataToSend;
		this.pag_handler = handler;
		this.paginator.offset = 0;
		this.paginator.actPage = 1;
		this.AjaxCall_(fCalled, dataToSend, handler);
	}
	function AjaxCall_(fCalled, dataToSend, handler) {
		limit = " limit " + this.paginator.offset + "," + this.paginator.rowPerPage;
		this.displayLoading();
		try {
			if (fCalled != '') {
				this.callURI = this.AjaxUrl+"?MODE="+fCalled+dataToSend+"&limit="+limit+"&destDIV="+this.div;
				if (window.XMLHttpRequest) {
					this.aThttpreq = new XMLHttpRequest();
					this.aThttpreq.onreadystatechange = handler;
					this.aThttpreq.open("GET",this.callURI,true);
					this.aThttpreq.send(null);
				} else if (window.ActiveXObject) {
					this.aThttpreq = new ActiveXObject("Microsoft.XMLHTTP");
					if (this.aThttpreq) {
						this.aThttpreq.open("GET",this.callURI,true);
						this.aThttpreq.onreadystatechange = handler;
						this.aThttpreq.send();
					}
				}
			}
		} catch (e) {
			alert('Exception su aTAjaxCall()');
		}
	}
	
	function getHTTPReq_readyState() {
		return this.aThttpreq.readyState;
	}
	
	this.getResponseArray = getResponseArray;
	function getResponseArray() {
		var strRes;
		strRes=this.aThttpreq.responseText;
		strRes=strRes.replace("<complete>","");
		strRes=strRes.replace("<script>","");
		strRes=strRes.replace("</script>","");
		strRes=strRes.replace("</complete>","");
		return strRes;
	}

	this.addHead = addHead;
	function addHead(label) {
		this.header[this.header.length] = label;
	}

	this.setDIV = setDIV;
	function setDIV(divname) {
		this.div = divname;
	}

	this.getDIV = getDIV;
	function getDIV() {
		return this.div;
	}

	function setOnClick(fToCall) {
		this.onClickFunction = fToCall;
	}
	
	this.getHeaderCell = getHeaderCell;
	function getHeaderCell(label) {
		retval = '<td class="LIST_Header" align=left>' + label + '</td>';
		return retval;
	}
	
	this.getHeaderRow = getHeaderRow;
	function getHeaderRow() {
		retval = '<tr class="LIST_Header">';
		for (i=0; i<this.header.length; i++) {
			retval += this.getHeaderCell(this.header[i]);
		}
		retval += '</tr>';
		return retval;
	}
	
	this.getTableCell = getTableCell;
	function getTableCell(cell) {
		if (cell == "") cell = "&nbsp;";
		retval = '<td class="LIST_" align=left>' + cell + '</td>';
		return retval;
	}
	
	this.getTableRow = getTableRow;
	
	
	function getTableRow(cells, rowid) {
		retval = "";
		if (this.oddeven) {
			retval = '<tr class=LIST_Pari';
			retval += ' onmouseover="this.className=\'LIST_RowSel\'"';
			retval += ' onmouseout="this.className=\'LIST_Pari\'"';
			if ((this.onClickFunction != "") && (this.deleteButton == '')) retval += ' onclick="' + this.onClickFunction + '(\'' + rowid + '\');"';
			retval += ' style="cursor: hand;">';
			this.oddeven = false;
		} else {
			retval = '<tr class=LIST_Dispari';
			retval += ' onmouseover="this.className=\'LIST_RowSel\'"';
			retval += ' onmouseout="this.className=\'LIST_Dispari\'"';
			if ((this.onClickFunction != "") && (this.deleteButton == '')) retval += ' onclick="' + this.onClickFunction + '(\'' + rowid + '\');"';
			retval += ' style="cursor: hand;">';
			this.oddeven = true;
		}
		retval += cells;
		retval += '</tr>';
		return retval;
	}

	this.getTableHeader = getTableHeader;
	function getTableHeader() {
		retval = '<table width=100% cellspacing=0 border=0 Class=LIST_Table>';
		retval += this.getHeaderRow();
		return retval;
	}

	this.getTableFooter = getTableFooter;
	function getTableFooter() {
		retval = '</table>';
		return retval;
	}
	
	function setJSON(json) {
		var myData = {
			header : [
				{ label: "Data" },
				{ label: "Titolo" },
				{ label: "Sottotitolo" },
				{ label: "Abstaract" }
			],
			records : [
					{ col : [
				        { value: "2010-05-23 17:30:00" },
				        { value: "Questo è il titolo" },
				        { value: "Questo è il sottotitolo" },
				        { value: "Questo è il contenuto" }
			    	] },
					{ col : [
				        { value: "2010-05-23 17:30:00" },
				        { value: "Questo è il titolo" },
				        { value: "Questo è il sottotitolo" },
				        { value: "Questo è il contenuto" }
			    	] },
					{ col : [
				        { value: "2010-05-23 17:30:00" },
				        { value: "Questo è il titolo" },
				        { value: "Questo è il sottotitolo" },
				        { value: "Questo è il contenuto" }
			    	] }
		    ]
		};
		if (json == '') this.jsonStr = YAHOO.lang.JSON.stringify(myData);
		else this.jsonStr = json;
		//alert(this.jsonStr);
	}
	
	this.cleanHTML = cleanHTML;
	function cleanHTML(html) {
		document.getElementById("htmlCleaner").innerHTML = html;
		//divContainer = document.getElementById("htmlCleaner");
		txt = document.getElementById("htmlCleaner").innerText;
		if (txt == undefined) txt = document.getElementById("htmlCleaner").textContent;
		txt = txt.replace(/@CR@/g, "<br>");
		return txt;
	}

	function displayTable() {

		// Recupera i dati da JSON
		
		htmlRet = "";
		this.header = new Array();
		this.oddeven = false;
		try {
		    this.jsonData = YAHOO.lang.JSON.parse(this.jsonStr);
		}
		catch (e) {
		    alert("Invalid JSON data !");
		}
		    
		try {
		
			this.paginator.totalRecords = this.jsonData.totalrecords;

		    // Set Header (skip first column)
			if (this.deleteButton != '') this.addHead('Azione');
		    for (i=1; i<this.jsonData.header.length; i++) this.addHead(this.jsonData.header[i].label);

			// Paginatore
			if (this.PaginatorPosition == 1) htmlRet += this.paginator.displayPaginator() + "<br>";
		    
		    switch (this.tableStyle) {
		    	case 1 : // Tabella normale
				    // Draw table
				    htmlRet += this.getTableHeader();

				    // Draw data records
				    for (r=0; r<this.jsonData.records.length; r++) {
				    	cells = "";
				    	rID = "0";
			    		for (c=0; c<this.jsonData.records[r].col.length; c++) {
					    	if (c == 0) {
								rID = '' + this.jsonData.records[r].col[c].value;
								
								if (this.deleteButton != '') {
									htmlCell = '';
									if (this.onClickFunction != '') htmlCell += '<a href="javascript:' + this.onClickFunction + '(\'' + rID +
										'\');">' +
										'<img src="' + this.paginatorImagePath + '_imgbase/modifica.gif" alt="Modifica..."/ border=0>' +
										'</a>&nbsp;';
									htmlCell += '<a href="javascript:deleteTableRow(\'' +
										this.deleteButton.replace(/@@ID@@/g, rID) +
										'\');">' +
										'<img src="' + this.paginatorImagePath + '_imgbase/cancella.gif" alt="Cancella..."/ border=0>' +
										'</a>';
									cells += this.getTableCell(htmlCell);
								}
							}
							else cells += this.getTableCell(cleanHTML(this.jsonData.records[r].col[c].value));
					    }
					    htmlRet += this.getTableRow(cells, rID);
				    }

					// Disegna la tabella
				    htmlRet += this.getTableFooter();
					break;

				case 2 : // Elenco
				    // Draw data records
				    for (r=0; r<this.jsonData.records.length; r++) {
				    	cells = "";
				    	rID = "0";
					    htmlRet += this.getProperty('RowHeader');
			    		for (c=0; c<this.jsonData.records[r].col.length; c++) {
					    	if (c == 0) rID = '' + this.jsonData.records[r].col[c].value;
					    	else cells += this.getProperty('Cell' + c + 'Header') +
					    		cleanHTML(this.jsonData.records[r].col[c].value) +
					    		this.getProperty('Cell' + c + 'Footer');
					    }
					    cells = cells.replace(/@@ID@@/g, rID);
					    htmlRet += cells + this.getProperty('RowFooter');
				    }
					break;
				case 3 : // Elenco
				    // Draw data records
				    //alert('ce passo')
				    for (r=0; r<this.jsonData.records.length; r++) {
				    	var color='#faf0e1';
				    	var bcolor='#e48f1b';
				    	var scolor='#875d22';
				    	
				    	var testo='PUBBLICATO SUL SITO';
				    	if (this.jsonData.records[r].col[5].value=='No'){color='#e2e2e2';bcolor='gray';scolor='#727272'; testo='<b>NON PUUBLICATO</b>';}
				    	htmlRet+='<table width=100% cellspacing=0 cellpadding=0 border=0 style="background-color:'+color+';border:1px solid '+bcolor+'; box-shadow: 2px 2px 2px '+scolor+'; -webkit-box-shadow:2px 2px 2px '+scolor+'; -moz-box-shadow: 2px 2px 2px '+scolor+';border-radius:8px; -moz-border-radius:8px; -webkit-border-radius:8px;;margin-bottom:10px; padding-top:3px;padding-top:3px">'
				    	htmlRet+='<tr>' 
				    	htmlRet+='<td style="font-size:16px"colspan=3 width=75%>' + this.jsonData.records[r].col[2].value + '</td>'
				    	htmlRet+='<td align=center colspan=2 style="font-size:13px" rowspan=2 width=25%><b>' + this.jsonData.records[r].col[3].value + '</b></td>'
				    	htmlRet+='</tr>'
				    	htmlRet+='<tr>' 
				    	htmlRet+='<td valign=top colspan=3 style="font-size:11px; border-bottom:1px solid silver">' + this.jsonData.records[r].col[10].value + '&nbsp</td>'
				    	htmlRet+='</tr>'
				    	htmlRet+='<tr>' 
				    	htmlRet+='<td valign=top rowspan=3 colspan=3 style="font-size:11px">' + cleanHTML(this.jsonData.records[r].col[15].value) + '&nbsp;' + cleanHTML(this.jsonData.records[r].col[16].value) + '&nbsp;</td>'
				    	htmlRet+='<td align=right style="font-size:10px;color:gray" width=10%>CANALE</td>'
				    	htmlRet+='<td style="font-size:12px;" width=15%>'+cleanHTML(this.jsonData.records[r].col[1].value)+'</td>'
				    	htmlRet+='</tr>'
				    	htmlRet+='<tr>' 
				    	htmlRet+='<td align=right style="font-size:10px;color:gray" width=10%>VISUALIZZA IN</td>'
				    	htmlRet+='<td style="font-size:12px;" width=15%>'+cleanHTML(this.jsonData.records[r].col[4].value)+'</td>'
				    	htmlRet+='</tr>'
				    	htmlRet+='<tr>' 
				    	htmlRet+='<td align=right style="font-size:10px;color:gray" width=10%>LINGUA</td>'
				    	htmlRet+='<td  style="font-size:12px;" width=15%>'+cleanHTML(this.jsonData.records[r].col[12].value)+'</td>'
				    	htmlRet+='</tr>'
				    	htmlRet+='<tr>' ;
				    	htmlRet+='<td align=right style="font-size:10px;color:gray" width=50% >'+testo+'</td>';
				    	htmlRet+='<td style="font-size:10px;color:gray" align=right>DAL</td>';
				    	htmlRet+='<td align=center style="font-size:11px;" >'+ this.jsonData.records[r].col[6].value +'</td>';
				    	htmlRet+='<td style="font-size:10px;color:gray" align=right>AL</td>';
				    	htmlRet+='<td  style="font-size:11px;" >'+ this.jsonData.records[r].col[7].value +'</td>';
				    	htmlRet+='</tr>';
				    	htmlRet+='<tr>' ;
				    	htmlRet+='<td align=right style="font-size:10px;color:gray" >ACCESSO AL CONTENUTO PROTETTO</td>';
				    	htmlRet+='<td style="font-size:10px;color:gray" align=right>SU WEB</td>';
				    	htmlRet+='<td align=center style="font-size:11px;" >'+ this.jsonData.records[r].col[8].value +'</td>';
				    	htmlRet+='<td style="font-size:10px;color:gray" align=right>SU IPHONE</td>';
				    	htmlRet+='<td style="font-size:11px;" >'+ this.jsonData.records[r].col[9].value +'</td>';
				    	htmlRet+='</tr>';
				    	htmlRet+='<tr>';
				    	htmlRet+='<td align=right style="font-size:10px;color:gray" colspan=2>QUESTO ARTICOLO PUO\' ESSERE COMMENTATO</td>';
				    	htmlRet+='<td align=center style="font-size:11px;" >'+ this.jsonData.records[r].col[13].value +'</td>';
				    	htmlRet+='<td colspan=2 align=right style="background-color:#efc78b;padding-top:2px; font-size:10px; color:#965b02">'

				    	htmlRet+='<img onclick="deleteTableRow(\'default.asp?Mode=ARTD&ID='
				    		+ this.jsonData.records[r].col[0].value 
				    		+'\');" border=0 align=absmiddle height=18 width=18 src="_imgbase/delete.png" style="cursor:pointer"> CANCELLA ';

				    	htmlRet+='<a href="Default.asp?Mode=ART&golang='+ this.jsonData.records[r].col[14].value 
				    		+'&ArtId='+ this.jsonData.records[r].col[0].value
				    		+'&goOnCommit=ART"><img border=0 align=absmiddle height=18 width=18  src="_imgbase/edit.png"></a> MODIFICA TESTO';

				    	htmlRet+='<a href="default.asp?Mode=ARTN&ID='
				    		+ this.jsonData.records[r].col[0].value 
				    		+'"><img border=0 align=absmiddle height=18 width=18  src="_imgbase/preferences.png"></a> PROPRIETA\'';
				    	
				    	
				    	htmlRet+='</td>'
				    	
				    	htmlRet+='</tr>'
				    				    	
				    	
					    htmlRet+= '</table>'
					    //htmlRet+=this.jsonData.records[r].col[3].value + '<br>'
					      
					    /*
			    		for (c=0; c<this.jsonData.records[r].col.length; c++) {
					    	if (c == 0) rID = '' + this.jsonData.records[r].col[c].value;
					    	else cells += this.getProperty('Cell' + c + 'Header') +
					    		cleanHTML(this.jsonData.records[r].col[c].value) +
					    		this.getProperty('Cell' + c + 'Footer');
					    }
					    cells = cells.replace(/@@ID@@/g, rID);
					    htmlRet += cells + this.getProperty('RowFooter');
					    */
				    }
					break;	
			}

			// Paginatore
			if (this.PaginatorPosition == 2) htmlRet += this.paginator.displayPaginator();
			document.getElementById(this.div).innerHTML = htmlRet;
		}
		catch (e) {
		    alert("Invalid access to data !");
		}
	}

	function displayLoading() {
		retval = '<img class="LOADING" src="' + this.paginatorImagePath + '_imgbase/loading.gif" alt="In caricamento..."/>';
		retval += '<div id="htmlCleaner" style="display:none"></div>';
		/*retval = "<table width=100% cellspacing=0 border=0 Class=LIST_Table>';
		retval += '<tr class="LIST_Header">';
		retval += '<td class="LIST_Header" align=left>In caricamento...</td>';
		retval += '</tr>';
		retval += '</table>';*/
		document.getElementById(this.div).innerHTML = retval;
	}
	
}

