function CGrid(oTable){
	this.table = oTable;
	this.document = null;
	this.markedRows = new Array;
	this.markAllowed=true;
	this.isRadio=false;
	this.clrDefault		= '',		//default color
	this.clrOver		= '#03B102',//over color
	this.clrMarkedBg	= '#2E2282',//marked color
	this.clrMarked		= '#FFFFFF',//marked color
	//this.clrMarkedOver	= '#DD7733'	//marked over color
	this.onover = null; //user handler
	this.onout = null; //user handler
	this.onclick = null; //user handler
	
	this.isMarked = function(no){return this.markedRows[no];}
		
	this.over = function(row){
		var no = row.getAttribute('row');
		row.style.backgroundImage="url(/img/halfgray.gif)";
		//row.style.backgroundColor=this.isMarked(no)?this.clrMarkedOver:this.clrOver;
		if(this.onover) this.onover(row);
	}
	this.out = function(row){
		var no = row.getAttribute('row');
		row.style.backgroundImage="none";
		//row.style.backgroundColor=this.isMarked(no)?this.clrMarked:this.clrDefault;
		if(this.onout) this.onout(row);
	}
	this.click = function(row){
		if(this.markAllowed){
			var no = row.getAttribute('row');
			if(this.isRadio){
				for(i=0;i<this.markedRows.length;i++){
					this.markedRows[i] = (no==i);
				}
				this.rowsStateUpdate();
			}else{
				this.markedRows[no] = !this.isMarked(no);
				this.rowStateUpdate(row);
			}
		}
		if(this.onclick) this.onclick(row);
	}
	
	// one row state update
	this.rowStateUpdate = function(row){
		var no = row.getAttribute('row');
		chk = this.document.getElementById('chkrow_'+no);
		if(chk){chk.checked = this.isMarked(no);}
		edt = this.document.getElementById('quantity_'+no);
		if(chk.checked && edt && parseInt(edt.value) == 0){
			edt.value = '1';
		}
		else if(!chk.checked && edt && parseInt(edt.value) != 0){
			edt.value = '0';
		}
		row.style.color=this.isMarked(no)?this.clrMarked:this.clrDefault;
		row.style.backgroundColor=this.isMarked(no)?this.clrMarkedBg:this.clrDefault;
	}
	
	// all rows state update
	this.rowsStateUpdate = function(){
		if(!this.markAllowed) return;
		var oThis = this;
		var oList = this.table.getElementsByTagName('td');
		if (oList!=null) {
			for (i=0; i<oList.length; i++){
				var row = oList.item(i)
				var no = row.getAttribute('row');
				if(typeof(no)=='string' && no!=''){
					this.rowStateUpdate(row);
				}
			}
		}
		oList = this.table.getElementsByTagName('tr');
		if (oList!=null) {
			for (i=0; i<oList.length; i++){
				var row = oList.item(i);
				var no = row.getAttribute('row');
				if(typeof(no)=='string' && no!=''){
					this.rowStateUpdate(row);
				}
			}
		}
	}
	
	this.init= function(){
		if(!this.table) return false;
		this.document = (this.table.ownerDocument)?this.table.ownerDocument:this.table.document;

		var oThis = this;
		var oList = this.table.getElementsByTagName('td');
		if (oList!=null) {
			for (i=0; i<oList.length; i++){
				var row = oList.item(i)
				var no = row.getAttribute('row');
				if(typeof(no)=='string' && no!=''){
					row.onmouseover = function(){oThis.over(this);}
					row.onmouseout = function(){oThis.out(this);}
					if(this.markAllowed){
						row.onclick = function(){oThis.click(this);}
						chk = this.document.getElementById('chkrow_'+no);
						this.markedRows[no] = (chk && chk.checked);
						this.rowStateUpdate(row);
					}
				}
			}
		}
		oList = this.table.getElementsByTagName('tr');
		if (oList!=null) {
			for (i=0; i<oList.length; i++){
				var row = oList.item(i);
				var no = row.getAttribute('row');
				if(typeof(no)=='string' && no!=''){
					row.onmouseover = function(){oThis.over(this);}
					row.onmouseout = function(){oThis.out(this);}
					if(this.markAllowed){
						row.onclick = function(){oThis.click(this);}
						chk = this.document.getElementById('chkrow_'+no);
						this.markedRows[no] = (chk && chk.checked);
						this.rowStateUpdate(row);
					}
				}
			}
		}
		return true;
	}
	
	function bCol(obj,col){
		obj.style.backgroundColor=col;
	}

	function selectRow(id){
		 location.href=id;
		 return false;
	}
	return this;
}


// CheckAll(frm)
//   checks all checkboxed in ръќ form
function CheckAll(frm, f)
{
	for (var i=0;i<frm.elements.length;i++)
	{
		var e = frm.elements[i];
		if ((e.name == 'chkrow[]') && (e.type=='checkbox'))
			e.checked = f;
	}
	frm.allbox.checked = f
}
// CheckCheckAll(frm)
//   checks all checkboxed in ръќ form
function CheckCheckAll(frm)
{
	var TotalBoxes = 0;
	var TotalOn = 0;
	for (var i=0;i<frm.elements.length;i++)
	{
		var e = frm.elements[i];
		if ((e.name == 'chkrow[]') && (e.type=='checkbox'))
		{
			TotalBoxes++;
			if (e.checked)
			{
				TotalOn++;
			}
		}
	}
	if ((TotalBoxes>0) && (TotalBoxes==TotalOn))
		{frm.allbox.checked=true;}
	else
		{frm.allbox.checked=false;}
}
