var Grid = function ()
{
	//	PUBLIC VAR
	//	vId				- grid container
	//	vDataUrl		- grid data URL
	//	vError			- Last Error
	//	vWidth			- Grid & paging Width
	//	vControls		- grid controls
	//	vControlType	- Control style show: 'line'/'row'; defailt 'row'
	//	vType			- unset or 'tree'
	//	vPicWidth		- pic width
	//	vShowPath		- Show Tree path. type is bool

	//	PRIVATE VAR
	//	vIdElement		- grid container Element
	//	vFiledList		- grid filed list
	//	vData			- grid data
	//	vCurrentDataId	- current id in data array
	//	vOrderArray		- grid order array
	//	vFilterArray	- grid filter array
	//	vControlId			- grid ID
	//	vControlIdElement	- grid ID Element
	//	vAJAXReceivedData - last data revived from server
	//	vControlLoadingId	- ID Loading DIV
	//	vControlPagingId	- ID Paging Element
	//	vPaging			- grid Paging data
	//	vControlLocked		- Grid is locked
	//	vGridPathId		- Grid path ID
	//	vGridPathElement - Grid path Element

	this.vOrderArray = [];
	this.vChilds = [];
	this.vControlType = 'row';
	this.vPostData		= [];
	this.vControls = [];

//	this.vPostData['p'] = 1;
	
//	var a = new Cookie('test_a');
	
//	var b = a.getSubValue('a_b');
//	alert(b);
//	a.setSubValue('a_b',222);


	this.tFilterTitle = 'Click to show/hide Filter';
	this.tApplyFilterTitle = 'Click to apply Filter';
	this.tRecordExists = 'Record already exista';

	this.vPicPlus = this.vPicPath + 'plus.gif';
	this.vPicMinus = this.vPicPath + 'minus.gif';
	this.vPicPlusMinus = this.vPicPath + 'plusminus.gif';
	this.vPicSpacer = '/admin/images/spacer.gif'
//	this.vMainClassName = 'grid_tab';
	this.vMainClassName = 'table-main';


	this.vControls['new'] = [];
	this.vControls['new']['title'] = 'Click to add new record';
	this.vControls['new']['pic'] = this.vPicPath + 'save.gif';

	this.vControls['edit'] = [];
	this.vControls['edit']['title'] = 'Click to edit record';
	this.vControls['edit']['pic'] = this.vPicPath + 'edit.gif';

	this.vControls['edit_on_fly'] = [];
	this.vControls['edit_on_fly']['title'] = 'Click to edit record';
	this.vControls['edit_on_fly']['title_save'] = 'Click to save record';
	this.vControls['edit_on_fly']['titleD'] = 'Click to edit record';
	this.vControls['edit_on_fly']['title_saveD'] = 'Click to save record';
	this.vControls['edit_on_fly']['pic'] = this.vPicPath + 'edit.gif';
	this.vControls['edit_on_fly']['save_pic'] = this.vPicPath + 'save.gif';

	this.vControls['delete'] = [];
	this.vControls['delete']['title'] = 'Click to delete record';
	this.vControls['delete']['confirm'] = 'Delete record?';
	this.vControls['delete']['pic'] = this.vPicPath + 'delete.gif';
}

Grid.prototype = new xAjax;

Grid.prototype.fInit = function ()
{
	if (!this.vIdElement)
		this.vIdElement = document.getElementById(this.vId);

	if (!this.vIdElement)
	{
		this.fError('Element `' + this.vId + '` not found in DOM');
		return;
	}

	if (!this.vDataUrl)
	{
		this.vError = 'vDataUrl is empty!';
		this.fError();
		return;
	}

	this.vControlId = this.vId + Math.random();
	this.vControlPagingId = this.vId + Math.random();
	this.vControlLoadingId = this.vId + Math.random();


	this.fGetData();
}

Grid.prototype.fGetData = function ()
{
	this.vPostData['order'] = this.vOrderArray;
	this.vPostData['filter'] = this.vFilterArray;

	this.fLockControl();

	this.AJAXPostData(this.vDataUrl, this.vPostData);
}

Grid.prototype.fLockControl = function ()
{
	if (!this.vControlLocked)
	{
		this.vControlLocked = true;

		if (this.vControlIdElement)
		{
			this.vControlIdElement.className = this.vMainClassName + 'LockGrid';
			var tDiv = document.createElement('DIV');
			tDiv.id = this.vControlLoadingId;
			tDiv.className = this.vMainClassName + 'grid_loading';
			tDiv.innerHTML = this.tLoading;
			tDiv.style.position = 'relative';
			tDiv.style.top = -this.vControlIdElement.clientHeight / 2;
			tDiv.style.height = '0';
			this.vControlLoading = tDiv;
			this.vIdElement.appendChild(tDiv);
			this.fLockElements();
		}
		else
		{
			var tDiv = document.createElement('DIV');
			tDiv.id = this.vControlLoadingId;
			tDiv.className = this.vMainClassName + 'grid_loading';
			tDiv.innerHTML = this.tLoading;
			this.vControlLoading = tDiv;
			this.vIdElement.appendChild(tDiv);
		}
	}
}

Grid.prototype.fUnLockControl = function ()
{
	if (this.vControlLocked)
	{
		if (this.vControlIdElement)
		{
			this.vControlIdElement.className = this.vMainClassName;
			this.fUnLockElements();
		}

		this.vControlLocked = false;

		if (this.vControlLoading)
			this.vControlLoading.parentNode.removeChild(this.vControlLoading);

		this.vControlLoadingId = null;
	}
}

Grid.prototype.fCleanContent = function ()
{
	var zz = document.getElementById(this.vControlLoadingId);
	if (zz)
		zz.parentNode.removeChild(zz);
	zz = document.getElementById(this.vControlId);
	if (zz)
		zz.parentNode.removeChild(zz);
	zz = document.getElementById(this.vControlPagingId);
	if (zz)
		zz.parentNode.removeChild(zz);
}

Grid.prototype.fShow = function ()
{
	this.a('Start');

	this.fUnLockControl();
	this.fCleanContent();

	this.fGridPaging();

	var tTable, tTbody, tTr, tTrFilter, tTd, tText, tImg, tBr, tInput, tFlag, tFieldsCount, tFilteredFieldsCount, tFilterCounter;
	var ttControlArray = [];
	var tShowFilter;
	var tt, tCounter;
	var p = this;

	if (this.vFiledList && this.vIdElement)
	{
		tFilterCounter = 0;

		tTable = document.createElement('TABLE');
		tTable.setAttribute('thisGrid', '1');
		tTable.border = 1;
		tTable.className = this.vMainClassName;
		tTable.cellPadding = 2;
		tTable.cellSpacing = 0;
		tTable.id = this.vControlId;
		tTable.width = this.vWidth || '95%';
		tTbody = document.createElement('TBODY');

		tTr = document.createElement('TR');
		tTr.className = 's_header';

		tTrFilter = document.createElement('TR');
		tTrFilter.style.display = 'none';
		tTrFilter.setAttribute('is_grid_filter', 1);

		tFieldsCount = 0;
		tFilteredFieldsCount = 0;

		if (this.vType == 'tree')
		{
			tTd = document.createElement('TD');
			tText = document.createTextNode('');
			tTd.align = 'center';
			tTd.width = this.vPicWidth;
			tTd.appendChild(tText);
			tTd.className = this.vMainClassName+'_header';
			tTr.appendChild(tTd);
		}

		for (var i in this.vFiledList)
		{
			tFieldsCount++;
			tTd = document.createElement('TD');
			tText = document.createTextNode(this.vFiledList[i]['title']);
			tTd.style.textAlign = 'center';
			tTd.align = 'center';
			if (this.vFiledList[i]['width'])
				tTd.width = this.vFiledList[i]['width'];
			tTd.appendChild(tText);
			tTd.className = this.vMainClassName + '_header';

			if (!this.vFiledList[i]['no_order'])
			{
				tTd.setAttribute('field_name', i);
				tTd.onmouseover = function () { if (!p.vControlLocked) {this.className = p.vMainClassName+'_header_over';} }
				tTd.onmouseout = function () { if (!p.vControlLocked) {this.className = p.vMainClassName+'_header';} }
				tTd.onclick = function () { if (!p.vControlLocked) {p.fGridReorder(this.getAttribute('field_name'));} }

				if (this.vOrderArray[i] == 2)
				{
					tImg = document.createElement('IMG');
					tImg.src = '/admin/images/arrowup.gif';
					tTd.appendChild(tImg);
				}
				else if(this.vOrderArray[i] == 1)
				{
					tImg = document.createElement('IMG');
					tImg.src = '/admin/images/arrowdown.gif';
					tTd.appendChild(tImg);
				}
			}

			if (!this.vFiledList[i]['no_filter'] && (this.vFilterArray[i] != '') && (this.vFilterArray[i] != null))
			{
				tImg = document.createElement('IMG');
				tImg.src = '/admin/images/filter.gif';
				tTd.appendChild(tImg);
			}

			tTr.appendChild(tTd);

// Filter
			tTd = document.createElement('TD');
			tTd.className = this.vMainClassName+'_header';
			if (!this.vFiledList[i]['no_filter'])
			{
				tFilterCounter++;
				if (this.vFiledList[i]['type'] == 'bool')
				{
					tInput = document.createElement('INPUT');
					tInput.type = 'hidden';
					var tttmp = this.vFilterArray[i];
					tInput.value = (tttmp != '' && tttmp != null?tttmp:'');
					tInput.setAttribute('is_filter_control', 1);
					tInput.setAttribute('field', i);
					tTd.appendChild(tInput);
					tImg = document.createElement('IMG');
					tImg.src = (tInput.value == '1'?this.vPicPlus:(tInput.value == '0'?this.vPicMinus:this.vPicPlusMinus));
					tImg.setAttribute('bool_state', (tInput.value == '1'?1:(tInput.value == '0'?2:0)));
					tImg.className = 'hand';
					tImg.onclick = function ()
					{
						if (!p.vControlLocked) {
							var zz = this.parentNode.getElementsByTagName('INPUT');
							if (this.getAttribute('bool_state') == 0)
							{
								this.setAttribute('bool_state', 1);
								zz[0].value = '1';
								this.src = p.vPicPlus;
								return false;
							}
							else if (this.getAttribute('bool_state') == 1)
							{
								zz[0].value = '0';
								this.setAttribute('bool_state', 2);
								this.src = p.vPicMinus;
								return false;
							}
							else
							{
								zz[0].value = '';
								this.setAttribute('bool_state', 0);
								this.src = p.vPicPlusMinus;
								return false;
							}
						}
					}
					tTd.align = 'center';
					tTd.appendChild(tImg);
				}
				else
				{
					tInput = document.createElement('INPUT');
					if ((this.vFilterArray[i] != '') && (this.vFilterArray[i] != null))
					{
						tInput.value = this.vFilterArray[i];
						tFilteredFieldsCount++;
					}
					tInput.setAttribute('is_filter_control', 1);
					tInput.setAttribute('field', i);
					tTd.appendChild(tInput);
				}
				tShowFilter = true;
			}
			tTrFilter.appendChild(tTd);
// /Filter
		}

// Apply Filter
		tTd = document.createElement('TD');
		tTd.className = this+'_tab_header';
		tTd.width = this.vPicWidth;
		tImg = document.createElement('IMG');
		tImg.src = '/admin/images/toolbar/filterplus.gif';
		tImg.title = this.tApplyFilterTitle;
		tImg.onclick = function ()
		{
			if (!p.vControlLocked) {
				p.vFilterArray = [];
				var InpF = this.parentNode.parentNode.getElementsByTagName('INPUT');
				var tt;
				for (var i=0; i<InpF.length; i++)
					if ((InpF[i].getAttribute('is_filter_control') == 1) && (InpF[i].value))
					{
						tt = true;
						p.vFilterArray[InpF[i].getAttribute('field')] = InpF[i].value;
					}
				if (!tt)
					p.vFilterArray = 'drop';
				p.fGetData();
				return false;
			}
		}
		tTd.align = 'center';
		tTd.onmouseover = function () { if (!p.vControlLocked) {this.className = p.vMainClassName+'_header_over';} }
		tTd.onmouseout = function () { if (!p.vControlLocked) {this.className = p.vMainClassName+'_header';} }
		tTd.appendChild(tImg);
		tTrFilter.appendChild(tTd);
// /Apply Filter

//	Show filter Button
		if ((tFilterCounter > 0) || (this.fGetControlCount() > 0))
		{
			tTd = document.createElement('TD');
			tTd.className = p.vMainClassName + '_header';
			tTd.align = 'center';
			if (this.vControlType == 'line')
			{
				var w_count = this.fGetControlCount() == 0?1:this.fGetControlCount();
				tTd.width = (w_count * this.vPicWidth);
			}
			else
				tTd.width = this.vPicWidth;
			if (tShowFilter)
			{
				tTd.onmouseover = function () { if (!p.vControlLocked) {this.className = p.vMainClassName+'_header_over';} }
				tTd.onmouseout = function () { if (!p.vControlLocked) {this.className = p.vMainClassName+'_header';} }
				tTd.onclick = function ()
				{
					if (!p.vControlLocked) {
						var zz = this.parentNode.parentNode;
						for (var i=0; i<zz.childNodes.length; i++)
						{
							if (zz.childNodes[i].getAttribute('is_grid_filter') == 1)
							{
								zz.childNodes[i].style.display = (zz.childNodes[i].style.display?'':'none');
							}
						}
					}
				}

				tImg = document.createElement('IMG');
				tImg.src = '/admin/images/toolbar/filter.gif';
				tImg.title = this.tFilterTitle;
				tTd.appendChild(tImg);
			}
			tTr.appendChild(tTd);
		}
//	/Show filter Button

		tTbody.appendChild(tTr);
		tTbody.appendChild(tTrFilter);

//	Process Data
		tCounter = 0;

//		this.a('Process Data');

		for (var j in this.vData)
		{
			tCounter++;
			tFlag = true;
			tTr = document.createElement('TR');
			tTr.className = p.vMainClassName+'_tr';
			tTr.setAttribute('db_id', this.vData[j]['id']);
			tTr.setAttribute('array_id', j);
			tTr.onmouseover = function () { if (!p.vControlLocked) {this.className = p.vMainClassName+'_tr_over';} }
			tTr.onmouseout = function () { if (!p.vControlLocked) {this.className = p.vMainClassName+'_tr';} }
//			tTr.onclick = function () { document.location = '/adm.php?module=shop&tpl=edit.tpl&id=' + this.getAttribute('db_id'); }

			if (this.vType == 'tree')
			{
				tTd = document.createElement('TD');
				tTd.onmouseover = function () { if (!p.vControlLocked) {this.className = p.vMainClassName+'_header_over';} }
				tTd.onmouseout = function () { if (!p.vControlLocked) {this.className = p.vMainClassName+'_header';} }
				tTd.align = 'center';
				tTd.className = p.vMainClassName+'_header';
				tTd.onclick = function ()
				{
					var zz = this.getElementsByTagName('IMG');
					if (this.getAttribute('opened') == 1)
					{
						zz[0].src = p.vPicPlus;
						this.setAttribute('opened', 0)
						this.rowSpan = null;
						p.vCurrentDataId = null;
					}
					else
					{
						zz[0].src = p.vPicMinus;
						this.setAttribute('opened', 1)
						this.rowSpan = 2;
						p.vCurrentDataId = this.parentNode.getAttribute('array_id');
					}
					eval(p.vControls['tree']['exec']);
				}
				tImg = document.createElement('IMG');
				tImg.src = this.vPicPlus;
				tTd.appendChild(tImg);
				tTr.appendChild(tTd);
			}

//			this.a('Start vFiledList');
			for (var i in this.vFiledList)
			{
				tTd = document.createElement('TD');
				if (this.vFiledList[i]['align'])
					tTd.align = this.vFiledList[i]['align'];

				tTd.setAttribute('field_name', i);
				tTd.setAttribute('data_id', j);

				tTd.onclick = function ()
				{
					if (p.aOnCellClick)
					{
						p.aOnCellClick(this.getAttribute('field_name'), this.getAttribute('data_id'), this);
					}
				}

				if (this.vFiledList[i]['width'])
					tTd.width = this.vFiledList[i]['width'];

				if (this.vFiledList[i]['type'] == 'img')
				{
					tImg = document.createElement('IMG');
					tImg.src = this.vData[j][i];
					tTd.appendChild(tImg);
				}
				else if (this.vFiledList[i]['type'] == 'html')
				{
					tTd.innerHTML = this.vData[j][i];
				}
				else if (this.vFiledList[i]['type'] == 'bool')
				{
//					tImg = document.createElement('IMG');
//					tImg.src = (this.vData[j][i] == '1'?this.vPicPlus:this.vPicMinus);
//					tImg.value = this.vData[j][i];
//					tTd.appendChild(tImg);
					tImg = document.createElement('IMG');
					tImg.src = (this.vData[j][i] == '1'?this.vPicPlus:this.vPicMinus);
					tImg.value = this.vData[j][i];
					tTd.appendChild(tImg);
					if (this.vFiledList[i]['onclick_save'])
					{
						tImg.val = this.vData[j][i];
						tImg.style.cursor = 'pointer';
						tImg.save_url = this.vFiledList[i]['onclick_save'];
						
						tImg.onclick = function()
						{
							this.val = 1 - this.val;
							this.src = (this.val == '1'?p.vPicPlus:p.vPicMinus);
							var data = [];
							data['id'] = this.parentNode.parentNode.getAttribute('db_id');
							data['value'] = this.val;
							p.AJAXPostData(this.save_url, data);
						}
					}
				}
				else
				{
					if (this.vFiledList[i]['class'])
					{
						tTd.className = this.vFiledList[i]['class'];
					}

					

					if (this.vData[j][i])
					{
						if (this.vFiledList[i]['link'])
						{
							
							var res = this.vFiledList[i]['link']['href'].match(/##.*?##/ig);
							for (var item in res)
							{
//								alert(res[item] + ' ' + item + ' ' + res);
								try{
									var iii = res[item].substring(2, res[item].length - 2);	
									var tLinkA = this.vFiledList[i]['link']['href'].replace(res[item], this.vData[j][iii]);
								}
								catch(e){};
							}
							if (!res)
							{
								var tLinkA = this.vFiledList[i]['link']['href'];
							}
							
							//this.vFiledList[i]['link']
							var tLink = document.createElement('A');
							tLink.href = tLinkA;
							tLink.innerHTML = this.vData[j][i];
							if (this.vFiledList[i]['link']['class'])
							{
								tLink.className = this.vFiledList[i]['link']['class'];
							}
							if (this.vFiledList[i]['link']['target'])
							{
								tLink.target = this.vFiledList[i]['link']['target'];
							}
							
							if (this.vFiledList[i]['link']['title'])
							{
								var res = this.vFiledList[i]['link']['title'].match(/##.*?##/ig);
								for (var item in res)
								{
									try{
									var iii = res[item].substring(2, res[item].length - 2);	
									var tTitleA = this.vFiledList[i]['link']['title'].replace(res[item], this.vData[j][iii]);
									}catch(e){};
								}
								if (!res)
								{
									var tTitleA = this.vFiledList[i]['link']['title'];
								}
								
								tLink.title = tTitleA;
							}

							tTd.appendChild(tLink);
						}
						else
						{
							tText = document.createTextNode(this.vData[j][i]);
							tTd.appendChild(tText);
						}
						if (this.vFiledList[i]['className'])
						{
							tTd.className = this.vFiledList[i]['className'];
						}
					}

				}

				if (this.vData[j]['class_for_td'])
				{
					tTd.className = 'class_for_td';
				}
				if (this.vData[j]['class_for_td1'])
				{
					tTd.className = 'class_for_td1';
				}
				if (this.vData[j]['class_for_td2'])
				{
					tTd.className = 'class_for_td2';
				}
				tTr.appendChild(tTd);
			}
//			this.a('End vFiledList');

			if ((tFilterCounter > 0) || (this.fGetControlCount() > 0))
			{
				tTd = document.createElement('TD');
				tTd.height = '100%';
				var w_count = 1;
				if (this.vControlType == 'line')
				{
					w_count = this.fGetControlCount() == 0?1:this.fGetControlCount();
				}
				tTd.width = (w_count * this.vPicWidth);

//				this.a('Start ShowControlTab');
				tTd.appendChild(this.ShowControlTab('', tCounter, j));
//				this.a('Stop ShowControlTab');
			}

//			this.a('End vFiledList 1');
			if (this.vData[j]['class_for_td'])
			{
				tTd.className = 'class_for_td';
			}
			if (this.vData[j]['class_for_td1'])
			{
				tTd.className = 'class_for_td1';
			}
			if (this.vData[j]['class_for_td2'])
			{
				tTd.className = 'class_for_td2';
			}
			tTr.appendChild(tTd);

			tTbody.appendChild(tTr);
//			this.a('End Step ' + j);
		}
//	/Process Data

//	Attach NEW
//		this.a('Start ShowNewRecord');
		var tt = this.ShowNewRecord();
		if (tt)
		{
			tTbody.appendChild(tt);
		}
//		this.a('Stop ShowNewRecord');
//	/Attach NEW

		if (!tFlag)
		{
			tTr = document.createElement('TR');
			tTd = document.createElement('TD');
			tTd.colSpan = tFieldsCount + 1;
			tTd.align = 'center';
			tTd.className = this.vMainClassName + 'no_data';
			var tmpSpan = document.createElement('SPAN');

			tmpSpan.innerHTML =	this.tNoDataFound + (tFilteredFieldsCount>0?' Please check filters.':'');
			tTd.appendChild(tmpSpan);
			tTr.appendChild(tTd);
			tTbody.appendChild(tTr);
		}

		tTable.appendChild(tTbody);

		try { this.vIdElement.parentNode.removeChild(this.vControlIdElement); } catch (e) { }
//		this.vIdElement.innerHTML = '';
		this.vIdElement.appendChild(tTable);
	}

	this.vControlIdElement = document.getElementById(this.vControlId);
	this.a('Stop');
	this.sl();
}

Grid.prototype.fGridReorder = function ()
{
	if (arguments[0])
	{
		if ((this.vOrderArray[arguments[0]] == null) || (this.vOrderArray[arguments[0]] == 0))
		{
			this.vOrderArray = [];
			this.vOrderArray[arguments[0]] = 1;
		}
		else if (this.vOrderArray[arguments[0]] == 1)
		{
			this.vOrderArray = [];
			this.vOrderArray[arguments[0]] = 2;
		}
		else
		{
			this.vOrderArray = [];
			this.vOrderArray[arguments[0]] = 0;
		}
		this.fGetData();
	}
}


Grid.prototype.fGridPaging = function ()
{
	var tTable, tTbody, tTr, tTd, tText, tA;
	var el = this.vPaging;
	var TabTo = this.vIdElement;

	var p = this;

	if (el && (el['total_pages'] > 1))
	{
		tTable = document.createElement('TABLE');
		tTable.border = 0;
		tTable.id = this.vControlPagingId;
		tTable.className = this.vMainClassName + 'pagtab';
		tTable.setAttribute('thisPaging', '1');
		tTbody = document.createElement('TBODY');

		tTr = document.createElement('TR');
		tTr.className = 's_header';

		if (el['pages_befor_paging'] == '1')
		{
			tTd = document.createElement('TD');
			tA = document.createElement('A');
			tA.innerHTML = 'First';
			tA.onclick = function () { if (!p.vControlLocked) { p.vPostData['p'] = 1; p.fGetData();} }
			tTd.appendChild(tA);
			tTr.appendChild(tTd);
			tTd = document.createElement('TD');
			tText = document.createTextNode('...');
			tTd.appendChild(tText);
			tTr.appendChild(tTd);
		}

		for (var i in el['pages'])
		{
			tTd = document.createElement('TD');

			if (el['pages'][i]['current'] == '1')
			{
				tTd.innerHTML = '&nbsp;' + el['pages'][i]['number'] + '&nbsp;';
				tTd.className = 'pagingselected';
			}
			else
			{
				tA = document.createElement('A');
				tA.innerHTML = '&nbsp;' + el['pages'][i]['number'] + '&nbsp;';
				tA.setAttribute('page', el['pages'][i]['number']);
				tA.href = 'javascript:;';
				tA.onclick = function () { if (!p.vControlLocked) {p.vPostData['p'] = this.getAttribute('page'); p.fGetData();} }
				tTd.appendChild(tA);
			}

			tTr.appendChild(tTd);
		}

		if (el['pages_after_paging'] == '1')
		{
			tTd = document.createElement('TD');
			tText = document.createTextNode('...');
			tTd.appendChild(tText);
			tTr.appendChild(tTd);
			tTd = document.createElement('TD');
			tA = document.createElement('A');
			tA.innerHTML = 'Last';
			tA.onclick = function () { if (!p.vControlLocked) {p.vPostData['p'] = el['total_pages']; p.fGetData();} }
			tTd.appendChild(tA);
			tTr.appendChild(tTd);
		}

		tTbody.appendChild(tTr);
		tTable.appendChild(tTbody);

		var dd = document.getElementById(this.vControlPagingId);
		if (dd)
			dd.parentNode.removeChild(dd);
		TabTo.appendChild(tTable);
	}
}

Grid.prototype.ShowNewRecord = function (ControllArray)
{
	var tTableControl, tTbodyControl, tTrControl, tTdControl, tTd, tText, tImg, tBr, iInput;
	var tShowNew;
	var p = this;

	tTrNew = document.createElement('TR');
	tTrNew.className = p.vMainClassName+'_new_tr';

	if (this.vType == 'tree')
	{
		tTd = document.createElement('TD');
		tTrNew.appendChild(tTd);
	}
	for (var i in this.vFiledList)
	{
		tTd = document.createElement('TD');
//		tTd.className = 'grid_tab_header';
		if (this.vFiledList[i]['new'] == 1)
		{
			if (this.vFiledList[i]['type'] == 'select')
			{
				iInput = new xSelect();
				iInput.vIdElement = tTd;
				iInput.vDataUrl = this.vFiledList[i]['select_data'].replace('##pid##', this.vPid).replace('##id##', '');
				iInput.fInit();
			}
			else if (this.vFiledList[i]['type'] == 'bool')
			{
				tTd.align = 'center';
				tInput = document.createElement('INPUT');
				tInput.value = (this.vFiledList[i]['default_new'] == 1?1:0);
				tInput.type = 'hidden';
				tInput.name = i;
				tInput.setAttribute('field', i);

				tImg = document.createElement('IMG');
				tImg.src = (this.vFiledList[i]['default_new'] == 1?this.vPicPlus:this.vPicMinus);
				tImg.setAttribute('bool_state', (this.vFiledList[i]['default_new'] == 1?1:0));
				tImg.className = 'hand';
				tImg.onclick = function ()
				{
					if (!p.vControlLocked) {
						var zz = this.parentNode.getElementsByTagName('INPUT');
						if (this.getAttribute('bool_state') == 0)
						{
							this.setAttribute('bool_state', 1);
							zz[0].value = '1';
							this.src = p.vPicPlus;
							return false;
						}
						else if (this.getAttribute('bool_state') == 1)
						{
							zz[0].value = '0';
							this.setAttribute('bool_state', 0);
							this.src = p.vPicMinus;
							return false;
						}
					}
				}
				tTd.appendChild(tInput);
				tTd.appendChild(tImg);
			}
			else if (this.vFiledList[i]['type'] == 'textarea')
			{
				tInput = document.createElement('TEXTAREA');
				if (this.vFiledList[i]['height'])
				{
					tInput.style.height = this.vFiledList[i]['height'];
				}
				tTd.appendChild(tInput);
			}
			else if (this.vFiledList[i]['type'] == 'date')
			{
				tInput = document.createElement('INPUT');
				tTd.appendChild(tInput);
				Calendar.setup(
				{
					inputField		: tInput,
					ifFormat		: this.vFiledList[i]['format'],
					showsTime		: true,
					singleClick		: false,
					step			: 1
				}
				)
			}
			else
			{
				tInput = document.createElement('INPUT');
				tTd.appendChild(tInput);
			}
			if (this.vFiledList[i]['valign'])
			{
				tTd.style.verticalAlign = this.vFiledList[i]['valign'];
			}

			tTd.setAttribute('field_name', i);
			tShowNew = true;
		}
		tTrNew.appendChild(tTd);
	}

	if (tShowNew)
	{
		tTd = document.createElement('TD');
		tTd.className = p.vMainClassName+'_new';

		tTableControl = document.createElement('TABLE');
		tTableControl.border = 0;
//		tTableControl.className = 'grid_tab';
		tTableControl.cellPadding = 2;
		tTableControl.cellSpacing = 0;
		tTableControl.height = '100%';
		tTableControl.style.height = '100%';
		tTbodyControl = document.createElement('TBODY');

		tTrControl = document.createElement('TR');

		tTdControl = document.createElement('TD');

		tImg = document.createElement('IMG');
		tImg.src = this.vControls['new']['pic'];
		tImg.onmouseover = function () {if (!p.vControlLocked) {this.className = 'hand'} }
		tImg.onmouseout = function () { if (!p.vControlLocked) {this.className = ''} }
		tImg.onclick = function ()
		{
			if (!p.vControlLocked)
			{
				var zz = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
				for (var i=0; i<zz.childNodes.length; i++)
				{
					if (zz.childNodes[i].tagName == 'TD' && zz.childNodes[i].getAttribute('field_name') != '' && zz.childNodes[i].getAttribute('field_name') != null && p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['new'] == 1)
					{
						p.vPostData[zz.childNodes[i].getAttribute('field_name')] = zz.childNodes[i].childNodes[0].value;

//						if (p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['type'] == 'select')
//							try { zz.childNodes[i].innerHTML = zz.childNodes[i].childNodes[0].options[zz.childNodes[i].childNodes[0].selectedIndex].text; } catch (e) { }
//						else
//							zz.childNodes[i].innerHTML = zz.childNodes[i].childNodes[0].value;
					}
				}
				p.fLockControl();
				p.AJAXPostData(p.vControls['new']['url'], p.vPostData);
				return false;
			}
		}
		tImg.title = this.vControls['new']['title'];
		tTdControl.appendChild(tImg);
		tTrControl.appendChild(tTdControl);
		tTbodyControl.appendChild(tTrControl);
		tTableControl.appendChild(tTbodyControl);

		tTd.appendChild(tTableControl);
		tTrNew.appendChild(tTd);

		return tTrNew;
	}
	else
	{
		return false;
	}
}

Grid.prototype.ShowControlTab = function (ControllArray, Counter)
{

	var tTableControl, tTbodyControl, tTrControl, tTdControl, tText, tImg, tBr, iInput, tInput;

	var p = this;
	var num_row = arguments[2];

	tTableControl = document.createElement('TABLE');
	tTableControl.border = 0;
//	tTableControl.className = 'grid_tab';
	tTableControl.cellPadding = 0;
	tTableControl.cellSpacing = 0;
	tTableControl.height = '100%';
	tTableControl.style.height = '100%';
	tTbodyControl = document.createElement('TBODY');

	if (this.vControlType == 'line')
	{
		tTrControl = document.createElement('TR');
		tTrControl.className = 's_header';
	}

	for (var i in this.vControls)
	{
		if ((i == 'edit'))
		{
//			var visible = true;
//			if (this.vControls[i]['show_when'])
//			{
//				
				var reg = /(##row_id##)/;
//				if (false == eval(this.vControls[i]['show_when'].replace(reg, num_row)))
//					visible = false;
//			}

			if (this.vControls[i]['visible'])
			if (eval(this.vControls[i]['visible'].replace(/(##row_id##)/, num_row)) == 1)
			{
				if (this.vControlType == 'row')
				{
					tTrControl = document.createElement('TR');
					tTrControl.className = 's_header';
				}

				tTdControl = document.createElement('TD');

				tImg = document.createElement('IMG');
				tImg.src = this.vControls[i]['pic'];
				tImg.onmouseover = function () {if (!p.vControlLocked) {this.className = 'hand'} }
				tImg.onmouseout = function () { if (!p.vControlLocked) {this.className = ''} }
				tImg.onclick = function ()
				{
					if (!p.vControlLocked)
					{
						document.location = p.vControls['edit']['url'].replace('##id##', this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute('db_id'));
						return false;
					}
				}
				tImg.title = this.vControls['edit']['title'];
				tTdControl.appendChild(tImg);
				tTdControl.width = this.vPicWidth;
				tTrControl.appendChild(tTdControl);

				if (this.vControlType == 'row')
				{
					tTbodyControl.appendChild(tTrControl);
				}
			}
		}

		else if (i == 'edit_on_fly')
		{
			if (this.vControls[i]['visible'])
			if (eval(this.vControls[i]['visible'].replace(/(##row_id##)/, num_row)) == 1)
			{
				if (this.vControlType == 'row')
				{
					tTrControl = document.createElement('TR');
					tTrControl.className = 's_header';
				}

				tTdControl = document.createElement('TD');
				
				//tImg = document.createElement('IMG');
				
				tImg = document.createElement('input');
				tImg.setAttribute('type', 'image');
				
				tImg.src = this.vControls['edit_on_fly']['pic'];
				tImg.className = 'control_out';
				tImg.onmouseover = function () {if (!p.vControlLocked) {this.className = 'hand'} }
				tImg.onmouseout = function () { if (!p.vControlLocked) {this.className = 'control_out'} }
				tImg.onclick = function tImg_click()
				{
					if (!p.vControlLocked)
					{
						if (this.getAttribute('save') == 1)
						{
							var zz = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
							p.vPostData['id'] = zz.getAttribute('db_id');
							for (var i=0; i<zz.childNodes.length; i++)
							{
								if (zz.childNodes[i].tagName == 'TD' && zz.childNodes[i].getAttribute('field_name') != '' && zz.childNodes[i].getAttribute('field_name') != null && p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['edit'] == 1)
								{
//									alert(zz.childNodes[i].childNodes[0].tagName);
									p.vPostData[zz.childNodes[i].getAttribute('field_name')] = zz.childNodes[i].childNodes[0].value;
/*									if (p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['type'] == 'select')
									{
										zz.childNodes[i].innerHTML = zz.childNodes[i].childNodes[0].options[zz.childNodes[i].childNodes[0].selectedIndex].text;
									}
									else if ((p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['type'] == 'bool'))
									{
									}
									else
									{
										zz.childNodes[i].innerHTML = zz.childNodes[i].childNodes[0].value;
									}*/
								}
							}
							p.fLockControl();
							p.AJAXPostData(p.vControls['edit_on_fly']['url'], p.vPostData);
//							this.src = p.vControls['edit_on_fly']['pic'];
//							this.setAttribute('save', 0);
						}
						else
						{
							var zz = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
							for (var i=0; i<zz.childNodes.length; i++)
							{
								if (zz.childNodes[i].tagName == 'TD' && zz.childNodes[i].getAttribute('field_name') != '' && zz.childNodes[i].getAttribute('field_name') != null && p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['edit'] == 1)
								{
									if (p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['type'] == 'bool')
									{
	//									alert(zz.childNodes[i].childNodes[0].value);
										tInput = document.createElement('INPUT');
										tInput.value = zz.childNodes[i].childNodes[0].value;
										tInput.type = 'hidden';
										tInput.name = zz.childNodes[i].getAttribute('field_name');
										tInput.setAttribute('field', zz.childNodes[i].getAttribute('field_name'));

										//tImg = document.createElement('IMG');

										tImg = document.createElement('input');
										tImg.setAttribute('type', 'image');
										
										tImg.src = (zz.childNodes[i].childNodes[0].value == '1'?p.vPicPlus:p.vPicMinus);
										tImg.setAttribute('bool_state', zz.childNodes[i].childNodes[0].value);
										tImg.className = 'hand';
										tImg.onclick = function ()
										{
											if (!p.vControlLocked) {
												var zz = this.parentNode.getElementsByTagName('INPUT');
												if (this.getAttribute('bool_state') == 0)
												{
													this.setAttribute('bool_state', 1);
													zz[0].value = '1';
													this.src = p.vPicPlus;
													return false;
												}
												else if (this.getAttribute('bool_state') == 1)
												{
													zz[0].value = '0';
													this.setAttribute('bool_state', 0);
													this.src = p.vPicMinus;
													return false;
												}
											}
										}
										zz.childNodes[i].innerHTML = '';
										zz.childNodes[i].appendChild(tInput);
										zz.childNodes[i].appendChild(tImg);
									}
									else if (p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['type'] == 'select')
									{
										zz.childNodes[i].id = 'g.s_' + Math.random();
										iInput = new xSelect();
										iInput.vId = zz.childNodes[i].id;
										iInput.vPostData['id'] = zz.getAttribute('db_id');
										iInput.vPostData['field_value'] = zz.childNodes[i].innerHTML;
										iInput.vDataUrl = p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['select_data'].replace('##id##', zz.getAttribute('db_id'));
										zz.childNodes[i].innerHTML = '';
										iInput.fInit();
									}
									else if (p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['type'] == 'textarea')
									{
										iInput = document.createElement('TEXTAREA');
										
										var tStr= zz.childNodes[i].innerHTML.replace(/&lt;/ig,"<");
										tStr= tStr.replace(/&gt;/ig,">");
										tStr= tStr.replace(/&amp;/ig,"&");
										iInput.value = tStr;
										
//										iInput.value = zz.childNodes[i].innerHTML;
										if (p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['height'])
										{
											iInput.style.height = p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['height'];
										}
										zz.childNodes[i].innerHTML = '';
										zz.childNodes[i].appendChild(iInput);
									}
									else if (p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['type'] == 'date')
									{
										iInput = document.createElement('INPUT');
										iInput.value = zz.childNodes[i].innerHTML;

										Calendar.setup(
										{
											inputField		: iInput,
											ifFormat		: p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['format'],
											showsTime		: true,
											singleClick		: false,
//											date			: ttt,//new Date(),	
											step			: 1
										}
										)
										zz.childNodes[i].innerHTML = '';
										zz.childNodes[i].appendChild(iInput);
									}
									else
									{
										iInput = document.createElement('INPUT');
										var tStr= zz.childNodes[i].innerHTML.replace(/&lt;/ig,"<");
										tStr= tStr.replace(/&gt;/ig,">");
										tStr= tStr.replace(/&amp;/ig,"&");
										iInput.value = tStr;
//										iInput.value = zz.childNodes[i].innerHTML;
										zz.childNodes[i].innerHTML = '';
										zz.childNodes[i].appendChild(iInput);
									}
									if (p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['valign'])
									{
										zz.childNodes[i].style.verticalAlign = p.vFiledList[zz.childNodes[i].getAttribute('field_name')]['valign'];
									}
								}
							}
							this.src = p.vControls['edit_on_fly']['save_pic'];
							this.title = p.vControls['edit_on_fly']['title_save'];
							this.setAttribute('save', 1);
							//document.location = ControllArray['edit'];
						}
						return false;
					} 
				}
				tImg.title = this.vControls['edit_on_fly']['title'];
				tTdControl.appendChild(tImg);
/* wini  */
				tImg.style.display="none";
				
				tImgD = document.createElement('a');
				tImgD.className="button";
				tImgD.setAttribute('href', '#');
				tImgD.innerHTML = '<span>'+this.vControls['edit_on_fly']['titleD']+'</span>';
				tImgD.onclick = function (){
					if (!p.vControlLocked) {
						this.innerHTML= '<span>'+p.vControls['edit_on_fly']['title_saveD']+'</span>';
					}
					this.blur();
					tImg.click();
					return false;
				}
				
				tTdControl.appendChild(tImgD);				
//*********
				tTdControl.width = this.vPicWidth;
				tTrControl.appendChild(tTdControl);
				if (this.vControlType == 'row')
				{
					tTbodyControl.appendChild(tTrControl);
				}
			}
		}

		else if (i == 'delete')
		{
			if (this.vControls[i]['visible'])
			if (eval(this.vControls[i]['visible'].replace(/(##row_id##)/, num_row)) == 1)
			{
				if (this.vControlType == 'row')
				{
					tTrControl = document.createElement('TR');
					tTrControl.className = 's_header';
				}

				tTdControl = document.createElement('TD');
				tImg = document.createElement('IMG');
				tImg.src = this.vControls[i]['pic'];
				tImg.onmouseover = function () { if (!p.vControlLocked) {this.className = 'hand'} }
				tImg.onmouseout = function () { if (!p.vControlLocked) {this.className = ''} }
				tImg.onclick = function ()
				{
					if (!p.vControlLocked)
					{
						if (confirm(p.vControls['delete']['confirm']))
						{
							var zz = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
							p.vPostData['id'] = zz.getAttribute('db_id');
							p.fLockControl();
							p.AJAXPostData(p.vControls['delete']['url'], p.vPostData);
						}
					}
					return false;
				}
				tImg.title = this.vControls['delete']['title'];
				tTdControl.appendChild(tImg);
				tTdControl.width = this.vPicWidth;
				tTrControl.appendChild(tTdControl);
				if (this.vControlType == 'row')
				{
					tTbodyControl.appendChild(tTrControl);
				}
			}
		}

		else if (i != 'new' && i != 'tree')
		{
			
			if (this.vControls[i]['visible'])
			if (eval(this.vControls[i]['visible'].replace(/(##row_id##)/, num_row)) == 1)
			{
				if (this.vControlType == 'row')
				{
					tTrControl = document.createElement('TR');
					tTrControl.className = 's_header';
				}

				tTdControl = document.createElement('TD');

				tImg = document.createElement('IMG');
				if (((Counter == 1) && (this.vControls[i]['no_first'] == '1') && (this.vPostData['p'] == 1)) || ((this.fGetObjCount(this.vData) == Counter) && (this.vControls[i]['no_last'] == '1') && (this.vPostData['p'] == this.vPaging['total_pages'])))
				{
					tImg.src = this.vPicSpacer;
					tImg.width = this.vPicWidth;
//					alert(this.vPostData['p'] + ' ' + this.vPaging['total_pages']);
				}
				else
				{
					tImg.src = this.vControls[i]['pic'];
					tImg.setAttribute('control_name', i);
					tImg.onmouseover = function () {if (!p.vControlLocked) {this.className = 'hand'} }
					tImg.onmouseout = function () { if (!p.vControlLocked) {this.className = ''} }
					tImg.onclick = function () {
						if (!p.vControlLocked)
						{
							if (!p.vControls[this.getAttribute('control_name')]['confirm'] || confirm(p.vControls[this.getAttribute('control_name')]['confirm']))
							{
								var zz = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
								if (p.vControls[this.getAttribute('control_name')]['exec'])
								{
									eval(p.vControls[this.getAttribute('control_name')]['exec']);
								}
								
								if (p.vControls[this.getAttribute('control_name')]['url'])
								{
									p.vPostData['id'] = zz.getAttribute('db_id');
									p.AJAXPostData(p.vControls[this.getAttribute('control_name')]['url'], p.vPostData);
								}
								if (p.vControls[this.getAttribute('control_name')]['go_to_url'])
								{
									document.location = p.vControls[this.getAttribute('control_name')]['go_to_url'].replace('##id##', zz.getAttribute('db_id'));
								}
							}
						}
						return false;
					}
					tImg.title = this.vControls[i]['title'];
				}
				tTdControl.appendChild(tImg);
				tTdControl.width = this.vPicWidth;
				tTrControl.appendChild(tTdControl);

				if (this.vControlType == 'row')
				{
					tTbodyControl.appendChild(tTrControl);
				}
			}
		}
	}
	
	if (!tTrControl.firstChild)
	{
		tTdControl = document.createElement('TD');
		tTdControl.innerHTML = '&nbsp;';
		tTrControl.appendChild(tTdControl);
	}

	if (this.vControlType == 'line')
	{
		tTbodyControl.appendChild(tTrControl);
	}

	tTableControl.appendChild(tTbodyControl);

	return tTableControl;
}

Grid.prototype.fCreateSefl = function ()
{
	this.vChilds[this.vChilds.length] = new Grid;

	var tTr, tTd;
	var El = arguments[0];
	var tt = El.parentNode; //.nextSibling;

	if (tt.getAttribute('opened') == 1)
	{
		var zz = tt.parentNode;
		zz.removeChild(tt.nextSibling);
		tt.setAttribute('opened', 0);
	}
	else
	{
		tTr = document.createElement('TR');
		tTr.className = p.vMainClassName+'_tr';
		tTd = document.createElement('TD');
		tTd.colSpan = gg.fGetObjCount(gg.vFiledList) + 1;// + (this.vType=='tree'?1:0);
		tTd.id = 'grid' + Math.random();
		tTd.align = 'right';
		tTr.appendChild(tTd);
		tt.parentNode.insertBefore(tTr, tt.nextSibling);
		tt.setAttribute('opened', 1)

		this.vChilds[this.vChilds.length-1].parent = this;
		this.vChilds[this.vChilds.length-1].vId = tTd.id;
		this.vChilds[this.vChilds.length-1].vWidth = '100%';
		this.vChilds[this.vChilds.length-1].vType = this.vType;
		this.vChilds[this.vChilds.length-1].tNoDataFound = this.tNoDataFound;
		this.vChilds[this.vChilds.length-1].vPostData['pid'] = tt.getAttribute('db_id');
		this.vChilds[this.vChilds.length-1].vDataUrl = this.vDataUrl;
		this.vChilds[this.vChilds.length-1].tLoading = this.tLoading;
		this.vChilds[this.vChilds.length-1].vControls = this.vControls;
		this.vChilds[this.vChilds.length-1].vControlType = this.vControlType;
		this.vChilds[this.vChilds.length-1].fInit();
	}


}

////////////////////////////////////////////////////////////////////////////////


