// ==ClosureCompiler==

// @compilation_level SIMPLE_OPTIMIZATIONS



/**

 * @license jqGrid  4.4.3  - jQuery Grid

 * Copyright (c) 2008, Tony Tomov, tony@trirand.com

 * Dual licensed under the MIT and GPL licenses

 * http://www.opensource.org/licenses/mit-license.php

 * http://www.gnu.org/licenses/gpl-2.0.html

 * Date: 2013-01-21

 */

//jsHint options

/*jshint evil:true, eqeqeq:false, eqnull:true, devel:true */

/*global jQuery, DOMParser, ActiveXObject */



(function ($) {

"use strict";

$.jgrid = $.jgrid || {};

$.extend($.jgrid,{

	version : "4.4.3",

	htmlDecode : function(value){

		if(value && (value=='&nbsp;' || value=='&#160;' || (value.length===1 && value.charCodeAt(0)===160))) { return "";}

		return !value ? value : String(value).replace(/&gt;/g, ">").replace(/&lt;/g, "<").replace(/&quot;/g, '"').replace(/&amp;/g, "&");		

	},

	htmlEncode : function (value){

		return !value ? value : String(value).replace(/&/g, "&amp;").replace(/\"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");

	},

	format : function(format){ //jqgformat

		var args = $.makeArray(arguments).slice(1);

		if(format==null) { format = ""; }

		return format.replace(/\{(\d+)\}/g, function(m, i){

			return args[i];

		});

	},

	getCellIndex : function (cell) {

		var c = $(cell);

		if (c.is('tr')) { return -1; }

		c = (!c.is('td') && !c.is('th') ? c.closest("td,th") : c)[0];

		if ($.browser.msie) { return $.inArray(c, c.parentNode.cells); }

		return c.cellIndex;

	},

	stripHtml : function(v) {

		v = String(v);

		var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;

		if (v) {

			v = v.replace(regexp,"");

			return (v && v !== '&nbsp;' && v !== '&#160;') ? v.replace(/\"/g,"'") : "";

		} 

			return v;

	},

	stripPref : function (pref, id) {

		var obj = $.type( pref );

		if( obj == "string" || obj =="number") {

			pref =  String(pref);

			id = pref !== "" ? String(id).replace(String(pref), "") : id;

		}

		return id;

	},

	stringToDoc : function (xmlString) {

		var xmlDoc;

		if(typeof xmlString !== 'string') { return xmlString; }

		try	{

			var parser = new DOMParser();

			xmlDoc = parser.parseFromString(xmlString,"text/xml");

		}

		catch(e) {

			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

			xmlDoc.async=false;

			xmlDoc.loadXML(xmlString);

		}

		return (xmlDoc && xmlDoc.documentElement && xmlDoc.documentElement.tagName != 'parsererror') ? xmlDoc : null;

	},

	parse : function(jsonString) {

		var js = jsonString;

		if (js.substr(0,9) == "while(1);") { js = js.substr(9); }

		if (js.substr(0,2) == "/*") { js = js.substr(2,js.length-4); }

		if(!js) { js = "{}"; }

		return ($.jgrid.useJSON===true && typeof JSON === 'object' && typeof JSON.parse === 'function') ?

			JSON.parse(js) :

			eval('(' + js + ')');

	},

	parseDate : function(format, date) {

		var tsp = {m : 1, d : 1, y : 1970, h : 0, i : 0, s : 0, u:0},k,hl,dM, regdate = /[\\\/:_;.,\t\T\s-]/;

		if(date && date != null){

			date = $.trim(date);

			date = date.split(regdate);

			if ($.jgrid.formatter.date.masks[format] !== undefined) {

				format = $.jgrid.formatter.date.masks[format];

			}

			format = format.split(regdate);

			var dfmt  = $.jgrid.formatter.date.monthNames;

			var afmt  = $.jgrid.formatter.date.AmPm;

			var h12to24 = function(ampm, h){

				if (ampm === 0){ if (h === 12) { h = 0;} }

				else { if (h !== 12) { h += 12; } }

				return h;

			};

			for(k=0,hl=format.length;k<hl;k++){

				if(format[k] == 'M') {

					dM = $.inArray(date[k],dfmt);

					if(dM !== -1 && dM < 12){

						date[k] = dM+1;

						tsp.m = date[k];

					}

				}

				if(format[k] == 'F') {

					dM = $.inArray(date[k],dfmt);

					if(dM !== -1 && dM > 11){

						date[k] = dM+1-12;

						tsp.m = date[k];

					}

				}

				if(format[k] == 'a') {

					dM = $.inArray(date[k],afmt);

					if(dM !== -1 && dM < 2 && date[k] == afmt[dM]){

						date[k] = dM;

						tsp.h = h12to24(date[k], tsp.h);

					}

				}

				if(format[k] == 'A') {

					dM = $.inArray(date[k],afmt);

					if(dM !== -1 && dM > 1 && date[k] == afmt[dM]){

						date[k] = dM-2;

						tsp.h = h12to24(date[k], tsp.h);

					}

				}

				if (format[k] === 'g') {

					tsp.h = parseInt(date[k], 10);

				}

				if(date[k] !== undefined) {

					tsp[format[k].toLowerCase()] = parseInt(date[k],10);

				}

			}

			tsp.m = parseInt(tsp.m,10)-1;

			var ty = tsp.y;

			if (ty >= 70 && ty <= 99) {tsp.y = 1900+tsp.y;}

			else if (ty >=0 && ty <=69) {tsp.y= 2000+tsp.y;}

			if(tsp.j !== undefined) { tsp.d = tsp.j; }

			if(tsp.n !== undefined) { tsp.m = parseInt(tsp.n,10)-1; }

		}

		return new Date(tsp.y, tsp.m, tsp.d, tsp.h, tsp.i, tsp.s, tsp.u);

	},

	jqID : function(sid){

		return String(sid).replace(/[!"#$%&'()*+,.\/:;<=>?@\[\\\]\^`{|}~]/g,"\\$&");

	},

	guid : 1,

	uidPref: 'jqg',

	randId : function( prefix )	{

		return (prefix || $.jgrid.uidPref) + ($.jgrid.guid++);

	},

	getAccessor : function(obj, expr) {

		var ret,p,prm = [], i;

		if( typeof expr === 'function') { return expr(obj); }

		ret = obj[expr];

		if(ret===undefined) {

			try {

				if ( typeof expr === 'string' ) {

					prm = expr.split('.');

				}

				i = prm.length;

				if( i ) {

					ret = obj;

					while (ret && i--) {

						p = prm.shift();

						ret = ret[p];

					}

				}

			} catch (e) {}

		}

		return ret;

	},

	getXmlData: function (obj, expr, returnObj) {

		var ret, m = typeof expr === 'string' ? expr.match(/^(.*)\[(\w+)\]$/) : null;

		if (typeof expr === 'function') { return expr(obj); }

		if (m && m[2]) {

			// m[2] is the attribute selector

			// m[1] is an optional element selector

			// examples: "[id]", "rows[page]"

			return m[1] ? $(m[1], obj).attr(m[2]) : $(obj).attr(m[2]);

		}

			ret = $(expr, obj);

			if (returnObj) { return ret; }

			//$(expr, obj).filter(':last'); // we use ':last' to be more compatible with old version of jqGrid

			return ret.length > 0 ? $(ret).text() : undefined;

	},

	cellWidth : function () {

		var $testDiv = $("<div class='ui-jqgrid' style='left:10000px'><table class='ui-jqgrid-btable' style='width:5px;'><tr class='jqgrow'><td style='width:5px;'></td></tr></table></div>"),

		testCell = $testDiv.appendTo("body")

			.find("td")

			.width();

		$testDiv.remove();

		return testCell !== 5;

	},

	cell_width : true, 

	ajaxOptions: {},

	from : function(source){

		// Original Author Hugo Bonacci

		// License MIT http://jlinq.codeplex.com/license

		var QueryObject=function(d,q){

		if(typeof d==="string"){

			d=$.data(d);

		}

		var self=this,

		_data=d,

		_usecase=true,

		_trim=false,

		_query=q,

		_stripNum = /[\$,%]/g,

		_lastCommand=null,

		_lastField=null,

		_orDepth=0,

		_negate=false,

		_queuedOperator="",

		_sorting=[],

		_useProperties=true;

		if(typeof d==="object"&&d.push) {

			if(d.length>0){

				if(typeof d[0]!=="object"){

					_useProperties=false;

				}else{

					_useProperties=true;

				}

			}

		}else{

			throw "data provides is not an array";

		}

		this._hasData=function(){

			return _data===null?false:_data.length===0?false:true;

		};

		this._getStr=function(s){

			var phrase=[];

			if(_trim){

				phrase.push("jQuery.trim(");

			}

			phrase.push("String("+s+")");

			if(_trim){

				phrase.push(")");

			}

			if(!_usecase){

				phrase.push(".toLowerCase()");

			}

			return phrase.join("");

		};

		this._strComp=function(val){

			if(typeof val==="string"){

				return".toString()";

			}

			return"";

		};

		this._group=function(f,u){

			return({field:f.toString(),unique:u,items:[]});

		};

		this._toStr=function(phrase){

			if(_trim){

				phrase=$.trim(phrase);

			}

			phrase=phrase.toString().replace(/\\/g,'\\\\').replace(/\"/g,'\\"');

			return _usecase ? phrase : phrase.toLowerCase();

		};

		this._funcLoop=function(func){

			var results=[];

			$.each(_data,function(i,v){

				results.push(func(v));

			});

			return results;

		};

		this._append=function(s){

			var i;

			if(_query===null){

				_query="";

			} else {

				_query+=_queuedOperator === "" ? " && " :_queuedOperator;

			}

			for (i=0;i<_orDepth;i++){

				_query+="(";

			}

			if(_negate){

				_query+="!";

			}

			_query+="("+s+")";

			_negate=false;

			_queuedOperator="";

			_orDepth=0;

		};

		this._setCommand=function(f,c){

			_lastCommand=f;

			_lastField=c;

		};

		this._resetNegate=function(){

			_negate=false;

		};

		this._repeatCommand=function(f,v){

			if(_lastCommand===null){

				return self;

			}

			if(f!==null&&v!==null){

				return _lastCommand(f,v);

			}

			if(_lastField===null){

				return _lastCommand(f);

			}

			if(!_useProperties){

				return _lastCommand(f);

			}

			return _lastCommand(_lastField,f);

		};

		this._equals=function(a,b){

			return(self._compare(a,b,1)===0);

		};

		this._compare=function(a,b,d){

			var toString = Object.prototype.toString;

			if( d === undefined) { d = 1; }

			if(a===undefined) { a = null; }

			if(b===undefined) { b = null; }

			if(a===null && b===null){

				return 0;

			}

			if(a===null&&b!==null){

				return 1;

			}

			if(a!==null&&b===null){

				return -1;

			}

			if (toString.call(a) === '[object Date]' && toString.call(b) === '[object Date]') {

				if (a < b) { return -d; }

				if (a > b) { return d; }

				return 0;

			}

			if(!_usecase && typeof a !== "number" && typeof b !== "number" ) {

				a=String(a);

				b=String(b);

			}

			if(a<b){return -d;}

			if(a>b){return d;}

			return 0;

		};

		this._performSort=function(){

			if(_sorting.length===0){return;}

			_data=self._doSort(_data,0);

		};

		this._doSort=function(d,q){

			var by=_sorting[q].by,

			dir=_sorting[q].dir,

			type = _sorting[q].type,

			dfmt = _sorting[q].datefmt;

			if(q==_sorting.length-1){

				return self._getOrder(d, by, dir, type, dfmt);

			}

			q++;

			var values=self._getGroup(d,by,dir,type,dfmt), results=[], i, j, sorted;

			for(i=0;i<values.length;i++){

				sorted=self._doSort(values[i].items,q);

				for(j=0;j<sorted.length;j++){

					results.push(sorted[j]);

				}

			}

			return results;

		};

		this._getOrder=function(data,by,dir,type, dfmt){

			var sortData=[],_sortData=[], newDir = dir=="a" ? 1 : -1, i,ab,j,

			findSortKey;



			if(type === undefined ) { type = "text"; }

			if (type == 'float' || type== 'number' || type== 'currency' || type== 'numeric') {

				findSortKey = function($cell) {

					var key = parseFloat( String($cell).replace(_stripNum, ''));

					return isNaN(key) ? 0.00 : key;

				};

			} else if (type=='int' || type=='integer') {

				findSortKey = function($cell) {

					return $cell ? parseFloat(String($cell).replace(_stripNum, '')) : 0;

				};

			} else if(type == 'date' || type == 'datetime') {

				findSortKey = function($cell) {

					return $.jgrid.parseDate(dfmt,$cell).getTime();

				};

			} else if($.isFunction(type)) {

				findSortKey = type;

			} else {

				findSortKey = function($cell) {

					$cell = $cell ? $.trim(String($cell)) : "";

					return _usecase ? $cell : $cell.toLowerCase();

				};

			}

			$.each(data,function(i,v){

				ab = by!=="" ? $.jgrid.getAccessor(v,by) : v;

				if(ab === undefined) { ab = ""; }

				ab = findSortKey(ab, v);

				_sortData.push({ 'vSort': ab,'index':i});

			});



			_sortData.sort(function(a,b){

				a = a.vSort;

				b = b.vSort;

				return self._compare(a,b,newDir);

			});

			j=0;

			var nrec= data.length;

			// overhead, but we do not change the original data.

			while(j<nrec) {

				i = _sortData[j].index;

				sortData.push(data[i]);

				j++;

			}

			return sortData;

		};

		this._getGroup=function(data,by,dir,type, dfmt){

			var results=[],

			group=null,

			last=null, val;

			$.each(self._getOrder(data,by,dir,type, dfmt),function(i,v){

				val = $.jgrid.getAccessor(v, by);

				if(val == null) { val = ""; }

				if(!self._equals(last,val)){

					last=val;

					if(group !== null){

						results.push(group);

					}

					group=self._group(by,val);

				}

				group.items.push(v);

			});

			if(group !== null){

				results.push(group);

			}

			return results;

		};

		this.ignoreCase=function(){

			_usecase=false;

			return self;

		};

		this.useCase=function(){

			_usecase=true;

			return self;

		};

		this.trim=function(){

			_trim=true;

			return self;

		};

		this.noTrim=function(){

			_trim=false;

			return self;

		};

		this.execute=function(){

			var match=_query, results=[];

			if(match === null){

				return self;

			}

			$.each(_data,function(){

				if(eval(match)){results.push(this);}

			});

			_data=results;

			return self;

		};

		this.data=function(){

			return _data;

		};

		this.select=function(f){

			self._performSort();

			if(!self._hasData()){ return[]; }

			self.execute();

			if($.isFunction(f)){

				var results=[];

				$.each(_data,function(i,v){

					results.push(f(v));

				});

				return results;

			}

			return _data;

		};

		this.hasMatch=function(){

			if(!self._hasData()) { return false; }

			self.execute();

			return _data.length>0;

		};

		this.andNot=function(f,v,x){

			_negate=!_negate;

			return self.and(f,v,x);

		};

		this.orNot=function(f,v,x){

			_negate=!_negate;

			return self.or(f,v,x);

		};

		this.not=function(f,v,x){

			return self.andNot(f,v,x);

		};

		this.and=function(f,v,x){

			_queuedOperator=" && ";

			if(f===undefined){

				return self;

			}

			return self._repeatCommand(f,v,x);

		};

		this.or=function(f,v,x){

			_queuedOperator=" || ";

			if(f===undefined) { return self; }

			return self._repeatCommand(f,v,x);

		};

		this.orBegin=function(){

			_orDepth++;

			return self;

		};

		this.orEnd=function(){

			if (_query !== null){

				_query+=")";

			}

			return self;

		};

		this.isNot=function(f){

			_negate=!_negate;

			return self.is(f);

		};

		this.is=function(f){

			self._append('this.'+f);

			self._resetNegate();

			return self;

		};

		this._compareValues=function(func,f,v,how,t){

			var fld;

			if(_useProperties){

				fld='jQuery.jgrid.getAccessor(this,\''+f+'\')';

			}else{

				fld='this';

			}

			if(v===undefined) { v = null; }

			//var val=v===null?f:v,

			var val =v,

			swst = t.stype === undefined ? "text" : t.stype;

			if(v !== null) {

			switch(swst) {

				case 'int':

				case 'integer':

					val = (isNaN(Number(val)) || val==="") ? '0' : val; // To be fixed with more inteligent code

					fld = 'parseInt('+fld+',10)';

					val = 'parseInt('+val+',10)';

					break;

				case 'float':

				case 'number':

				case 'numeric':

					val = String(val).replace(_stripNum, '');

					val = (isNaN(Number(val)) || val==="") ? '0' : val; // To be fixed with more inteligent code

					fld = 'parseFloat('+fld+')';

					val = 'parseFloat('+val+')';

					break;

				case 'date':

				case 'datetime':

					val = String($.jgrid.parseDate(t.newfmt || 'Y-m-d',val).getTime());

					fld = 'jQuery.jgrid.parseDate("'+t.srcfmt+'",'+fld+').getTime()';

					break;

				default :

					fld=self._getStr(fld);

					val=self._getStr('"'+self._toStr(val)+'"');

			}

			}

			self._append(fld+' '+how+' '+val);

			self._setCommand(func,f);

			self._resetNegate();

			return self;

		};

		this.equals=function(f,v,t){

			return self._compareValues(self.equals,f,v,"==",t);

		};

		this.notEquals=function(f,v,t){

			return self._compareValues(self.equals,f,v,"!==",t);

		};

		this.isNull = function(f,v,t){

			return self._compareValues(self.equals,f,null,"===",t);

		};

		this.greater=function(f,v,t){

			return self._compareValues(self.greater,f,v,">",t);

		};

		this.less=function(f,v,t){

			return self._compareValues(self.less,f,v,"<",t);

		};

		this.greaterOrEquals=function(f,v,t){

			return self._compareValues(self.greaterOrEquals,f,v,">=",t);

		};

		this.lessOrEquals=function(f,v,t){

			return self._compareValues(self.lessOrEquals,f,v,"<=",t);

		};

		this.startsWith=function(f,v){

			var val = (v==null) ? f: v,

			length=_trim ? $.trim(val.toString()).length : val.toString().length;

			if(_useProperties){

				self._append(self._getStr('jQuery.jgrid.getAccessor(this,\''+f+'\')')+'.substr(0,'+length+') == '+self._getStr('"'+self._toStr(v)+'"'));

			}else{

				length=_trim?$.trim(v.toString()).length:v.toString().length;

				self._append(self._getStr('this')+'.substr(0,'+length+') == '+self._getStr('"'+self._toStr(f)+'"'));

			}

			self._setCommand(self.startsWith,f);

			self._resetNegate();

			return self;

		};

		this.endsWith=function(f,v){

			var val = (v==null) ? f: v,

			length=_trim ? $.trim(val.toString()).length:val.toString().length;

			if(_useProperties){

				self._append(self._getStr('jQuery.jgrid.getAccessor(this,\''+f+'\')')+'.substr('+self._getStr('jQuery.jgrid.getAccessor(this,\''+f+'\')')+'.length-'+length+','+length+') == "'+self._toStr(v)+'"');

			} else {

				self._append(self._getStr('this')+'.substr('+self._getStr('this')+'.length-"'+self._toStr(f)+'".length,"'+self._toStr(f)+'".length) == "'+self._toStr(f)+'"');

			}

			self._setCommand(self.endsWith,f);self._resetNegate();

			return self;

		};

		this.contains=function(f,v){

			if(_useProperties){

				self._append(self._getStr('jQuery.jgrid.getAccessor(this,\''+f+'\')')+'.indexOf("'+self._toStr(v)+'",0) > -1');

			}else{

				self._append(self._getStr('this')+'.indexOf("'+self._toStr(f)+'",0) > -1');

			}

			self._setCommand(self.contains,f);

			self._resetNegate();

			return self;

		};

		this.groupBy=function(by,dir,type, datefmt){

			if(!self._hasData()){

				return null;

			}

			return self._getGroup(_data,by,dir,type, datefmt);

		};

		this.orderBy=function(by,dir,stype, dfmt){

			dir = dir == null ? "a" :$.trim(dir.toString().toLowerCase());

			if(stype == null) { stype = "text"; }

			if(dfmt == null) { dfmt = "Y-m-d"; }

			if(dir=="desc"||dir=="descending"){dir="d";}

			if(dir=="asc"||dir=="ascending"){dir="a";}

			_sorting.push({by:by,dir:dir,type:stype, datefmt: dfmt});

			return self;

		};

		return self;

		};

	return new QueryObject(source,null);

	},

	getMethod: function (name) {

        return this.getAccessor($.fn.jqGrid, name);

	},

	extend : function(methods) {

		$.extend($.fn.jqGrid,methods);

		if (!this.no_legacy_api) {

			$.fn.extend(methods);

		}

	}

});



$.fn.jqGrid = function( pin ) {

	if (typeof pin === 'string') {

		var fn = $.jgrid.getMethod(pin);

		if (!fn) {

			throw ("jqGrid - No such method: " + pin);

		}

		var args = $.makeArray(arguments).slice(1);

		return fn.apply(this,args);

	}

	return this.each( function() {

		if(this.grid) {return;}



		var p = $.extend(true,{

			url: "",

			height: 150,

			page: 1,

			rowNum: 20,

			rowTotal : null,

			records: 0,

			pager: "",

			pgbuttons: true,

			pginput: true,

			colModel: [],

			rowList: [],

			colNames: [],

			sortorder: "asc",

			sortname: "",

			datatype: "xml",

			mtype: "GET",

			altRows: false,

			selarrrow: [],

			savedRow: [],

			shrinkToFit: true,

			xmlReader: {},

			jsonReader: {},

			subGrid: false,

			subGridModel :[],

			reccount: 0,

			lastpage: 0,

			lastsort: 0,

			selrow: null,

			beforeSelectRow: null,

			onSelectRow: null,

			onSortCol: null,

			ondblClickRow: null,

			onRightClickRow: null,

			onPaging: null,

			onSelectAll: null,

			loadComplete: null,

			gridComplete: null,

			loadError: null,

			loadBeforeSend: null,

			afterInsertRow: null,

			beforeRequest: null,

			beforeProcessing : null,

			onHeaderClick: null,

			viewrecords: false,

			loadonce: false,

			multiselect: false,

			multikey: false,

			editurl: null,

			search: false,

			caption: "",

			hidegrid: true,

			hiddengrid: false,

			postData: {},

			userData: {},

			treeGrid : false,

			treeGridModel : 'nested',

			treeReader : {},

			treeANode : -1,

			ExpandColumn: null,

			tree_root_level : 0,

			prmNames: {page:"page",rows:"rows", sort: "sidx",order: "sord", search:"_search", nd:"nd", id:"id",oper:"oper",editoper:"edit",addoper:"add",deloper:"del", subgridid:"id", npage: null, totalrows:"totalrows"},

			forceFit : false,

			gridstate : "visible",

			cellEdit: false,

			cellsubmit: "remote",

			nv:0,

			loadui: "enable",

			toolbar: [false,""],

			scroll: false,

			multiboxonly : false,

			deselectAfterSort : true,

			scrollrows : false,

			autowidth: false,

			scrollOffset :18,

			cellLayout: 5,

			subGridWidth: 20,

			multiselectWidth: 20,

			gridview: false,

			rownumWidth: 25,

			rownumbers : false,

			pagerpos: 'center',

			recordpos: 'right',

			footerrow : false,

			userDataOnFooter : false,

			hoverrows : true,

			altclass : 'ui-priority-secondary',

			viewsortcols : [false,'vertical',true],

			resizeclass : '',

			autoencode : false,

			remapColumns : [],

			ajaxGridOptions :{},

			direction : "ltr",

			toppager: false,

			headertitles: false,

			scrollTimeout: 40,

			data : [],

			_index : {},

			grouping : false,

			groupingView : {groupField:[],groupOrder:[], groupText:[],groupColumnShow:[],groupSummary:[], showSummaryOnHide: false, sortitems:[], sortnames:[], summary:[],summaryval:[], plusicon: 'ui-icon-circlesmall-plus', minusicon: 'ui-icon-circlesmall-minus', displayField: []},

			ignoreCase : false,

			cmTemplate : {},

			idPrefix : ""

		}, $.jgrid.defaults, pin || {});

		var ts= this, grid={

			headers:[],

			cols:[],

			footers: [],

			dragStart: function(i,x,y) {

				this.resizing = { idx: i, startX: x.clientX, sOL : y[0]};

				this.hDiv.style.cursor = "col-resize";

				this.curGbox = $("#rs_m"+$.jgrid.jqID(p.id),"#gbox_"+$.jgrid.jqID(p.id));

				this.curGbox.css({display:"block",left:y[0],top:y[1],height:y[2]});

				$(ts).triggerHandler("jqGridResizeStart", [x, i]);

				if($.isFunction(p.resizeStart)) { p.resizeStart.call(this,x,i); }

				document.onselectstart=function(){return false;};

			},

			dragMove: function(x) {

				if(this.resizing) {

					var diff = x.clientX-this.resizing.startX,

					h = this.headers[this.resizing.idx],

					newWidth = p.direction === "ltr" ? h.width + diff : h.width - diff, hn, nWn;

					if(newWidth > 33) {

						this.curGbox.css({left:this.resizing.sOL+diff});

						if(p.forceFit===true ){

							hn = this.headers[this.resizing.idx+p.nv];

							nWn = p.direction === "ltr" ? hn.width - diff : hn.width + diff;

							if(nWn >33) {

								h.newWidth = newWidth;

								hn.newWidth = nWn;

							}

						} else {

							this.newWidth = p.direction === "ltr" ? p.tblwidth+diff : p.tblwidth-diff;

							h.newWidth = newWidth;

						}

					}

				}

			},

			dragEnd: function() {

				this.hDiv.style.cursor = "default";

				if(this.resizing) {

					var idx = this.resizing.idx,

					nw = this.headers[idx].newWidth || this.headers[idx].width;

					nw = parseInt(nw,10);

					this.resizing = false;

					$("#rs_m"+$.jgrid.jqID(p.id)).css("display","none");

					p.colModel[idx].width = nw;

					this.headers[idx].width = nw;

					this.headers[idx].el.style.width = nw + "px";

					this.cols[idx].style.width = nw+"px";

					if(this.footers.length>0) {this.footers[idx].style.width = nw+"px";}

					if(p.forceFit===true){

						nw = this.headers[idx+p.nv].newWidth || this.headers[idx+p.nv].width;

						this.headers[idx+p.nv].width = nw;

						this.headers[idx+p.nv].el.style.width = nw + "px";

						this.cols[idx+p.nv].style.width = nw+"px";

						if(this.footers.length>0) {this.footers[idx+p.nv].style.width = nw+"px";}

						p.colModel[idx+p.nv].width = nw;

					} else {

						p.tblwidth = this.newWidth || p.tblwidth;

						$('table:first',this.bDiv).css("width",p.tblwidth+"px");

						$('table:first',this.hDiv).css("width",p.tblwidth+"px");

						this.hDiv.scrollLeft = this.bDiv.scrollLeft;

						if(p.footerrow) {

							$('table:first',this.sDiv).css("width",p.tblwidth+"px");

							this.sDiv.scrollLeft = this.bDiv.scrollLeft;

						}

					}

					$(ts).triggerHandler("jqGridResizeStop", [nw, idx]);

					if($.isFunction(p.resizeStop)) { p.resizeStop.call(this,nw,idx); }

				}

				this.curGbox = null;

				document.onselectstart=function(){return true;};

			},

			populateVisible: function() {

				if (grid.timer) { clearTimeout(grid.timer); }

				grid.timer = null;

				var dh = $(grid.bDiv).height();

				if (!dh) { return; }

				var table = $("table:first", grid.bDiv);

				var rows, rh;

				if(table[0].rows.length) {

					try {

						rows = table[0].rows[1];

						rh = rows ? $(rows).outerHeight() || grid.prevRowHeight : grid.prevRowHeight;

					} catch (pv) {

						rh = grid.prevRowHeight;

					}

				}

				if (!rh) { return; }

				grid.prevRowHeight = rh;

				var rn = p.rowNum;

				var scrollTop = grid.scrollTop = grid.bDiv.scrollTop;

				var ttop = Math.round(table.position().top) - scrollTop;

				var tbot = ttop + table.height();

				var div = rh * rn;

				var page, npage, empty;

				if ( tbot < dh && ttop <= 0 &&

					(p.lastpage===undefined||parseInt((tbot + scrollTop + div - 1) / div,10) <= p.lastpage))

				{

					npage = parseInt((dh - tbot + div - 1) / div,10);

					if (tbot >= 0 || npage < 2 || p.scroll === true) {

						page = Math.round((tbot + scrollTop) / div) + 1;

						ttop = -1;

					} else {

						ttop = 1;

					}

				}

				if (ttop > 0) {

					page = parseInt(scrollTop / div,10) + 1;

					npage = parseInt((scrollTop + dh) / div,10) + 2 - page;

					empty = true;

				}

				if (npage) {

					if (p.lastpage && (page > p.lastpage || p.lastpage==1 || (page === p.page && page===p.lastpage)) ) {

						return;

					}

					if (grid.hDiv.loading) {

						grid.timer = setTimeout(grid.populateVisible, p.scrollTimeout);

					} else {

						p.page = page;

						if (empty) {

							grid.selectionPreserver(table[0]);

							grid.emptyRows.call(table[0], false, false);

						}

						grid.populate(npage);

					}

				}

			},

			scrollGrid: function( e ) {

				if(p.scroll) {

					var scrollTop = grid.bDiv.scrollTop;

					if(grid.scrollTop === undefined) { grid.scrollTop = 0; }

					if (scrollTop != grid.scrollTop) {

						grid.scrollTop = scrollTop;

						if (grid.timer) { clearTimeout(grid.timer); }

						grid.timer = setTimeout(grid.populateVisible, p.scrollTimeout);

					}

				}

				grid.hDiv.scrollLeft = grid.bDiv.scrollLeft;

				if(p.footerrow) {

					grid.sDiv.scrollLeft = grid.bDiv.scrollLeft;

				}

				if( e ) { e.stopPropagation(); }

			},

			selectionPreserver : function(ts) {

				var p = ts.p,

				sr = p.selrow, sra = p.selarrrow ? $.makeArray(p.selarrrow) : null,

				left = ts.grid.bDiv.scrollLeft,

				restoreSelection = function() {

					var i;

					p.selrow = null;

					p.selarrrow = [];

					if(p.multiselect && sra && sra.length>0) {

						for(i=0;i<sra.length;i++){

							if (sra[i] != sr) {

								$(ts).jqGrid("setSelection",sra[i],false, null);

							}

						}

					}

					if (sr) {

						$(ts).jqGrid("setSelection",sr,false,null);

					}

					ts.grid.bDiv.scrollLeft = left;

					$(ts).unbind('.selectionPreserver', restoreSelection);

				};

				$(ts).bind('jqGridGridComplete.selectionPreserver', restoreSelection);				

			}

		};

		if(this.tagName.toUpperCase()!='TABLE') {

			alert("Element is not a table");

			return;

		}

		if(document.documentMode !== undefined ) { // IE only

			if(document.documentMode <= 5) {

				alert("Grid can not be used in this ('quirks') mode!");

				return;

			}

		}

		$(this).empty().attr("tabindex","0");

		this.p = p ;

		this.p.useProp = !!$.fn.prop;

		var i, dir;

		if(this.p.colNames.length === 0) {

			for (i=0;i<this.p.colModel.length;i++){

				this.p.colNames[i] = this.p.colModel[i].label || this.p.colModel[i].name;

			}

		}

		if( this.p.colNames.length !== this.p.colModel.length ) {

			alert($.jgrid.errors.model);

			return;

		}

		var gv = $("<div class='ui-jqgrid-view'></div>"), ii,

		isMSIE = $.browser.msie ? true:false;

		ts.p.direction = $.trim(ts.p.direction.toLowerCase());

		if($.inArray(ts.p.direction,["ltr","rtl"]) == -1) { ts.p.direction = "ltr"; }

		dir = ts.p.direction;



		$(gv).insertBefore(this);

		$(this).appendTo(gv).removeClass("scroll");

		var eg = $("<div class='ui-jqgrid ui-widget ui-widget-content ui-corner-all'></div>");

		$(eg).insertBefore(gv).attr({"id" : "gbox_"+this.id,"dir":dir});

		$(gv).appendTo(eg).attr("id","gview_"+this.id);

		if (isMSIE && $.browser.version <= 6) {

			ii = '<iframe style="display:block;position:absolute;z-index:-1;filter:Alpha(Opacity=\'0\');" src="javascript:false;"></iframe>';

		} else { ii="";}

		$("<div class='ui-widget-overlay jqgrid-overlay' id='lui_"+this.id+"'></div>").append(ii).insertBefore(gv);

		$("<div class='loading ui-state-default ui-state-active' id='load_"+this.id+"'>"+this.p.loadtext+"</div>").insertBefore(gv);

		$(this).attr({cellspacing:"0",cellpadding:"0",border:"0","role":"grid","aria-multiselectable":!!this.p.multiselect,"aria-labelledby":"gbox_"+this.id});

		var sortkeys = ["shiftKey","altKey","ctrlKey"],

		intNum = function(val,defval) {

			val = parseInt(val,10);

			if (isNaN(val)) { return defval || 0;}

			return val;

		},

		formatCol = function (pos, rowInd, tv, rawObject, rowId, rdata){

			var cm = ts.p.colModel[pos],

			ral = cm.align, result="style=\"", clas = cm.classes, nm = cm.name, celp, acp=[];

			if(ral) { result += "text-align:"+ral+";"; }

			if(cm.hidden===true) { result += "display:none;"; }

			if(rowInd===0) {

				result += "width: "+grid.headers[pos].width+"px;";

			} else if (cm.cellattr && $.isFunction(cm.cellattr))

			{

				celp = cm.cellattr.call(ts, rowId, tv, rawObject, cm, rdata);

				if(celp && typeof celp === "string") {

					celp = celp.replace(/style/i,'style').replace(/title/i,'title');

					if(celp.indexOf('title') > -1) { cm.title=false;}

					if(celp.indexOf('class') > -1) { clas = undefined;}

					acp = celp.split("style");

					if(acp.length === 2 ) {

						acp[1] =  $.trim(acp[1].replace("=",""));

						if(acp[1].indexOf("'") === 0 || acp[1].indexOf('"') === 0) {

							acp[1] = acp[1].substring(1);

						}

						result += acp[1].replace(/'/gi,'"');

					} else {

						result += "\"";

					}

				}

			}

			if(!acp.length) { acp[0] = ""; result += "\"";}

			result += (clas !== undefined ? (" class=\""+clas+"\"") :"") + ((cm.title && tv) ? (" title=\""+$.jgrid.stripHtml(tv)+"\"") :"");

			result += " aria-describedby=\""+ts.p.id+"_"+nm+"\"";

			return result + acp[0];

		},

		cellVal =  function (val) {

			return val == null || val === "" ? "&#160;" : (ts.p.autoencode ? $.jgrid.htmlEncode(val) : String(val));

		},

		formatter = function (rowId, cellval , colpos, rwdat, _act){

			var cm = ts.p.colModel[colpos],v;

			if(cm.formatter !== undefined) {

				rowId = String(ts.p.idPrefix) !== "" ? $.jgrid.stripPref(ts.p.idPrefix, rowId) : rowId;

				var opts= {rowId: rowId, colModel:cm, gid:ts.p.id, pos:colpos };

				if($.isFunction( cm.formatter ) ) {

					v = cm.formatter.call(ts,cellval,opts,rwdat,_act);

				} else if($.fmatter){

					v = $.fn.fmatter.call(ts,cm.formatter,cellval,opts,rwdat,_act);

				} else {

					v = cellVal(cellval);

				}

			} else {

				v = cellVal(cellval);

			}

			return v;

		},

		addCell = function(rowId,cell,pos,irow, srvr, rdata) {

			var v,prp;

			v = formatter(rowId,cell,pos,srvr,'add');

			prp = formatCol( pos,irow, v, srvr, rowId, rdata);

			return "<td role=\"gridcell\" "+prp+">"+v+"</td>";

		},

		addMulti = function(rowid,pos,irow,checked){

			var	v = "<input role=\"checkbox\" type=\"checkbox\""+" id=\"jqg_"+ts.p.id+"_"+rowid+"\" class=\"cbox\" name=\"jqg_"+ts.p.id+"_"+rowid+"\"" + (checked ? "checked=\"checked\"" : "")+"/>",

			prp = formatCol( pos,irow,'',null, rowid, true);

			return "<td role=\"gridcell\" "+prp+">"+v+"</td>";

		},

		addRowNum = function (pos,irow,pG,rN) {

			var v =  (parseInt(pG,10)-1)*parseInt(rN,10)+1+irow,

			prp = formatCol( pos,irow,v, null, irow, true);

			return "<td role=\"gridcell\" class=\"ui-state-default jqgrid-rownum\" "+prp+">"+v+"</td>";

		},

		reader = function (datatype) {

			var field, f=[], j=0, i;

			for(i =0; i<ts.p.colModel.length; i++){

				field = ts.p.colModel[i];

				if (field.name !== 'cb' && field.name !=='subgrid' && field.name !=='rn') {

					f[j]= datatype == "local" ?

					field.name :

					( (datatype=="xml" || datatype === "xmlstring") ? field.xmlmap || field.name : field.jsonmap || field.name );

					j++;

				}

			}

			return f;

		},

		orderedCols = function (offset) {

			var order = ts.p.remapColumns;

			if (!order || !order.length) {

				order = $.map(ts.p.colModel, function(v,i) { return i; });

			}

			if (offset) {

				order = $.map(order, function(v) { return v<offset?null:v-offset; });

			}

			return order;

		},

		emptyRows = function (scroll, locdata) {

			var firstrow;

			if (this.p.deepempty) {

				$(this.rows).slice(1).remove();

			} else {

				firstrow = this.rows.length > 0 ? this.rows[0] : null;

				$(this.firstChild).empty().append(firstrow);

			}

			if (scroll && this.p.scroll) {

				$(this.grid.bDiv.firstChild).css({height: "auto"});

				$(this.grid.bDiv.firstChild.firstChild).css({height: 0, display: "none"});

				if (this.grid.bDiv.scrollTop !== 0) {

					this.grid.bDiv.scrollTop = 0;

				}

			}

			if(locdata === true && this.p.treeGrid) {

				this.p.data = []; this.p._index = {};

			}

		},

		refreshIndex = function() {

			var datalen = ts.p.data.length, idname, i, val,

			ni = ts.p.rownumbers===true ? 1 :0,

			gi = ts.p.multiselect ===true ? 1 :0,

			si = ts.p.subGrid===true ? 1 :0;



			if(ts.p.keyIndex === false || ts.p.loadonce === true) {

				idname = ts.p.localReader.id;

			} else {

				idname = ts.p.colModel[ts.p.keyIndex+gi+si+ni].name;

			}

			for(i =0;i < datalen; i++) {

				val = $.jgrid.getAccessor(ts.p.data[i],idname);

				if (val === undefined) { val=String(i+1); }

				ts.p._index[val] = i;

			}

		},

		constructTr = function(id, hide, altClass, rd, cur, selected) {

			var tabindex = '-1', restAttr = '', attrName, style = hide ? 'display:none;' : '',

				classes = 'ui-widget-content jqgrow ui-row-' + ts.p.direction + altClass + (selected ? ' ui-state-highlight' : ''),

				rowAttrObj = $.isFunction(ts.p.rowattr) ? ts.p.rowattr.call(ts, rd, cur) : {};

			if(!$.isEmptyObject( rowAttrObj )) {

				if (rowAttrObj.hasOwnProperty("id")) {

					id = rowAttrObj.id;

					delete rowAttrObj.id;

				}

				if (rowAttrObj.hasOwnProperty("tabindex")) {

					tabindex = rowAttrObj.tabindex;

					delete rowAttrObj.tabindex;

				}

				if (rowAttrObj.hasOwnProperty("style")) {

					style += rowAttrObj.style;

					delete rowAttrObj.style;

				}

				if (rowAttrObj.hasOwnProperty("class")) {

					classes += ' ' + rowAttrObj['class'];

					delete rowAttrObj['class'];

				}

				// dot't allow to change role attribute

				try { delete rowAttrObj.role; } catch(ra){}

				for (attrName in rowAttrObj) {

					if (rowAttrObj.hasOwnProperty(attrName)) {

						restAttr += ' ' + attrName + '=' + rowAttrObj[attrName];

					}

				}

			}

			return '<tr role="row" id="' + id + '" tabindex="' + tabindex + '" class="' + classes + '"' +

				(style === '' ? '' : ' style="' + style + '"') + restAttr + '>';

		},

		addXmlData = function (xml,t, rcnt, more, adjust) {

			var startReq = new Date(),

			locdata = (ts.p.datatype != "local" && ts.p.loadonce) || ts.p.datatype == "xmlstring",

			xmlid = "_id_", xmlRd = ts.p.xmlReader,

			frd = ts.p.datatype == "local" ? "local" : "xml";

			if(locdata) {

				ts.p.data = [];

				ts.p._index = {};

				ts.p.localReader.id = xmlid;

			}

			ts.p.reccount = 0;

			if($.isXMLDoc(xml)) {

				if(ts.p.treeANode===-1 && !ts.p.scroll) {

					emptyRows.call(ts, false, true);

					rcnt=1;

				} else { rcnt = rcnt > 1 ? rcnt :1; }

			} else { return; }

			var self= $(ts), i,fpos,ir=0,v,gi=ts.p.multiselect===true?1:0,si=0,addSubGridCell,ni=ts.p.rownumbers===true?1:0,idn, getId,f=[],F,rd ={}, xmlr,rid, rowData=[], cn=(ts.p.altRows === true) ? " "+ts.p.altclass:"",cn1;

			if(ts.p.subGrid===true) {

				si = 1;

				addSubGridCell = $.jgrid.getMethod("addSubGridCell");

			}

			if(!xmlRd.repeatitems) {f = reader(frd);}

			if( ts.p.keyIndex===false) {

				idn = $.isFunction( xmlRd.id ) ?  xmlRd.id.call(ts, xml) : xmlRd.id;

			} else {

				idn = ts.p.keyIndex;

			}

			if(f.length>0 && !isNaN(idn)) {

				if (ts.p.remapColumns && ts.p.remapColumns.length) {

					idn = $.inArray(idn, ts.p.remapColumns);

				}

				idn=f[idn];

			}

			if( String(idn).indexOf("[") === -1 ) {

				if (f.length) {

					getId = function( trow, k) {return $(idn,trow).text() || k;};

				} else {

					getId = function( trow, k) {return $(xmlRd.cell,trow).eq(idn).text() || k;};

				}

			}

			else {

				getId = function( trow, k) {return trow.getAttribute(idn.replace(/[\[\]]/g,"")) || k;};

			}

			ts.p.userData = {};

			ts.p.page = $.jgrid.getXmlData( xml,xmlRd.page ) || ts.p.page || 0;

			ts.p.lastpage = $.jgrid.getXmlData( xml,xmlRd.total );

			if(ts.p.lastpage===undefined) { ts.p.lastpage=1; }

			ts.p.records = $.jgrid.getXmlData( xml,xmlRd.records ) || 0;

			if($.isFunction(xmlRd.userdata)) {

				ts.p.userData = xmlRd.userdata.call(ts, xml) || {};

			} else {

				$.jgrid.getXmlData(xml, xmlRd.userdata, true).each(function() {ts.p.userData[this.getAttribute("name")]= $(this).text();});

			}

			var gxml = $.jgrid.getXmlData( xml, xmlRd.root, true);

			gxml = $.jgrid.getXmlData( gxml, xmlRd.row, true);

			if (!gxml) { gxml = []; }

			var gl = gxml.length, j=0, grpdata=[], rn = parseInt(ts.p.rowNum,10), br=ts.p.scroll?$.jgrid.randId():1, altr;

			if (gl > 0 &&  ts.p.page <= 0) { ts.p.page = 1; }

			if(gxml && gl){

			if (adjust) { rn *= adjust+1; }

			var afterInsRow = $.isFunction(ts.p.afterInsertRow), hiderow=false, groupingPrepare;

			if(ts.p.grouping)  {

				hiderow = ts.p.groupingView.groupCollapse === true;

				groupingPrepare = $.jgrid.getMethod("groupingPrepare");

			}

			while (j<gl) {

				xmlr = gxml[j];

				rid = getId(xmlr,br+j);

				rid  = ts.p.idPrefix + rid;

				altr = rcnt === 0 ? 0 : rcnt+1;

				cn1 = (altr+j)%2 == 1 ? cn : '';

				var iStartTrTag = rowData.length;

				rowData.push("");

				if( ni ) {

					rowData.push( addRowNum(0,j,ts.p.page,ts.p.rowNum) );

				}

				if( gi ) {

					rowData.push( addMulti(rid,ni,j, false) );

				}

				if( si ) {

					rowData.push( addSubGridCell.call(self,gi+ni,j+rcnt) );

				}

				if(xmlRd.repeatitems){

					if (!F) { F=orderedCols(gi+si+ni); }

					var cells = $.jgrid.getXmlData( xmlr, xmlRd.cell, true);

					$.each(F, function (k) {

						var cell = cells[this];

						if (!cell) { return false; }

						v = cell.textContent || cell.text;

						rd[ts.p.colModel[k+gi+si+ni].name] = v;

						rowData.push( addCell(rid,v,k+gi+si+ni,j+rcnt,xmlr, rd) );

					});

				} else {

					for(i = 0; i < f.length;i++) {

						v = $.jgrid.getXmlData( xmlr, f[i]);

						rd[ts.p.colModel[i+gi+si+ni].name] = v;

						rowData.push( addCell(rid, v, i+gi+si+ni, j+rcnt, xmlr, rd) );

					}

				}

				rowData[iStartTrTag] = constructTr(rid, hiderow, cn1, rd, xmlr, false);

				rowData.push("</tr>");

				if(ts.p.grouping) {

					grpdata = groupingPrepare.call(self,rowData, grpdata, rd, j);

					rowData = [];

				}

				if(locdata || ts.p.treeGrid === true) {

					rd[xmlid] = rid;

					ts.p.data.push(rd);

					ts.p._index[rid] = ts.p.data.length-1;

				}

				if(ts.p.gridview === false ) {

					$("tbody:first",t).append(rowData.join(''));

					self.triggerHandler("jqGridAfterInsertRow", [rid, rd, xmlr]);

					if(afterInsRow) {ts.p.afterInsertRow.call(ts,rid,rd,xmlr);}

					rowData=[];

				}

				rd={};

				ir++;

				j++;

				if(ir==rn) {break;}

			}

			}

			if(ts.p.gridview === true) {

				fpos = ts.p.treeANode > -1 ? ts.p.treeANode: 0;

				if(ts.p.grouping) {

					self.jqGrid('groupingRender',grpdata,ts.p.colModel.length);

					grpdata = null;

				} else if(ts.p.treeGrid === true && fpos > 0) {

					$(ts.rows[fpos]).after(rowData.join(''));

				} else {

					$("tbody:first",t).append(rowData.join(''));

				}

			}

			if(ts.p.subGrid === true ) {

				try {self.jqGrid("addSubGrid",gi+ni);} catch (_){}

			}

			ts.p.totaltime = new Date() - startReq;

			if(ir>0) { if(ts.p.records===0) { ts.p.records=gl;} }

			rowData =null;

			if( ts.p.treeGrid === true) {

				try {self.jqGrid("setTreeNode", fpos+1, ir+fpos+1);} catch (e) {}

			}

			if(!ts.p.treeGrid && !ts.p.scroll) {ts.grid.bDiv.scrollTop = 0;}

			ts.p.reccount=ir;

			ts.p.treeANode = -1;

			if(ts.p.userDataOnFooter) { self.jqGrid("footerData","set",ts.p.userData,true); }

			if(locdata) {

				ts.p.records = gl;

				ts.p.lastpage = Math.ceil(gl/ rn);

			}

			if (!more) { ts.updatepager(false,true); }

			if(locdata) {

				while (ir<gl) {

					xmlr = gxml[ir];

					rid = getId(xmlr,ir+br);

					rid  = ts.p.idPrefix + rid;

					if(xmlRd.repeatitems){

						if (!F) { F=orderedCols(gi+si+ni); }

						var cells2 = $.jgrid.getXmlData( xmlr, xmlRd.cell, true);

						$.each(F, function (k) {

							var cell = cells2[this];

							if (!cell) { return false; }

							v = cell.textContent || cell.text;

							rd[ts.p.colModel[k+gi+si+ni].name] = v;

						});

					} else {

						for(i = 0; i < f.length;i++) {

							v = $.jgrid.getXmlData( xmlr, f[i]);

							rd[ts.p.colModel[i+gi+si+ni].name] = v;

						}

					}

					rd[xmlid] = rid;

					ts.p.data.push(rd);

					ts.p._index[rid] = ts.p.data.length-1;

					rd = {};

					ir++;

				}

			}

		},

		addJSONData = function(data,t, rcnt, more, adjust) {

			var startReq = new Date();

			if(data) {

				if(ts.p.treeANode === -1 && !ts.p.scroll) {

					emptyRows.call(ts, false, true);

					rcnt=1;

				} else { rcnt = rcnt > 1 ? rcnt :1; }

			} else { return; }



			var dReader, locid = "_id_", frd,

			locdata = (ts.p.datatype != "local" && ts.p.loadonce) || ts.p.datatype == "jsonstring";

			if(locdata) { ts.p.data = []; ts.p._index = {}; ts.p.localReader.id = locid;}

			ts.p.reccount = 0;

			if(ts.p.datatype == "local") {

				dReader =  ts.p.localReader;

				frd= 'local';

			} else {

				dReader =  ts.p.jsonReader;

				frd='json';

			}

			var self = $(ts), ir=0,v,i,j,f=[],F,cur,gi=ts.p.multiselect?1:0,si=0,addSubGridCell,ni=ts.p.rownumbers===true?1:0,len,drows,idn,rd={}, fpos, idr,rowData=[],cn=(ts.p.altRows === true) ? " "+ts.p.altclass:"",cn1,lp;

			ts.p.page = $.jgrid.getAccessor(data,dReader.page) || ts.p.page || 0;

			lp = $.jgrid.getAccessor(data,dReader.total);

			if(ts.p.subGrid===true) {

				si = 1;

				addSubGridCell = $.jgrid.getMethod("addSubGridCell");

			}

			ts.p.lastpage = lp === undefined ? 1 : lp;

			ts.p.records = $.jgrid.getAccessor(data,dReader.records) || 0;

			ts.p.userData = $.jgrid.getAccessor(data,dReader.userdata) || {};

			if(!dReader.repeatitems) {

				F = f = reader(frd);

			}

			if( ts.p.keyIndex===false ) {

				idn = $.isFunction(dReader.id) ? dReader.id.call(ts, data) : dReader.id; 

			} else {

				idn = ts.p.keyIndex;

			}

			if(f.length>0 && !isNaN(idn)) {

				if (ts.p.remapColumns && ts.p.remapColumns.length) {

					idn = $.inArray(idn, ts.p.remapColumns);

				}

				idn=f[idn];

			}

			drows = $.jgrid.getAccessor(data,dReader.root);

			if (!drows) { drows = []; }

			len = drows.length; i=0;

			if (len > 0 && ts.p.page <= 0) { ts.p.page = 1; }

			var rn = parseInt(ts.p.rowNum,10),br=ts.p.scroll?$.jgrid.randId():1, altr, selected=false, selr;

			if (adjust) { rn *= adjust+1; }

			if(ts.p.datatype === "local" && !ts.p.deselectAfterSort) {

				selected = true;

			}

			var afterInsRow = $.isFunction(ts.p.afterInsertRow), grpdata=[],hiderow=false, groupingPrepare;

			if(ts.p.grouping)  {

				hiderow = ts.p.groupingView.groupCollapse === true;

				groupingPrepare = $.jgrid.getMethod("groupingPrepare");

			}

			while (i<len) {

				cur = drows[i];

				idr = $.jgrid.getAccessor(cur,idn);

				if(idr === undefined) {

					idr = br+i;

					if(f.length===0){

						if(dReader.cell){

							var ccur = $.jgrid.getAccessor(cur,dReader.cell);

							idr = ccur !== undefined ? ccur[idn] || idr : idr;

							ccur=null;

						}

					}

				}

				idr  = ts.p.idPrefix + idr;

				altr = rcnt === 1 ? 0 : rcnt;

				cn1 = (altr+i)%2 == 1 ? cn : '';

				if( selected) {

					if( ts.p.multiselect) {

						selr = ($.inArray(idr, ts.p.selarrrow) !== -1);

					} else {

						selr = (idr === ts.p.selrow);

					}

				}

				var iStartTrTag = rowData.length;

				rowData.push("");

				if( ni ) {

					rowData.push( addRowNum(0,i,ts.p.page,ts.p.rowNum) );

				}

				if( gi ){

					rowData.push( addMulti(idr,ni,i,selr) );

				}

				if( si ) {

					rowData.push( addSubGridCell.call(self,gi+ni,i+rcnt) );

				}

				if (dReader.repeatitems) {

					if(dReader.cell) {cur = $.jgrid.getAccessor(cur,dReader.cell);}

					if (!F) { F=orderedCols(gi+si+ni); }

				}

				for (j=0;j<F.length;j++) {

					v = $.jgrid.getAccessor(cur,F[j]);

					rd[ts.p.colModel[j+gi+si+ni].name] = v;

					rowData.push( addCell(idr,v,j+gi+si+ni,i+rcnt,cur, rd) );

				}

				rowData[iStartTrTag] = constructTr(idr, hiderow, cn1, rd, cur, selr);

				rowData.push( "</tr>" );

				if(ts.p.grouping) {

					grpdata = groupingPrepare.call(self,rowData, grpdata, rd, i);

					rowData = [];

				}

				if(locdata || ts.p.treeGrid===true) {

					rd[locid] = idr;

					ts.p.data.push(rd);

					ts.p._index[idr] = ts.p.data.length-1;

				}

				if(ts.p.gridview === false ) {

					$("#"+$.jgrid.jqID(ts.p.id)+" tbody:first").append(rowData.join(''));

					self.triggerHandler("jqGridAfterInsertRow", [idr, rd, cur]);

					if(afterInsRow) {ts.p.afterInsertRow.call(ts,idr,rd,cur);}

					rowData=[];//ari=0;

				}

				rd={};

				ir++;

				i++;

				if(ir==rn) { break; }

			}

			if(ts.p.gridview === true ) {

				fpos = ts.p.treeANode > -1 ? ts.p.treeANode: 0;

				if(ts.p.grouping) {

					self.jqGrid('groupingRender',grpdata,ts.p.colModel.length);

					grpdata = null;

				} else if(ts.p.treeGrid === true && fpos > 0) {

					$(ts.rows[fpos]).after(rowData.join(''));

				} else {

					$("#"+$.jgrid.jqID(ts.p.id)+" tbody:first").append(rowData.join(''));

				}

			}

			if(ts.p.subGrid === true ) {

				try { self.jqGrid("addSubGrid",gi+ni);} catch (_){}

			}

			ts.p.totaltime = new Date() - startReq;

			if(ir>0) {

				if(ts.p.records===0) { ts.p.records=len; }

			}

			rowData = null;

			if( ts.p.treeGrid === true) {

				try {self.jqGrid("setTreeNode", fpos+1, ir+fpos+1);} catch (e) {}

			}

			if(!ts.p.treeGrid && !ts.p.scroll) {ts.grid.bDiv.scrollTop = 0;}

			ts.p.reccount=ir;

			ts.p.treeANode = -1;

			if(ts.p.userDataOnFooter) { self.jqGrid("footerData","set",ts.p.userData,true); }

			if(locdata) {

				ts.p.records = len;

				ts.p.lastpage = Math.ceil(len/ rn);

			}

			if (!more) { ts.updatepager(false,true); }

			if(locdata) {

				while (ir<len && drows[ir]) {

					cur = drows[ir];

					idr = $.jgrid.getAccessor(cur,idn);

					if(idr === undefined) {

						idr = br+ir;

						if(f.length===0){

							if(dReader.cell){

								var ccur2 = $.jgrid.getAccessor(cur,dReader.cell);

								idr = ccur2[idn] || idr;

								ccur2=null;

							}

						}

					}

					if(cur) {

						idr  = ts.p.idPrefix + idr;

						if (dReader.repeatitems) {

							if(dReader.cell) {cur = $.jgrid.getAccessor(cur,dReader.cell);}

							if (!F) { F=orderedCols(gi+si+ni); }

						}



						for (j=0;j<F.length;j++) {

							v = $.jgrid.getAccessor(cur,F[j]);

							rd[ts.p.colModel[j+gi+si+ni].name] = v;

						}

						rd[locid] = idr;

						ts.p.data.push(rd);

						ts.p._index[idr] = ts.p.data.length-1;

						rd = {};

					}

					ir++;

				}

			}

		},

		addLocalData = function() {

			var st, fndsort=false, cmtypes={}, grtypes=[], grindexes=[], srcformat, sorttype, newformat;

			if(!$.isArray(ts.p.data)) {

				return;

			}

			var grpview = ts.p.grouping ? ts.p.groupingView : false, lengrp, gin;

			$.each(ts.p.colModel,function(){

				sorttype = this.sorttype || "text";

				if(sorttype == "date" || sorttype == "datetime") {

					if(this.formatter && typeof this.formatter === 'string' && this.formatter == 'date') {

						if(this.formatoptions && this.formatoptions.srcformat) {

							srcformat = this.formatoptions.srcformat;

						} else {

							srcformat = $.jgrid.formatter.date.srcformat;

						}

						if(this.formatoptions && this.formatoptions.newformat) {

							newformat = this.formatoptions.newformat;

						} else {

							newformat = $.jgrid.formatter.date.newformat;

						}

					} else {

						srcformat = newformat = this.datefmt || "Y-m-d";

					}

					cmtypes[this.name] = {"stype": sorttype, "srcfmt": srcformat,"newfmt":newformat};

				} else {

					cmtypes[this.name] = {"stype": sorttype, "srcfmt":'',"newfmt":''};

				}

				if(ts.p.grouping ) {

					for(gin =0, lengrp = grpview.groupField.length; gin< lengrp; gin++) {

						if( this.name == grpview.groupField[gin]) {

							var grindex = this.name;

							if (this.index) {

								grindex = this.index;

							}

							grtypes[gin] = cmtypes[grindex];

							grindexes[gin]= grindex;

						}

					}

				}

				if(!fndsort && (this.index == ts.p.sortname || this.name == ts.p.sortname)){

					st = this.name; // ???

					fndsort = true;

				}

			});

			if(ts.p.treeGrid) {

				$(ts).jqGrid("SortTree", st, ts.p.sortorder, cmtypes[st].stype, cmtypes[st].srcfmt);

				return;

			}

			var compareFnMap = {

				'eq':function(queryObj) {return queryObj.equals;},

				'ne':function(queryObj) {return queryObj.notEquals;},

				'lt':function(queryObj) {return queryObj.less;},

				'le':function(queryObj) {return queryObj.lessOrEquals;},

				'gt':function(queryObj) {return queryObj.greater;},

				'ge':function(queryObj) {return queryObj.greaterOrEquals;},

				'cn':function(queryObj) {return queryObj.contains;},

				'nc':function(queryObj,op) {return op === "OR" ? queryObj.orNot().contains : queryObj.andNot().contains;},

				'bw':function(queryObj) {return queryObj.startsWith;},

				'bn':function(queryObj,op) {return op === "OR" ? queryObj.orNot().startsWith : queryObj.andNot().startsWith;},

				'en':function(queryObj,op) {return op === "OR" ? queryObj.orNot().endsWith : queryObj.andNot().endsWith;},

				'ew':function(queryObj) {return queryObj.endsWith;},

				'ni':function(queryObj,op) {return op === "OR" ? queryObj.orNot().equals : queryObj.andNot().equals;},

				'in':function(queryObj) {return queryObj.equals;},

				'nu':function(queryObj) {return queryObj.isNull;},

				'nn':function(queryObj,op) {return op === "OR" ? queryObj.orNot().isNull : queryObj.andNot().isNull;}



			},

			query = $.jgrid.from(ts.p.data);

			if (ts.p.ignoreCase) { query = query.ignoreCase(); }

			function tojLinq ( group ) {

				var s = 0, index, gor, ror, opr, rule;

				if (group.groups != null) {

					gor = group.groups.length && group.groupOp.toString().toUpperCase() === "OR";

					if (gor) {

						query.orBegin();

					}

					for (index = 0; index < group.groups.length; index++) {

						if (s > 0 && gor) {

							query.or();

						}

						try {

							tojLinq(group.groups[index]);

						} catch (e) {alert(e);}

						s++;

					}

					if (gor) {

						query.orEnd();

					}

				}

				if (group.rules != null) {

					if(s>0) {

						var result = query.select();

						query = $.jgrid.from( result);

						if (ts.p.ignoreCase) { query = query.ignoreCase(); } 

					}

					try{

						ror = group.rules.length && group.groupOp.toString().toUpperCase() === "OR";

						if (ror) {

							query.orBegin();

						}

						for (index = 0; index < group.rules.length; index++) {

							rule = group.rules[index];

							opr = group.groupOp.toString().toUpperCase();

							if (compareFnMap[rule.op] && rule.field ) {

								if(s > 0 && opr && opr === "OR") {

									query = query.or();

								}

								query = compareFnMap[rule.op](query, opr)(rule.field, rule.data, cmtypes[rule.field]);

							}

							s++;

						}

						if (ror) {

							query.orEnd();

						}

					} catch (g) {alert(g);}

				}

			}



			if (ts.p.search === true) {

				var srules = ts.p.postData.filters;

				if(srules) {

					if(typeof srules === "string") { srules = $.jgrid.parse(srules);}

					tojLinq( srules );

				} else {

					try {

						query = compareFnMap[ts.p.postData.searchOper](query)(ts.p.postData.searchField, ts.p.postData.searchString,cmtypes[ts.p.postData.searchField]);

					} catch (se){}

				}

			}

			if(ts.p.grouping) {

				for(gin=0; gin<lengrp;gin++) {

					query.orderBy(grindexes[gin],grpview.groupOrder[gin],grtypes[gin].stype, grtypes[gin].srcfmt);

				}

			}

			if (st && ts.p.sortorder && fndsort) {

				if(ts.p.sortorder.toUpperCase() == "DESC") {

					query.orderBy(ts.p.sortname, "d", cmtypes[st].stype, cmtypes[st].srcfmt);

				} else {

					query.orderBy(ts.p.sortname, "a", cmtypes[st].stype, cmtypes[st].srcfmt);

				}

			}

			var queryResults = query.select(),

			recordsperpage = parseInt(ts.p.rowNum,10),

			total = queryResults.length,

			page = parseInt(ts.p.page,10),

			totalpages = Math.ceil(total / recordsperpage),

			retresult = {};

			queryResults = queryResults.slice( (page-1)*recordsperpage , page*recordsperpage );

			query = null;

			cmtypes = null;

			retresult[ts.p.localReader.total] = totalpages;

			retresult[ts.p.localReader.page] = page;

			retresult[ts.p.localReader.records] = total;

			retresult[ts.p.localReader.root] = queryResults;

			retresult[ts.p.localReader.userdata] = ts.p.userData;

			queryResults = null;

			return  retresult;

		},

		updatepager = function(rn, dnd) {

			var cp, last, base, from,to,tot,fmt, pgboxes = "", sppg,

			tspg = ts.p.pager ? "_"+$.jgrid.jqID(ts.p.pager.substr(1)) : "",

			tspg_t = ts.p.toppager ? "_"+ts.p.toppager.substr(1) : "";

			base = parseInt(ts.p.page,10)-1;

			if(base < 0) { base = 0; }

			base = base*parseInt(ts.p.rowNum,10);

			to = base + ts.p.reccount;

			if (ts.p.scroll) {

				var rows = $("tbody:first > tr:gt(0)", ts.grid.bDiv);

				base = to - rows.length;

				ts.p.reccount = rows.length;

				var rh = rows.outerHeight() || ts.grid.prevRowHeight;

				if (rh) {

					var top = base * rh;

					var height = parseInt(ts.p.records,10) * rh;

					$(">div:first",ts.grid.bDiv).css({height : height}).children("div:first").css({height:top,display:top?"":"none"});

				}

				ts.grid.bDiv.scrollLeft = ts.grid.hDiv.scrollLeft;

			}

			pgboxes = ts.p.pager || "";

			pgboxes += ts.p.toppager ?  (pgboxes ? "," + ts.p.toppager : ts.p.toppager) : "";

			if(pgboxes) {

				fmt = $.jgrid.formatter.integer || {};

				cp = intNum(ts.p.page);

				last = intNum(ts.p.lastpage);

				$(".selbox",pgboxes)[ this.p.useProp ? 'prop' : 'attr' ]("disabled",false);

				if(ts.p.pginput===true) {

					$('.ui-pg-input',pgboxes).val(ts.p.page);

					sppg = ts.p.toppager ? '#sp_1'+tspg+",#sp_1"+tspg_t : '#sp_1'+tspg;

					$(sppg).html($.fmatter ? $.fmatter.util.NumberFormat(ts.p.lastpage,fmt):ts.p.lastpage);



				}

				if (ts.p.viewrecords){

					if(ts.p.reccount === 0) {

						$(".ui-paging-info",pgboxes).html(ts.p.emptyrecords);

					} else {

						from = base+1;

						tot=ts.p.records;

						if($.fmatter) {

							from = $.fmatter.util.NumberFormat(from,fmt);

							to = $.fmatter.util.NumberFormat(to,fmt);

							tot = $.fmatter.util.NumberFormat(tot,fmt);

						}

						$(".ui-paging-info",pgboxes).html($.jgrid.format(ts.p.recordtext,from,to,tot));

					}

				}

				if(ts.p.pgbuttons===true) {

					if(cp<=0) {cp = last = 0;}

					if(cp==1 || cp === 0) {

						$("#first"+tspg+", #prev"+tspg).addClass('ui-state-disabled').removeClass('ui-state-hover');

						if(ts.p.toppager) { $("#first_t"+tspg_t+", #prev_t"+tspg_t).addClass('ui-state-disabled').removeClass('ui-state-hover'); }

					} else {

						$("#first"+tspg+", #prev"+tspg).removeClass('ui-state-disabled');

						if(ts.p.toppager) { $("#first_t"+tspg_t+", #prev_t"+tspg_t).removeClass('ui-state-disabled'); }

					}

					if(cp==last || cp === 0) {

						$("#next"+tspg+", #last"+tspg).addClass('ui-state-disabled').removeClass('ui-state-hover');

						if(ts.p.toppager) { $("#next_t"+tspg_t+", #last_t"+tspg_t).addClass('ui-state-disabled').removeClass('ui-state-hover'); }

					} else {

						$("#next"+tspg+", #last"+tspg).removeClass('ui-state-disabled');

						if(ts.p.toppager) { $("#next_t"+tspg_t+", #last_t"+tspg_t).removeClass('ui-state-disabled'); }

					}

				}

			}

			if(rn===true && ts.p.rownumbers === true) {

				$("td.jqgrid-rownum",ts.rows).each(function(i){

					$(this).html(base+1+i);

				});

			}

			if(dnd && ts.p.jqgdnd) { $(ts).jqGrid('gridDnD','updateDnD');}

			$(ts).triggerHandler("jqGridGridComplete");

			if($.isFunction(ts.p.gridComplete)) {ts.p.gridComplete.call(ts);}

			$(ts).triggerHandler("jqGridAfterGridComplete");

		},

		beginReq = function() {

			ts.grid.hDiv.loading = true;

			if(ts.p.hiddengrid) { return;}

			switch(ts.p.loadui) {

				case "disable":

					break;

				case "enable":

					$("#load_"+$.jgrid.jqID(ts.p.id)).show();

					break;

				case "block":

					$("#lui_"+$.jgrid.jqID(ts.p.id)).show();

					$("#load_"+$.jgrid.jqID(ts.p.id)).show();

					break;

			}

		},

		endReq = function() {

			ts.grid.hDiv.loading = false;

			switch(ts.p.loadui) {

				case "disable":

					break;

				case "enable":

					$("#load_"+$.jgrid.jqID(ts.p.id)).hide();

					break;

				case "block":

					$("#lui_"+$.jgrid.jqID(ts.p.id)).hide();

					$("#load_"+$.jgrid.jqID(ts.p.id)).hide();

					break;

			}

		},

		populate = function (npage) {

			if(!ts.grid.hDiv.loading) {

				var pvis = ts.p.scroll && npage === false,

				prm = {}, dt, dstr, pN=ts.p.prmNames;

				if(ts.p.page <=0) { ts.p.page = 1; }

				if(pN.search !== null) {prm[pN.search] = ts.p.search;} if(pN.nd !== null) {prm[pN.nd] = new Date().getTime();}

				if(pN.rows !== null) {prm[pN.rows]= ts.p.rowNum;} if(pN.page !== null) {prm[pN.page]= ts.p.page;}

				if(pN.sort !== null) {prm[pN.sort]= ts.p.sortname;} if(pN.order !== null) {prm[pN.order]= ts.p.sortorder;}

				if(ts.p.rowTotal !== null && pN.totalrows !== null) { prm[pN.totalrows]= ts.p.rowTotal; }

				var lcf = $.isFunction(ts.p.loadComplete), lc = lcf ? ts.p.loadComplete : null;

				var adjust = 0;

				npage = npage || 1;

				if (npage > 1) {

					if(pN.npage !== null) {

						prm[pN.npage] = npage;

						adjust = npage - 1;

						npage = 1;

					} else {

						lc = function(req) {

							ts.p.page++;

							ts.grid.hDiv.loading = false;

							if (lcf) {

								ts.p.loadComplete.call(ts,req);

							}

							populate(npage-1);

						};

					}

				} else if (pN.npage !== null) {

					delete ts.p.postData[pN.npage];

				}

				if(ts.p.grouping) {

					$(ts).jqGrid('groupingSetup');

					var grp = ts.p.groupingView, gi, gs="";

					for(gi=0;gi<grp.groupField.length;gi++) {

						var index = grp.groupField[gi];

						$.each(ts.p.colModel, function(cmIndex, cmValue) {

							if (cmValue.name == index && cmValue.index){

								index = cmValue.index;

							}

						} );

						gs += index +" "+grp.groupOrder[gi]+", ";

					}

					prm[pN.sort] = gs + prm[pN.sort];

				}

				$.extend(ts.p.postData,prm);

				var rcnt = !ts.p.scroll ? 1 : ts.rows.length-1;

				var bfr = $(ts).triggerHandler("jqGridBeforeRequest");

				if (bfr === false || bfr === 'stop') { return; }

				if ($.isFunction(ts.p.datatype)) { ts.p.datatype.call(ts,ts.p.postData,"load_"+ts.p.id); return;}

				if ($.isFunction(ts.p.beforeRequest)) {

					bfr = ts.p.beforeRequest.call(ts);

					if(bfr === undefined) { bfr = true; }

					if ( bfr === false ) { return; }

				}

				dt = ts.p.datatype.toLowerCase();

				switch(dt)

				{

				case "json":

				case "jsonp":

				case "xml":

				case "script":

					$.ajax($.extend({

						url:ts.p.url,

						type:ts.p.mtype,

						dataType: dt ,

						data: $.isFunction(ts.p.serializeGridData)? ts.p.serializeGridData.call(ts,ts.p.postData) : ts.p.postData,

						success:function(data,st, xhr) {

							if ($.isFunction(ts.p.beforeProcessing)) {

								if (ts.p.beforeProcessing.call(ts, data, st, xhr) === false) {

									endReq();

									return;

								}

							}

							if(dt === "xml") { addXmlData(data,ts.grid.bDiv,rcnt,npage>1,adjust); }

							else { addJSONData(data,ts.grid.bDiv,rcnt,npage>1,adjust); }

							$(ts).triggerHandler("jqGridLoadComplete", [data]);

							if(lc) { lc.call(ts,data); }

							$(ts).triggerHandler("jqGridAfterLoadComplete", [data]);

							if (pvis) { ts.grid.populateVisible(); }

							if( ts.p.loadonce || ts.p.treeGrid) {ts.p.datatype = "local";}

							data=null;

							if (npage === 1) { endReq(); }

						},

						error:function(xhr,st,err){

							if($.isFunction(ts.p.loadError)) { ts.p.loadError.call(ts,xhr,st,err); }

							if (npage === 1) { endReq(); }

							xhr=null;

						},

						beforeSend: function(xhr, settings ){

							var gotoreq = true;

							if($.isFunction(ts.p.loadBeforeSend)) {

								gotoreq = ts.p.loadBeforeSend.call(ts,xhr, settings); 

							}

							if(gotoreq === undefined) { gotoreq = true; }

							if(gotoreq === false) {

								return false;

							}

								beginReq();

							}

					},$.jgrid.ajaxOptions, ts.p.ajaxGridOptions));

				break;

				case "xmlstring":

					beginReq();

					dstr = $.jgrid.stringToDoc(ts.p.datastr);

					addXmlData(dstr,ts.grid.bDiv);

					$(ts).triggerHandler("jqGridLoadComplete", [dstr]);

					if(lcf) {ts.p.loadComplete.call(ts,dstr);}

					$(ts).triggerHandler("jqGridAfterLoadComplete", [dstr]);

					ts.p.datatype = "local";

					ts.p.datastr = null;

					endReq();

				break;

				case "jsonstring":

					beginReq();

					if(typeof ts.p.datastr === 'string') { dstr = $.jgrid.parse(ts.p.datastr); }

					else { dstr = ts.p.datastr; }

					addJSONData(dstr,ts.grid.bDiv);

					$(ts).triggerHandler("jqGridLoadComplete", [dstr]);

					if(lcf) {ts.p.loadComplete.call(ts,dstr);}

					$(ts).triggerHandler("jqGridAfterLoadComplete", [dstr]);

					ts.p.datatype = "local";

					ts.p.datastr = null;

					endReq();

				break;

				case "local":

				case "clientside":

					beginReq();

					ts.p.datatype = "local";

					var req = addLocalData();

					addJSONData(req,ts.grid.bDiv,rcnt,npage>1,adjust);

					$(ts).triggerHandler("jqGridLoadComplete", [req]);

					if(lc) { lc.call(ts,req); }

					$(ts).triggerHandler("jqGridAfterLoadComplete", [req]);

					if (pvis) { ts.grid.populateVisible(); }

					endReq();

				break;

				}

			}

		},

		setHeadCheckBox = function ( checked ) {

			$('#cb_'+$.jgrid.jqID(ts.p.id),ts.grid.hDiv)[ts.p.useProp ? 'prop': 'attr']("checked", checked);

			var fid = ts.p.frozenColumns ? ts.p.id+"_frozen" : "";

			if(fid) {

				$('#cb_'+$.jgrid.jqID(ts.p.id),ts.grid.fhDiv)[ts.p.useProp ? 'prop': 'attr']("checked", checked);

			}

		},

		setPager = function (pgid, tp){

			// TBD - consider escaping pgid with pgid = $.jgrid.jqID(pgid);

			var sep = "<td class='ui-pg-button ui-state-disabled' style='width:4px;'><span class='ui-separator'></span></td>",

			pginp = "",

			pgl="<table cellspacing='0' cellpadding='0' border='0' style='table-layout:auto;' class='ui-pg-table'><tbody><tr>",

			str="", pgcnt, lft, cent, rgt, twd, tdw, i,

			clearVals = function(onpaging){

				var ret;

				if ($.isFunction(ts.p.onPaging) ) { ret = ts.p.onPaging.call(ts,onpaging); }

				ts.p.selrow = null;

				if(ts.p.multiselect) {ts.p.selarrrow =[]; setHeadCheckBox( false );}

				ts.p.savedRow = [];

				if(ret=='stop') {return false;}

				return true;

			};

			pgid = pgid.substr(1);

			tp += "_" + pgid;

			pgcnt = "pg_"+pgid;

			lft = pgid+"_left"; cent = pgid+"_center"; rgt = pgid+"_right";

			$("#"+$.jgrid.jqID(pgid) )

			.append("<div id='"+pgcnt+"' class='ui-pager-control' role='group'><table cellspacing='0' cellpadding='0' border='0' class='ui-pg-table' style='width:100%;table-layout:fixed;height:100%;' role='row'><tbody><tr><td id='"+lft+"' align='left'></td><td id='"+cent+"' align='center' style='white-space:pre;'></td><td id='"+rgt+"' align='right'></td></tr></tbody></table></div>")

			.attr("dir","ltr"); //explicit setting

			if(ts.p.rowList.length >0){

				str = "<td dir='"+dir+"'>";

				str +="<select class='ui-pg-selbox' role='listbox'>";

				for(i=0;i<ts.p.rowList.length;i++){

					str +="<option role=\"option\" value=\""+ts.p.rowList[i]+"\""+((ts.p.rowNum == ts.p.rowList[i])?" selected=\"selected\"":"")+">"+ts.p.rowList[i]+"</option>";

				}

				str +="</select></td>";

			}

			if(dir=="rtl") { pgl += str; }

			if(ts.p.pginput===true) { pginp= "<td dir='"+dir+"'>"+$.jgrid.format(ts.p.pgtext || "","<input class='ui-pg-input' type='text' size='2' maxlength='7' value='0' role='textbox'/>","<span id='sp_1_"+$.jgrid.jqID(pgid)+"'></span>")+"</td>";}

			if(ts.p.pgbuttons===true) {

				var po=["first"+tp,"prev"+tp, "next"+tp,"last"+tp]; if(dir=="rtl") { po.reverse(); }

				pgl += "<td id='"+po[0]+"' class='ui-pg-button ui-corner-all'><span class='ui-icon ui-icon-seek-first'></span></td>";

				pgl += "<td id='"+po[1]+"' class='ui-pg-button ui-corner-all'><span class='ui-icon ui-icon-seek-prev'></span></td>";

				pgl += pginp !== "" ? sep+pginp+sep:"";

				pgl += "<td id='"+po[2]+"' class='ui-pg-button ui-corner-all'><span class='ui-icon ui-icon-seek-next'></span></td>";

				pgl += "<td id='"+po[3]+"' class='ui-pg-button ui-corner-all'><span class='ui-icon ui-icon-seek-end'></span></td>";

			} else if (pginp !== "") { pgl += pginp; }

			if(dir=="ltr") { pgl += str; }

			pgl += "</tr></tbody></table>";

			if(ts.p.viewrecords===true) {$("td#"+pgid+"_"+ts.p.recordpos,"#"+pgcnt).append("<div dir='"+dir+"' style='text-align:"+ts.p.recordpos+"' class='ui-paging-info'></div>");}

			$("td#"+pgid+"_"+ts.p.pagerpos,"#"+pgcnt).append(pgl);

			tdw = $(".ui-jqgrid").css("font-size") || "11px";

			$(document.body).append("<div id='testpg' class='ui-jqgrid ui-widget ui-widget-content' style='font-size:"+tdw+";visibility:hidden;' ></div>");

			twd = $(pgl).clone().appendTo("#testpg").width();

			$("#testpg").remove();

			if(twd > 0) {

				if(pginp !== "") { twd += 50; } //should be param

				$("td#"+pgid+"_"+ts.p.pagerpos,"#"+pgcnt).width(twd);

			}

			ts.p._nvtd = [];

			ts.p._nvtd[0] = twd ? Math.floor((ts.p.width - twd)/2) : Math.floor(ts.p.width/3);

			ts.p._nvtd[1] = 0;

			pgl=null;

			$('.ui-pg-selbox',"#"+pgcnt).bind('change',function() {

				if(!clearVals('records')) { return false; }

				ts.p.page = Math.round(ts.p.rowNum*(ts.p.page-1)/this.value-0.5)+1;

				ts.p.rowNum = this.value;

				if(ts.p.pager) { $('.ui-pg-selbox',ts.p.pager).val(this.value); }

				if(ts.p.toppager) { $('.ui-pg-selbox',ts.p.toppager).val(this.value); }

				populate();

				return false;

			});

			if(ts.p.pgbuttons===true) {

			$(".ui-pg-button","#"+pgcnt).hover(function(){

				if($(this).hasClass('ui-state-disabled')) {

					this.style.cursor='default';

				} else {

					$(this).addClass('ui-state-hover');

					this.style.cursor='pointer';

				}

			},function() {

				if(!$(this).hasClass('ui-state-disabled')) {

					$(this).removeClass('ui-state-hover');

					this.style.cursor= "default";

				}

			});

			$("#first"+$.jgrid.jqID(tp)+", #prev"+$.jgrid.jqID(tp)+", #next"+$.jgrid.jqID(tp)+", #last"+$.jgrid.jqID(tp)).click( function() {

				var cp = intNum(ts.p.page,1),

				last = intNum(ts.p.lastpage,1), selclick = false,

				fp=true, pp=true, np=true,lp=true;

				if(last ===0 || last===1) {fp=false;pp=false;np=false;lp=false; }

				else if( last>1 && cp >=1) {

					if( cp === 1) { fp=false; pp=false; }

					//else if( cp>1 && cp <last){ }

					else if( cp===last){ np=false;lp=false; }

				} else if( last>1 && cp===0 ) { np=false;lp=false; cp=last-1;}

				if(!clearVals(this.id)) { return false; }

				if( this.id === 'first'+tp && fp ) { ts.p.page=1; selclick=true;}

				if( this.id === 'prev'+tp && pp) { ts.p.page=(cp-1); selclick=true;}

				if( this.id === 'next'+tp && np) { ts.p.page=(cp+1); selclick=true;}

				if( this.id === 'last'+tp && lp) { ts.p.page=last; selclick=true;}

				if(selclick) {

					populate();

				}

				return false;

			});

			}

			if(ts.p.pginput===true) {

			$('input.ui-pg-input',"#"+pgcnt).keypress( function(e) {

				var key = e.charCode || e.keyCode || 0;

				if(key == 13) {

					if(!clearVals('user')) { return false; }

					ts.p.page = ($(this).val()>0) ? $(this).val():ts.p.page;

					populate();

					return false;

				}

				return this;

			});

			}

		},

		sortData = function (index, idxcol,reload,sor){

			if(!ts.p.colModel[idxcol].sortable) { return; }

			var so;

			if(ts.p.savedRow.length > 0) {return;}

			if(!reload) {

				if( ts.p.lastsort == idxcol ) {

					if( ts.p.sortorder == 'asc') {

						ts.p.sortorder = 'desc';

					} else if(ts.p.sortorder == 'desc') { ts.p.sortorder = 'asc';}

				} else { ts.p.sortorder = ts.p.colModel[idxcol].firstsortorder || 'asc'; }

				ts.p.page = 1;

			}

			if(sor) {

				if(ts.p.lastsort == idxcol && ts.p.sortorder == sor && !reload) { return; }

				ts.p.sortorder = sor;

			}

			var previousSelectedTh = ts.grid.headers[ts.p.lastsort].el, newSelectedTh = ts.grid.headers[idxcol].el;



			$("span.ui-grid-ico-sort",previousSelectedTh).addClass('ui-state-disabled');

			$(previousSelectedTh).attr("aria-selected","false");

			$("span.ui-icon-"+ts.p.sortorder,newSelectedTh).removeClass('ui-state-disabled');

			$(newSelectedTh).attr("aria-selected","true");

			if(!ts.p.viewsortcols[0]) {

				if(ts.p.lastsort != idxcol) {

					$("span.s-ico",previousSelectedTh).hide();

					$("span.s-ico",newSelectedTh).show();

				}

			}

			index = index.substring(5 + ts.p.id.length + 1); // bad to be changed!?!

			ts.p.sortname = ts.p.colModel[idxcol].index || index;

			so = ts.p.sortorder;

			if ($(ts).triggerHandler("jqGridSortCol", [index, idxcol, so]) === 'stop') {

				ts.p.lastsort = idxcol;

				return;

			}

			if($.isFunction(ts.p.onSortCol)) {if (ts.p.onSortCol.call(ts,index,idxcol,so)=='stop') {ts.p.lastsort = idxcol; return;}}

			if(ts.p.datatype == "local") {

				if(ts.p.deselectAfterSort) {$(ts).jqGrid("resetSelection");}

			} else {

				ts.p.selrow = null;

				if(ts.p.multiselect){setHeadCheckBox( false );}

				ts.p.selarrrow =[];

				ts.p.savedRow =[];

			}

			if(ts.p.scroll) {

				var sscroll = ts.grid.bDiv.scrollLeft;

				emptyRows.call(ts, true, false);

				ts.grid.hDiv.scrollLeft = sscroll;

			}

			if(ts.p.subGrid && ts.p.datatype=='local') {

				$("td.sgexpanded","#"+$.jgrid.jqID(ts.p.id)).each(function(){

					$(this).trigger("click");

				});

			}

			populate();

			ts.p.lastsort = idxcol;

			if(ts.p.sortname != index && idxcol) {ts.p.lastsort = idxcol;}

		},

		setColWidth = function () {

			var initwidth = 0, brd=$.jgrid.cell_width? 0: intNum(ts.p.cellLayout,0), vc=0, lvc, scw=intNum(ts.p.scrollOffset,0),cw,hs=false,aw,gw=0,

			cl = 0, cr;

			$.each(ts.p.colModel, function() {

				if(this.hidden === undefined) {this.hidden=false;}

				if(ts.p.grouping && ts.p.autowidth) {

					var ind = $.inArray(this.name, ts.p.groupingView.groupField);

					if(ind !== -1) {

						this.hidden = !ts.p.groupingView.groupColumnShow[ind];

					}

				}

				this.widthOrg = cw = intNum(this.width,0);

				if(this.hidden===false){

					initwidth += cw+brd;

					if(this.fixed) {

						gw += cw+brd;

					} else {

						vc++;

					}

					cl++;

				}

			});

			if(isNaN(ts.p.width)) {

				ts.p.width  = initwidth + ((ts.p.shrinkToFit ===false && !isNaN(ts.p.height)) ? scw : 0);

			}

			grid.width = ts.p.width;

			ts.p.tblwidth = initwidth;

			if(ts.p.shrinkToFit ===false && ts.p.forceFit === true) {ts.p.forceFit=false;}

			if(ts.p.shrinkToFit===true && vc > 0) {

				aw = grid.width-brd*vc-gw;

				if(!isNaN(ts.p.height)) {

					aw -= scw;

					hs = true;

				}

				initwidth =0;

				$.each(ts.p.colModel, function(i) {

					if(this.hidden === false && !this.fixed){

						cw = Math.round(aw*this.width/(ts.p.tblwidth-brd*vc-gw));

						this.width =cw;

						initwidth += cw;

						lvc = i;

					}

				});

				cr =0;

				if (hs) {

					if(grid.width-gw-(initwidth+brd*vc) !== scw){

						cr = grid.width-gw-(initwidth+brd*vc)-scw;

					}

				} else if(!hs && Math.abs(grid.width-gw-(initwidth+brd*vc)) !== 1) {

					cr = grid.width-gw-(initwidth+brd*vc);

				}

				ts.p.colModel[lvc].width += cr;

				ts.p.tblwidth = initwidth+cr+brd*vc+gw;

				if(ts.p.tblwidth > ts.p.width) {

					ts.p.colModel[lvc].width -= (ts.p.tblwidth - parseInt(ts.p.width,10));

					ts.p.tblwidth = ts.p.width;

				}

			}

		},

		nextVisible= function(iCol) {

			var ret = iCol, j=iCol, i;

			for (i = iCol+1;i<ts.p.colModel.length;i++){

				if(ts.p.colModel[i].hidden !== true ) {

					j=i; break;

				}

			}

			return j-ret;

		},

		getOffset = function (iCol) {

			var i, ret = {}, brd1 = $.jgrid.cell_width ? 0 : ts.p.cellLayout;

			ret[0] =  ret[1] = ret[2] = 0;

			for(i=0;i<=iCol;i++){

				if(ts.p.colModel[i].hidden === false ) {

					ret[0] += ts.p.colModel[i].width+brd1;

				}

			}

			if(ts.p.direction=="rtl") { ret[0] = ts.p.width - ret[0]; }

			ret[0] = ret[0] - ts.grid.bDiv.scrollLeft;

			if($(ts.grid.cDiv).is(":visible")) {ret[1] += $(ts.grid.cDiv).height() +parseInt($(ts.grid.cDiv).css("padding-top"),10)+parseInt($(ts.grid.cDiv).css("padding-bottom"),10);}

			if(ts.p.toolbar[0]===true && (ts.p.toolbar[1]=='top' || ts.p.toolbar[1]=='both')) {ret[1] += $(ts.grid.uDiv).height()+parseInt($(ts.grid.uDiv).css("border-top-width"),10)+parseInt($(ts.grid.uDiv).css("border-bottom-width"),10);}

			if(ts.p.toppager) {ret[1] += $(ts.grid.topDiv).height()+parseInt($(ts.grid.topDiv).css("border-bottom-width"),10);}

			ret[2] += $(ts.grid.bDiv).height() + $(ts.grid.hDiv).height();

			return ret;

		},

		getColumnHeaderIndex = function (th) {

			var i, headers = ts.grid.headers, ci = $.jgrid.getCellIndex(th);

			for (i = 0; i < headers.length; i++) {

				if (th === headers[i].el) {

					ci = i;

					break;

				}

			}

			return ci;

		};

		this.p.id = this.id;

		if ($.inArray(ts.p.multikey,sortkeys) == -1 ) {ts.p.multikey = false;}

		ts.p.keyIndex=false;

		for (i=0; i<ts.p.colModel.length;i++) {

			ts.p.colModel[i] = $.extend(true, {}, ts.p.cmTemplate, ts.p.colModel[i].template || {}, ts.p.colModel[i]);

			if (ts.p.keyIndex === false && ts.p.colModel[i].key===true) {

				ts.p.keyIndex = i;

			}

		}

		ts.p.sortorder = ts.p.sortorder.toLowerCase();

		$.jgrid.cell_width = $.jgrid.cellWidth();

		if(ts.p.grouping===true) {

			ts.p.scroll = false;

			ts.p.rownumbers = false;

			//ts.p.subGrid = false; expiremental

			ts.p.treeGrid = false;

			ts.p.gridview = true;

		}

		if(this.p.treeGrid === true) {

			try { $(this).jqGrid("setTreeGrid");} catch (_) {}

			if(ts.p.datatype != "local") { ts.p.localReader = {id: "_id_"};	}

		}

		if(this.p.subGrid) {

			try { $(ts).jqGrid("setSubGrid");} catch (s){}

		}

		if(this.p.multiselect) {

			this.p.colNames.unshift("<input role='checkbox' id='cb_"+this.p.id+"' class='cbox' type='checkbox'/>");

			this.p.colModel.unshift({name:'cb',width:$.jgrid.cell_width ? ts.p.multiselectWidth+ts.p.cellLayout : ts.p.multiselectWidth,sortable:false,resizable:false,hidedlg:true,search:false,align:'center',fixed:true});

		}

		if(this.p.rownumbers) {

			this.p.colNames.unshift("");

			this.p.colModel.unshift({name:'rn',width:ts.p.rownumWidth,sortable:false,resizable:false,hidedlg:true,search:false,align:'center',fixed:true});

		}

		ts.p.xmlReader = $.extend(true,{

			root: "rows",

			row: "row",

			page: "rows>page",

			total: "rows>total",

			records : "rows>records",

			repeatitems: true,

			cell: "cell",

			id: "[id]",

			userdata: "userdata",

			subgrid: {root:"rows", row: "row", repeatitems: true, cell:"cell"}

		}, ts.p.xmlReader);

		ts.p.jsonReader = $.extend(true,{

			root: "rows",

			page: "page",

			total: "total",

			records: "records",

			repeatitems: true,

			cell: "cell",

			id: "id",

			userdata: "userdata",

			subgrid: {root:"rows", repeatitems: true, cell:"cell"}

		},ts.p.jsonReader);

		ts.p.localReader = $.extend(true,{

			root: "rows",

			page: "page",

			total: "total",

			records: "records",

			repeatitems: false,

			cell: "cell",

			id: "id",

			userdata: "userdata",

			subgrid: {root:"rows", repeatitems: true, cell:"cell"}

		},ts.p.localReader);

		if(ts.p.scroll){

			ts.p.pgbuttons = false; ts.p.pginput=false; ts.p.rowList=[];

		}

		if(ts.p.data.length) { refreshIndex(); }

		var thead = "<thead><tr class='ui-jqgrid-labels' role='rowheader'>",

		tdc, idn, w, res, sort,

		td, ptr, tbody, imgs,iac="",idc="";

		if(ts.p.shrinkToFit===true && ts.p.forceFit===true) {

			for (i=ts.p.colModel.length-1;i>=0;i--){

				if(!ts.p.colModel[i].hidden) {

					ts.p.colModel[i].resizable=false;

					break;

				}

			}

		}

		if(ts.p.viewsortcols[1] == 'horizontal') {iac=" ui-i-asc";idc=" ui-i-desc";}

		tdc = isMSIE ?  "class='ui-th-div-ie'" :"";

		imgs = "<span class='s-ico' style='display:none'><span sort='asc' class='ui-grid-ico-sort ui-icon-asc"+iac+" ui-state-disabled ui-icon ui-icon-triangle-1-n ui-sort-"+dir+"'></span>";

		imgs += "<span sort='desc' class='ui-grid-ico-sort ui-icon-desc"+idc+" ui-state-disabled ui-icon ui-icon-triangle-1-s ui-sort-"+dir+"'></span></span>";

		for(i=0;i<this.p.colNames.length;i++){

			var tooltip = ts.p.headertitles ? (" title=\""+$.jgrid.stripHtml(ts.p.colNames[i])+"\"") :"";

			thead += "<th id='"+ts.p.id+"_"+ts.p.colModel[i].name+"' role='columnheader' class='ui-state-default ui-th-column ui-th-"+dir+"'"+ tooltip+">";

			idn = ts.p.colModel[i].index || ts.p.colModel[i].name;

			thead += "<div id='jqgh_"+ts.p.id+"_"+ts.p.colModel[i].name+"' "+tdc+">"+ts.p.colNames[i];

			if(!ts.p.colModel[i].width)  { ts.p.colModel[i].width = 150; }

			else { ts.p.colModel[i].width = parseInt(ts.p.colModel[i].width,10); }

			if(typeof ts.p.colModel[i].title !== "boolean") { ts.p.colModel[i].title = true; }

			if (idn == ts.p.sortname) {

				ts.p.lastsort = i;

			}

			thead += imgs+"</div></th>";

		}

		thead += "</tr></thead>";

		imgs = null;

		$(this).append(thead);

		$("thead tr:first th",this).hover(function(){$(this).addClass('ui-state-hover');},function(){$(this).removeClass('ui-state-hover');});

		if(this.p.multiselect) {

			var emp=[], chk;

			$('#cb_'+$.jgrid.jqID(ts.p.id),this).bind('click',function(){

				ts.p.selarrrow = [];

				var froz = ts.p.frozenColumns === true ? ts.p.id + "_frozen" : "";

				if (this.checked) {

					$(ts.rows).each(function(i) {

						if (i>0) {

							if(!$(this).hasClass("ui-subgrid") && !$(this).hasClass("jqgroup") && !$(this).hasClass('ui-state-disabled')){

								$("#jqg_"+$.jgrid.jqID(ts.p.id)+"_"+$.jgrid.jqID(this.id) )[ts.p.useProp ? 'prop': 'attr']("checked",true);

								$(this).addClass("ui-state-highlight").attr("aria-selected","true");  

								ts.p.selarrrow.push(this.id);

								ts.p.selrow = this.id;

								if(froz) {

									$("#jqg_"+$.jgrid.jqID(ts.p.id)+"_"+$.jgrid.jqID(this.id), ts.grid.fbDiv )[ts.p.useProp ? 'prop': 'attr']("checked",true);

									$("#"+$.jgrid.jqID(this.id), ts.grid.fbDiv).addClass("ui-state-highlight");

								}

							}

						}

					});

					chk=true;

					emp=[];

				}

				else {

					$(ts.rows).each(function(i) {

						if(i>0) {

							if(!$(this).hasClass("ui-subgrid") && !$(this).hasClass('ui-state-disabled')){

								$("#jqg_"+$.jgrid.jqID(ts.p.id)+"_"+$.jgrid.jqID(this.id) )[ts.p.useProp ? 'prop': 'attr']("checked", false);

								$(this).removeClass("ui-state-highlight").attr("aria-selected","false");

								emp.push(this.id);

								if(froz) {

									$("#jqg_"+$.jgrid.jqID(ts.p.id)+"_"+$.jgrid.jqID(this.id), ts.grid.fbDiv )[ts.p.useProp ? 'prop': 'attr']("checked",false);

									$("#"+$.jgrid.jqID(this.id), ts.grid.fbDiv).removeClass("ui-state-highlight");

								}

							}

						}

					});

					ts.p.selrow = null;

					chk=false;

				}

				$(ts).triggerHandler("jqGridSelectAll", [chk ? ts.p.selarrrow : emp, chk]);

				if($.isFunction(ts.p.onSelectAll)) {ts.p.onSelectAll.call(ts, chk ? ts.p.selarrrow : emp,chk);}

			});

		}



		if(ts.p.autowidth===true) {

			var pw = $(eg).innerWidth();

			ts.p.width = pw > 0?  pw: 'nw';

		}

		setColWidth();

		$(eg).css("width",grid.width+"px").append("<div class='ui-jqgrid-resize-mark' id='rs_m"+ts.p.id+"'>&#160;</div>");

		$(gv).css("width",grid.width+"px");

		thead = $("thead:first",ts).get(0);

		var	tfoot = "";

		if(ts.p.footerrow) { tfoot += "<table role='grid' style='width:"+ts.p.tblwidth+"px' class='ui-jqgrid-ftable' cellspacing='0' cellpadding='0' border='0'><tbody><tr role='row' class='ui-widget-content footrow footrow-"+dir+"'>"; }

		var thr = $("tr:first",thead),

		firstr = "<tr class='jqgfirstrow' role='row' style='height:auto'>";

		ts.p.disableClick=false;

		$("th",thr).each(function ( j ) {

			w = ts.p.colModel[j].width;

			if(ts.p.colModel[j].resizable === undefined) {ts.p.colModel[j].resizable = true;}

			if(ts.p.colModel[j].resizable){

				res = document.createElement("span");

				$(res).html("&#160;").addClass('ui-jqgrid-resize ui-jqgrid-resize-'+dir);

				if(!$.browser.opera) { $(res).css("cursor","col-resize"); }

				$(this).addClass(ts.p.resizeclass);

			} else {

				res = "";

			}

			$(this).css("width",w+"px").prepend(res);

			var hdcol = "";

			if( ts.p.colModel[j].hidden ) {

				$(this).css("display","none");

				hdcol = "display:none;";

			}

			firstr += "<td role='gridcell' style='height:0px;width:"+w+"px;"+hdcol+"'></td>";

			grid.headers[j] = { width: w, el: this };

			sort = ts.p.colModel[j].sortable;

			if( typeof sort !== 'boolean') {ts.p.colModel[j].sortable =  true; sort=true;}

			var nm = ts.p.colModel[j].name;

			if( !(nm == 'cb' || nm=='subgrid' || nm=='rn') ) {

				if(ts.p.viewsortcols[2]){

					$(">div",this).addClass('ui-jqgrid-sortable');

				}

			}

			if(sort) {

				if(ts.p.viewsortcols[0]) {$("div span.s-ico",this).show(); if(j==ts.p.lastsort){ $("div span.ui-icon-"+ts.p.sortorder,this).removeClass("ui-state-disabled");}}

				else if( j == ts.p.lastsort) {$("div span.s-ico",this).show();$("div span.ui-icon-"+ts.p.sortorder,this).removeClass("ui-state-disabled");}

			}

			if(ts.p.footerrow) { tfoot += "<td role='gridcell' "+formatCol(j,0,'', null, '', false)+">&#160;</td>"; }

		}).mousedown(function(e) {

			if ($(e.target).closest("th>span.ui-jqgrid-resize").length != 1) { return; }

			var ci = getColumnHeaderIndex(this);

			if(ts.p.forceFit===true) {ts.p.nv= nextVisible(ci);}

			grid.dragStart(ci, e, getOffset(ci));

			return false;

		}).click(function(e) {

			if (ts.p.disableClick) {

				ts.p.disableClick = false;

				return false;

			}

			var s = "th>div.ui-jqgrid-sortable",r,d;

			if (!ts.p.viewsortcols[2]) { s = "th>div>span>span.ui-grid-ico-sort"; }

			var t = $(e.target).closest(s);

			if (t.length != 1) { return; }

			var ci = getColumnHeaderIndex(this);

			if (!ts.p.viewsortcols[2]) { r=true;d=t.attr("sort"); }

			sortData( $('div',this)[0].id, ci, r, d);

			return false;

		});

		if (ts.p.sortable && $.fn.sortable) {

			try {

				$(ts).jqGrid("sortableColumns", thr);

			} catch (e){}

		}

		if(ts.p.footerrow) { tfoot += "</tr></tbody></table>"; }

		firstr += "</tr>";

		tbody = document.createElement("tbody");

		this.appendChild(tbody);

		$(this).addClass('ui-jqgrid-btable').append(firstr);

		firstr = null;

		var hTable = $("<table class='ui-jqgrid-htable' style='width:"+ts.p.tblwidth+"px' role='grid' aria-labelledby='gbox_"+this.id+"' cellspacing='0' cellpadding='0' border='0'></table>").append(thead),

		hg = (ts.p.caption && ts.p.hiddengrid===true) ? true : false,

		hb = $("<div class='ui-jqgrid-hbox" + (dir=="rtl" ? "-rtl" : "" )+"'></div>");

		thead = null;

		grid.hDiv = document.createElement("div");

		$(grid.hDiv)

			.css({ width: grid.width+"px"})

			.addClass("ui-state-default ui-jqgrid-hdiv")

			.append(hb);

		$(hb).append(hTable);

		hTable = null;

		if(hg) { $(grid.hDiv).hide(); }

		if(ts.p.pager){

			// TBD -- escape ts.p.pager here?

			if(typeof ts.p.pager === "string") {if(ts.p.pager.substr(0,1) !="#") { ts.p.pager = "#"+ts.p.pager;} }

			else { ts.p.pager = "#"+ $(ts.p.pager).attr("id");}

			$(ts.p.pager).css({width: grid.width+"px"}).appendTo(eg).addClass('ui-state-default ui-jqgrid-pager ui-corner-bottom');

			if(hg) {$(ts.p.pager).hide();}

			setPager(ts.p.pager,'');

		}

		if( ts.p.cellEdit === false && ts.p.hoverrows === true) {

		$(ts).bind('mouseover',function(e) {

			ptr = $(e.target).closest("tr.jqgrow");

			if($(ptr).attr("class") !== "ui-subgrid") {

				$(ptr).addClass("ui-state-hover");

			}

		}).bind('mouseout',function(e) {

			ptr = $(e.target).closest("tr.jqgrow");

			$(ptr).removeClass("ui-state-hover");

		});

		}

		var ri,ci, tdHtml;

		$(ts).before(grid.hDiv).click(function(e) {

			td = e.target;

			ptr = $(td,ts.rows).closest("tr.jqgrow");

			if($(ptr).length === 0 || ptr[0].className.indexOf( 'ui-state-disabled' ) > -1 || ($(td,ts).closest("table.ui-jqgrid-btable").attr('id') || '').replace("_frozen","") !== ts.id ) {

				return this;

			}

			var scb = $(td).hasClass("cbox"),

			cSel = $(ts).triggerHandler("jqGridBeforeSelectRow", [ptr[0].id, e]);

			cSel = (cSel === false || cSel === 'stop') ? false : true;

			if(cSel && $.isFunction(ts.p.beforeSelectRow)) { cSel = ts.p.beforeSelectRow.call(ts,ptr[0].id, e); }

			if (td.tagName == 'A' || ((td.tagName == 'INPUT' || td.tagName == 'TEXTAREA' || td.tagName == 'OPTION' || td.tagName == 'SELECT' ) && !scb) ) { return; }

			if(cSel === true) {

				ri = ptr[0].id;

				ci = $.jgrid.getCellIndex(td);

				tdHtml = $(td).closest("td,th").html();

				$(ts).triggerHandler("jqGridCellSelect", [ri,ci,tdHtml,e]);

				if($.isFunction(ts.p.onCellSelect)) {

					ts.p.onCellSelect.call(ts,ri,ci,tdHtml,e);

				}

				if(ts.p.cellEdit === true) {

					if(ts.p.multiselect && scb){

						$(ts).jqGrid("setSelection", ri ,true,e);

					} else {

						ri = ptr[0].rowIndex;

						try {$(ts).jqGrid("editCell",ri,ci,true);} catch (_) {}

					}

				} else if ( !ts.p.multikey ) {

					if(ts.p.multiselect && ts.p.multiboxonly) {

						if(scb){$(ts).jqGrid("setSelection",ri,true,e);}

						else {

							var frz = ts.p.frozenColumns ? ts.p.id+"_frozen" : "";

							$(ts.p.selarrrow).each(function(i,n){

								var ind = ts.rows.namedItem(n);

								$(ind).removeClass("ui-state-highlight");

								$("#jqg_"+$.jgrid.jqID(ts.p.id)+"_"+$.jgrid.jqID(n))[ts.p.useProp ? 'prop': 'attr']("checked", false);

								if(frz) {

									$("#"+$.jgrid.jqID(n), "#"+$.jgrid.jqID(frz)).removeClass("ui-state-highlight");

									$("#jqg_"+$.jgrid.jqID(ts.p.id)+"_"+$.jgrid.jqID(n), "#"+$.jgrid.jqID(frz))[ts.p.useProp ? 'prop': 'attr']("checked", false);

								}

							});

							ts.p.selarrrow = [];

							$(ts).jqGrid("setSelection",ri,true,e);

						}

					} else {

						$(ts).jqGrid("setSelection",ri,true,e);

					}

				} else {

					if(e[ts.p.multikey]) {

						$(ts).jqGrid("setSelection",ri,true,e);

					} else if(ts.p.multiselect && scb) {

						scb = $("#jqg_"+$.jgrid.jqID(ts.p.id)+"_"+ri).is(":checked");

						$("#jqg_"+$.jgrid.jqID(ts.p.id)+"_"+ri)[ts.p.useProp ? 'prop' : 'attr']("checked", scb);

					}

				}

			}

		}).bind('reloadGrid', function(e,opts) {

			if(ts.p.treeGrid ===true) {	ts.p.datatype = ts.p.treedatatype;}

			if (opts && opts.current) {

				ts.grid.selectionPreserver(ts);

			}

			if(ts.p.datatype=="local"){ $(ts).jqGrid("resetSelection");  if(ts.p.data.length) { refreshIndex();} }

			else if(!ts.p.treeGrid) {

				ts.p.selrow=null;

				if(ts.p.multiselect) {ts.p.selarrrow =[];setHeadCheckBox(false);}

				ts.p.savedRow = [];

			}

			if(ts.p.scroll) {emptyRows.call(ts, true, false);}

			if (opts && opts.page) {

				var page = opts.page;

				if (page > ts.p.lastpage) { page = ts.p.lastpage; }

				if (page < 1) { page = 1; }

				ts.p.page = page;

				if (ts.grid.prevRowHeight) {

					ts.grid.bDiv.scrollTop = (page - 1) * ts.grid.prevRowHeight * ts.p.rowNum;

				} else {

					ts.grid.bDiv.scrollTop = 0;

				}

			}

			if (ts.grid.prevRowHeight && ts.p.scroll) {

				delete ts.p.lastpage;

				ts.grid.populateVisible();

			} else {

				ts.grid.populate();

			}

			if(ts.p._inlinenav===true) {$(ts).jqGrid('showAddEditButtons');}

			return false;

		})

		.dblclick(function(e) {

			td = e.target;

			ptr = $(td,ts.rows).closest("tr.jqgrow");

			if($(ptr).length === 0 ){return;}

			ri = ptr[0].rowIndex;

			ci = $.jgrid.getCellIndex(td);

			$(ts).triggerHandler("jqGridDblClickRow", [$(ptr).attr("id"),ri,ci,e]);

			if ($.isFunction(this.p.ondblClickRow)) { ts.p.ondblClickRow.call(ts,$(ptr).attr("id"),ri,ci, e); }

		})

		.bind('contextmenu', function(e) {

			td = e.target;

			ptr = $(td,ts.rows).closest("tr.jqgrow");

			if($(ptr).length === 0 ){return;}

			if(!ts.p.multiselect) {	$(ts).jqGrid("setSelection",ptr[0].id,true,e);	}

			ri = ptr[0].rowIndex;

			ci = $.jgrid.getCellIndex(td);

			$(ts).triggerHandler("jqGridRightClickRow", [$(ptr).attr("id"),ri,ci,e]);

			if ($.isFunction(this.p.onRightClickRow)) { ts.p.onRightClickRow.call(ts,$(ptr).attr("id"),ri,ci, e); }

		});

		grid.bDiv = document.createElement("div");

		if(isMSIE) { if(String(ts.p.height).toLowerCase() === "auto") { ts.p.height = "100%"; } }

		$(grid.bDiv)

			.append($('<div style="position:relative;'+(isMSIE && $.browser.version < 8 ? "height:0.01%;" : "")+'"></div>').append('<div></div>').append(this))

			.addClass("ui-jqgrid-bdiv")

			.css({ height: ts.p.height+(isNaN(ts.p.height)?"":"px"), width: (grid.width)+"px"})

			.scroll(grid.scrollGrid);

		$("table:first",grid.bDiv).css({width:ts.p.tblwidth+"px"});

		if( isMSIE ) {

			if( $("tbody",this).length == 2 ) { $("tbody:gt(0)",this).remove();}

			if( ts.p.multikey) {$(grid.bDiv).bind("selectstart",function(){return false;});}

		} else {

			if( ts.p.multikey) {$(grid.bDiv).bind("mousedown",function(){return false;});}

		}

		if(hg) {$(grid.bDiv).hide();}

		grid.cDiv = document.createElement("div");

		var arf = ts.p.hidegrid===true ? $("<a role='link' href='javascript:void(0)'/>").addClass('ui-jqgrid-titlebar-close HeaderButton').hover(

			function(){ arf.addClass('ui-state-hover');},

			function() {arf.removeClass('ui-state-hover');})

		.append("<span class='ui-icon ui-icon-circle-triangle-n'></span>").css((dir=="rtl"?"left":"right"),"0px") : "";

		$(grid.cDiv).append(arf).append("<span class='ui-jqgrid-title"+(dir=="rtl" ? "-rtl" :"" )+"'>"+ts.p.caption+"</span>")

		.addClass("ui-jqgrid-titlebar ui-widget-header ui-corner-top ui-helper-clearfix");

		$(grid.cDiv).insertBefore(grid.hDiv);

		if( ts.p.toolbar[0] ) {

			grid.uDiv = document.createElement("div");

			if(ts.p.toolbar[1] == "top") {$(grid.uDiv).insertBefore(grid.hDiv);}

			else if (ts.p.toolbar[1]=="bottom" ) {$(grid.uDiv).insertAfter(grid.hDiv);}

			if(ts.p.toolbar[1]=="both") {

				grid.ubDiv = document.createElement("div");

				$(grid.uDiv).insertBefore(grid.hDiv).addClass("ui-userdata ui-state-default").attr("id","t_"+this.id);

				$(grid.ubDiv).insertAfter(grid.hDiv).addClass("ui-userdata ui-state-default").attr("id","tb_"+this.id);

				if(hg)  {$(grid.ubDiv).hide();}

			} else {

				$(grid.uDiv).width(grid.width).addClass("ui-userdata ui-state-default").attr("id","t_"+this.id);

			}

			if(hg) {$(grid.uDiv).hide();}

		}

		if(ts.p.toppager) {

			ts.p.toppager = $.jgrid.jqID(ts.p.id)+"_toppager";

			grid.topDiv = $("<div id='"+ts.p.toppager+"'></div>")[0];

			ts.p.toppager = "#"+ts.p.toppager;

			$(grid.topDiv).insertBefore(grid.hDiv).addClass('ui-state-default ui-jqgrid-toppager').width(grid.width);

			setPager(ts.p.toppager,'_t');

		}

		if(ts.p.footerrow) {

			grid.sDiv = $("<div class='ui-jqgrid-sdiv'></div>")[0];

			hb = $("<div class='ui-jqgrid-hbox"+(dir=="rtl"?"-rtl":"")+"'></div>");

			$(grid.sDiv).append(hb).insertAfter(grid.hDiv).width(grid.width);

			$(hb).append(tfoot);

			grid.footers = $(".ui-jqgrid-ftable",grid.sDiv)[0].rows[0].cells;

			if(ts.p.rownumbers) { grid.footers[0].className = 'ui-state-default jqgrid-rownum'; }

			if(hg) {$(grid.sDiv).hide();}

		}

		hb = null;

		if(ts.p.caption) {

			var tdt = ts.p.datatype;

			if(ts.p.hidegrid===true) {

				$(".ui-jqgrid-titlebar-close",grid.cDiv).click( function(e){

					var onHdCl = $.isFunction(ts.p.onHeaderClick),

					elems = ".ui-jqgrid-bdiv, .ui-jqgrid-hdiv, .ui-jqgrid-pager, .ui-jqgrid-sdiv",

					counter, self = this;

					if(ts.p.toolbar[0]===true) {

						if( ts.p.toolbar[1]=='both') {

							elems += ', #' + $(grid.ubDiv).attr('id');

						}

						elems += ', #' + $(grid.uDiv).attr('id');

					}

					counter = $(elems,"#gview_"+$.jgrid.jqID(ts.p.id)).length;



					if(ts.p.gridstate == 'visible') {

						$(elems,"#gbox_"+$.jgrid.jqID(ts.p.id)).slideUp("fast", function() {

							counter--;

							if (counter === 0) {

								$("span",self).removeClass("ui-icon-circle-triangle-n").addClass("ui-icon-circle-triangle-s");

								ts.p.gridstate = 'hidden';

								if($("#gbox_"+$.jgrid.jqID(ts.p.id)).hasClass("ui-resizable")) { $(".ui-resizable-handle","#gbox_"+$.jgrid.jqID(ts.p.id)).hide(); }

								$(ts).triggerHandler("jqGridHeaderClick", [ts.p.gridstate,e]);

								if(onHdCl) {if(!hg) {ts.p.onHeaderClick.call(ts,ts.p.gridstate,e);}}

							}

						});

					} else if(ts.p.gridstate == 'hidden'){

						$(elems,"#gbox_"+$.jgrid.jqID(ts.p.id)).slideDown("fast", function() {

							counter--;

							if (counter === 0) {

								$("span",self).removeClass("ui-icon-circle-triangle-s").addClass("ui-icon-circle-triangle-n");

								if(hg) {ts.p.datatype = tdt;populate();hg=false;}

								ts.p.gridstate = 'visible';

								if($("#gbox_"+$.jgrid.jqID(ts.p.id)).hasClass("ui-resizable")) { $(".ui-resizable-handle","#gbox_"+$.jgrid.jqID(ts.p.id)).show(); }

								$(ts).triggerHandler("jqGridHeaderClick", [ts.p.gridstate,e]);

								if(onHdCl) {if(!hg) {ts.p.onHeaderClick.call(ts,ts.p.gridstate,e);}}

							}

						});

					}

					return false;

				});

				if(hg) {ts.p.datatype="local"; $(".ui-jqgrid-titlebar-close",grid.cDiv).trigger("click");}

			}

		} else {$(grid.cDiv).hide();}

		$(grid.hDiv).after(grid.bDiv)

		.mousemove(function (e) {

			if(grid.resizing){grid.dragMove(e);return false;}

		});

		$(".ui-jqgrid-labels",grid.hDiv).bind("selectstart", function () { return false; });

		$(document).mouseup(function () {

			if(grid.resizing) {	grid.dragEnd(); return false;}

			return true;

		});

		ts.formatCol = formatCol;

		ts.sortData = sortData;

		ts.updatepager = updatepager;

		ts.refreshIndex = refreshIndex;

		ts.setHeadCheckBox = setHeadCheckBox;

		ts.constructTr = constructTr;

		ts.formatter = function ( rowId, cellval , colpos, rwdat, act){return formatter(rowId, cellval , colpos, rwdat, act);};

		$.extend(grid,{populate : populate, emptyRows: emptyRows});

		this.grid = grid;

		ts.addXmlData = function(d) {addXmlData(d,ts.grid.bDiv);};

		ts.addJSONData = function(d) {addJSONData(d,ts.grid.bDiv);};

		this.grid.cols = this.rows[0].cells;



		populate();ts.p.hiddengrid=false;

	});

};

$.jgrid.extend({

	getGridParam : function(pName) {

		var $t = this[0];

		if (!$t || !$t.grid) {return;}

		if (!pName) { return $t.p; }

		return $t.p[pName] !== undefined ? $t.p[pName] : null;

	},

	setGridParam : function (newParams){

		return this.each(function(){

			if (this.grid && typeof newParams === 'object') {$.extend(true,this.p,newParams);}

		});

	},

	getDataIDs : function () {

		var ids=[], i=0, len, j=0;

		this.each(function(){

			len = this.rows.length;

			if(len && len>0){

				while(i<len) {

					if($(this.rows[i]).hasClass('jqgrow')) {

						ids[j] = this.rows[i].id;

						j++;

					}

					i++;

				}

			}

		});

		return ids;

	},

	setSelection : function(selection,onsr, e) {

		return this.each(function(){

			var $t = this, stat,pt, ner, ia, tpsr, fid;

			if(selection === undefined) { return; }

			onsr = onsr === false ? false : true;

			pt=$t.rows.namedItem(String(selection));

			if(!pt || !pt.className || pt.className.indexOf( 'ui-state-disabled' ) > -1 ) { return; }

			function scrGrid(iR){

				var ch = $($t.grid.bDiv)[0].clientHeight,

				st = $($t.grid.bDiv)[0].scrollTop,

				rpos = $($t.rows[iR]).position().top,

				rh = $t.rows[iR].clientHeight;

				if(rpos+rh >= ch+st) { $($t.grid.bDiv)[0].scrollTop = rpos-(ch+st)+rh+st; }

				else if(rpos < ch+st) {

					if(rpos < st) {

						$($t.grid.bDiv)[0].scrollTop = rpos;

					}

				}

			}

			if($t.p.scrollrows===true) {

				ner = $t.rows.namedItem(selection).rowIndex;

				if(ner >=0 ){

					scrGrid(ner);

				}

			}

			if($t.p.frozenColumns === true ) {

				fid = $t.p.id+"_frozen";

			}

			if(!$t.p.multiselect) {	

				if(pt.className !== "ui-subgrid") {

					if( $t.p.selrow != pt.id) {

						$($t.rows.namedItem($t.p.selrow)).removeClass("ui-state-highlight").attr({"aria-selected":"false", "tabindex" : "-1"});

						$(pt).addClass("ui-state-highlight").attr({"aria-selected":"true", "tabindex" : "0"});//.focus();

						if(fid) {

							$("#"+$.jgrid.jqID($t.p.selrow), "#"+$.jgrid.jqID(fid)).removeClass("ui-state-highlight");

							$("#"+$.jgrid.jqID(selection), "#"+$.jgrid.jqID(fid)).addClass("ui-state-highlight");

						}

						stat = true;

					} else {

						stat = false;

					}

					$t.p.selrow = pt.id;

					$($t).triggerHandler("jqGridSelectRow", [pt.id, stat, e]);

					if( $t.p.onSelectRow && onsr) { $t.p.onSelectRow.call($t, pt.id, stat, e); }

				}

			} else {

				//unselect selectall checkbox when deselecting a specific row

				$t.setHeadCheckBox( false );

				$t.p.selrow = pt.id;

				ia = $.inArray($t.p.selrow,$t.p.selarrrow);

				if (  ia === -1 ){

					if(pt.className !== "ui-subgrid") { $(pt).addClass("ui-state-highlight").attr("aria-selected","true");}

					stat = true;

					$t.p.selarrrow.push($t.p.selrow);

				} else {

					if(pt.className !== "ui-subgrid") { $(pt).removeClass("ui-state-highlight").attr("aria-selected","false");}

					stat = false;

					$t.p.selarrrow.splice(ia,1);

					tpsr = $t.p.selarrrow[0];

					$t.p.selrow = (tpsr === undefined) ? null : tpsr;

				}

				$("#jqg_"+$.jgrid.jqID($t.p.id)+"_"+$.jgrid.jqID(pt.id))[$t.p.useProp ? 'prop': 'attr']("checked",stat);

				if(fid) {

					if(ia === -1) {

						$("#"+$.jgrid.jqID(selection), "#"+$.jgrid.jqID(fid)).addClass("ui-state-highlight");

					} else {

						$("#"+$.jgrid.jqID(selection), "#"+$.jgrid.jqID(fid)).removeClass("ui-state-highlight");

					}

					$("#jqg_"+$.jgrid.jqID($t.p.id)+"_"+$.jgrid.jqID(selection), "#"+$.jgrid.jqID(fid))[$t.p.useProp ? 'prop': 'attr']("checked",stat);

				}

				$($t).triggerHandler("jqGridSelectRow", [pt.id, stat, e]);

				if( $t.p.onSelectRow && onsr) { $t.p.onSelectRow.call($t, pt.id , stat, e); }

			}

		});

	},

	resetSelection : function( rowid ){

		return this.each(function(){

			var t = this, ind, sr, fid;

			if( t.p.frozenColumns === true ) {

				fid = t.p.id+"_frozen";

			}

			if(rowid !== undefined ) {

				sr = rowid === t.p.selrow ? t.p.selrow : rowid;

				$("#"+$.jgrid.jqID(t.p.id)+" tbody:first tr#"+$.jgrid.jqID(sr)).removeClass("ui-state-highlight").attr("aria-selected","false");

				if (fid) { $("#"+$.jgrid.jqID(sr), "#"+$.jgrid.jqID(fid)).removeClass("ui-state-highlight"); }

				if(t.p.multiselect) {

					$("#jqg_"+$.jgrid.jqID(t.p.id)+"_"+$.jgrid.jqID(sr), "#"+$.jgrid.jqID(t.p.id))[t.p.useProp ? 'prop': 'attr']("checked",false);

					if(fid) { $("#jqg_"+$.jgrid.jqID(t.p.id)+"_"+$.jgrid.jqID(sr), "#"+$.jgrid.jqID(fid))[t.p.useProp ? 'prop': 'attr']("checked",false); }

					t.setHeadCheckBox( false);

				}

				sr = null;

			} else if(!t.p.multiselect) {

				if(t.p.selrow) {

					$("#"+$.jgrid.jqID(t.p.id)+" tbody:first tr#"+$.jgrid.jqID(t.p.selrow)).removeClass("ui-state-highlight").attr("aria-selected","false");

					if(fid) { $("#"+$.jgrid.jqID(t.p.selrow), "#"+$.jgrid.jqID(fid)).removeClass("ui-state-highlight"); }

					t.p.selrow = null;

				}

			} else {

				$(t.p.selarrrow).each(function(i,n){

					ind = t.rows.namedItem(n);

					$(ind).removeClass("ui-state-highlight").attr("aria-selected","false");

					$("#jqg_"+$.jgrid.jqID(t.p.id)+"_"+$.jgrid.jqID(n))[t.p.useProp ? 'prop': 'attr']("checked",false);

					if(fid) { 

						$("#"+$.jgrid.jqID(n), "#"+$.jgrid.jqID(fid)).removeClass("ui-state-highlight"); 

						$("#jqg_"+$.jgrid.jqID(t.p.id)+"_"+$.jgrid.jqID(n), "#"+$.jgrid.jqID(fid))[t.p.useProp ? 'prop': 'attr']("checked",false);

					}

				});

				t.setHeadCheckBox( false );

				t.p.selarrrow = [];

			}

			if(t.p.cellEdit === true) {

				if(parseInt(t.p.iCol,10)>=0  && parseInt(t.p.iRow,10)>=0) {

					$("td:eq("+t.p.iCol+")",t.rows[t.p.iRow]).removeClass("edit-cell ui-state-highlight");

					$(t.rows[t.p.iRow]).removeClass("selected-row ui-state-hover");

				}

			}

			t.p.savedRow = [];

		});

	},

	getRowData : function( rowid ) {

		var res = {}, resall, getall=false, len, j=0;

		this.each(function(){

			var $t = this,nm,ind;

			if(rowid === undefined) {

				getall = true;

				resall = [];

				len = $t.rows.length;

			} else {

				ind = $t.rows.namedItem(rowid);

				if(!ind) { return res; }

				len = 2;

			}

			while(j<len){

				if(getall) { ind = $t.rows[j]; }

				if( $(ind).hasClass('jqgrow') ) {

					$('td[role="gridcell"]',ind).each( function(i) {

						nm = $t.p.colModel[i].name;

						if ( nm !== 'cb' && nm !== 'subgrid' && nm !== 'rn') {

							if($t.p.treeGrid===true && nm == $t.p.ExpandColumn) {

								res[nm] = $.jgrid.htmlDecode($("span:first",this).html());

							} else {

								try {

									res[nm] = $.unformat.call($t,this,{rowId:ind.id, colModel:$t.p.colModel[i]},i);

								} catch (e){

									res[nm] = $.jgrid.htmlDecode($(this).html());

								}

							}

						}

					});

					if(getall) { resall.push(res); res={}; }

				}

				j++;

			}

		});

		return resall || res;

	},

	delRowData : function(rowid) {

		var success = false, rowInd, ia, ri;

		this.each(function() {

			var $t = this;

			rowInd = $t.rows.namedItem(rowid);

			if(!rowInd) {return false;}

				ri = rowInd.rowIndex;

				$(rowInd).remove();

				$t.p.records--;

				$t.p.reccount--;

				$t.updatepager(true,false);

				success=true;

				if($t.p.multiselect) {

					ia = $.inArray(rowid,$t.p.selarrrow);

					if(ia != -1) { $t.p.selarrrow.splice(ia,1);}

				}

				if ($t.p.multiselect && $t.p.selarrrow.length > 0) {

					$t.p.selrow = $t.p.selarrrow[$t.p.selarrrow.length-1];

				} else {

					$t.p.selrow = null;

				}

			if($t.p.datatype == 'local') {

				var id = $.jgrid.stripPref($t.p.idPrefix, rowid),

				pos = $t.p._index[id];

				if(pos !== undefined) {

					$t.p.data.splice(pos,1);

					$t.refreshIndex();

				}

			}

			if( $t.p.altRows === true && success ) {

				var cn = $t.p.altclass;

				$($t.rows).each(function(i){

					if(i % 2 ==1) { $(this).addClass(cn); }

					else { $(this).removeClass(cn); }

				});

			}

		});

		return success;

	},

	setRowData : function(rowid, data, cssp) {

		var nm, success=true, title;

		this.each(function(){

			if(!this.grid) {return false;}

			var t = this, vl, ind, cp = typeof cssp, lcdata={};

			ind = t.rows.namedItem(rowid);

			if(!ind) { return false; }

			if( data ) {

				try {

					$(this.p.colModel).each(function(i){

						nm = this.name;

						if( data[nm] !== undefined) {

							lcdata[nm] = this.formatter && typeof this.formatter === 'string' && this.formatter == 'date' ? $.unformat.date.call(t,data[nm],this) : data[nm];

							vl = t.formatter( rowid, data[nm], i, data, 'edit');

							title = this.title ? {"title":$.jgrid.stripHtml(vl)} : {};

							if(t.p.treeGrid===true && nm == t.p.ExpandColumn) {

								$("td[role='gridcell']:eq("+i+") > span:first",ind).html(vl).attr(title);

							} else {

								$("td[role='gridcell']:eq("+i+")",ind).html(vl).attr(title);

							}

						}

					});

					if(t.p.datatype == 'local') {

						var id = $.jgrid.stripPref(t.p.idPrefix, rowid),

						pos = t.p._index[id], key;

						if(t.p.treeGrid) {

							for(key in t.p.treeReader){

								if(t.p.treeReader.hasOwnProperty(key)) {

									delete lcdata[t.p.treeReader[key]];

								}

							}

						}

						if(pos !== undefined) {

							t.p.data[pos] = $.extend(true, t.p.data[pos], lcdata);

						}

						lcdata = null;

					}

				} catch (e) {

					success = false;

				}

			}

			if(success) {

				if(cp === 'string') {$(ind).addClass(cssp);} else if(cp === 'object') {$(ind).css(cssp);}

				$(t).triggerHandler("jqGridAfterGridComplete");

			}

		});

		return success;

	},

	addRowData : function(rowid,rdata,pos,src) {

		if(!pos) {pos = "last";}

		var success = false, nm, row, gi, si, ni,sind, i, v, prp="", aradd, cnm, cn, data, cm, id;

		if(rdata) {

			if($.isArray(rdata)) {

				aradd=true;

				pos = "last";

				cnm = rowid;

			} else {

				rdata = [rdata];

				aradd = false;

			}

			this.each(function() {

				var t = this, datalen = rdata.length;

				ni = t.p.rownumbers===true ? 1 :0;

				gi = t.p.multiselect ===true ? 1 :0;

				si = t.p.subGrid===true ? 1 :0;

				if(!aradd) {

					if(rowid !== undefined) { rowid = String(rowid);}

					else {

						rowid = $.jgrid.randId();

						if(t.p.keyIndex !== false) {

							cnm = t.p.colModel[t.p.keyIndex+gi+si+ni].name;

							if(rdata[0][cnm] !== undefined) { rowid = rdata[0][cnm]; }

						}

					}

				}

				cn = t.p.altclass;

				var k = 0, cna ="", lcdata = {},

				air = $.isFunction(t.p.afterInsertRow) ? true : false;

				while(k < datalen) {

					data = rdata[k];

					row=[];

					if(aradd) {

						try {

							rowid = data[cnm];

							if(rowid===undefined) {

								rowid = $.jgrid.randId();

							}

						}

						catch (e) {rowid = $.jgrid.randId();}

						cna = t.p.altRows === true ?  (t.rows.length-1)%2 === 0 ? cn : "" : "";

					}

					id = rowid;

					rowid  = t.p.idPrefix + rowid;

					if(ni){

						prp = t.formatCol(0,1,'',null,rowid, true);

						row[row.length] = "<td role=\"gridcell\" class=\"ui-state-default jqgrid-rownum\" "+prp+">0</td>";

					}

					if(gi) {

						v = "<input role=\"checkbox\" type=\"checkbox\""+" id=\"jqg_"+t.p.id+"_"+rowid+"\" class=\"cbox\"/>";

						prp = t.formatCol(ni,1,'', null, rowid, true);

						row[row.length] = "<td role=\"gridcell\" "+prp+">"+v+"</td>";

					}

					if(si) {

						row[row.length] = $(t).jqGrid("addSubGridCell",gi+ni,1);

					}

					for(i = gi+si+ni; i < t.p.colModel.length;i++){

						cm = t.p.colModel[i];

						nm = cm.name;

						lcdata[nm] = data[nm];

						v = t.formatter( rowid, $.jgrid.getAccessor(data,nm), i, data );

						prp = t.formatCol(i,1,v, data, rowid, lcdata);

						row[row.length] = "<td role=\"gridcell\" "+prp+">"+v+"</td>";

					}

					row.unshift( t.constructTr(rowid, false, cna, lcdata, data, false ) );

					row[row.length] = "</tr>";

					if(t.rows.length === 0){

						$("table:first",t.grid.bDiv).append(row.join(''));

					} else {

					switch (pos) {

						case 'last':

							$(t.rows[t.rows.length-1]).after(row.join(''));

							sind = t.rows.length-1;

							break;

						case 'first':

							$(t.rows[0]).after(row.join(''));

							sind = 1;

							break;

						case 'after':

							sind = t.rows.namedItem(src);

							if (sind) {

								if($(t.rows[sind.rowIndex+1]).hasClass("ui-subgrid")) { $(t.rows[sind.rowIndex+1]).after(row); }

								else { $(sind).after(row.join('')); }

							}

							sind++;

							break;

						case 'before':

							sind = t.rows.namedItem(src);

							if(sind) {$(sind).before(row.join(''));sind=sind.rowIndex;}

							sind--;

							break;

					}

					}

					if(t.p.subGrid===true) {

						$(t).jqGrid("addSubGrid",gi+ni, sind);

					}

					t.p.records++;

					t.p.reccount++;

					$(t).triggerHandler("jqGridAfterInsertRow", [rowid,data,data]);

					if(air) { t.p.afterInsertRow.call(t,rowid,data,data); }

					k++;

					if(t.p.datatype == 'local') {

						lcdata[t.p.localReader.id] = id;

						t.p._index[id] = t.p.data.length;

						t.p.data.push(lcdata);

						lcdata = {};

					}

				}

				if( t.p.altRows === true && !aradd) {

					if (pos == "last") {

						if ((t.rows.length-1)%2 == 1)  {$(t.rows[t.rows.length-1]).addClass(cn);}

					} else {

						$(t.rows).each(function(i){

							if(i % 2 ==1) { $(this).addClass(cn); }

							else { $(this).removeClass(cn); }

						});

					}

				}

				t.updatepager(true,true);

				success = true;

			});

		}

		return success;

	},

	footerData : function(action,data, format) {

		var nm, success=false, res={}, title;

		function isEmpty(obj) {

			var i;

			for(i in obj) {

				if (obj.hasOwnProperty(i)) { return false; }

			}

			return true;

		}

		if(action === undefined) { action = "get"; }

		if(typeof format !== "boolean") { format  = true; }

		action = action.toLowerCase();

		this.each(function(){

			var t = this, vl;

			if(!t.grid || !t.p.footerrow) {return false;}

			if(action == "set") { if(isEmpty(data)) { return false; } }

			success=true;

			$(this.p.colModel).each(function(i){

				nm = this.name;

				if(action == "set") {

					if( data[nm] !== undefined) {

						vl = format ? t.formatter( "", data[nm], i, data, 'edit') : data[nm];

						title = this.title ? {"title":$.jgrid.stripHtml(vl)} : {};

						$("tr.footrow td:eq("+i+")",t.grid.sDiv).html(vl).attr(title);

						success = true;

					}

				} else if(action == "get") {

					res[nm] = $("tr.footrow td:eq("+i+")",t.grid.sDiv).html();

				}

			});

		});

		return action == "get" ? res : success;

	},

	showHideCol : function(colname,show) {

		return this.each(function() {

			var $t = this, fndh=false, brd=$.jgrid.cell_width ? 0: $t.p.cellLayout, cw;

			if (!$t.grid ) {return;}

			if( typeof colname === 'string') {colname=[colname];}

			show = show != "none" ? "" : "none";

			var sw = show === "" ? true :false,

			gh = $t.p.groupHeader && (typeof $t.p.groupHeader === 'object' || $.isFunction($t.p.groupHeader) );

			if(gh) { $($t).jqGrid('destroyGroupHeader', false); }

			$(this.p.colModel).each(function(i) {

				if ($.inArray(this.name,colname) !== -1 && this.hidden === sw) {

					if($t.p.frozenColumns === true && this.frozen === true) {

						return true;

					}

					$("tr",$t.grid.hDiv).each(function(){

						$(this.cells[i]).css("display", show);

					});

					$($t.rows).each(function(){

						if (!$(this).hasClass("jqgroup")) {

							$(this.cells[i]).css("display", show);

						}

					});

					if($t.p.footerrow) { $("tr.footrow td:eq("+i+")", $t.grid.sDiv).css("display", show); }

					cw =  parseInt(this.width,10);

					if(show === "none") {

						$t.p.tblwidth -= cw+brd;

					} else {

						$t.p.tblwidth += cw+brd;

					}

					this.hidden = !sw;

					fndh=true;

					$($t).triggerHandler("jqGridShowHideCol", [sw,this.name,i]);

				}

			});

			if(fndh===true) {

				if($t.p.shrinkToFit === true && !isNaN($t.p.height)) { $t.p.tblwidth += parseInt($t.p.scrollOffset,10);}

				$($t).jqGrid("setGridWidth",$t.p.shrinkToFit === true ? $t.p.tblwidth : $t.p.width );

			}

			if( gh )  {

				$($t).jqGrid('setGroupHeaders',$t.p.groupHeader);

			}

		});

	},

	hideCol : function (colname) {

		return this.each(function(){$(this).jqGrid("showHideCol",colname,"none");});

	},

	showCol : function(colname) {

		return this.each(function(){$(this).jqGrid("showHideCol",colname,"");});

	},

	remapColumns : function(permutation, updateCells, keepHeader)

	{

		function resortArray(a) {

			var ac;

			if (a.length) {

				ac = $.makeArray(a);

			} else {

				ac = $.extend({}, a);

			}

			$.each(permutation, function(i) {

				a[i] = ac[this];

			});

		}

		var ts = this.get(0);

		function resortRows(parent, clobj) {

			$(">tr"+(clobj||""), parent).each(function() {

				var row = this;

				var elems = $.makeArray(row.cells);

				$.each(permutation, function() {

					var e = elems[this];

					if (e) {

						row.appendChild(e);

					}

				});

			});

		}

		resortArray(ts.p.colModel);

		resortArray(ts.p.colNames);

		resortArray(ts.grid.headers);

		resortRows($("thead:first", ts.grid.hDiv), keepHeader && ":not(.ui-jqgrid-labels)");

		if (updateCells) {

			resortRows($("#"+$.jgrid.jqID(ts.p.id)+" tbody:first"), ".jqgfirstrow, tr.jqgrow, tr.jqfoot");

		}

		if (ts.p.footerrow) {

			resortRows($("tbody:first", ts.grid.sDiv));

		}

		if (ts.p.remapColumns) {

			if (!ts.p.remapColumns.length){

				ts.p.remapColumns = $.makeArray(permutation);

			} else {

				resortArray(ts.p.remapColumns);

			}

		}

		ts.p.lastsort = $.inArray(ts.p.lastsort, permutation);

		if(ts.p.treeGrid) { ts.p.expColInd = $.inArray(ts.p.expColInd, permutation); }

		$(ts).triggerHandler("jqGridRemapColumns", [permutation, updateCells, keepHeader]);

	},

	setGridWidth : function(nwidth, shrink) {

		return this.each(function(){

			if (!this.grid ) {return;}

			var $t = this, cw,

			initwidth = 0, brd=$.jgrid.cell_width ? 0: $t.p.cellLayout, lvc, vc=0, hs=false, scw=$t.p.scrollOffset, aw, gw=0,

			cl = 0,cr;

			if(typeof shrink !== 'boolean') {

				shrink=$t.p.shrinkToFit;

			}

			if(isNaN(nwidth)) {return;}

			nwidth = parseInt(nwidth,10); 

			$t.grid.width = $t.p.width = nwidth;

			$("#gbox_"+$.jgrid.jqID($t.p.id)).css("width",nwidth+"px");

			$("#gview_"+$.jgrid.jqID($t.p.id)).css("width",nwidth+"px");

			$($t.grid.bDiv).css("width",nwidth+"px");

			$($t.grid.hDiv).css("width",nwidth+"px");

			if($t.p.pager ) {$($t.p.pager).css("width",nwidth+"px");}

			if($t.p.toppager ) {$($t.p.toppager).css("width",nwidth+"px");}

			if($t.p.toolbar[0] === true){

				$($t.grid.uDiv).css("width",nwidth+"px");

				if($t.p.toolbar[1]=="both") {$($t.grid.ubDiv).css("width",nwidth+"px");}

			}

			if($t.p.footerrow) { $($t.grid.sDiv).css("width",nwidth+"px"); }

			if(shrink ===false && $t.p.forceFit === true) {$t.p.forceFit=false;}

			if(shrink===true) {

				$.each($t.p.colModel, function() {

					if(this.hidden===false){

						cw = this.widthOrg;

						initwidth += cw+brd;

						if(this.fixed) {

							gw += cw+brd;

						} else {

							vc++;

						}

						cl++;

					}

				});

				if(vc  === 0) { return; }

				$t.p.tblwidth = initwidth;

				aw = nwidth-brd*vc-gw;

				if(!isNaN($t.p.height)) {

					if($($t.grid.bDiv)[0].clientHeight < $($t.grid.bDiv)[0].scrollHeight || $t.rows.length === 1){

						hs = true;

						aw -= scw;

					}

				}

				initwidth =0;

				var cle = $t.grid.cols.length >0;

				$.each($t.p.colModel, function(i) {

					if(this.hidden === false && !this.fixed){

						cw = this.widthOrg;

						cw = Math.round(aw*cw/($t.p.tblwidth-brd*vc-gw));

						if (cw < 0) { return; }

						this.width =cw;

						initwidth += cw;

						$t.grid.headers[i].width=cw;

						$t.grid.headers[i].el.style.width=cw+"px";

						if($t.p.footerrow) { $t.grid.footers[i].style.width = cw+"px"; }

						if(cle) { $t.grid.cols[i].style.width = cw+"px"; }

						lvc = i;

					}

				});



				if (!lvc) { return; }



				cr =0;

				if (hs) {

					if(nwidth-gw-(initwidth+brd*vc) !== scw){

						cr = nwidth-gw-(initwidth+brd*vc)-scw;

					}

				} else if( Math.abs(nwidth-gw-(initwidth+brd*vc)) !== 1) {

					cr = nwidth-gw-(initwidth+brd*vc);

				}

				$t.p.colModel[lvc].width += cr;

				$t.p.tblwidth = initwidth+cr+brd*vc+gw;

				if($t.p.tblwidth > nwidth) {

					var delta = $t.p.tblwidth - parseInt(nwidth,10);

					$t.p.tblwidth = nwidth;

					cw = $t.p.colModel[lvc].width = $t.p.colModel[lvc].width-delta;

				} else {

					cw= $t.p.colModel[lvc].width;

				}

				$t.grid.headers[lvc].width = cw;

				$t.grid.headers[lvc].el.style.width=cw+"px";

				if(cle) { $t.grid.cols[lvc].style.width = cw+"px"; }

				if($t.p.footerrow) {

					$t.grid.footers[lvc].style.width = cw+"px";

				}

			}

			if($t.p.tblwidth) {

				$('table:first',$t.grid.bDiv).css("width",$t.p.tblwidth+"px");

				$('table:first',$t.grid.hDiv).css("width",$t.p.tblwidth+"px");

				$t.grid.hDiv.scrollLeft = $t.grid.bDiv.scrollLeft;

				if($t.p.footerrow) {

					$('table:first',$t.grid.sDiv).css("width",$t.p.tblwidth+"px");

				}

			}

		});

	},

	setGridHeight : function (nh) {

		return this.each(function (){

			var $t = this;

			if(!$t.grid) {return;}

			var bDiv = $($t.grid.bDiv);

			bDiv.css({height: nh+(isNaN(nh)?"":"px")});

			if($t.p.frozenColumns === true){

				//follow the original set height to use 16, better scrollbar width detection

				$('#'+$.jgrid.jqID($t.p.id)+"_frozen").parent().height(bDiv.height() - 16);

			}

			$t.p.height = nh;

			if ($t.p.scroll) { $t.grid.populateVisible(); }

		});

	},

	setCaption : function (newcap){

		return this.each(function(){

			this.p.caption=newcap;

			$("span.ui-jqgrid-title, span.ui-jqgrid-title-rtl",this.grid.cDiv).html(newcap);

			$(this.grid.cDiv).show();

		});

	},

	setLabel : function(colname, nData, prop, attrp ){

		return this.each(function(){

			var $t = this, pos=-1;

			if(!$t.grid) {return;}

			if(colname !== undefined) {

				$($t.p.colModel).each(function(i){

					if (this.name == colname) {

						pos = i;return false;

					}

				});

			} else { return; }

			if(pos>=0) {

				var thecol = $("tr.ui-jqgrid-labels th:eq("+pos+")",$t.grid.hDiv);

				if (nData){

					var ico = $(".s-ico",thecol);

					$("[id^=jqgh_]",thecol).empty().html(nData).append(ico);

					$t.p.colNames[pos] = nData;

				}

				if (prop) {

					if(typeof prop === 'string') {$(thecol).addClass(prop);} else {$(thecol).css(prop);}

				}

				if(typeof attrp === 'object') {$(thecol).attr(attrp);}

			}

		});

	},

	setCell : function(rowid,colname,nData,cssp,attrp, forceupd) {

		return this.each(function(){

			var $t = this, pos =-1,v, title;

			if(!$t.grid) {return;}

			if(isNaN(colname)) {

				$($t.p.colModel).each(function(i){

					if (this.name == colname) {

						pos = i;return false;

					}

				});

			} else {pos = parseInt(colname,10);}

			if(pos>=0) {

				var ind = $t.rows.namedItem(rowid);

				if (ind){

					var tcell = $("td:eq("+pos+")",ind);

					if(nData !== "" || forceupd === true) {

						v = $t.formatter(rowid, nData, pos,ind,'edit');

						title = $t.p.colModel[pos].title ? {"title":$.jgrid.stripHtml(v)} : {};

						if($t.p.treeGrid && $(".tree-wrap",$(tcell)).length>0) {

							$("span",$(tcell)).html(v).attr(title);

						} else {

							$(tcell).html(v).attr(title);

						}

						if($t.p.datatype == "local") {

							var cm = $t.p.colModel[pos], index;

							nData = cm.formatter && typeof cm.formatter === 'string' && cm.formatter == 'date' ? $.unformat.date.call($t,nData,cm) : nData;

							index = $t.p._index[rowid];

							if(index !== undefined) {

								$t.p.data[index][cm.name] = nData;

							}

						}

					}

					if(typeof cssp === 'string'){

						$(tcell).addClass(cssp);

					} else if(cssp) {

						$(tcell).css(cssp);

					}

					if(typeof attrp === 'object') {$(tcell).attr(attrp);}

				}

			}

		});

	},

	getCell : function(rowid,col) {

		var ret = false;

		this.each(function(){

			var $t=this, pos=-1;

			if(!$t.grid) {return;}

			if(isNaN(col)) {

				$($t.p.colModel).each(function(i){

					if (this.name === col) {

						pos = i;return false;

					}

				});

			} else {pos = parseInt(col,10);}

			if(pos>=0) {

				var ind = $t.rows.namedItem(rowid);

				if(ind) {

					try {

						ret = $.unformat.call($t,$("td:eq("+pos+")",ind),{rowId:ind.id, colModel:$t.p.colModel[pos]},pos);

					} catch (e){

						ret = $.jgrid.htmlDecode($("td:eq("+pos+")",ind).html());

					}

				}

			}

		});

		return ret;

	},

	getCol : function (col, obj, mathopr) {

		var ret = [], val, sum=0, min, max, v;

		obj = typeof obj !== 'boolean' ? false : obj;

		if(mathopr === undefined) { mathopr = false; }

		this.each(function(){

			var $t=this, pos=-1;

			if(!$t.grid) {return;}

			if(isNaN(col)) {

				$($t.p.colModel).each(function(i){

					if (this.name === col) {

						pos = i;return false;

					}

				});

			} else {pos = parseInt(col,10);}

			if(pos>=0) {

				var ln = $t.rows.length, i =0;

				if (ln && ln>0){

					while(i<ln){

						if($($t.rows[i]).hasClass('jqgrow')) {

							try {

								val = $.unformat.call($t,$($t.rows[i].cells[pos]),{rowId:$t.rows[i].id, colModel:$t.p.colModel[pos]},pos);

							} catch (e) {

								val = $.jgrid.htmlDecode($t.rows[i].cells[pos].innerHTML);

							}

							if(mathopr) {

								v = parseFloat(val);

								sum += v;

								if (max === undefined) {max = min = v;}

								min = Math.min(min, v);

								max = Math.max(max, v);

							}

							else if(obj) { ret.push( {id:$t.rows[i].id,value:val} ); }

							else { ret.push( val ); }

						}

						i++;

					}

					if(mathopr) {

						switch(mathopr.toLowerCase()){

							case 'sum': ret =sum; break;

							case 'avg': ret = sum/ln; break;

							case 'count': ret = ln; break;

							case 'min': ret = min; break;

							case 'max': ret = max; break;

						}

					}

				}

			}

		});

		return ret;

	},

	clearGridData : function(clearfooter) {

		return this.each(function(){

			var $t = this;

			if(!$t.grid) {return;}

			if(typeof clearfooter !== 'boolean') { clearfooter = false; }

			if($t.p.deepempty) {$("#"+$.jgrid.jqID($t.p.id)+" tbody:first tr:gt(0)").remove();}

			else {

				var trf = $("#"+$.jgrid.jqID($t.p.id)+" tbody:first tr:first")[0];

				$("#"+$.jgrid.jqID($t.p.id)+" tbody:first").empty().append(trf);

			}

			if($t.p.footerrow && clearfooter) { $(".ui-jqgrid-ftable td",$t.grid.sDiv).html("&#160;"); }

			$t.p.selrow = null; $t.p.selarrrow= []; $t.p.savedRow = [];

			$t.p.records = 0;$t.p.page=1;$t.p.lastpage=0;$t.p.reccount=0;

			$t.p.data = []; $t.p._index = {};

			$t.updatepager(true,false);

		});

	},

	getInd : function(rowid,rc){

		var ret =false,rw;

		this.each(function(){

			rw = this.rows.namedItem(rowid);

			if(rw) {

				ret = rc===true ? rw: rw.rowIndex;

			}

		});

		return ret;

	},

	bindKeys : function( settings ){

		var o = $.extend({

			onEnter: null,

			onSpace: null,

			onLeftKey: null,

			onRightKey: null,

			scrollingRows : true

		},settings || {});

		return this.each(function(){

			var $t = this;

			if( !$('body').is('[role]') ){$('body').attr('role','application');}

			$t.p.scrollrows = o.scrollingRows;

			$($t).keydown(function(event){

				var target = $($t).find('tr[tabindex=0]')[0], id, r, mind,

				expanded = $t.p.treeReader.expanded_field;

				//check for arrow keys

				if(target) {

					mind = $t.p._index[target.id];

					if(event.keyCode === 37 || event.keyCode === 38 || event.keyCode === 39 || event.keyCode === 40){

						// up key

						if(event.keyCode === 38 ){

							r = target.previousSibling;

							id = "";

							if(r) {

								if($(r).is(":hidden")) {

									while(r) {

										r = r.previousSibling;

										if(!$(r).is(":hidden") && $(r).hasClass('jqgrow')) {id = r.id;break;}

									}

								} else {

									id = r.id;

								}

							}

							$($t).jqGrid('setSelection', id, true, event);

							event.preventDefault();

						}

						//if key is down arrow

						if(event.keyCode === 40){

							r = target.nextSibling;

							id ="";

							if(r) {

								if($(r).is(":hidden")) {

									while(r) {

										r = r.nextSibling;

										if(!$(r).is(":hidden") && $(r).hasClass('jqgrow') ) {id = r.id;break;}

									}

								} else {

									id = r.id;

								}

							}

							$($t).jqGrid('setSelection', id, true, event);

							event.preventDefault();

						}

						// left

						if(event.keyCode === 37 ){

							if($t.p.treeGrid && $t.p.data[mind][expanded]) {

								$(target).find("div.treeclick").trigger('click');

							}

							$($t).triggerHandler("jqGridKeyLeft", [$t.p.selrow]);

							if($.isFunction(o.onLeftKey)) {

								o.onLeftKey.call($t, $t.p.selrow);

							}

						}

						// right

						if(event.keyCode === 39 ){

							if($t.p.treeGrid && !$t.p.data[mind][expanded]) {

								$(target).find("div.treeclick").trigger('click');

							}

							$($t).triggerHandler("jqGridKeyRight", [$t.p.selrow]);

							if($.isFunction(o.onRightKey)) {

								o.onRightKey.call($t, $t.p.selrow);

							}

						}

					}

					//check if enter was pressed on a grid or treegrid node

					else if( event.keyCode === 13 ){

						$($t).triggerHandler("jqGridKeyEnter", [$t.p.selrow]);

						if($.isFunction(o.onEnter)) {

							o.onEnter.call($t, $t.p.selrow);

						}

					} else if(event.keyCode === 32) {

						$($t).triggerHandler("jqGridKeySpace", [$t.p.selrow]);

						if($.isFunction(o.onSpace)) {

							o.onSpace.call($t, $t.p.selrow);

						}

					}

				}

			});

		});

	},

	unbindKeys : function(){

		return this.each(function(){

			$(this).unbind('keydown');

		});

	},

	getLocalRow : function (rowid) {

		var ret = false, ind;

		this.each(function(){

			if(rowid !== undefined) {

				ind = this.p._index[rowid];

				if(ind >= 0 ) {

					ret = this.p.data[ind];

				}

			}

		});

		return ret;

	}

});

})(jQuery);

