var id=$.$GET('id');
if(!Jade.Utils.IsInEmployees(id)){
	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{
		Jade.Redirect('?employees');
	}
}

{//Ajax Data
	ajaxManager.Create('data');
	{//Data
		var ajaxData=$.AjaxDB("SELECT Tarjeta,Nombre,Apellidos,NIF,FechaNacimiento,Direccion,Poblacion,Provincia,CP,Telefono,Movil,Mail,Alias,Password,Privilegios,Observaciones,"+
		"IdHorario,IdUbicacion,IdCentro,IdResponsable,IdCalendario,IdPerfil,Coste1,Coste2,Coste3,Coste4,Coste5,TurnoNoche FROM EMPLEADOS{c} WHERE Codigo='"+id+"'",Ajax.Dontwait);
		ajaxData.Add(ajaxManager,'data');
		ajaxData.oncomplete=function(){
			if(!this.json.error){
				if(this.json.row.length!=0){
					var item = this.json.row[0];
					txtCode.SetText(id);
					set_personal_data(txtCard,'card','Tarjeta',item.Tarjeta);
					set_personal_data(txtName,'name','Nombre',item.Nombre);
					set_personal_data(txtSurnames,'surnames','Apellidos',item.Apellidos);
					set_personal_data(txtDni,'dni','NIF',item.NIF);
					set_personal_data(cmbBirthDate,'birthdate','FechaNacimiento',item.FechaNacimiento);
					set_personal_data(txtAddress,'address','Direccion',item.Direccion);
					set_personal_data(txtTown,'town','Poblacion',item.Poblacion);
					set_personal_data(txtCounty,'county','Provincia',item.Provincia);
					set_personal_data(txtCP,'cp','CP',item.CP);
					set_personal_data(txtTelephone,'telephone','Telefono',item.Telefono);
					set_personal_data(txtMobile,'mobile','Movil',item.Movil);
					set_personal_data(txtMail,'mail','Mail',item.Mail);
					set_personal_data(cmbDepartment,['',COMPANY.department],'IdUbicacion',item.IdUbicacion);
					set_personal_data(cmbCenter,['',COMPANY.center],'IdCentro',item.IdCentro);
					set_personal_data(cmbSupervisor,'supervisor','IdResponsable',item.IdResponsable);
					set_personal_data(cmbWorkday,'workday','IdHorario',item.IdHorario);
					set_personal_data(chkNightTurn,'night_turn','TurnoNoche',item.TurnoNoche);
					
					set_personal_data(txtRate1,['rate',' 1'],'Coste1',item.Coste1);
					set_personal_data(txtRate2,['rate',' 2'],'Coste2',item.Coste2);
					set_personal_data(txtRate3,['rate',' 3'],'Coste3',item.Coste3);
					set_personal_data(txtRate4,['rate',' 4'],'Coste4',item.Coste4);
					set_personal_data(txtRate5,['rate',' 5'],'Coste5',item.Coste5);
					
					set_personal_data(txtAlias,'alias',['terminal','Alias'],item.Alias);
					set_personal_data(txtPassword,'pass',['terminal','Password'],item.Password);
					set_personal_data(cmbPrivileges,'privileges',['terminal','Privilegios'],item.Privilegios);
					
					set_personal_data(txtComments,'comments',['comments','Observaciones'],item.Observaciones);
					
					set_personal_data(cmbPublicProfile,'public_profile_assigned','IdPerfil',item.IdPerfil);
					set_personal_data(cmbPublicCalendar,'public_calendar_assigned','IdCalendario',item.IdCalendario);
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//Status
		var ajaxStatus=$.AjaxDB("SELECT Indice,Fecha FROM EMPLEADOS_ALTABAJA{c} WHERE IdEmpleado='"+id+"' ORDER BY Fecha");
		ajaxStatus.Add(ajaxManager,'data');
		ajaxStatus.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 icon = 'status '+((i%2==0) ? 'up': 'down');
						grStatus.NewRow([icon, item.Fecha,'']);
						petition.status.mod.push({date: item.Fecha});
					}
					ChangeEmployeeStatus();
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
	{//Employee duty time
		var ajaxDutytime=$.AjaxDB("SELECT * FROM EMPLEADOS_HORAS_ESTANDAR{c} WHERE IdEmpleado='"+id+"' AND Anho>'"+aDutytime.first+"' AND Anho<'"+aDutytime.last+"' ORDER BY Anho,Mes");
		ajaxDutytime.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 total= $.Convert.ToInt(item.Total);
						aDutytime.year[item.Anho][item.Mes]={value: total, old: total};
					}
				}
			}
			else{ $.Debug.Add(this.text); }
			cmbPayroll_Year.SetSelectedKey(aDutytime.current);
		}
		ajaxManager.group['data'].Add(ajaxDutytime);
	}
}
{//Ajax Functions
	function Load_Fingerprints(){
		var ajaxFingers = new $.AjaxDB("SELECT Numero FROM EMPLEADOS_BIOMETRIAS{c} WHERE IdEmpleado='"+id+"' AND Tipo='1' AND Algoritmo='"+cmbFingerprintAlgoritm.selectedKey+"'",Ajax.Dontwait);
		ajaxFingers.oncomplete=function(){
			for(var i=0;i<10;i++){
				finger[i].style.display = 'none';
			}
			if(!this.json.error){
				if(this.json.row.length!=0){
					for(var i=0,iLen=this.json.row.length;i<iLen;i++){
						finger[this.json.row[i].Numero].style.display = 'block';
					}
				}
			}
			else{ $.Debug.Add(this.text); }
		}	
	}
	function Load_Notes(){
		var month = $.Strings.PadLeft(notes_cmbMonth.selectedKey,'0',2);
		var year = notes_cmbYear.selectedKey;
		var ajaxNotes=$.AjaxDB("SELECT Fecha,Hora,Texto FROM OBSERVACIONES{c} WHERE IdEmpleado='"+id+"' AND Fecha>'"+year+month+"00' AND Fecha<'"+year+month+"32' ORDER BY Fecha,Hora ASC",Ajax.Dontwait);
		ajaxNotes.oncomplete=function(){
			grNotes.ClearRows();
			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 text=$.Strings.VisualTrim(item.Texto,550);
						var row=grNotes.NewRow([$.Strings.ToDate(item.Fecha), $.Strings.ToTime(item.Hora), text, [$.lng('modify'), 'edit'], [$.lng('erease'), 'delete'], text], i);
						row.cells[0].value=item.Fecha;
						row.cells[1].value=item.Hora;
						petition.notes.mod.push({date: item.Fecha, time: item.Hora, text: item.Texto});
					}
				}
			}
			else{ $.Debug.Add(this.text); }
		}
	}
}

ajaxManager.group['data'].oncomplete=function(){
	var base64=$.DBImage("Foto","EMPLEADOS","Codigo='"+id+"'");
	imgFoto.src=base64;
	
	Load_Profile(true);
	cmbPublicProfile.onchange=function(){ Load_Profile(true); }
	
	Load_Calendar(true);
	cmbPublicCalendar.onchange=function(){ Load_Calendar(true); }
	cmbCalendarMonth.onchange=function(){ FillCalendarMonth(true); }
	cmbCalendarYear.onchange=function(){ Load_Calendar(true); }
	
	Load_Fingerprints();
	cmbFingerprintAlgoritm.onchange=function(){ Load_Fingerprints(); }
	
	Load_Notes();
	notes_cmbMonth.onchangekey=function(){ Load_Notes(); }
	notes_cmbYear.onchangekey=function(){ Load_Notes(); }
	
	cmdOk.SetEnabled($.Utils.PrivilegeValue('empl_priv')>1);
}
function AjaxBasicCompleted(){ ajaxManager.group['data'].Start(); }

txtCode.SetEnabled(false);



cmdOk.onclick=function(){ ShowPetitions(); }
cmdCancel.onclick=function(){ $.Redirect('?employee&id='+id); }