if(typeof(USER)=="undefined" || USER.root==0){ Jade.Redirect(); }
else{ 
	var lblQuery = new Jade.Objects.Text('Query:');
	lblQuery.Location(15,15,100,20);
	lblQuery.Add();
	var txtQuery = new Jade.Objects.TextArea("SELECT IdEmpleado,Fecha,Hora,IdTerminal FROM FICHAJES002 WHERE IdEmpleado='00001'");
	txtQuery.Location(100,10,890,80);
	txtQuery.Add();
	var cmdRun = new Jade.Objects.Button('Run');
	cmdRun.Location(100,100,890,20);
	cmdRun.Add();
	var lblResult = new Jade.Objects.Text('Result:');
	lblResult.Location(15,135,100,20);
	lblResult.Add();
	
	var columns = new Jade.Grids.ColumnModel();
	var grid = new Jade.Grids.Grid(columns);
	grid.Location(100,130,890,380);
	grid.Add();
	
	cmdRun.onclick=function(){
		grid.Loading(true);
		var ajax = new Jade.Ajax();
		ajax.AddParam('query',txtQuery.GetText());
		ajax.Send("lib/php/ajax_db.php");
		ajax.oncomplete=function(){
			grid.ClearRows();
			if(!this.json.error){
				columns = new Jade.Grids.ColumnModel();
				if(this.json.row.length!=0){
					for(var i in this.json.row[0]){
						columns.Add(new Jade.Grids.SimpleColumn({key: '-',  text: i, width: '120'}));	
					}
				}
				grid.Loading(false);
				grid = new Jade.Grids.Grid(columns);
				grid.Location(100,130,(columns.columns.length*120)+20,380);
				grid.Add();
				for(var i=0,iLen=this.json.row.length;i<iLen;i++){
					var dbRow=this.json.row[i];
					var values=[];
					for(var x in dbRow){
						values.push(dbRow[x]);
					}
					grid.NewRow(values);
				}
				grid.Loading(true);
			}
			else{
				alert(this.json.error);
			}
			grid.Loading(false);
		}
	}
}