function record(id,shape,color,clarity,cut,size,cert,discount,priceUSD,reserved)
{
	this.id=id;
	this.shape=shape;
	this.color=color;
	this.clarity=clarity;
	this.cut=cut;
	this.size=size;
	this.certificate=cert;
	this.discount=discount;
	this.reserved=reserved;
	this.priceUSD=priceUSD;
	//this.markup=markup;
}


function Records()
{
	this._allrecords = new Array();	
	this.Count = function()
	{
		return this._allrecords.length;
	}	
	this.Clear = function()
	{
		this._allrecords=null;
		this._allrecords = new Array();
	}
	this.Add = function(rec,pos)
	{
		this._allrecords[pos]=rec;
	}	
	this.Get = function(pos)
	{
		return this._allrecords[pos];
	}
}



function Grid(container,tbl,thumb)
{
	this.obj=tbl;
	this.container=container;
	this.topPos=0;
	this.visibleCount=21;
	this.mRecords = new Records();
	this.datasetCount=0;
	this.oldRowClassName=null;
	this.datasetCount=0;
	this.maxRecordsReturned=200;
	this.container.style.height=this.visibleCount*19+"px";
	this.thumb=thumb;
	this.getTopPos = function()
	{
		return this.topPos;
	}
	this.setTopPos = function(s)
	{

		if (this.mRecords.Count() < this.visibleCount)
			s=0;
		
		if (s>this.datasetCount-this.visibleCount)
			s=this.datasetCount-this.visibleCount;
		
		if (s<0)
			s=0;
		this.topPos=s;
	}
	this.getRecords = function() {
		return this.mRecords;
	}
	this.setDatasetCount=function(s) {
		this.datasetCount=s;
	}
	this.getDatasetCount=function(s) {
		return this.datasetCount;
	}
	this.Clear=function() {
		this.getRecords().Clear();
		this.setTopPos(0);
		this.thumb.style.top="0px";
	}
	this.DoIncrementalLoad=function() {
		//scan all records starting at top until top+visiblecount and see if there are any empty records sets
		var top=this.getTopPos();
		var stop=Math.min(top+this.visibleCount,this.datasetCount);	

		for(var i=top; i<stop; i++)
		{
			var rec=this.mRecords.Get(i);

			if (typeof rec == "undefined")
			{
				 return true;
			}
		}
		return false;
		
	}
	this.LastTopRecordIndex=function()
	{
		var totalRecords = this.getDatasetCount()-this.visibleCount;

		if (totalRecords<0) totalRecords=0;
		return totalRecords;
	}
	this.addCell=function(row) {
		var cell = row.insertCell(-1);
		cell.className='tdresults';
		return cell;
	}
	this.addText2Cell=function(cell,value) {
		var textNode = document.createTextNode(value);
		cell.appendChild(textNode);
	}
	this.addLink2Cell=function(cell, value, action) {
		
		var textNode = document.createElement('a');
		textNode.setAttribute('value',value);
		cell.appendChild(textNode);
		cell.innerHTML+="<a href=\"#\" onclick=\""+action+"\">"+value+"</a>&nbsp;&nbsp;";
		cell.className='tdresults';
		
	}
	
	this.UpdateGridview = function() {
		//First erase the existing data from the table
		
		while (this.obj.rows.length>1)
			this.obj.deleteRow(-1);
		var totalRecords = Math.min(this.visibleCount,this.getRecords().Count()-this.getTopPos());

		var recIndex=this.getTopPos();
		for(var i=0; i<totalRecords; i++)
		{
			var rec=this.getRecords().Get(recIndex++);
			if (typeof rec == "undefined") continue;
		
			var rowCount=this.obj.rows.length;
			var row = this.obj.insertRow(-1);
			if (i%2)
				row.className='trresults_row1';
			else
				row.className='trresults_row2';
			row.onmouseover= function() {
				if (!this.oldRowClassName)
					this.oldRowClassName=this.className;
			
				this.className='trresults_highlighted';
			}
			row.onmouseout= function() {
				if (this.oldRowClassName)
				{
					this.className=this.oldRowClassName;
					this.oldRowClassName=null;
				}
			}

			// left cell
			var cell=this.addCell(row);
			this.addText2Cell(cell,rec.shape);
			cell=this.addCell(row);
			this.addText2Cell(cell,rec.color);
			cell=this.addCell(row);
			this.addText2Cell(cell,rec.clarity);
			//this.addCell(row,rec.cut);

			cell=this.addCell(row);
			this.addText2Cell(cell,rec.size);
			cell=this.addCell(row);
			this.addText2Cell(cell,rec.certificate);
			cell=this.addCell(row);
			if (displayCurrency=="USD")
				this.addText2Cell(cell,"$ "+rec.priceUSD);
			else
			if (displayCurrency=="ZAR")
				this.addText2Cell(cell,"R "+Math.ceil(USD_ZAR_rate*rec.priceUSD*1.14));

			cell=this.addCell(row);
			this.addLink2Cell(cell,"Request","RequestToBuy("+(recIndex-1)+"); return false;");
			this.addLink2Cell(cell,"Chart","Chart("+rec.id+",'"+rec.shape+"',"+rec.size+",'"+rec.color+"','"+rec.clarity+"'); return false;");

		
		}
	}

	
	
}
