
// ##############  dataColumn definition ####################

				
function dataColumn(name,width)
{
  this.name = name;
	this.width = width;
	return this;
}

// ##############  dataTable definition ####################


dataTable.prototype.OnDataReceived = OnDataReceived;
dataTable.prototype.FillRow = FillRow;
dataTable.prototype.FillCell = FillCell;
dataTable.prototype.GetData = GetData;
dataTable.prototype.Clear = Clear;
dataTable.prototype.NextPage = NextPage;
dataTable.prototype.PrevPage = PrevPage;
dataTable.prototype.FirstPage = FirstPage;
dataTable.prototype.LastPage = LastPage;
dataTable.prototype.UpdateLinks = UpdateLinks;

function dataTable(rowCount, columns, name)
{
  this.name = name;
	this.id = name;
	this.columns = columns;
	this.RecordCount = rowCount;
	this.From = 1;
	this.By = 10;
	this.http = getHTTPObject();
	this.TmpFrom = -1;
	this.CellStyle = "";
	this.CellStyleAlternate = "";
	this.GetDataUrl = "async_DBTable_GetData.cfm";
	return this;
}


function OnDataReceived()
{
  if(this.http.readyState == 4)
	{
	  if(this.http.status == 200)
		{
		
		  this.Clear();
					
		  this.From = this.TmpFrom;			
			this.UpdateLinks();
			
		  var xmlDoc = this.http.responseXML;
			var DataItems = xmlDoc.getElementsByTagName("DataItem");
			for(var idx = 0; idx < DataItems.length; idx++)
			{
			  this.FillRow(DataItems.item(idx));
			}
		}
		else
		{
		  alert("Error receiving data. HTTP Error Code: " + this.http.status);
		}
	}		  				
}

function FillRow(dataRow)
{
  var tableObj = document.getElementById("table_"+this.id);
	var rowObj;
	rowObj = tableObj.insertRow(-1);			
	
	for(columnIdx = 0; columnIdx < this.columns.length; columnIdx++)
	{ 
    var columnData;
    try
    {
  	  columnData = dataRow.getElementsByTagName(this.columns[columnIdx].name).item(0).firstChild.data;
    }
    catch(err)
    {
      columnData = "";
    }
		var ColumnWidth = this.columns[columnIdx].width;
		this.FillCell(rowObj, columnData, columnIdx, ColumnWidth);
	}	
	
	if(rowObj.rowIndex % 2)
	{
	  rowObj.className = this.CellStyle;
	}
	else
	{
	  rowObj.className = this.CellStyleAlternate;
  }
}

function FillCell(rowObj, columnData, cellIdx, ColumnWidth)
{
  var cellObj = rowObj.insertCell(cellIdx);
	cellObj.innerHTML = unescape(columnData);
	cellObj.width = ColumnWidth;
}

function GetData(tFrom)
{
//  prompt("",this.GetDataUrl+"?Id="+this.id+"&from="+tFrom+"&by="+this.By);
  this.http.open("GET",this.GetDataUrl+"?Id="+this.id+"&from="+tFrom+"&by="+this.By,true);
	this.http.onreadystatechange = eval(this.name+"_dataReceived");
	this.http.send(null);
}

function Clear()
{
  var tableObj = document.getElementById("table_"+this.id);
	for(var idx = tableObj.rows.length - 1; idx > 0; idx--)
	{
	  tableObj.deleteRow(idx);
	}
}


function NextPage()
{
  var From = this.From + this.By;
	this.TmpFrom = From;
	this.GetData(From);
}

function PrevPage()
{
  var From = this.From - this.By;
	if(From < 1)
	{
	  From = 1;
	}
	this.TmpFrom = From;
	this.GetData(From);
}

function FirstPage()
{
  this.TmpFrom = 1;
	this.GetData(1);
}

function LastPage()
{
  var From = Math.floor((this.RecordCount - 1)/this.By)+1;
	From = (From-1) * this.By + 1;
  this.TmpFrom = From;
	this.GetData(From);
}

function UpdateLinks()
{
  var objTopLinks = document.getElementById("TopLinks_"+this.id);
	var objBottomLinks = document.getElementById("BottomLinks_"+this.id);
	
	var PageCount = Math.floor((this.RecordCount - 1)/this.By)+1;
	var CurrentPage = Math.floor((this.From - 1)/this.By)+1;
	
	var LinksHTML = "";
	
	LinksHTML += "Page " + CurrentPage + " of " + PageCount + "<BR>";
		
	if(this.From > 1)
	{
	  LinksHTML += "<a href=\"javascript: " + this.id + ".FirstPage()\">First</a> | ";
		LinksHTML += "<a href=\"javascript: " + this.id + ".PrevPage()\"><strong>&nbsp;&lt;&lt;&nbsp;Previous</strong>&nbsp;</a>  | ";
	}
	else
	{
	  LinksHTML += "First | <strong>&nbsp;&lt;&lt;&nbsp;Previous</strong>&nbsp; |";
	}
	
	if(this.From + this.By <= this.RecordCount)
	{
	  LinksHTML += "<a href=\"javascript: " + this.id + ".NextPage()\"><strong>&nbsp;Next&nbsp;&gt;&gt;&nbsp;</strong></a> | ";
	  LinksHTML += "<a href=\"javascript: " + this.id + ".LastPage()\">Last</a>";		
	}
	else
	{
	  LinksHTML += "<strong>&nbsp;Next&nbsp;&gt;&gt;&nbsp;</strong> | Last";
	}
	
	objTopLinks.innerHTML = LinksHTML;
	objBottomLinks.innerHTML = LinksHTML;
	
}




