
  /////////////////
 // CCursorBar.js
//
function CCursorBar(id, dataSource, tableDest, cls)
{	
	if ( IsEmpty(tableDest) ) tableDest = null;
	if ( IsEmpty(cls) ) cls = "CCursorBar";

	this.id = id;
	this.cls = cls;
	this.firstPage = 0;
	this.currPage = 0;
	this.lastPage = 0;

	this.dataSource = dataSource;
	this.dataSource.onquery = function(){};
	this.dataSource.onquerySubject = this;

	this.tableDest = tableDest;
	
	this.hint = true;
	this.hintFirst = "Prima pagina";
	this.hintPrev = "Pagina precedente";
	this.hintGoto = "Vai alla pagina selezionata";
	this.hintPage = "Pagina selezionata";
	this.hintNext = "Pagina successiva";
	this.hintLast = "Ultima pagina";
	this.hintIndex = "Numero di pagine";
}

//
// CCursorBar_callback
//
function CCursorBar_callback()
{
	this.lastPage = this.dataSource.pageCount;
	if ( this.lastPage > 0 )
		this.firstPage = 1;
	else
		this.firstPage = 0;
	this.currPage = this.dataSource.pageNumber;

	this.refresh();

	if ( this.tableDest != null )
		this.tableDest.setHTML(this.dataSource.responseHTML("Data"));
	else
		this.onquery();
}
CCursorBar.prototype.callback = CCursorBar_callback;

//
// CCursorBar_writeHTML
//
function CCursorBar_writeHTML()
{
	var btn;
	var id = this.id;
	var cls = this.cls;

	var HTML = '<FORM CLASS='+ cls +'-clsFORM ID='+ id +'_idFORM STYLE="margin:0;" METHOD="post" ACTION="" TARGET="_self"';
	HTML += ' onsubmit="'+ id +'.page(); return false;"';
	if ( IE )
		HTML += ' onkeydown="'+ id +'.onkeydown(window.event.keyCode);"';
	else
		HTML += ' onkeydown="'+ id +'.onkeydown(event.which);"';

	HTML += '>\n<TABLE CLASS='+ cls +'-clsTABLE ID='+ id +'_idTABLE CELLSPACING=0 CELLPADDING=0>';
	HTML += '\n<TR CLASS='+ cls +'-clsTR ID='+ id +'_idTR>'; 
	
	HTML += '\n<TD CLASS="'+ cls +'-clsTD-Button"><INPUT TYPE=button CLASS="'+ cls +'-clsBUTTON clsImgBtn clsBkImgFirst" TITLE="'+ this.hintFirst +'" VALUE="    " onclick="'+ id +'.first();"></TD>';
	HTML += '\n<TD CLASS="'+ cls +'-clsTD-Button"><INPUT TYPE=button CLASS="'+ cls +'-clsBUTTON clsImgBtn clsBkImgPrev" TITLE="'+ this.hintPrev +'" VALUE="    " onclick="'+ id +'.prev();"></TD>';
	HTML += '\n<TD CLASS="'+ cls +'-clsTD-Button"><INPUT TYPE=button CLASS="'+ cls +'-clsBUTTON clsImgBtn clsBkImgGoto" TITLE="'+ this.hintGoto +'" VALUE="    " onclick="'+ id +'.page();"></TD>';
	HTML += '\n<TD CLASS="'+ cls +'-clsTD-Page"><INPUT TYPE=text CLASS='+ cls +'-clsTEXT-Page ID='+ id +'_idTEXT_Page TITLE="'+ this.hintPage +'" VALUE=""></TD>';
	HTML += '\n<TD CLASS="'+ cls +'-clsTD-Button"><INPUT TYPE=button CLASS="'+ cls +'-clsBUTTON clsImgBtn clsBkImgNext" TITLE="'+ this.hintNext +'" VALUE="    "  onclick="'+ id +'.next();"></TD>';
	HTML += '\n<TD CLASS="'+ cls +'-clsTD-Button"><INPUT TYPE=button CLASS="'+ cls +'-clsBUTTON clsImgBtn clsBkImgLast" TITLE="'+ this.hintLast +'" VALUE="    "  onclick="'+ id +'.last();"></TD>';
	HTML += '\n<TD CLASS="'+ cls +'-clsTD-Index" ID='+ id +'_idTD_Index WIDTH=100% NOWRAP TITLE="'+ this.hintIndex +'" onselectstart="return false;" ondragstart="return false;">&nbsp;</TD>';
	
	HTML += '\n</TR></TABLE></FORM>\n';

	document.write(HTML);
}
CCursorBar.prototype.writeHTML = CCursorBar_writeHTML;

function CCursorBar_refresh()
{
	this.currPage = this.dataSource.pageNumber;
	this.lastPage = this.dataSource.pageCount;

	target = document.getElementById(this.id+"_idTEXT_Page");
	if ( !IsEmpty(target) )
		target.value = this.currPage;
	target = document.getElementById(this.id+"_idTD_Index");
	if ( !IsEmpty(target) )
		target.innerHTML = "&nbsp; " + this.currPage + " / " + this.lastPage;	
}
CCursorBar.prototype.refresh = CCursorBar_refresh;

function CCursorBar_first()
{
	this.currPage = 1;
	this.dataSource.query(1);
}
CCursorBar.prototype.first = CCursorBar_first;

function CCursorBar_prev()
{
	this.currPage--;
	if (this.currPage < 1) 
		this.currPage = 1;
	this.dataSource.query(this.currPage);
}
CCursorBar.prototype.prev = CCursorBar_prev;

function CCursorBar_page(page)
{	
try
{
	var askPage;

	if ( arguments.length > 0 )
	{
		askPage = page;
		if ( askPage < this.firstPage )
			askPage = this.firstPage;
		if ( askPage > this.lastPage )
			askPage = this.lastPage;
	}
	else
	{
		var target = document.getElementById(this.id+"_idTEXT_Page");
		if ( IsEmpty(target) )
			askPage = this.firstPage;
		else
			askPage = parseInt(target.value);
	}
	if ( isNaN(askPage) || askPage < this.firstPage || askPage > this.lastPage )
	{
		window.alert("Pagina non disponibile");
		return; 
	}
	else
		this.currPage = askPage;	
	this.dataSource.query(askPage);
}
catch(e)
{
	CatchAlert("CCursorBar.page()");
}
}
CCursorBar.prototype.page = CCursorBar_page;

function CCursorBar_next()
{
	if ( this.lastPage <= 0 )
		return;
	this.currPage++;
	if (this.currPage > this.lastPage) 
		this.currPage = this.lastPage;
	this.dataSource.query(this.currPage);
}
CCursorBar.prototype.next = CCursorBar_next;

function CCursorBar_last()
{
	if ( this.lastPage <= 0 )
		return;
	this.currPage = this.lastPage;
	this.dataSource.query(this.currPage);
}
CCursorBar.prototype.last = CCursorBar_last;

function CCursorBar_eof()
{
	this.currPage = 0;
	this.dataSource.query(this.currPage);
}
CCursorBar.prototype.eof = CCursorBar_eof;

function CCursorBar_onkeydown(keyCode)
{
	switch ( keyCode )
	{
		case 36:	// Home
			this.first();
			break;

		case 33:	// PgUp
			this.prev();
			break;

		case 34:	// PgDn
			this.next();
			break;

		case 35:	// End
			this.last();
			break;
	}
}
CCursorBar.prototype.onkeydown = CCursorBar_onkeydown;
