var ajaxManager=$.AjaxManager();
{//Ajax basic
	ajaxManager.Create('basic');
	{
		var aTerms=[];
		aTerms['0']=$.Lang.Get('term_week');
		aTerms['1']=$.Lang.Get('term_month');
		aTerms['2']=$.Lang.Get('term_year');
		
	}
	{//DayTypes
		var aDayTypes=[];
		var ajaxDayTypes=$.AjaxDB("SELECT * FROM TIPO_DIA{c} ORDER BY Codigo");
		ajaxDayTypes.Add(ajaxManager,'basic');
		ajaxDayTypes.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++){
					var item=this.json.row[i];
					aDayTypes[item.Codigo]={};
					aDayTypes[item.Codigo].Text=item.Codigo+" - "+item.Descripcion;
					aDayTypes[item.Codigo].Codigo=item.Codigo;
					aDayTypes[item.Codigo].Descripcion=item.Descripcion;
					aDayTypes[item.Codigo].Laborable=item.Laborable;
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//Events
		var aEvents=[];
		var ajaxEvents=$.AjaxDB("SELECT * FROM INCIDENCIAS{c} ORDER BY Codigo");
		ajaxEvents.Add(ajaxManager,'basic');
		ajaxEvents.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++)	{
					var item=this.json.row[i];
					aEvents[item.Codigo]={};
					aEvents[item.Codigo].Text=item.Codigo+" - "+item.Descripcion;
					aEvents[item.Codigo].Codigo=item.Codigo;
					aEvents[item.Codigo].Descripcion=item.Descripcion;
					aEvents[item.Codigo].Valorada=item.Valorada;
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//Accruals
		var aAccruals=[];
		var ajaxAccruals=$.AjaxDB("SELECT * FROM ACUMULADORES{c} ORDER BY Codigo");
		ajaxAccruals.Add(ajaxManager,'basic');
		ajaxAccruals.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++)	{
					var item=this.json.row[i];
					aAccruals[item.Codigo]={};
					aAccruals[item.Codigo].Text=item.Codigo+" - "+item.Descripcion;
					aAccruals[item.Codigo].Codigo=item.Codigo;
					aAccruals[item.Codigo].Descripcion=item.Descripcion;
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//Breaks
		var aBreaks=[];
		var ajaxBreaks=$.AjaxDB("SELECT * FROM INTERRUPCIONES{c} ORDER BY Codigo");
		ajaxBreaks.Add(ajaxManager,'basic');
		ajaxBreaks.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++){
					var item=this.json.row[i];
					aBreaks[item.Codigo]={};
					aBreaks[item.Codigo].Text=item.Codigo+" - "+item.Descripcion;
					aBreaks[item.Codigo].Codigo=item.Codigo;
					aBreaks[item.Codigo].Descripcion=item.Descripcion;
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//Payrolls
		var aPayrolls=[];
		var ajaxPayrolls=$.AjaxDB("SELECT * FROM COSTES{c} ORDER BY Codigo");
		ajaxPayrolls.Add(ajaxManager,'basic');
		ajaxPayrolls.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length; i<iLen;i++){
					var item=this.json.row[i];
					aPayrolls[item.Codigo]={};
					aPayrolls[item.Codigo].Text=item.Codigo+" - "+item.Descripcion;
					aPayrolls[item.Codigo].Codigo=item.Codigo;
					aPayrolls[item.Codigo].Descripcion=item.Descripcion;
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//Departments
		var aDepartments=[];
		var ajaxDepartments=$.AjaxDB("SELECT Codigo,Descripcion FROM UBICACIONES{c} ORDER BY Codigo");
		ajaxDepartments.Add(ajaxManager,'basic');
		ajaxDepartments.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen; i++){
					var item=this.json.row[i];
					aDepartments[item.Codigo]={};
					aDepartments[item.Codigo].Text=item.Codigo+' - '+item.Descripcion;
					aDepartments[item.Codigo].Codigo=item.Codigo;
					aDepartments[item.Codigo].Descripcion=item.Descripcion;
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//Centers
		var aCenters=[];
		var ajaxCenters=$.AjaxDB("SELECT Codigo,Descripcion FROM CENTROS{c} ORDER BY Codigo");
		ajaxCenters.Add(ajaxManager,'basic');
		ajaxCenters.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++){
					var item=this.json.row[i];
					aCenters[item.Codigo]={};
					aCenters[item.Codigo].Text=item.Codigo+' - '+item.Descripcion;
					aCenters[item.Codigo].Codigo= item.Codigo;
					aCenters[item.Codigo].Descripcion=item.Descripcion;
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//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); }
		}
	}
	{//Workday
		var aWorkdays=[];
		var ajaxWorkdays=new $.AjaxDB("SELECT * FROM HORARIOS{c} ORDER BY Codigo");
		ajaxWorkdays.Add(ajaxManager,'basic');
		ajaxWorkdays.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++){
					var item=this.json.row[i];
					aWorkdays[item.Codigo]={};
					aWorkdays[item.Codigo].Text=item.Codigo+' - '+item.Descripcion;
					aWorkdays[item.Codigo].Codigo=item.Codigo;
					aWorkdays[item.Codigo].Descripcion=item.Descripcion;
					aWorkdays[item.Codigo].TotalPrimera=item.TotalPrimera;
					aWorkdays[item.Codigo].TotalSegunda=item.TotalSegunda;
					aWorkdays[item.Codigo].Variantes = [];
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//Profiles
		var aProfiles=[];
		var ajaxProfiles=$.AjaxDB("SELECT Codigo,Descripcion FROM PERFILES{c} ORDER BY Codigo");
		ajaxProfiles.Add(ajaxManager,'basic');
		ajaxProfiles.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++){
					var item=this.json.row[i];
					aProfiles[item.Codigo]={};
					aProfiles[item.Codigo].Text=item.Codigo+' - '+item.Descripcion;
					aProfiles[item.Codigo].Codigo=item.Codigo+' - '+item.Descripcion;
					aProfiles[item.Codigo].Descripcion=item.Codigo+' - '+item.Descripcion;
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//Calendars
		var aCalendars=[];
		var ajaxCalendars=$.AjaxDB("SELECT Codigo,Descripcion FROM CALENDARIO_PERFIL{c} ORDER BY Codigo");
		ajaxCalendars.Add(ajaxManager,'basic');
		ajaxCalendars.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++){
					var item=this.json.row[i];
					aCalendars[item.Codigo]={};
					aCalendars[item.Codigo].Text=item.Codigo+' - '+item.Descripcion;
					aCalendars[item.Codigo].Codigo=item.Codigo+' - '+item.Descripcion;
					aCalendars[item.Codigo].Descripcion=item.Codigo+' - '+item.Descripcion;
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//Attendance periods
		var aAttPeriods=[];
		var ajaxAttPeriods=$.AjaxDB("SELECT * FROM PERIODOS_PRESENCIA{c} ORDER BY Mes");
		ajaxAttPeriods.Add(ajaxManager,'basic');
		ajaxAttPeriods.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++){
					var item=this.json.row[i];
					aAttPeriods[parseInt(item.Mes,10)]={Desde: item.Desde,Hasta: item.Hasta};
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}	
	{//Terminals
		var aTerminals=[];
		var ajaxTerminals=$.AjaxDB("SELECT * FROM TERMINALES{c} ORDER BY Codigo");
		ajaxTerminals.Add(ajaxManager,'basic');
		ajaxTerminals.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++){
					var item=this.json.row[i];
					aTerminals[item.Codigo]={}
					aTerminals[item.Codigo].Text=item.Codigo+" - "+item.Descripcion;
					aTerminals[item.Codigo].Codigo=item.Codigo;
					aTerminals[item.Codigo].Descripcion=item.Descripcion;
					aTerminals[item.Codigo].Tipo=item.Tipo;
					if(typeof(aTerminalSlots[item.Codigo])=="undefined"){
						aTerminalSlots[item.Codigo]={};
						aTerminalSlots[item.Codigo].item=[];
					}
					if(typeof(aTerminalGroups[item.Codigo])=="undefined"){
						aTerminalGroups[item.Codigo]={};
						aTerminalGroups[item.Codigo].item=[];
					}
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//TerminalSlots
		var aTerminalSlots=[];
		var ajaxTerminalSlots=$.AjaxDB("SELECT * FROM TERMINALES_FRANJAS{c} ORDER BY Codigo");
		ajaxTerminalSlots.Add(ajaxManager,'basic');
		ajaxTerminalSlots.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++){
					var item=this.json.row[i];
					if(typeof(aTerminalSlots[item.IdTerminal])=="undefined"){
						aTerminalSlots[item.IdTerminal]={};
						aTerminalSlots[item.IdTerminal].item=[];
					}
					aTerminalSlots[item.IdTerminal].item[item.Codigo]={};
					aTerminalSlots[item.IdTerminal].item[item.Codigo].Text=item.Codigo+" - "+item.Descripcion;
					aTerminalSlots[item.IdTerminal].item[item.Codigo].Codigo=item.Codigo;
					aTerminalSlots[item.IdTerminal].item[item.Codigo].Descripcion=item.Descripcion;
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//TerminalGroup
		var aTerminalGroups=[];
		var ajaxTerminalGroups=$.AjaxDB("SELECT * FROM TERMINALES_GRUPOS{c} ORDER BY Codigo");
		ajaxTerminalGroups.Add(ajaxManager,'basic');
		ajaxTerminalGroups.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++){
					var item=this.json.row[i];
					if(typeof(aTerminalGroups[item.IdTerminal])=="undefined"){
						aTerminalGroups[item.IdTerminal]={};
						aTerminalGroups[item.IdTerminal].item=[];
					}
					aTerminalGroups[item.IdTerminal].item[item.Codigo]={};
					aTerminalGroups[item.IdTerminal].item[item.Codigo].Text=item.Codigo+" - "+item.Descripcion;
					aTerminalGroups[item.IdTerminal].item[item.Codigo].Codigo=item.Codigo;
					aTerminalGroups[item.IdTerminal].item[item.Codigo].Descripcion=item.Descripcion;
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//WorkdayVariants
		var aWorkdayVariants=[];
		var ajaxWorkdayVariants=$.AjaxDB("SELECT * FROM HORARIO_VARIANTES{c} ORDER BY IdHorario");
		ajaxWorkdayVariants.Add(ajaxManager,'basic');
		ajaxWorkdayVariants.oncomplete=function(){
			if(!this.json.error){
				for(var i=0,iLen=this.json.row.length;i<iLen;i++){
					var item=this.json.row[i];
					if(typeof(aWorkdayVariants[item.IdHorarioPadre])=="undefined"){ 
						aWorkdayVariants[item.IdHorarioPadre]={ Variants: [] }; 
					}
					aWorkdayVariants[item.IdHorarioPadre].Variants[item.IdVariante] = {
						IdHorario:item.IdHorario 
					};
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
}
{//Ajax functions
	function Load_Profile(private_profile){
		var profile = cmbPublicProfile.selectedKey;
		{//Varios
			var ajaxVarios=new $.Ajax();
			if(private_profile){ ajaxVarios.AddParam('query',"SELECT * FROM PERFILES_VARIOS{c} WHERE (IdPerfil='"+profile+"' AND Publico='1') OR (IdPerfil='"+id+"' AND Publico='0') ORDER BY Publico ASC"); }
			else{ ajaxVarios.AddParam('query',"SELECT * FROM PERFILES_VARIOS{c} WHERE IdPerfil='"+profile+"' AND Publico='1' ORDER BY Publico ASC"); }
			ajaxVarios.Send('lib/php/ajax_db.php');
			ajaxVarios.oncomplete=function(){
				if(!this.json.error){
					if(this.json.row.length!=0){
						var item=this.json.row[0];
						if(item.AusenciaIncidencia=='S004'){ optAbsence1.SetCheck(); }
						else{ optAbsence2.SetCheck(); }
						for(var j in aEvents){
							if(!$.Utils.IsSystemIncidence(j)) { cmbAbsenceIncident.AddItem(aEvents[j].Text,j); }
						}
						cmbAbsenceIncident.SetSelectedKey(item.AusenciaIncidencia);
						if(item.AsignacionCostes=='1'){ optProfile_Payroll1.SetCheck(); }
						else{ optProfile_Payroll2.SetCheck(); }
					}
				}
				else{ $.Debug.Add(this.text); }
			}
		}
		{//Rounding
			var ajaxRounding = $.Ajax();
			if(private_profile){ ajaxRounding.AddParam('query',"SELECT * FROM PERFILES_E_S{c} WHERE (IdPerfil='"+profile+"' AND Publico='1') OR (IdPerfil='"+id+"' AND Publico='0') ORDER BY IdTipoDia,Desde,Hasta,Redondeada,Tipo,Publico DESC"); }
			else{ ajaxRounding.AddParam('query',"SELECT * FROM PERFILES_E_S{c} WHERE IdPerfil='"+profile+"' AND Publico='1' ORDER BY IdTipoDia,Desde,Hasta,Redondeada,Tipo,Publico DESC"); }
			ajaxRounding.Send('lib/php/ajax_db.php');
			grRoundings.ClearRows();
			ajaxRounding.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];
							var values = [item.IdTipoDia, item.Desde, item.Hasta, item.Redondeada, item.Tipo, '', '', '', item.IdTipoDia, item.Desde, item.Hasta, item.Redondeada, item.Tipo];
							var lastRow = grRoundings.rows[grRoundings.rows.length-1];
							if(grRoundings.rows.length!=0 && lastRow.cells[0].references.object.selectedKey==values[0] && lastRow.cells[1].value==values[1] && lastRow.cells[2].value==values[2] && lastRow.cells[3].value==values[3] &&lastRow.cells[4].references.object.selectedKey==values[4]){
								lastRow.cells[5].SetValue([$.lng('unapply'), 'unapply']);
								lastRow.ChangeStyle('no-editable');
								row.cells[6].value=1;
								row.cells[7].value=1;
							}
							else{
								var row = grRoundings.NewRow(values, item.Indice);
								var combo = row.cells[0].references.object;
								for( var j in aDayTypes){
									combo.AddItem(aDayTypes[j].Text,j);
								}
								combo.SetSelectedKey(item.IdTipoDia);
								combo = row.cells[4].references.object;
								combo.AddItem($.Lang.Get('entry'),'0');
								combo.AddItem($.Lang.Get('exit'),'1');
								combo.SetSelectedKey(item.Tipo);
								if(item.Publico==1){
									row.SetEditable(false);
									if(private_profile){
										row.cells[5].SetValue([$.lng('apply'), 'apply']);
										row.cells[6].value=0;
										row.cells[7].value=0;
									}
									else{
										row.cells[5].SetValue([$.lng('unapply'), 'unapply']);
										row.ChangeStyle('no-editable');
										row.cells[6].value=1;
										row.cells[7].value=1;
									}
								}
								else{
									row.cells[5].SetValue([$.lng('erease'), 'delete']);
									row.ChangeStyle('editable');
									row.cells[6].value=2;
									row.cells[7].value=2;
								}
							}
						}
						grRoundings.columnmodel.columns[6].order();
					}
				}
				else{ $.Debug.Add(this.text); }
			}
		}
		{//AutoEvents
			var ajaxAutoEvents = $.Ajax();
			if(private_profile){ ajaxAutoEvents.AddParam('query',"SELECT * FROM PERFILES_INCIDENCIAS{c} WHERE (IdPerfil='"+profile+"' AND Publico='1') OR (IdPerfil='"+id+"' AND Publico='0') ORDER BY IdTipoDia,Desde,Hasta,IdIncidencia,Publico DESC"); }
			else{ ajaxAutoEvents.AddParam('query',"SELECT * FROM PERFILES_INCIDENCIAS{c} WHERE IdPerfil='"+profile+"' AND Publico='1' ORDER BY IdTipoDia,Desde,Hasta,IdIncidencia,Publico DESC"); }
			ajaxAutoEvents.Send('lib/php/ajax_db.php');
			grAutoEvents.ClearRows();
			ajaxAutoEvents.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];
							var values=[item.IdTipoDia, item.Desde, item.Hasta, item.IdIncidencia, item.Tipo,'','','',item.IdTipoDia, item.Desde, item.Hasta, item.IdIncidencia, item.Tipo];
							var lastRow = grAutoEvents.rows[grAutoEvents.rows.length-1];
							if(grAutoEvents.rows.length>0 && lastRow.cells[0].references.object.selectedKey==values[0] && lastRow.cells[1].value==values[1] && lastRow.cells[2].value==values[2] && lastRow.cells[3].references.object.selectedKey==values[3] && lastRow.cells[4].references.object.selectedKey==values[4]){
								lastRow.cells[5].SetValue([$.lng('unapply'), 'unapply']);
								lastRow.ChangeStyle('no-editable');
								row.cells[6].value=1;
								row.cells[7].value=1;
							}
							else{
								var row = grAutoEvents.NewRow(values, item.Indice);
								var combo = row.cells[0].references.object;
								for(var j in aDayTypes){
									combo.AddItem(aDayTypes[j].Text,j);
								}
								combo.SetSelectedKey(item.IdTipoDia);
								combo=row.cells[3].references.object;
								for(var j in aEvents){
									if(!$.Utils.IsSystemIncidence(j))combo.AddItem(aEvents[j].Text,j);
								}
								combo.SetSelectedKey(item.IdIncidencia);
								combo=row.cells[4].references.object;
								combo.AddItem($.Lang.Get('entry'),'0');
								combo.AddItem($.Lang.Get('exit'),'1');
								combo.SetSelectedKey(item.Tipo);
								if(item.Publico==1){
									row.SetEditable(false);
									if(private_profile){
										row.cells[5].SetValue([$.lng('apply'), 'apply']);
										row.cells[6].value=0;
										row.cells[7].value=0;
									}
									else{
										row.cells[5].SetValue([$.lng('unapply'), 'unapply']);
										row.ChangeStyle('no-editable');
										row.cells[6].value=1;
										row.cells[7].value=1;
									}
								}
								else{
									row.cells[5].SetValue([$.lng('erease'), 'delete']);
									row.ChangeStyle('editable');
									row.cells[6].value=2;
									row.cells[7].value=2;
								}
							}
						}
						grAutoEvents.columnmodel.columns[6].order();
					}
				}
				else{ $.Debug.Add(this.text); }
			}
		}	
		{//AccrualBalance
			var ajaxAccrualBalance=new $.Ajax();
			if(private_profile){ ajaxAccrualBalance.AddParam('query',"SELECT * FROM PERFILES_SALDOS{c} WHERE (IdPerfil='"+profile+"' AND Publico='1') OR (IdPerfil='"+id+"' AND Publico='0') ORDER BY IdAcumulador,Periodo,Publico DESC"); }
			else{ ajaxAccrualBalance.AddParam('query',"SELECT * FROM PERFILES_SALDOS{c} WHERE IdPerfil='"+profile+"' AND Publico='1' ORDER BY IdAcumulador,Periodo,Publico DESC"); }
			ajaxAccrualBalance.Send('lib/php/ajax_db.php');
			grAccrualBalance.ClearRows();
			ajaxAccrualBalance.oncomplete=function(){
				if(!this.json.error){
					if(this.json.row.length!=0){
						for(var i=0; i<this.json.row.length; i++){
							var item=this.json.row[i];
							if($.isDefined(aAccruals[item.IdAcumulador])){
								var values=[item.IdAcumulador, item.VecesPermitidas, item.TiempoPermitido, item.Periodo, item.Anho, '','','', item.IdAcumulador, item.VecesPermitidas, item.TiempoPermitido, item.Periodo, item.Anho];
								var lastRow = grAccrualBalance.rows[grAccrualBalance.rows.length-1];
								if(grAccrualBalance.rows.length!=0 && lastRow.cells[0].references.object.selectedKey==values[0] && lastRow.cells[1].value==values[1] && lastRow.cells[2].value==values[2] && lastRow.cells[3].references.object.selectedKey==values[3] && lastRow.cells[4].references.object.selectedKey==values[4]){
									lastRow.cells[5].SetValue([$.lng('unapply'), 'unapply']);
									lastRow.ChangeStyle('no-editable');
									row.cells[6].value=1;
									row.cells[7].value=1;
								}
								else{
									var row=grAccrualBalance.NewRow(values,item.Indice);
									var combo=row.cells[0].references.object;
									for(var j in aAccruals){
										combo.AddItem(aAccruals[j].Text,j);
									}
									combo.SetSelectedKey(item.IdAcumulador);
									combo=row.cells[3].references.object;
									combo.AddItem($.Lang.Get('term_week'),'0');
									combo.AddItem($.Lang.Get('term_month'),'1');
									combo.AddItem($.Lang.Get('term_year'),'2');
									combo.SetSelectedKey(item.Periodo);
									
									
									combo = row.cells[4].references.object;
									var currentYear=$.DateTime.CurrentYear();
									combo.AddItem($.lng('none_m'), 'None');
									for(var j=currentYear-10,jLen=currentYear+10;j<=jLen;j++){
										combo.AddItem(j, j);
									}
									combo.SetSelectedKey(item.Anho);
									
									if(item.Publico==1){
										row.SetEditable(false);
										if(private_profile){
											row.cells[5].SetValue([$.lng('apply'), 'apply']);
											row.cells[6].value=0;
											row.cells[7].value=0;
										}
										else{
											row.cells[5].SetValue([$.lng('unapply'), 'unapply']);
											row.ChangeStyle('no-editable');
											row.cells[6].value=1;
											row.cells[7].value=1;
										}
									}
									else{
										row.cells[5].SetValue([$.lng('erease'), 'delete']);
										row.ChangeStyle('editable');
										row.cells[6].value=2;
										row.cells[7].value=2;
									}
								}
							}
						}
						grAccrualBalance.columnmodel.columns[6].order();
					}
				}
				else{ $.Debug.Add(this.text); }
			}
		}
		{//Breaks
			var ajaxBreaks = $.Ajax();
			if(private_profile){ ajaxBreaks.AddParam('query',"SELECT * FROM PERFILES_INTERRUPCIONES{c} WHERE (IdPerfil='"+profile+"' AND Publico='1') OR (IdPerfil='"+id+"' AND Publico='0') ORDER BY IdInterrupcion,IdTipoDia,Publico DESC"); }
			else{ ajaxBreaks.AddParam('query',"SELECT * FROM PERFILES_INTERRUPCIONES{c} WHERE IdPerfil='"+profile+"' AND Publico='1' ORDER BY IdInterrupcion,IdTipoDia,Publico DESC"); }
			ajaxBreaks.Send('lib/php/ajax_db.php');
			grBreaks.ClearRows();
			ajaxBreaks.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($.isDefined(aDayTypes[item.IdTipoDia]) && $.isDefined(aBreaks[item.IdInterrupcion])){
								var values = [item.IdTipoDia, item.IdInterrupcion,'','','',item.IdTipoDia, item.IdInterrupcion,];
								var lastRow = grBreaks.rows[grBreaks.rows.length-1];
								if(grBreaks.rows.length!=0 && lastRow.cells[0].references.object.selectedKey==values[0] && lastRow.cells[1].references.object.selectedKey==values[1]){
									lastRow.cells[2].SetValue([$.lng('unapply'), 'unapply']);
									lastRow.ChangeStyle('no-editable');
									row.cells[3].value=1;
									row.cells[4].value=1;
								}
								else{
									var row=grBreaks.NewRow(values, item.Indice);
									var combo = row.cells[0].references.object;
									for(var j in aDayTypes){
										combo.AddItem(aDayTypes[j].Text, j);
									}
									combo.SetSelectedKey(item.IdTipoDia);
									combo=row.cells[1].references.object;
									for(var j in aBreaks){
										combo.AddItem(aBreaks[j].Text, j);
									}
									combo.SetSelectedKey(item.IdInterrupcion);
									if(item.Publico==1){
										row.SetEditable(false);
										if(private_profile){
											row.cells[2].SetValue([$.lng('apply'), 'apply']);
											row.cells[3].value=0;
											row.cells[4].value=0;
										}
										else{
											row.cells[2].SetValue([$.lng('unapply'), 'unapply']);
											row.ChangeStyle('no-editable');
											row.cells[3].value=1;
											row.cells[4].value=1;
										}
									}
									else{
										row.cells[2].SetValue([$.lng('erease'), 'delete']);
										row.ChangeStyle('editable');
										row.cells[3].value=2;
										row.cells[4].value=2;
									}
								}
							}
						}
						grBreaks.columnmodel.columns[3].order();
					}
				}
				else{ $.Debug.Add(this.text); }
			}
		}
		{//Payrolls
			var ajaxPayrolls=new $.Ajax();
			if(private_profile){ ajaxPayrolls.AddParam('query',"SELECT * FROM PERFILES_COSTES{c} WHERE (IdPerfil='"+profile+"' AND Publico='1') OR (IdPerfil='"+id+"' AND Publico='0') ORDER BY IdCoste,IdTipoDia,Publico DESC"); }
			else{ ajaxPayrolls.AddParam('query',"SELECT * FROM PERFILES_COSTES{c} WHERE IdPerfil='"+profile+"' AND Publico='1' ORDER BY IdCoste,IdTipoDia,Publico DESC"); }
			ajaxPayrolls.Send('lib/php/ajax_db.php');
			grPayroll.ClearRows();
			ajaxPayrolls.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];
							var values=[item.IdTipoDia, item.IdCoste,'','','', item.IdTipoDia, item.IdCoste];
							var lastRow=grPayroll.rows[grPayroll.rows.length-1];
							if(grPayroll.rows.length!=0 && lastRow.cells[0].references.object.selectedKey==values[0] && lastRow.cells[1].references.object.selectedKey==values[1]){
								lastRow.cells[2].SetValue([$.lng('unapply'), 'unapply']);
								lastRow.ChangeStyle('no-editable');
								row.cells[3].value=1;
								row.cells[4].value=1;
							}
							else{
								var row=grPayroll.NewRow(values,item.Indice);
								var combo=row.cells[0].references.object;
								for(var j in aDayTypes){
									combo.AddItem(aDayTypes[j].Text,j);
								}
								combo.SetSelectedKey(item.IdTipoDia);
								combo=row.cells[1].references.object;
								for(var j in aPayrolls){
									combo.AddItem(aPayrolls[j].Text,j);
								}
								combo.SetSelectedKey(item.IdCoste);

								if(item.Publico==1){
									row.SetEditable(false);
									if(private_profile){
										row.cells[2].SetValue([$.lng('apply'), 'apply']);
										row.cells[3].value=0;
										row.cells[4].value=0;
									}
									else{
										row.cells[2].SetValue([$.lng('unapply'), 'unapply']);
										row.ChangeStyle('no-editable');
										row.cells[3].value=1;
										row.cells[4].value=1;
									}
								}
								else{
									row.cells[2].SetValue([$.lng('erease'), 'delete']);
									row.ChangeStyle('editable');
									row.cells[3].value=2;
									row.cells[4].value=2;
								}
						
							}
						}
						grPayroll.columnmodel.columns[3].order();
					}
				}
				else{ $.Debug.Add(this.text); }
			}
		}
		{//AccessDays
			var ajaxAccessDays = $.Ajax();
			if(private_profile){ ajaxAccessDays.AddParam('query',"SELECT * FROM PERFILES_SIN_ACCESO{c} WHERE (IdPerfil='"+profile+"' AND Publico='1') OR (IdPerfil='"+id+"' AND Publico='0') ORDER BY Desde,Hasta,Publico DESC"); }
			else{ ajaxAccessDays.AddParam('query',"SELECT * FROM PERFILES_SIN_ACCESO{c} WHERE IdPerfil='"+profile+"' AND Publico='1' ORDER BY Desde,Hasta,Publico DESC"); }
			ajaxAccessDays.Send('lib/php/ajax_db.php');
			grNoAccessDays.ClearRows();
			ajaxAccessDays.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];
							var values = [item.Desde, item.Hasta,'','','',item.Desde, item.Hasta];
							var lastRow = grNoAccessDays.rows[grNoAccessDays.rows.length-1];
							if(grNoAccessDays.rows.length!=0 && lastRow.cells[0].value==values[0] && lastRow.cells[1].value==values[1]){
								lastRow.cells[2].SetValue([$.lng('unapply'), 'unapply']);
								lastRow.ChangeStyle('no-editable');
								row.cells[3].value=1;
								row.cells[4].value=1;
							}
							else{
								var row = grNoAccessDays.AddRow(new $.Grids.Row(values), item.Indice);
								if(item.Publico==1){
									row.SetEditable(false);
									if(private_profile){
										row.cells[2].SetValue([$.lng('apply'), 'apply']);
										row.cells[3].value=0;
										row.cells[4].value=0;
									}
									else{
										row.cells[2].SetValue([$.lng('unapply'), 'unapply']);
										row.ChangeStyle('no-editable');
										row.cells[3].value=1;
										row.cells[4].value=1;
									}
								}
								else{
									row.cells[2].SetValue([$.lng('erease'), 'delete']);
									row.ChangeStyle('editable');
									row.cells[3].value=2;
									row.cells[4].value=2;
								}
							}
						}
						grNoAccessDays.columnmodel.columns[3].order();
					}
				}
				else{ $.Debug.Add(this.text); }
			}
		}
		{//Access
			var ajaxAccess=new $.Ajax();
			if(private_profile){ ajaxAccess.AddParam('query',"SELECT * FROM PERFILES_ACCESOS{c} WHERE (IdPerfil='"+profile+"' AND Publico='1') OR (IdPerfil='"+id+"' AND Publico='0') ORDER BY IdTerminal,Publico DESC"); }
			else{ ajaxAccess.AddParam('query',"SELECT * FROM PERFILES_ACCESOS{c} WHERE IdPerfil='"+profile+"' AND Publico='1' ORDER BY IdTerminal,Publico DESC"); }
			ajaxAccess.Send('lib/php/ajax_db.php');
			grAccess.ClearRows();
			ajaxAccess.oncomplete=function(){
				if(!this.json.error){
					for(i=0,iLen=this.json.row.length;i<iLen;i++){
						var item=this.json.row[i];
						var terminal=aTerminals[item.IdTerminal];
						if($.isDefined(terminal)){
							var values=[terminal.Text,'0','0','0','0',terminal.Codigo,terminal.Tipo];
							var lastRow = grAccess.rows[grAccess.rows.length-1];
							if(grAccess.rows.length!=0 && lastRow.cells[0].value==values[0]){
								lastRow.cells[1].references.object.SetSelectedKey(item.Tipo);
								lastRow.cells[2].references.object.SetSelectedKey(item.Opcion1); 
								lastRow.cells[3].references.object.SetSelectedKey(item.Opcion2); 
								lastRow.cells[4].references.object.SetSelectedKey(item.Opcion3);
								lastRow.cells[5].value=terminal.Codigo;
								lastRow.cells[6].value=terminal.Tipo;
							}
							else{
								var row=grAccess.NewRow([terminal.Text,'0','0','0','0',terminal.Codigo,terminal.Tipo]);
								row.cells[1].onchangekey=function(){
									var row=this.references.row;
									FillAccessCombo(row.cells[2]);
									FillAccessCombo(row.cells[3]);
									FillAccessCombo(row.cells[4]);
								}
								var combo = row.cells[1].references.object;
								if(terminal.Tipo=='2'){
									combo.AddItem($.Lang.Get('no_access'),'0');
									combo.AddItem($.Lang.Get('with_access'),'1');
									combo.SetSelectedKey(item.Acceso);
								}
								else{
									combo.AddItem($.Lang.Get('no_access'),'3');
									combo.AddItem($.Lang.Get('group'),'0');
									combo.AddItem($.Lang.Get('slots'),'1');
									if(item.Acceso=='0'){ combo.SetSelectedKey('3'); }
									else{
										combo.SetSelectedKey(item.Tipo); 
										row.cells[2].references.object.SetSelectedKey(item.Opcion1); 
										row.cells[3].references.object.SetSelectedKey(item.Opcion2); 
										row.cells[4].references.object.SetSelectedKey(item.Opcion3);
									}
								}					
							}
						}
					}
				}
				else{ $.Debug.Add(this.text); }
			}
		}
		{//AccessTime
			var ajaxProfileAccessTime = $.Ajax();
			if(private_profile){ ajaxProfileAccessTime.AddParam('query',"SELECT * FROM PERFILES_FORZAR_ACCESO{c} WHERE (IdPerfil='"+profile+"' AND Publico='1') OR (IdPerfil='"+id+"' AND Publico='0') ORDER BY IdTipoDia,Desde,Hasta,Publico DESC"); }
			else{ ajaxProfileAccessTime.AddParam('query',"SELECT * FROM PERFILES_FORZAR_ACCESO{c} WHERE IdPerfil='"+profile+"' AND Publico='1' ORDER BY IdTipoDia,Desde,Hasta,Publico DESC"); }			
			ajaxProfileAccessTime.Send('lib/php/ajax_db.php');
			grOnlyAccessTime.ClearRows();
			ajaxProfileAccessTime.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];
							var values = [item.IdTipoDia, item.Desde, item.Hasta, '', '', '', item.IdTipoDia, item.Desde, item.Hasta];
							var lastRow = grOnlyAccessTime.rows[grOnlyAccessTime.rows.length-1];
							if(grOnlyAccessTime.rows.length!=0 && lastRow.cells[0].references.object.selectedKey==values[0] && lastRow.cells[1].value==values[1] && lastRow.cells[2].value==values[2]){
								lastRow.cells[3].SetValue([$.lng('unapply'), 'unapply']);
								lastRow.ChangeStyle('no-editable');
								row.cells[4].value=1;
								row.cells[5].value=1;
							}
							else{
								var row = grOnlyAccessTime.NewRow(values, item.Indice);
								row.cells[0].onbeginedit = function(){
									var combo = this.references.object;
									for(var j in aDayTypes){
										combo.AddItem(aDayTypes[j].Text,j);
									}
								}
								row.cells[0].onbeginedit();
								row.cells[0].references.object.SetSelectedKey(item.IdTipoDia);
								if(item.Publico==1){
									row.SetEditable(false);
									if(private_profile){
										row.cells[3].SetValue([$.lng('apply'), 'apply']);
										row.cells[4].value=0;
										row.cells[5].value=0;
									}
									else{
										row.cells[3].SetValue([$.lng('unapply'), 'unapply']);
										row.ChangeStyle('no-editable');
										row.cells[4].value=1;
										row.cells[5].value=1;
									}
								}
								else{
									row.cells[3].SetValue([$.lng('erease'), 'delete']);
									row.ChangeStyle('editable');
									row.cells[4].value=2;
									row.cells[5].value=2;
								}
							}
						}
						grOnlyAccessTime.columnmodel.columns[4].order();
					}
				}
				else{ $.Debug.Add(this.text); }
			}
		}
	}
	function Load_Calendar(private_calendar){
		calendar._public.year=[];
		calendar._private.year=[];
		calendar._changes.year=[];

		var ajaxPublic = $.Ajax();
		if(private_calendar){ ajaxPublic.AddParam('query',"SELECT * FROM CALENDARIO{c} WHERE ((IdCalendario='"+cmbPublicCalendar.selectedKey+"' AND Publico='1') OR (IdCalendario='"+id+"' AND Publico='0')) AND Fecha>'"+cmbCalendarYear.selectedKey+"0100' AND Fecha<'"+cmbCalendarYear.selectedKey+"1300' ORDER BY Fecha ASC"); }
		else{ ajaxPublic.AddParam('query',"SELECT * FROM CALENDARIO{c} WHERE IdCalendario='"+cmbPublicCalendar.selectedKey+"' AND Publico='1' AND Fecha>'"+cmbCalendarYear.selectedKey+"0100' AND Fecha<'"+cmbCalendarYear.selectedKey+"1300' ORDER BY Fecha ASC"); }
		ajaxPublic.Send('lib/php/ajax_db.php');
		ajaxPublic.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];
						var day = parseInt(item.Fecha.substr(6),10);
						var month = parseInt(item.Fecha.substr(4,2),10);
						var year = parseInt(item.Fecha.substr(0,4),10);
						var oCalendar={};
						if(item.Publico=='0'){ oCalendar=calendar._private; }
						else{ oCalendar=calendar._public; }
						if(typeof(oCalendar.year[year])=="undefined"){
							oCalendar.year[year]={}; 
							oCalendar.year[year].month=[];
						}
						if(typeof(oCalendar.year[year].month[month])=="undefined"){
							oCalendar.year[year].month[month]={}; 
							oCalendar.year[year].month[month].day=[];
						}
						oCalendar.year[year].month[month].day[day]={};
						oCalendar.year[year].month[month].day[day].day=day;
						oCalendar.year[year].month[month].day[day].Fecha=item.Fecha;
						oCalendar.year[year].month[month].day[day].IdTipoDia=item.IdTipoDia;
						oCalendar.year[year].month[month].day[day].IdVariante=item.IdVariante;
						oCalendar.year[year].month[month].day[day].TipoVariante=(item.TipoVariante==1);					
						oCalendar.year[year].month[month].day[day].IdIncidencia=item.IdIncidencia;
						oCalendar.year[year].month[month].day[day].IdHorario=item.IdHorario;
						oCalendar.year[year].month[month].day[day].Observaciones=item.Observaciones;
					}
				}
				FillCalendarMonth(true);
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	function FillCalendarMonth(fade){
		var month = parseInt(cmbCalendarMonth.selectedKey);
		var year = parseInt(cmbCalendarYear.selectedKey);
		calendar.Draw(month,year);
		for(var i=0,iLen=calendar.days.length;i<iLen;i++){
			var cube = calendar.days[i];
			var day = parseInt(cube.day);

			var are_change=false;
			var are_private=false;
			var are_public=false;
			try {
				if(typeof(calendar._changes.year[year])!="undefined" && typeof(calendar._changes.year[year].month[month])!="undefined" && typeof(calendar._changes.year[year].month[month].day[day])!="undefined") { are_change = true; }
			} catch (e) { are_change=false; }
			try {
				if(typeof(calendar._private.year[year])!="undefined" && typeof(calendar._private.year[year].month[month])!="undefined" && typeof(calendar._private.year[year].month[month].day[day])!="undefined") { are_private = true; }
			} catch (e) { are_private=false; }
			try {
				if(typeof(calendar._public.year[year])!="undefined" && typeof(calendar._public.year[year].month[month])!="undefined" && typeof(calendar._public.year[year].month[month].day[day])!="undefined") { are_public = true; }
			} catch (e) { are_public=false; }

			var item={};
			if(are_change){ item=calendar._changes.year[year].month[month].day[day]; }
			else if(are_private){ item = calendar._private.year[year].month[month].day[day]; }
			else if(are_public){ item = calendar._public.year[year].month[month].day[day]; }
			cube.SetColor(item.IdTipoDia, item.IdVariante, item.IdIncidencia);
		}
		if(fade==true) { /*$.FX.Fade(calendar,1,100,1,1);*/ }
	}
	function Load_Dutytime(month){
		month=parseInt(month,10);
		var year = parseInt(cmbPayroll_Year.selectedKey, 10);
		var workday=$.Strings.PadLeft(cmbWorkday.selectedKey, '0', 4);
		var calendar=$.Strings.PadLeft(cmbPublicCalendar.selectedKey, '0', 9);
		var time=0;
		
		{//Obtener periodo de presencia
			var startDate = aAttPeriods[month].Desde
			var endDate = aAttPeriods[month].Hasta;
			if(startDate<endDate){
				startDate = year+startDate;
				endDate = year+endDate;
			}
			else if(month==1){
				startDate = (parseInt(year)-1)+startDate;
				endDate = year+endDate;
			}
			else{
				startDate = year+startDate;
				endDate = (parseInt(year)+1)+endDate;
			}
		}
		{//Obtener tiempo
			var query;
			if(typeof(id)!="undefined"){
				query=$.Strings.Format("SELECT Fecha, IdTipoDia, IdVariante, IdIncidencia, IdHorario FROM CALENDARIO{c} WHERE Fecha >= '{0}' AND Fecha <= '{1}' AND ((IdCalendario = '{2}' AND Publico = '0') OR (IdCalendario = '{3}' AND Publico = '1' AND (Fecha NOT IN (SELECT Fecha FROM CALENDARIO{c} WHERE Fecha >= '{0}' AND Fecha <= '{1}' AND IdCalendario = '{2}' AND Publico = '0'))))",startDate,endDate,id,calendar);
			}
			else{
				query=$.Strings.Format("SELECT Fecha, IdTipoDia, IdVariante, IdIncidencia, IdHorario FROM CALENDARIO{c} WHERE Fecha >= '{0}' AND Fecha <= '{1}' AND IdCalendario = '{2}' AND Publico = '1'", startDate, endDate,calendar);
			}

			var ajaxDB=$.AjaxDB(query,Ajax.Dontwait);
			ajaxDB.year=year;
			ajaxDB.month=month;
			ajaxDB.oncomplete=function(){
				if(!ajaxDB.json.error){
					for(var i=0,iLen=ajaxDB.json.row.length;i<iLen;i++){
						var item=ajaxDB.json.row[i];
						if(IsHiredInThisDate(item.Fecha)){
							if(item.IdIncidencia==''){
								if(item.IdVariante!=''){
									variante = SearchVariant(workday,item.IdVariante);
									time+=TimeOfDay( $.iif(variante=='',workday,variante));
								}
								else if(aDayTypes[item.IdTipoDia].Laborable=='1'){
									time+=TimeOfDay(workday);
								}
							}
							else{
								variante=item.IdHorario;
								if(aEvents[item.IdIncidencia].Valorada=='1'){ time+=TimeOfDay($.iif(variante=='',workday,variante)); }
							}
						}
					}
				}
				else{ $.Debug.Add(this.text); }
				grDutytime.rows[this.month-1].cells[1].SetValue(time);
				aDutytime.year[this.year][this.month].value=time;
			}
		}
	}
	function IsHiredInThisDate(date){
			var movements=0;
            for(var i=0,iLen=grStatus.rows.length; i<iLen; i++){
                if(grStatus.rows[i].cells[1].value <= date){ movements++; }
            }
            return (movements%2 == 1);
	}
	function TimeOfDay(code){
		return parseInt(aWorkdays[code].TotalPrimera)+parseInt(aWorkdays[code].TotalSegunda);
	}
	function SearchVariant(workday,variant){
		var ret = '';
		if(typeof(aWorkdayVariants[workday])!="undefined" && typeof(aWorkdayVariants[workday].Variants[variant])!="undefined"){
			ret=aWorkdayVariants[workday].Variants[variant].IdHorario;
		}
		return ret;
	}
}
{//Ajax Manager
	ajaxManager.group['basic'].Start();
	ajaxManager.group['basic'].oncomplete=function(){
		for(var i in aDepartments){ cmbDepartment.AddItem(aDepartments[i].Text,i); }
		for(var i in aCenters){ cmbCenter.AddItem(aCenters[i].Text,i); }
		for(var i in aDayTypes){
			cmbDayData_Daytype.AddItem(aDayTypes[i].Text,i);
			txtCalInfo[parseInt(i,10)].SetText(aDayTypes[i].Text);
			$.FX.Show(boxCalInfo[parseInt(i,10)]);
			$.FX.Show(txtCalInfo[parseInt(i,10)]);
		}

		cmbDayData_Variant.AddItem($.Lang.Get('none_f'),'');
		for(var i=1;i<=9;i++){ cmbDayData_Variant.AddItem($.Lang.Get('variant')+' '+i,i); }

		cmbDayData_Incidence.AddItem($.Lang.Get('none_f'),'');
		for(var i in aEvents){
			if(!$.Utils.IsSystemIncidence(i) && i!='0000') {
				cmbDayData_Incidence.AddItem(aEvents[i].Text,i); 
			}
		}

		cmbDayData_Workday.AddItem($.Lang.Get('none_m'),'');
		for(var i in aWorkdays){
			cmbWorkday.AddItem(aWorkdays[i].Text,i);
			cmbDayData_Workday.AddItem(aWorkdays[i].Text,i);
		}

		cmbSupervisor.AddItem($.Lang.Get('none_m'),'None');
		for(var i in aEmployees){
			if((typeof(id)!="undefined" && i!=id) || typeof(id)=="undefined"){
				cmbSupervisor.AddItem(aEmployees[i].Text,i);
			}
		}

		for(var i in aProfiles){ cmbPublicProfile.AddItem(aProfiles[i].Text,i); }

		for(var i in aCalendars){ cmbPublicCalendar.AddItem(aCalendars[i].Text,i); }

		if(typeof(AjaxBasicCompleted)!="undefined"){ AjaxBasicCompleted(); }
	}
}