function TQueryGrid() { 
		this.form = {
			PaginationFrameHalfLength : 5,
			WorkplaceDivID: "TblBody", 
			LeightIndicatorDivID: "TblLength", 
			RangeFormID: "RangeForm", 
			PaginationFormID: "PaginationForm",
			TblLength : 0,		
			StatusWindowID : "StatusWindow",

			columns : new Array(),
			filters : new Array(),

			test			:	""
		};

		this.message = {
				sort_asc	: "Сортировать по возрастанию", 
				sort_desc	: "Сортировать по убыванию", 		
				loading		:	"Загрузка данных...",					
				
				test			:	""
		};
				
		this.config = {
				dir				: "",														
				srv_url		: "controller.php",							
				icon_up		:	"",														
				icon_down	:	"",														
				lang 			: "ru",													
				rows_no		:	1,														
				context_menu:	0,													
				highlight_rows	:	0,											
				show_status	:	1,													
				
				test			:	""
		};
		
		
		this.params = {
				sql				: "",														
				limit			: 20,														
				offset		:	0,														
				fields		:	"",
				proc			:	"",
				orderby 	: "",
				direction : "",
				extra 		: "",
				test			:	""
		};
		
};


function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}


function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}


TQueryGrid.prototype.init = function (params) {
	this.config.icon_up	 = "<img src=\"" + this.config.dir + "/arr_top.gif\" width=\"13\" height=\"8\" border=\"0\" alt=\"" + this.message.sort_asc + "\">";
	this.config.icon_down	=	"<img src=\"" + this.config.dir + "/arr_bottom.gif\" width=\"13\" height=\"8\" border=\"0\" alt=\"" + this.message.sort_desc + "\">";
	if (this.config.show_status == 1) {
		var s = document.getElementById(this.form.StatusWindowID) ;// s = document.createElement('div'), s = document.createElement('div'),
		var subdiv = document.createElement('div');
		
				subdiv.style.top = "40%";// position: relative;");
				subdiv.style.position = "relative";
				subdiv.innerHTML = this.message.loading;
				s.appendChild(subdiv);
	}	
}


TQueryGrid.prototype.settings = function (obj_name, json) {
	obj = this.form[obj_name];
	obj[obj.length] = eval( '(' + json + ')' ); 
	this.params.fields = this.tableFieldList();
}

TQueryGrid.prototype.gridPaginationABC = function () {
	var Output = '';	
	return "";
}

TQueryGrid.prototype.gridPagination = function () {
	var Output = '';

	var Offset = this.params.offset;
	var Range = this.params.limit;
	
	var RangeFinish = Offset*1+Range*1;
	var PageNumber = 1;
	var Params = '';

	if(Offset>=Range) { 
		Params = Offset-Range; if(Params<0) Params=0;
		FirstFinish = 0;
		Output += '<a onclick="document.QueryGrid.setGridRange('+(FirstFinish*1)+');">&lt;&lt;</a>';
		Output += '<a onclick="document.QueryGrid.setGridRange('+(Params*1)+');">&lt;</a>';		
	} else {
		Output += '<a>&nbsp;</a>';
		Output += '<a>&nbsp;</a>';		
	}
	
	CurrentPage = Math.ceil(Offset/Range);
	PageNumber = Math.ceil(this.form.TblLength/Range);
	CurrentPage += 1;	

	for(i=1; i<=PageNumber;i++) {		
		if(i>=CurrentPage-this.form.PaginationFrameHalfLength && i<=CurrentPage+this.form.PaginationFrameHalfLength) {
			Output += (i == CurrentPage) ? '&nbsp;<b>'+i+'</b>&nbsp;' : '<a onclick="document.QueryGrid.setGridRange('+((i-1)*Range)+');">'+i+'</a>';
		}
	}

	if((this.form.TblLength>Range && !RangeFinish) || this.form.TblLength>RangeFinish) {
		Output += '<a onclick="document.QueryGrid.setGridRange('+(RangeFinish*1)+');">&gt;</a>';
		LastFinish = Range*(PageNumber-1);
		Output += '<a onclick="document.QueryGrid.setGridRange('+(LastFinish*1)+');">&gt;&gt;</a>';
	}
	return "Стр. " + Output;
}

TQueryGrid.prototype.tableFieldList = function () {
	var output = '';
	for(i=0;i<this.form.columns.length;i++) {
		output += this.form.columns[i].field+',';
	}
	return output;
}

TQueryGrid.prototype.tableHeaders = function () {
	var output = '<thead><tr>';
	output += (this.config.rows_no == 1) ? "<th>№</th>" : "";	
	for(i=0;i<this.form.columns.length;i++) {
		output += '<th width="'+this.form.columns[i].width+'" nowrap="nowrap"><table cellpadding="0" cellspacing="0" border="0"><tr><td>'+this.form.columns[i].title+'</td><td><a onclick="document.QueryGrid.sortTbl(\''+this.form.columns[i].field+'\',\'asc\')">'+this.config.icon_up+'</a><br /><a onclick="document.QueryGrid.sortTbl(\''+this.form.columns[i].field+'\',\'desc\')">'+this.config.icon_down+'</a></td></tr></table></th>';
	}
	output += '</tr></thead>';
	return output;
}

TQueryGrid.prototype.buildList = function (json) {
	var output = '';
	var res = '';
	
	var in_list = eval( '(' + json + ')' ); 
	
	this.form.TblLength = in_list.tlength;
	objLeightIndicator = document.getElementById(this.form.LeightIndicatorDivID);
	if (objLeightIndicator) objLeightIndicator.innerHTML = in_list.tlength;
	
	objPaginationForm = document.getElementById(this.form.PaginationFormID);
	if (objPaginationForm) {
		objPaginationForm.innerHTML = this.gridPagination();
		objPaginationForm.innerHTML += this.gridPaginationABC();
	}
		
	
	for(i=0;i<in_list.value.length;i++) {
		if(this.config.context_menu == 1)
			output += '<tr id="id_'+i+'" oncontextmenu="return gridShowContextMenu(\'id_'+i+'\', event)">';
		else	
			output += '<tr id="id_'+i+'" class=>';
		
	
		output += (this.config.rows_no == 1) ? "<td class=rowno>" + (this.params.offset*1+i+1) + "</td>" : "";
		for(j=0;j<in_list.columns.length;j++) {
			eval ( "res = in_list.value[i]."+in_list.columns[j]+";" );			
			output += '<td';
			output += (this.form.columns[j].style) ? " style=\""+this.form.columns[j].style+"\"" : "";
			output += '>'+res+'</td>';
		}
		output += '</tr>';
	}
	if(output.length==0) return '<tr><td colspan="5" class="grid_indication"></td></tr>';
	return this.tableHeaders()+ "<tbody>" +output+ "</tbody>";
}

TQueryGrid.prototype.sortTbl = function (field,direction) {
	this.params.orderby = field;
	this.params.direction = direction;
	return this.makeRequest();
}

TQueryGrid.prototype.setGridRange = function (offset, limit) {
	this.params.offset = offset;
	this.params.limit = (limit) ? limit : this.params.limit;
	this.makeRequest();
}

TQueryGrid.prototype.makeRequest = function (postData) {	
	if (this.form.StatusWindowID) document.getElementById(this.form.StatusWindowID).style.display = "block";
	postData = "";
  for ( property in this.params ) {
			pre = (postData) ? "&" : "";
			propertyValue = this.params[property];
			postData += (propertyValue) ? pre + property + "=" + propertyValue : "";
  }  

	setCookie ("lp", postData);
	serverRequest(this.config.srv_url, postData, callback);
}

var callback = function(obj) {
		var my_grid = document.QueryGrid;
		document.getElementById(my_grid.form.WorkplaceDivID).innerHTML = "Нет информации по запросу";
		if (my_grid.form.StatusWindowID) document.getElementById(my_grid.form.StatusWindowID).style.display = "none";
		
		var message = "";
		if (! obj) return "";
		if (! obj.responseJS) return "";
		if (! obj.responseJS.data) return "";
		message = obj.responseJS.data;		
		if(message.substr(0,1)=="{") {							
			document.getElementById(my_grid.form.WorkplaceDivID).innerHTML = '<table id="TableData" class="grid_table" cellspacing="0" cellpadding="0">' + my_grid.buildList(message) + "</table>";
		} else {
		}
		

}; 

function serverRequest(url, data, callback) {		
		var req = new JsHttpRequest();
    req.onreadystatechange  = function() {             
       if(req.readyState == 4 && callback) callback(req);
    };	
		req.loader = 'script';
    req.open(null, url, true); //POST
    req.send( { q: data } );
    return true;
}  