	ajaxData_Employees.Add(ajaxManager,'basic');
	ajaxData_Departments.Add(ajaxManager,'basic');
	ajaxData_Centers.Add(ajaxManager,'basic');
	ajaxData_Events.Add(ajaxManager,'basic');
	ajaxData_Terminals.Add(ajaxManager,'basic');
	

	ajaxManager.group['basic'].Start();
	ajaxManager.group['basic'].oncomplete=function(){
	
		cmbFromEmployee.AddItem($.Lang.Get('all_p_m'),'All');
		cmbToEmployee.AddItem($.Lang.Get('none_m'),'None');
		for(var i in aEmployees){
			cmbFromEmployee.AddItem(aEmployees[i].Text,aEmployees[i].Codigo);
			cmbToEmployee.AddItem(aEmployees[i].Text,aEmployees[i].Codigo);
		}
		cmbDepartment.AddItem($.Lang.Get('all_p_m'),'All');
		for(var i in aDepartments){ cmbDepartment.AddItem(aDepartments[i].Text,i); }
		
		cmbCenter.AddItem($.Lang.Get('all_p_m'),'All');
		for(var i in aCenters){ cmbCenter.AddItem(aCenters[i].Text,i); }
		
		cmbEvents1.AddItem($.Lang.Get('all_p_f'),'All');
		cmbEvents2.AddItem($.Lang.Get('all_p_f'),'All');
		for(var i in aEvents){
			cmbEvents1.AddItem(aEvents[i].Text,i);
			cmbEvents2.AddItem(aEvents[i].Text,i);
		}
		
		cmbTerminals.AddItem($.Lang.Get('all_p_m'),'All');
		for(var i in aTerminals){ cmbTerminals.AddItem(aTerminals[i].Text,i); }
	}

{//Controles
	var gpHeader=$.Objects.Container();
	gpHeader.Location(0,0,1000,245);
	gpHeader.SetStyle('transparent');
	gpHeader.Add();

	var selectedReport=null;
	{//Controles por defecto-----------------------------------------------------------------------------------------------------------------------------------
		var lblReportType=$.Objects.Text($.Lang.Get('report_type')+':');
		lblReportType.Location(10,15,100,20);
		lblReportType.Add(gpHeader);
		var cmbReport=$.Objects.ComboBox();
		cmbReport.Location(160,10,830,20);
		cmbReport.Add(gpHeader);	

		var lblTitle=$.Objects.Text($.Lang.Get('title')+':');
		lblTitle.Location(10,40,100,20);
		lblTitle.Add(gpHeader);
		var txtTitle=$.Objects.TextBox();
		txtTitle.Location(160,35,830,20);
		txtTitle.Add(gpHeader);

		var lblFromEmployee=$.Objects.Text($.Lang.Get('from_employee')+':');
		lblFromEmployee.Location(10,65,150,20);
		lblFromEmployee.Add(gpHeader);
		var cmbFromEmployee=$.Objects.ComboBox();
		cmbFromEmployee.Location(160,60,310,20);
		cmbFromEmployee.Add(gpHeader);
		
		$.ControlAdd('searchemployee', [475,60,20,20], gpHeader, cmbFromEmployee);
		
		var lblFromEmployee=$.Objects.Text($.Lang.Get('to_employee')+':');
		lblFromEmployee.Location(505,65,150,20);
		lblFromEmployee.Add(gpHeader);
		var cmbToEmployee=$.Objects.ComboBox();
		cmbToEmployee.Location(655,60,310,20);
		cmbToEmployee.Add(gpHeader);
		
		$.ControlAdd('searchemployee', [970,60,20,20], gpHeader, cmbToEmployee);

		var lblDepartment=$.Objects.Text(COMPANY.department+':');
		lblDepartment.Location(10,90,150,20);
		lblDepartment.Add(gpHeader);
		var cmbDepartment=$.Objects.ComboBox();
		cmbDepartment.Location(160,85,335,20);
		cmbDepartment.Add(gpHeader);	
		
		var lblCenter=$.Objects.Text(COMPANY.center+':');
		lblCenter.Location(505,90,150,20);
		lblCenter.Add(gpHeader);
		var cmbCenter=$.Objects.ComboBox();
		cmbCenter.Location(655,85,335,20);
		cmbCenter.Add(gpHeader);

		var lblFromDate=$.Objects.Text($.Lang.Get('from_date')+':');
		lblFromDate.Location(10,115,150,20);
		lblFromDate.Add(gpHeader);
		var cmbFromDate=$.Objects.DatePicker();
		cmbFromDate.Location(160,110,335,20);
		cmbFromDate.Add(gpHeader);

		var lblToDate=$.Objects.Text($.Lang.Get('to_date')+':');
		lblToDate.Location(505,115,150,20);
		lblToDate.Add(gpHeader);
		var cmbToDate=$.Objects.DatePicker();
		cmbToDate.Location(655,110,335,20);
		cmbToDate.Add(gpHeader);

		var cmdView=$.Objects.ButtonIcon($.Lang.Get('generate_report'), 'size16 view');
		cmdView.Location(655,215,335,22);
		cmdView.Add(gpHeader);
}
	{//Controles: Historico general----------------------------------------------------------------------------------------------------------------------------

		var divGeneralLog=document.createElement('div');
		divGeneralLog.style.position = 'absolute';
		divGeneralLog.style.left='10px';
		divGeneralLog.style.top='135px';
		divGeneralLog.style.width = '980px';
		divGeneralLog.style.height = '80px';
		gpHeader.appendChild(divGeneralLog);
		
		var lblEvents1=$.Objects.Text($.Lang.Get('incidence')+':');
		lblEvents1.Location(0,5,150,20);
		lblEvents1.Add(divGeneralLog);
		var cmbEvents1=$.Objects.ComboBox();
		cmbEvents1.Location(150,0,335,20);
		cmbEvents1.Add(divGeneralLog);

		var lblOrder=$.Objects.Text($.Lang.Get('order_by')+':');
		lblOrder.Location(495,5,150,20);
		lblOrder.Add(divGeneralLog);
		var cmbOrder=$.Objects.ComboBox();
		cmbOrder.Location(645,0,335,20);
		cmbOrder.Add(divGeneralLog);	

		var chkPageEmployee1=$.Objects.CheckBox($.Lang.Get('page_per_employee'));
		chkPageEmployee1.Location(0,55,300,20);
		chkPageEmployee1.Add(divGeneralLog);
		
		var chkViewChecks=$.Objects.CheckBox($.Lang.Get('view_punchs'));
		chkViewChecks.Location(300,55,250,20);
		chkViewChecks.Add(divGeneralLog);
	}
	{//Controles: Absentismo-----------------------------------------------------------------------------------------------------------------------------------
		var divAbsenteeism = document.createElement('div');
		divAbsenteeism.style.position = 'absolute';
		divAbsenteeism.style.left='10px';
		divAbsenteeism.style.top='135px';
		divAbsenteeism.style.width = '980px';
		divAbsenteeism.style.height = '80px';
		gpHeader.appendChild(divAbsenteeism);
		
		var chkPageEmployee2=$.Objects.CheckBox($.Lang.Get('page_per_employee'));
		chkPageEmployee2.Location(0,55,300,20);
		chkPageEmployee2.Add(divAbsenteeism);
		var chkOnlyEmployees=$.Objects.CheckBox($.Lang.Get('only_employees_with_one_absence'));
		chkOnlyEmployees.Location(300,55,300,20);
		chkOnlyEmployees.Add(divAbsenteeism);
	}
	{//Controles: Accessos-------------------------------------------------------------------------------------------------------------------------------------

		var divAccess = document.createElement('div');
		divAccess.style.position = 'absolute';
		divAccess.style.left='10px';
		divAccess.style.top='135px';
		divAccess.style.width = '980px';
		divAccess.style.height = '80px';
		gpHeader.appendChild(divAccess);
		
		var lblBreakdown=$.Objects.Text($.Lang.Get('breakdown')+':');
		lblBreakdown.Location(0,5,150,20);
		lblBreakdown.Add(divAccess);
		var cmbBreakdown=$.Objects.ComboBox();
		cmbBreakdown.Location(150,0,335,20);
		cmbBreakdown.Add(divAccess);

		var lblGroup=$.Objects.Text($.Lang.Get('group_by')+':');
		lblGroup.Location(495,5,150,20);
		lblGroup.Add(divAccess);
		var cmbGroup=$.Objects.ComboBox();
		cmbGroup.Location(645,0,335,20);
		cmbGroup.Add(divAccess)

		var chkTotal=$.Objects.CheckBox($.Lang.Get('view_total'));
		chkTotal.Location(0,55,300,20);
		chkTotal.Add(divAccess);
	}
	{//Controles: Fichajes acceso------------------------------------------------------------------------------------------------------------------------------
		var divAccessPunch = document.createElement('div');
		divAccessPunch.style.position = 'absolute';
		divAccessPunch.style.left='10px';
		divAccessPunch.style.top='135px';
		divAccessPunch.style.width = '980px';
		divAccessPunch.style.height = '80px';
		gpHeader.appendChild(divAccessPunch);
		
		var lblFromTime=$.Objects.Text($.Lang.Get('from_time')+':');
		lblFromTime.Location(0,5,150,20);
		lblFromTime.Add(divAccessPunch);
		var cmbFromTime=$.Objects.TimeBox(false);
		cmbFromTime.Location(150,0,335,20);
		cmbFromTime.Add(divAccessPunch);
		
		var lblToTime=$.Objects.Text($.Lang.Get('to_time')+':');
		lblToTime.Location(495,5,150,20);
		lblToTime.Add(divAccessPunch);
		var cmbToTime=$.Objects.TimeBox(false);
		cmbToTime.Location(645,0,335,20);
		cmbToTime.Add(divAccessPunch);
		
		var lblTerminals=$.Objects.Text($.Lang.Get('terminal')+':');
		lblTerminals.Location(0,30,150,20);
		lblTerminals.Add(divAccessPunch);
		var cmbTerminals=$.Objects.ComboBox();
		cmbTerminals.Location(150,25,335,20);
		cmbTerminals.Add(divAccessPunch);
		
		var lblEvents2=$.Objects.Text($.Lang.Get('incidence')+':');
		lblEvents2.Location(495,30,150,20);
		lblEvents2.Add(divAccessPunch);
		var cmbEvents2=$.Objects.ComboBox();
		cmbEvents2.Location(645,25,335,20);
		cmbEvents2.Add(divAccessPunch);
		
		var lblOrder2=$.Objects.Text($.Lang.Get('order_by')+':');
		lblOrder2.Location(0,55,150,20);
		lblOrder2.Add(divAccessPunch);
		var cmbOrder2=$.Objects.ComboBox();
		cmbOrder2.Location(150,50,335,20);
		cmbOrder2.Add(divAccessPunch);	
		
	}	
	{//Controles: Listado--------------------------------------------------------------------------------------------------------------------------------------
		var cmReports=$.Grids.ColumnModel({editable: false,fixedfocus: true});
		cmReports.Add($.Grids.SimpleColumn({key: 'employee', text: $.Lang.Get('report_title'), width: '675'}));
	
		var grReports=$.Grids.Grid(cmReports);
		grReports.Location(0,255,700,285);
		grReports.Add();
	
		var cmdNew=$.Objects.ButtonIcon($.Lang.Get('new_m'), "size16 new");
		cmdNew.Location(705,255,295,22);
		cmdNew.Add();
	
		var cmdSave=$.Objects.ButtonIcon($.Lang.Get('save'), "size16 save");
		cmdSave.Location(705,282,295,22);
		cmdSave.Add();	
	
		var cmdDelete=$.Objects.ButtonIcon($.Lang.Get('strDelete'), "size16 del");
		cmdDelete.Location(705,309,295,22);
		cmdDelete.Add();
	}
	{//Valores: Combos-----------------------------------------------------------------------------------------------------------------------------------------
		cmbOrder.AddItem($.Lang.Get('employees_code'),'Employee\'s code');
		cmbOrder.AddItem($.Lang.Get('employees_name'),'Employee\'s name');
		
		cmbOrder2.AddItem($.Lang.Get('employee'),'0');
		cmbOrder2.AddItem($.Lang.Get('date_time'),'1');
		
		cmbBreakdown.AddItem($.Lang.Get('breakdown'),'Breakdown');
		cmbBreakdown.AddItem($.Lang.Get('no_breakdown'),'NoBreakdown');
		cmbGroup.AddItem($.Lang.Get('employee'),'Employee');
		cmbGroup.AddItem($.Lang.Get('terminal'),'Terminal');	
	}
	{//Eventos: Listado
		function Load_Report_List() {
			selectedReport=null;
			
			var ajaxRptList=$.AjaxDB($.Strings.Format("SELECT IdListado,Titulo FROM LISTADOS{c} WHERE Tipo='{0}' AND (IdUsuario={1} OR IdListado IN "+
			"(SELECT IdConfiguracion FROM LISTADOS_VISIBILIDAD{c} WHERE IdUsuario={1}))",cmbReport.selectedKey,USER.id),Ajax.Dontwait);
			ajaxRptList.oncomplete=function(){
				if(!this.json.error){
					grReports.ClearRows();
					for(var i=0; i<this.json.row.length!=0;i++){
						var item=this.json.row[i];
						grReports.NewRow([item.Titulo], item.IdListado);
					}
				}
				else { $.Debug.Add(this.text); }
			}
		}
		grReports.onrowclick=function(row){
			selectedReport=row.key;
			var params={};

			var ajaxdb=$.AjaxDB("SELECT * FROM LISTADOS{c} WHERE IdListado="+row.key, Ajax.Wait);
			var error=ajaxdb.json.error;
			if(!error){
				params=ajaxdb.json.row[0]; 
				ajaxdb=$.AjaxDB("SELECT Tipo,Valor FROM LISTADOS_FILTROS{c} WHERE IdListado="+row.key, Ajax.Wait);
				error=ajaxdb.json.error;
				if(!error){
					for(var i=0; i<ajaxdb.json.row.length!=0;i++){
						var item=ajaxdb.json.row[i];
						params[item.Tipo]=item.Valor;
					}
				}
				else{ $.Debug.Add(this.text); }
			}
			else{$.Debug.Add(this.text); }
			
			if(!error){ Load_Report_Config(params); }
		}
		function Load_Report_Config(values){
			cmdSave.SetEnabled(USER.id==values.IdUsuario);
			cmdDelete.SetEnabled(USER.id==values.IdUsuario);
			
			txtTitle.SetText(values.Titulo);
			cmbFromEmployee.SetSelectedKey(values.Desde);
			cmbToEmployee.SetSelectedKey(values.Hasta);
			
			cmbDepartment.SetSelectedKey(values.Departamento);
			cmbCenter.SetSelectedKey(values.Centro);
			
			cmbFromDate.SetValue(values.FechaDesde);
			cmbToDate.SetValue(values.FechaHasta);
			
			switch(cmbReport.selectedKey){
				case '12':
					Load_Report_Config_GeneralLog(values);
					break;
				case '13':
					Load_Report_Config_Absenteeism(values);
					break;
				case '14':
					Load_Report_Config_Access(values);
					break;
				case '15':
					Load_Report_Config_AccessPunch(values);
					break;
			}
		}
		function Load_Report_Config_GeneralLog(values){
			cmbEvents1.SetSelectedKey(values.Incidencia);
			cmbOrder.SetSelectedKey(values.OrdenarPor);
			chkPageEmployee1.SetCheck(values.PaginaPorEmpleado==1);
			chkViewChecks.SetCheck(values.VerFichajes==1);
		}
		function Load_Report_Config_Absenteeism(values){
			chkPageEmployee2.SetCheck(values.PaginaPorEmpleado==1);
			chkOnlyEmployees.SetCheck(values.AlmenosConUnaAusencia==1);
		}
		function Load_Report_Config_Access(values){
			cmbBreakdown.SetSelectedKey(values.Desglose);
			cmbGroup.SetSelectedKey(values.OrdenarPor);
			chkTotal.SetCheck(values.VerTotal==1);
		}
		function Load_Report_Config_AccessPunch(values){
			cmbFromTime.SetValue(values.HoraDesde);
			cmbToTime.SetValue(values.HoraHasta);
			cmbTerminals.SetSelectedIndex(values.Terminal);
			cmbEvents2.SetSelectedIndex(values.Incidencia);
		}
	}
	{//Eventos: Botones
		cmdNew.onclick=function(){
			grReports.ClearFocus();
			selectedReport=null;
			cmdSave.SetEnabled(true);
			cmdDelete.SetEnabled(true);
			DefaultValues();
		}
		cmdSave.onclick=function(){
			var is_new=(selectedReport==null);
			var key=is_new?$.DateTime.Ticks():selectedReport;
			
			if(is_new){ $.AjaxDBSave('LISTADOS{c}', ['IdListado', 'Tipo', 'IdUsuario', 'Titulo', 'Desde', 'Hasta'], [key, cmbReport.selectedKey, USER.id, txtTitle.GetText(), cmbFromEmployee.selectedKey, cmbToEmployee.selectedKey]); }
			else{ $.AjaxDBSave('LISTADOS{c}', ['Tipo', 'IdUsuario', 'Titulo', 'Desde', 'Hasta'], [cmbReport.selectedKey, USER.id, txtTitle.GetText(), cmbFromEmployee.selectedKey, cmbToEmployee.selectedKey], "IdListado='"+key+"'"); }
			

			SaveReportField(is_new, key, 'Departamento', cmbDepartment.selectedKey);
			SaveReportField(is_new, key, 'Centro', cmbCenter.selectedKey);
			SaveReportField(is_new, key, 'FechaDesde', cmbFromDate.GetValue());
			SaveReportField(is_new, key, 'FechaHasta', cmbToDate.GetValue());
			
			switch(cmbReport.selectedKey){
				case '12':
					SaveReportField(is_new, key, 'Incidencia', cmbEvents1.selectedKey);
					SaveReportField(is_new, key, 'OrdenarPor', cmbOrder.selectedKey);
					SaveReportField(is_new, key, 'PaginaPorEmpleado', (chkPageEmployee1.GetCheck()?1:0));
					SaveReportField(is_new, key, 'VerFichajes', (chkViewChecks.GetCheck()?1:0));
					break;
				case '13':
					SaveReportField(is_new, key, 'PaginaPorEmpleado', (chkPageEmployee2.GetCheck()?1:0));
					SaveReportField(is_new, key, 'AlmenosConUnaAusencia', (chkOnlyEmployees.GetCheck()?1:0));
					break;
				case '14':
					SaveReportField(is_new, key, 'Desglose', cmbBreakdown.selectedKey);
					SaveReportField(is_new, key, 'OrdenarPor', cmbGroup.selectedKey);
					SaveReportField(is_new, key, 'VerTotal', (chkTotal.GetCheck()?1:0));
					break;
				case '15':
					SaveReportField(is_new, key, 'HoraDesde', cmbFromDate.GetValue());
					SaveReportField(is_new, key, 'HoraHasta', cmbToDate.GetValue());
					SaveReportField(is_new, key, 'Terminal', cmbTerminals.selectedKey);
					SaveReportField(is_new, key, 'Incidencia', cmbEvents2.selectedKey);
					SaveReportField(is_new, key, 'OrdenarPor', cmbOrder2.selectedKey);
					break;
			}
			
			Load_Report_List();
			DefaultValues();
		}
		cmdDelete.onclick=function(){
			if(selectedReport!=null){
				$.AjaxDBDelete("LISTADOS{c}", 'IdListado='+selectedReport);
				$.AjaxDBDelete("LISTADOS_FILTROS{c}", 'IdListado='+selectedReport);
				Load_Report_List();
				DefaultValues();
			}
		}
		cmdView.onclick= function() {
			var reportType = "";
			switch(cmbReport.selectedKey){
				case '12':
					reportType='general_log';
					break;
				case '13':
					reportType='absenteeism';
					break;
				case '14':
					reportType='access';
					break;
				case '15':
					reportType='access_checks';
					break;
			}
			var report=$.Report(reportType);
			report.AddValue('title',txtTitle.GetText());
			report.Show(true);
		}	
	}
}	

	function DefaultValues() {
		var strYear = $.DateTime.CurrentYear();
		var strMonth = $.Strings.PadLeft($.DateTime.CurrentMonth(),'0',2);
		var strLastDay = $.Strings.PadLeft($.DateTime.Monthdays(strMonth, strYear),'0',2);
		txtTitle.SetText(cmbReport.value);
		if(cmbFromEmployee.items.length!=0) { cmbFromEmployee.SetSelectedIndex(0); }
		if(cmbToEmployee.items.length!=0) { cmbToEmployee.SetSelectedIndex(0); }
		if(cmbDepartment.items.length!=0) { cmbDepartment.SetSelectedIndex(0); }
		if(cmbCenter.items.length!=0) { cmbCenter.SetSelectedIndex(0); }
		cmbFromDate.SetValue(strYear+strMonth+'01');
		cmbToDate.SetValue(strYear+strMonth+strLastDay);

		$.FX.Hide(divGeneralLog);
		$.FX.Hide(divAbsenteeism);
		$.FX.Hide(divAccess);
		$.FX.Hide(divAccessPunch);
		switch(cmbReport.selectedKey) {
			case '12':
				$.FX.Show(divGeneralLog);
				DefaultValues_GeneralLog();
				break;
			case '13':
				$.FX.Show(divAbsenteeism);
				DefaultValues_Absenteeism();
				break;
			case '14':
				$.FX.Show(divAccess);
				DefaultValues_Access();
				break;
			case '15':
				$.FX.Show(divAccessPunch);
				DefaultValues_AccessPunch();
				break;
		}
	}
	function DefaultValues_GeneralLog() {
		cmbEvents1.SetSelectedIndex(0);
		cmbOrder.SetSelectedIndex(0);
		chkPageEmployee1.SetCheck(true);
		chkViewChecks.SetCheck(true);
	}
	function DefaultValues_Absenteeism(){
		chkPageEmployee2.SetCheck(true);
		chkOnlyEmployees.SetCheck(false);
	}
	function DefaultValues_Access(){
		cmbBreakdown.SetSelectedIndex(0);
		cmbGroup.SetSelectedIndex(0);
		chkTotal.SetCheck(true);
	}
	function DefaultValues_AccessPunch(){
		cmbFromTime.SetValue('0000');
		cmbToTime.SetValue('2359');
		cmbTerminals.SetSelectedIndex(0);
		cmbEvents2.SetSelectedIndex(0);
		cmbOrder2.SetSelectedIndex(0);
	}
	cmbReport.onchangekey = function(){
		DefaultValues();
		Load_Report_List();
	}
	cmbReport.AddItem($.Lang.Get('reports_general_log_punch'),'12');
	cmbReport.AddItem($.Lang.Get('reports_absenteeism'),'13');
	cmbReport.AddItem($.Lang.Get('reports_access'),'14');
	cmbReport.AddItem($.Lang.Get('reports_access_punches'),'15');
	
	function SaveReportField(is_new, id, field, value){
		if(is_new){ $.AjaxDBSave('LISTADOS_FILTROS{c}', ['IdListado', 'Tipo', 'Valor'], [id, field, value]); }
		else{ $.AjaxDBSave('LISTADOS_FILTROS{c}', ['Valor'], [value], "IdListado='"+id+"' AND Tipo='"+field+"'");  }
	}