if(EMPLOYEES.length==1 && USER.empl_acc_all=='0' && EMPLOYEES[0]==USER.employee && (Jade.Utils.Privilege('employee_data') || Jade.Utils.Privilege('employee_profile') || Jade.Utils.Privilege('employee_calendar') || Jade.Utils.Privilege('employee_duty_time') || Jade.Utils.Privilege('hiring_dismissals') || Jade.Utils.Privilege('employee_terminal') || Jade.Utils.Privilege('employee_notes'))){
	Jade.Redirect('?employee&id='+USER.employee);
}
else{

	{//Controls----------------------------------------------------------------------------------------------------------------------------------------------------------------------
		var gpHeader=Jade.Objects.Container();
		gpHeader.Location(0,0,1000,40);
		gpHeader.Add();
		
		var cmdNew=Jade.Objects.ButtonIcon(Jade.Lang.Get('new_employee'),'new');
		cmdNew.Location(10,9,200,22);
		cmdNew.Add(gpHeader);
		
		var cmdDel=Jade.Objects.ButtonIcon(Jade.Lang.Get('del_employee'),'del');
		cmdDel.Location(220,9,200,22);
		cmdDel.Add(gpHeader);
		
		var cmGrid=Jade.Grids.ColumnModel({sortable: true, editable: false});
		cmGrid.Add(Jade.Grids.SpecialCheckColumn({key: 'select', text: '', width: '22',sortable: false}));
		cmGrid.Add(Jade.Grids.SimpleColumn({key: 'code', text: Jade.Lang.Get('code'), width: '82'}));
		cmGrid.Add(Jade.Grids.SimpleColumn({key: 'card', text: Jade.Lang.Get('card'), width: '93'}));
		cmGrid.Add(Jade.Grids.SimpleColumn({key: 'name', text: Jade.Lang.Get('name'), width: '145'}));
		cmGrid.Add(Jade.Grids.SimpleColumn({key: 'surname', text: Jade.Lang.Get('surnames'), width: '230'}));
		cmGrid.Add(Jade.Grids.SimpleColumn({key: 'alias', text: Jade.Lang.Get('alias'), width: '80'}));
		cmGrid.Add(Jade.Grids.SimpleColumn({key: 'department', text: COMPANY.department, width: '163'}));
		cmGrid.Add(Jade.Grids.SimpleColumn({key: 'center', text: COMPANY.center, width: '163'}));
		
		cmGrid.Add(Jade.Grids.HideColumn({key: 'department'}));
		cmGrid.Add(Jade.Grids.HideColumn({key: 'center'}));
		cmGrid.Add(Jade.Grids.HideColumn({key: 'workday'}));
		cmGrid.Add(Jade.Grids.HideColumn({key: 'profile'}));
		cmGrid.Add(Jade.Grids.HideColumn({key: 'calendar'}));
		
		var grid=Jade.Grids.Grid(cmGrid);
		grid.Pagination(200);
		grid.Location(0,50,1000,490);
		
		grid.Add();
	}
	{//Events------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		if(Jade.Utils.PrivilegeValue('empl_priv')<2 || !(Jade.Utils.Privilege('empl_acc_all')||Jade.Utils.Privilege('empl_acc_department')||Jade.Utils.Privilege('empl_acc_center')||Jade.Utils.Privilege('empl_acc_supervisor')) ){
			Jade.FX.Hide(gpHeader);
			grid.Location(0,5,1000,535)
		}
		if(Jade.Utils.Privilege('employee_data') || Jade.Utils.Privilege('employee_profile') || Jade.Utils.Privilege('employee_calendar') || Jade.Utils.Privilege('employee_duty_time') || Jade.Utils.Privilege('hiring_dismissals') || Jade.Utils.Privilege('employee_terminal') || Jade.Utils.Privilege('employee_notes')){
			grid.onrowclick=function(row){
				Jade.Redirect('?employee&id='+row.key);
			}
			
		}
		cmdNew.onclick=function(){ Jade.Redirect('?new_employee'); }
	}
	{//Ajax--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		var ajaxList=Jade.AjaxDB("SELECT E.Codigo, E.Tarjeta, E.Nombre, E.Apellidos, E.Alias, E.IdHorario, E.IdPerfil, E.IdCalendario, " +
						"E.IdUbicacion, U.Descripcion DescripcionUbicacion, E.IdCentro, C.Descripcion DescripcionCentro " +
						"FROM EMPLEADOS{c} E INNER JOIN CENTROS{c} C ON E.IdCentro=C.Codigo INNER JOIN UBICACIONES{c} U ON E.IdUbicacion=U.Codigo ORDER BY E.Codigo",Ajax.Dontwait);
		grid.Loading(true);
		ajaxList.oncomplete=function(){
			if(!this.json.error){
				if(this.json.row.length!=0){
					for(var i=0,iLen=this.json.row.length;i<iLen;i++){
						var item=this.json.row[i];
						if(Jade.Utils.IsInEmployees(item.Codigo)){
							grid.NewRow(['',item.Codigo, item.Tarjeta, item.Nombre, item.Apellidos, item.Alias,
							item.IdUbicacion +' - '+item.DescripcionUbicacion, item.IdCentro +' - '+item.DescripcionCentro,
							item.IdUbicacion, item.IdCentro, item.IdHorario, item.IdPerfil, item.IdCalendario], item.Codigo);
						}
					}
				}
			}
			else(Jade.Debug.Add(this.text));
			grid.Loading(false);
			grid.Show();
			
		}
	}

	{//Delete petitions
		{
			var pttWin=Jade.Objects.Window(805,185);
			var pttLabel=Jade.Objects.Text(Jade.Lang.Get('petitions_del_employee')+':');
			pttLabel.Location(15,10,775,20);
			pttLabel.Add(pttWin);
			var pttAccept = Jade.Objects.Button(Jade.Lang.Get('accept'));
			pttAccept.Location(15,150,385,20);
			pttAccept.Add(pttWin);
			var pttCancel = Jade.Objects.Button(Jade.Lang.Get('cancel'));
			pttCancel.Location(405,150,385,20);
			pttCancel.Add(pttWin);
			var pttGrid=Jade.Objects.SimpleGrid();
			pttGrid.Location(15,40,780,100);
			pttGrid.AddHeader(Jade.Lang.Get('code'),100);
			pttGrid.AddHeader(Jade.Lang.Get('name'),180);
			pttGrid.AddHeader(Jade.Lang.Get('surnames'),500);
			pttGrid.Add(pttWin);		
		}
		var aEmployeesSelected=[];
		cmdDel.onclick=function(){
			pttGrid.Clear();
			for(var i=0,iLen=grid.rows.length;i<iLen;i++){
				var row=grid.rows[i];
				if(row.cells[0].value==true){ 
					aEmployeesSelected.push({code: row.cells[1].value,
												name: row.cells[3].value, 
												surname: row.cells[4].value,
												department: row.cells[8].value,
												center: row.cells[9].value,
												workday: row.cells[10].value,
												profile: row.cells[11].value,
												calendar: row.cells[12].value
											});
				}
			}
			if(aEmployeesSelected.length!=0){
				for(var i=0,iLen=aEmployeesSelected.length;i<iLen;i++){
					var employee=aEmployeesSelected[i];
					pttGrid.AddStyledItem([employee.code, employee.name, employee.surname], 'petition_del');
				}
				pttWin.Show();
			}
		}
		pttCancel.onclick=function(){ aEmployeesSelected=[]; pttWin.Hide(); }
		pttAccept.onclick=function(){
			var Fecha=Jade.DateTime.toStoreDate(new Date());
			var Hora=Jade.DateTime.toStoreTime(new Date());
			var IdPeticion=Jade.Encrypt.MD5(USER.id+Fecha+Hora);
			
			var employee_field=(aEmployeesSelected.length>1)?'':aEmployeesSelected[0].code;

			var fields=['IdPeticion','IdUsuario','Fecha','Hora','Familia','Clave','Estado','EstadoEnvio'];
			var values=[IdPeticion,USER.id,Fecha,Hora,'0',employee_field,'0','0'];
			Jade.AjaxDBSave("PETICIONES{c}",fields,values);
			
			for(var i=0,iLen=aEmployeesSelected.length;i<iLen;i++){
				var fields=['IdPeticion','SubFamilia','Accion','Estado','Valor'];
				var valor="<Petition><PrimaryKey><EmployeeId>"+aEmployeesSelected[i].code+"</EmployeeId></PrimaryKey>" +
				"<OldValues><Name>"+aEmployeesSelected[i].name+
				"</Name><Surname>"+aEmployeesSelected[i].surname+
				"</Surname><Department>"+aEmployeesSelected[i].department+
				"</Department><Center>"+aEmployeesSelected[i].center+
				"</Center><Workday>"+aEmployeesSelected[i].workday+
				"</Workday><Profile>"+aEmployeesSelected[i].profile+
				"</Profile><Calendar>"+aEmployeesSelected[i].calendar+
				"</Calendar></OldValues></Petition>";
				
				Jade.AjaxDBSave("PETICIONES_LINEAS{c}", fields, [IdPeticion, '20', '2', '0', valor]);
			}
			aEmployeesSelected=[];
			pttWin.Hide();
		}
	}
}