{//CONTROLS
	{//RESUMEN
		var gpResume = $.Objects.Container();
		gpResume.Location(0,0,1000,50)
		gpResume.Add();
		var lblResume0 = $.Objects.Text('<b>'+$.lng('resume_0')+':</b>');
		lblResume0.Location(15,10,970,20);
		lblResume0.Add(gpResume);
		var lblResume1 = $.Objects.Text('<b>'+$.lng('resume_1')+':</b>');
		lblResume1.Location(15,25,970,20);
		lblResume1.Add(gpResume);
	}
	{//FICHAJES
		var gpChecks=$.Objects.Container();
		gpChecks.Location(0,55,1000,240);
		gpChecks.Add(); 
		var lblChecks=$.Objects.Text($.lng('resume_checks')+':');
		lblChecks.Location(10,5,970,20);
		lblChecks.Add(gpChecks);
		var lstChecks=$.Objects.SimpleGrid();
		lstChecks.Location(15,20,970,195);
		lstChecks.AddHeader($.lng('cod'),85,'fixed');
		lstChecks.AddHeader($.lng('name'),195,'fixed');
		lstChecks.AddHeader($.lng('date'),90,'fixed');
		lstChecks.AddHeader($.lng('check'),100);
		lstChecks.AddHeader($.lng('check'),100);
		lstChecks.AddHeader($.lng('check'),100);
		lstChecks.AddHeader($.lng('check'),100);
		lstChecks.AddHeader($.lng('check'),100);
		lstChecks.AddHeader($.lng('check'),100);
		lstChecks.Add(gpChecks);
		if($.Utils.Privilege('checks_maintenance')){
			var lblChecksMore=$.Objects.Link($.lng('see_more'));
			lblChecksMore.SetHref('?punches');
			lblChecksMore.SetTextAlign('right');
			lblChecksMore.Location(15,220,970,20);
			lblChecksMore.Add(gpChecks);
		}
	}
	{//PETICIONES
		var gpPetitions = new $.Objects.Container();
		gpPetitions.Location(0,300,1000,235);
		gpPetitions.Add();
		var lblPetitions = new $.Objects.Text($.lng('resume_petitions')+':');
		lblPetitions.Location(10,5,970,20);
		lblPetitions.Add(gpPetitions);
		var lstPetitions = new $.Objects.SimpleGrid();
		lstPetitions.Location(15,20,970,190);
		lstPetitions.AddHeader($.lng('date'),95);
		lstPetitions.AddHeader($.lng('time'),75);
		lstPetitions.AddHeader($.lng('user'),150);
		lstPetitions.AddHeader($.lng('petition'),550);
		lstPetitions.AddHeader($.lng('status'),100);
		lstPetitions.Add(gpPetitions);
		if($.Utils.PrivilegeValue('empl_priv')!=0){
			var lblPetitions = new $.Objects.Text($.lng('resume_petitions')+':');
			lblPetitions.Location(10,5,970,20);
			lblPetitions.Add(gpPetitions);
			var lblPetitionsMore = new $.Objects.Text('<a href="?petitions" class="jade_link">'+$.lng('see_more')+'</a>');
			lblPetitionsMore.SetTextAlign('right');
			lblPetitionsMore.Location(15,215,970,20);
			lblPetitionsMore.Add(gpPetitions);
		}
	}
}
{//Ajax
	var ajaxManager=$.AjaxManager();
	{//Basic
		ajaxManager.Create('basic');
		{//Employees-----------------------------------------------------------------------------------------------------------------------------------------------
			var aEmployees=[];
			var ajaxEmployees=$.AjaxDB("SELECT Codigo,Nombre,Apellidos FROM EMPLEADOS{c} ORDER BY Codigo");
			ajaxEmployees.Add(ajaxManager,'basic');
			ajaxEmployees.oncomplete=function(){
				if(!this.json.error){
					for(var i=0,iLen=this.json.row.length;i<iLen;i++){
						var item=this.json.row[i];
						aEmployees[item.Codigo]={};
						aEmployees[item.Codigo].Text=item.Codigo+' - '+item.Nombre+' '+item.Apellidos;
						aEmployees[item.Codigo].Codigo=item.Codigo;
						aEmployees[item.Codigo].Nombre=item.Nombre;
						aEmployees[item.Codigo].Apellidos=item.Apellidos;
					}
				}
				else{ $.Debug.Add(this.text); }
			}
		}
	}
	{//Data
		ajaxManager.Create('data');
		{//Ultima captura de fichajes
			switch(CONFIG.DB_TYPE){
				case '0': query="SELECT * FROM TERMINALES_HISTORICO{c} WHERE IdOrden='0' ORDER BY Fecha DESC, Hora DESC LIMIT 0,1"; break;
				case '1': query="SELECT TOP 1 * FROM TERMINALES_HISTORICO{c} WHERE IdOrden='0' ORDER BY Fecha DESC, Hora DESC"; break;
				case '2': query="SELECT * FROM (SELECT * FROM TERMINALES_HISTORICO{c} WHERE IdOrden='0' ORDER BY Fecha DESC, Hora) WHERE rownum<=1"; break;
			}
			var ajaxResume0=$.AjaxDB(query);
			ajaxResume0.Add(ajaxManager,'data');
			ajaxResume0.oncomplete=function(){
				if(!this.json.error){
					if(this.json.row.length!=0){
						lblResume0.SetText('<b>'+$.lng('resume_0')+':</b> '+$.Strings.ToDate(this.json.row[0].Fecha)+' '+$.Strings.ToTime(this.json.row[0].Hora));
					}
				}
				else{ $.Debug.Add(this.text); }
			}
		}
		{//Ultimo cierre realizado
			switch(CONFIG.DB_TYPE){
				case '0': query="SELECT FechaValor FROM FICHAJES{c} WHERE Cerrado='1' ORDER BY FechaValor DESC LIMIT 0,1"; break;
				case '1': query="SELECT TOP 1 FechaValor FROM FICHAJES{c} WHERE Cerrado='1' ORDER BY FechaValor DESC"; break;
				case '2': query="SELECT * FROM (SELECT FechaValor FROM FICHAJES{c} WHERE Cerrado='1' ORDER BY FechaValor) WHERE rownum<=1"; break;
			}		
			var ajaxResume1=$.AjaxDB(query);
			ajaxResume1.Add(ajaxManager,'data');
			ajaxResume1.oncomplete = function() {
				if($.isDefined(this.json) && !this.json.error){
					if(this.json.row.length!=0){
						lblResume1.SetText('<b>'+$.lng('resume_1')+':</b> '+$.Strings.ToDate(this.json.row[0].FechaValor));
					}
				}
				else{ $.Debug.Add(this.text); }
			}
		}
		{//Ultimos fichajes
			var ajaxChecks=$.AjaxDB("SELECT MAX(Fecha) As Fecha FROM FICHAJES{c}");
			ajaxChecks.Add(ajaxManager,'data');
			ajaxChecks.oncomplete=function(){
				if(!this.json.error){
					var ajaxChecks0=$.AjaxDB("SELECT F.IdEmpleado, E.Nombre, E.Apellidos, F.Fecha, F.Hora, F.IdIncidencia, I.Descripcion " +
									"FROM FICHAJES{c} F INNER JOIN EMPLEADOS{c} E ON F.IdEmpleado=E.Codigo INNER JOIN INCIDENCIAS{c} I ON F.IdIncidencia=I.Codigo "+
									"WHERE Fecha='"+this.json.row[0].Fecha+"' ORDER BY F.IdEmpleado, F.Fecha, F.Hora", Ajax.Dontwait);
					ajaxChecks0.oncomplete=function(){
						if(!this.json.error){
							if(this.json.row.length!=0){
								var firstRow=this.json.row[0];
								var employee = {id: firstRow.IdEmpleado, nombre: firstRow.Nombre+' '+firstRow.Apellidos, fecha: $.Strings.ToDate(firstRow.Fecha), fichajes: []};
								for(var i=0,iLen=this.json.row.length;i<iLen;i++){
									var row=this.json.row[i];
									if($.Utils.IsInEmployees(row.IdEmpleado)){
										if(employee.id!=row.IdEmpleado){
											AddChecksLines(employee);
											employee.id=row.IdEmpleado;
											employee.nombre=row.Nombre+' '+row.Apellidos;
										}
										employee.fichajes.push($.Strings.ToTime(row.Hora,false)+' '+$.Pagesheet.FormatIncidence(row.IdIncidencia));
									}
									if(i==this.json.row.length-1){ AddChecksLines(employee); }
								}
							}
						}
						else{ $.Debug.Add(this.text); }
					}
				}
				else{ $.Debug.Add(this.text); }
			}	
		}
		{//Ultimas peticiones
			var query=''
			switch(CONFIG.DB_TYPE){
				case '0':
					query="SELECT * FROM PETICIONES{c} p INNER JOIN users u ON p.IdUsuario=u.id ORDER BY Fecha DESC, Hora DESC LIMIT 0,12";
					break;
				case '1':
					query="SELECT TOP 12 * FROM PETICIONES{c} p INNER JOIN users u ON p.IdUsuario=u.id ORDER BY Fecha DESC, Hora DESC";
					break;
				case '2':
					query="SELECT * FROM (SELECT * FROM PETICIONES{c} p INNER JOIN users u ON p.IdUsuario=u.id ORDER BY Fecha DESC, Hora DESC) WHERE rownum<=10";
					break;
			}
			var ajaxPetitions=$.AjaxDB(query);
			ajaxPetitions.Add(ajaxManager,'data');
			ajaxPetitions.oncomplete=function(){
				if(!this.json.error){
					var pttCount=0;
					for(var i=0,iLen=this.json.row.length;i<iLen;i++){
						if($.Utils.IsInEmployees(this.json.row[i].Clave)){
							var row=this.json.row[i];
							var values=[$.Strings.ToDate(row.Fecha),$.Strings.ToTime(row.Hora),$.Encrypt.Decrypt(row.name),$.Utils.PetitionFamily(row.Familia,row.Clave),$.lng('petition_status_'+row.Estado)]
							var styles=['','','','','petition_'+row.Estado];
							lstPetitions.AddStyledItem(values,styles);
							if(row.Estado=='0'){ pttCount++; }
						}
					}
				}
				else{ $.Debug.Add(this.text); }
			}
			if($.Utils.PrivilegeValue('empl_priv')!=0){
				var ajaxSumPetitions=$.AjaxDB("SELECT Clave FROM PETICIONES{c} WHERE Estado='0'");
				ajaxSumPetitions.Add(ajaxManager,'data');
				ajaxSumPetitions.oncomplete=function(){
					if(!this.json.error){
						var count = 0;
						for(var i=0, ilen=this.json.row.length; i<ilen; i++){
							if($.Utils.IsInEmployees(this.json.row[i].clave)){
								count++;
							}
						}
						if(count>0){
							lblPetitionsMore.SetText($.Strings.Format($.lng('pending_petitions_'+((count==1)?'1':'n')),'<b>'+count+'</b>')+': '+lblPetitionsMore.GetText());
						}
					}
					else{ $.Debug.Add(this.text); }
				}
			}
		}
	}
	{
		ajaxManager.group['basic'].Start();
		ajaxManager.group['basic'].oncomplete=function(){ ajaxManager.group['data'].Start(); }
	}
}
function AddChecksLines(empleado){
	for(var x=0,xLen=empleado.fichajes.length;x<xLen;x+=6){
		var fichaje1=typeof(empleado.fichajes[x])=="undefined" ? '' : empleado.fichajes[x];
		var fichaje2=typeof(empleado.fichajes[x+1])=="undefined" ? '' : empleado.fichajes[x+1];
		var fichaje3=typeof(empleado.fichajes[x+2])=="undefined" ? '' : empleado.fichajes[x+2];
		var fichaje4=typeof(empleado.fichajes[x+3])=="undefined" ? '' : empleado.fichajes[x+3];
		var fichaje5=typeof(empleado.fichajes[x+4])=="undefined" ? '' : empleado.fichajes[x+4];
		var fichaje6=typeof(empleado.fichajes[x+5])=="undefined" ? '' : empleado.fichajes[x+5];
		if(x==0){ lstChecks.AddItem(empleado.id,empleado.nombre,empleado.fecha,fichaje1,fichaje2,fichaje3,fichaje4,fichaje5,fichaje6); }
		else{ lstChecks.AddItem('','','',fichaje1,fichaje2,fichaje3,fichaje4,fichaje5,fichaje6); }
	}
	empleado.fichajes = [];
}