var ajaxManager=Jade.AjaxManager();
{//Ajax basic--------------------------------------------------------------------------------------------------------------------------------------------------
	ajaxManager.Create('basic');
	{//DayTypes------------------------------------------------------------------------------------------------------------------------------------------------
		var aDayTypes=[];
		var ajaxDayTypes=Jade.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{ Jade.Debug.Add(this.text); }
		}
	}
	{//Events--------------------------------------------------------------------------------------------------------------------------------------------------
		var aEvents=[];
		var ajaxEvents=Jade.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{ Jade.Debug.Add(this.text); }
		}
	}
	{//Workday-------------------------------------------------------------------------------------------------------------------------------------------------
		var aWorkdays=[];
		var ajaxWorkdays=Jade.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;
				}
			}
			else{ Jade.Debug.Add(this.text); }
		}
	}	
	{//Calendars-----------------------------------------------------------------------------------------------------------------------------------------------
		var aCalendars=[];
		var ajaxCalendars=Jade.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{ Jade.Debug.Add(this.text); }
		}
	}	
}	
{//Ajax functions----------------------------------------------------------------------------------------------------------------------------------------------
	function Load_Calendar(){
		var calendar_id = cmbCalendar.selectedKey;
		calendar._public.year=[];
		calendar._changes.year=[];
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		var ajaxPublic=Jade.AjaxDB("SELECT * FROM CALENDARIO{c} WHERE IdCalendario='"+calendar_id+"' AND Fecha>'"+cmbCalendarYear.selectedKey+"0100' AND Fecha<'"+cmbCalendarYear.selectedKey+"1300' AND Publico='1' ORDER BY Fecha ASC",Ajax.Dontwait);
		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);
						
						if(typeof(calendar._public.year[year])=="undefined"){
							calendar._public.year[year]={}; 
							calendar._public.year[year].month=[];
						}
						if(typeof(calendar._public.year[year].month[month])=="undefined"){
							calendar._public.year[year].month[month]={}; 
							calendar._public.year[year].month[month].day=[];
						}
						calendar._public.year[year].month[month].day[day]={};
						calendar._public.year[year].month[month].day[day].day=day;
						calendar._public.year[year].month[month].day[day].Fecha=item.Fecha;
						calendar._public.year[year].month[month].day[day].IdTipoDia=item.IdTipoDia;
						calendar._public.year[year].month[month].day[day].IdVariante=item.IdVariante;
						calendar._public.year[year].month[month].day[day].TipoVariante=(item.TipoVariante==1);					
						calendar._public.year[year].month[month].day[day].IdIncidencia=item.IdIncidencia;
						calendar._public.year[year].month[month].day[day].IdHorario=item.IdHorario;
						calendar._public.year[year].month[month].day[day].Observaciones=item.Observaciones;
					}
				}
				FillCalendarMonth(true);
			}
			else{ Jade.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_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._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_public){ item = calendar._public.year[year].month[month].day[day]; }
			cube.SetColor(item.IdTipoDia, item.IdVariante, item.IdIncidencia);
		}
		if(fade==true) { /*Jade.FX.Fade(calendar,1,100,1,1);*/ }
	}	
}
{//Ajax Manager------------------------------------------------------------------------------------------------------------------------------------------------
	ajaxManager.group['basic'].Start();
	ajaxManager.group['basic'].oncomplete=function(){
		for(var i in aDayTypes){
			cmbDayData_Daytype.AddItem(aDayTypes[i].Text,i);
			txtCalInfo[parseInt(i,10)].SetText(aDayTypes[i].Text);
			Jade.FX.Show(boxCalInfo[parseInt(i,10)]);
			Jade.FX.Show(txtCalInfo[parseInt(i,10)]);
		}
		cmbDayData_Variant.AddItem(Jade.Lang.Get('none_f'),0);
		for(var i=1;i<=9;i++){ cmbDayData_Variant.AddItem(Jade.Lang.Get('variant')+' '+i,i); }
		
		cmbDayData_Incidence.AddItem(Jade.Lang.Get('none_f'),0);
		for(var i in aEvents){ if(!Jade.Utils.IsSystemIncidence(i)){ cmbDayData_Incidence.AddItem(aEvents[i].Text,i); } }
		
		cmbDayData_Workday.AddItem(Jade.Lang.Get('none_m'),0);
		for(var i in aWorkdays){ cmbDayData_Workday.AddItem(aWorkdays[i].Text,i); }
		
		for(var i in aCalendars){ cmbCalendar.AddItem(aCalendars[i].Text,i); }
		
		Load_Calendar(false);
		cmbCalendar.onchange=function(){ Load_Calendar(false); }
		cmbCalendarMonth.onchange=function(){ FillCalendarMonth(true); }
		cmbCalendarYear.onchange=function(){ Load_Calendar(false); }
	}
}
{//Controles---------------------------------------------------------------------------------------------------------------------------------------------------
	var gpPublicCalendar=Jade.Objects.Container();
	gpPublicCalendar.Location(0,0,1000,40);
	gpPublicCalendar.SetStyle('transparent');
	gpPublicCalendar.Add();
	var lblPublicCalendar=Jade.Objects.Text(Jade.Lang.Get('public_calendar')+' :');
	lblPublicCalendar.Location(15,15,200,20);
	lblPublicCalendar.Add(gpPublicCalendar);
	var cmbCalendar=Jade.Objects.ComboBox();
	cmbCalendar.Location(210,10,780,20);
	cmbCalendar.Add(gpPublicCalendar);
	
	var gpCalendarMonth=Jade.Objects.Container();
	gpCalendarMonth.Location(0,45,380,380);
	gpCalendarMonth.Add();
	var cmbCalendarMonth=Jade.Objects.ComboBox();
	cmbCalendarMonth.Location(12,13,236,20);
	cmbCalendarMonth.Add(gpCalendarMonth);
	var cmbCalendarYear=Jade.Objects.ComboBox();
	cmbCalendarYear.Location(260,13,109,20);
	cmbCalendarYear.Add(gpCalendarMonth);
	var calendar=Jade.Objects.CalendarMonth(50);
	calendar.Position(11,40);
	calendar.Add(gpCalendarMonth);
	calendar._public={};
	calendar._changes={};
	
	var gpDayData=Jade.Objects.Container();
	gpDayData.Location(390,45,610,380);
	gpDayData.Add();
	var lblDayData_Daytype=Jade.Objects.Text(Jade.Lang.Get('day_type')+' :');
	lblDayData_Daytype.Location(15,20,200,20);
	lblDayData_Daytype.Add(gpDayData);
	var cmbDayData_Daytype=Jade.Objects.ComboBox();
	cmbDayData_Daytype.Location(200,15,395,20);
	cmbDayData_Daytype.Add(gpDayData);
	var lblDayData_Variant=Jade.Objects.Text(Jade.Lang.Get('variant')+' :');
	lblDayData_Variant.Location(15,50,200,20);
	lblDayData_Variant.Add(gpDayData);
	var cmbDayData_Variant=Jade.Objects.ComboBox();
	cmbDayData_Variant.Location(200,45,395,20);
	cmbDayData_Variant.Add(gpDayData);
	var chkDayData_Variant=Jade.Objects.CheckBox(Jade.Lang.Get('force_variant'));
	chkDayData_Variant.Location(200,75,395,20);
	chkDayData_Variant.Add(gpDayData);
	var lblDayData_Incidence=Jade.Objects.Text(Jade.Lang.Get('incidence')+' :');
	lblDayData_Incidence.Location(15,110,200,20);
	lblDayData_Incidence.Add(gpDayData);
	var cmbDayData_Incidence=Jade.Objects.ComboBox();
	cmbDayData_Incidence.Location(200,105,395,20);
	cmbDayData_Incidence.Add(gpDayData);
	var lblDayData_Workday=Jade.Objects.Text(Jade.Lang.Get('workday')+' :');
	lblDayData_Workday.Location(15,140,200,20);
	lblDayData_Workday.Add(gpDayData);
	var cmbDayData_Workday=Jade.Objects.ComboBox();
	cmbDayData_Workday.Location(200,135,395,20);
	cmbDayData_Workday.Add(gpDayData);
	var lblDayData_Comments=Jade.Objects.Text(Jade.Lang.Get('comments')+' :');
	lblDayData_Comments.Location(15,170,175,20);
	lblDayData_Comments.Add(gpDayData);
	var txtDayData_Comments=Jade.Objects.TextArea();
	txtDayData_Comments.Location(200,165,395,120);
	txtDayData_Comments.Add(gpDayData);
	
	var cmdCalendar_Clean=Jade.Objects.Button(Jade.Lang.Get('clean_selected_days'));
	cmdCalendar_Clean.Location(15,295,580,20);
	cmdCalendar_Clean.Add(gpDayData);
	var cmdCalendar_Apply=Jade.Objects.Button(Jade.Lang.Get('apply_changes_to_selection'));
	cmdCalendar_Apply.Location(15,320,580,20);
	cmdCalendar_Apply.Add(gpDayData);
	var cmdCalendar_Generate=Jade.Objects.Button(Jade.Lang.Get('calendar_auto_generation'));
	cmdCalendar_Generate.Location(15,345,580,20);
	cmdCalendar_Generate.Add(gpDayData);

	var gpCalendarLegend = Jade.Objects.Container();
	gpCalendarLegend.Location(0,430,1000,107);
	gpCalendarLegend.SetStyle('transparent');
	gpCalendarLegend.Add();
	var boxCalInfo_1=Jade.Objects._create();
	boxCalInfo_1.Location(20,21,12,12);
	boxCalInfo_1.className='leyend_box day_incidence';
	boxCalInfo_1.Add(gpCalendarLegend);
	var lblCalInfo_1=Jade.Objects.Text(Jade.Lang.Get('incidences'));
	lblCalInfo_1.Location(40,22,200,20);
	lblCalInfo_1.Add(gpCalendarLegend);
	var boxCalInfo_2=Jade.Objects._create();
	boxCalInfo_2.Location(20,41,12,12);
	boxCalInfo_2.className='leyend_box day_variant';
	boxCalInfo_2.Add(gpCalendarLegend);
	var lblCalInfo_2=Jade.Objects.Text(Jade.Lang.Get('variants'));
	lblCalInfo_2.Location(40,42,200,20);
	lblCalInfo_2.Add(gpCalendarLegend);
	var lblCalInfo_3=Jade.Objects.Text(Jade.Lang.Get('day_types')+':');
	lblCalInfo_3.Location(200,15,200,20);
	lblCalInfo_3.Add(gpCalendarLegend);
	var boxCalInfo=[];
	var txtCalInfo=[];
	for(var i=0,x=330,y=6;i<10;i++){
		var x=330+(150*(Math.floor(i/3)));
		var y=16+(19*(i%3));
		boxCalInfo[i]=Jade.Objects._create();
		boxCalInfo[i].Location(x,y,12,12);
		boxCalInfo[i].className='leyend_box daytype_'+i;
		boxCalInfo[i].Add(gpCalendarLegend);
		txtCalInfo[i]=Jade.Objects.Text(Jade.Lang.Get('variants')+i);
		txtCalInfo[i].Location(x+20,y+2,200,20);
		txtCalInfo[i].Add(gpCalendarLegend);
		Jade.FX.Hide(boxCalInfo[i]);
		Jade.FX.Hide(txtCalInfo[i]);
	}
}
{//Eventos-------------------------------------------------------------------------------------------------------------------------------------------------
	calendar.ondayclick=function(e,cube){
		var day = parseInt(cube.day);
		var month = parseInt(cmbCalendarMonth.selectedKey);
		var year = parseInt(cmbCalendarYear.selectedKey);
		var are_change=false;
		var are_public=false;
		try{ if(typeof(calendar._changes.year[year].month[month].day[day])!="undefined") { are_change = true; } }
		catch(e) { are_change=false; }
		try { if(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_public){ item = calendar._public.year[year].month[month].day[day]; }
		if(typeof(item.Fecha)!="undefined"){
			cmbDayData_Daytype.SetSelectedKey(item.IdTipoDia);
			cmbDayData_Variant.SetSelectedKey(item.IdVariante);
			chkDayData_Variant.SetCheck(item.TipoVariante);
			cmbDayData_Incidence.SetSelectedKey(item.IdIncidencia);
			cmbDayData_Workday.SetSelectedKey(item.IdHorario);
			txtDayData_Comments.SetText(item.Observaciones);
		}
	}
	cmdCalendar_Clean.onclick=function(){
		var month=parseInt(cmbCalendarMonth.selectedKey);
		var year=parseInt(cmbCalendarYear.selectedKey);
		for(var i=0,iLen=calendar.selectedDays.length;i<iLen;i++){
			var cube = calendar.selectedDays[i];
			var day = parseInt(cube.day);
			var are_private=false;
			var item={};
			try{ if(typeof(calendar._private.year[year].month[month].day[day])!="undefined"){ are_private = true; } }
			catch(e){ are_private=false; }
			if(are_private){
				var item=calendar._public.year[year].month[month].day[day];
				cube.SetColor(item.IdTipoDia, item.IdVariante, item.IdIncidencia);
				//----------------------------------------------------------------------------------
				if(typeof(calendar._changes.year[year])=="undefined"){
					calendar._changes.year[year]={}; 
					calendar._changes.year[year].month = new Array;
				}
				if(typeof(calendar._changes.year[year].month[month])=="undefined"){
					calendar._changes.year[year].month[month]={}; 
					calendar._changes.year[year].month[month].day=new Array;
				}
				calendar._changes.year[year].month[month].day[day]=item;
			}
		}
		calendar.ClearSelected();
		calendar.ClearSelected();
		cmbDayData_Daytype.SetSelectedIndex(0);
		cmbDayData_Variant.SetSelectedIndex(0);
		chkDayData_Variant.SetCheck(true);
		cmbDayData_Incidence.SetSelectedIndex(0);
		cmbDayData_Workday.SetSelectedIndex(0);
		txtDayData_Comments.SetText('');
	}
	cmdCalendar_Apply.onclick=function(){
		var month=parseInt(cmbCalendarMonth.selectedKey);
		var year=parseInt(cmbCalendarYear.selectedKey);
		for(var i=0,iLen=calendar.selectedDays.length;i<iLen;i++){
			var cube=calendar.selectedDays[i];
			var day=parseInt(cube.day);
			
			cube.SetColor(cmbDayData_Daytype.selectedKey, cmbDayData_Variant.selectedKey, cmbDayData_Incidence.selectedKey);
			if(typeof(calendar._changes.year[year])=="undefined"){
				calendar._changes.year[year]={month:[]}; 
			}
			if(typeof(calendar._changes.year[year].month[month])=="undefined"){
				calendar._changes.year[year].month[month]={day:[]}; 
			}
			calendar._changes.year[year].month[month].day[day]={};
			calendar._changes.year[year].month[month].day[day].day=day;
			calendar._changes.year[year].month[month].day[day].Fecha=Jade.Strings.PadLeft(year,'0',4)+Jade.Strings.PadLeft(month,'0',2)+Jade.Strings.PadLeft(day,'0',2);
			calendar._changes.year[year].month[month].day[day].IdTipoDia=cmbDayData_Daytype.selectedKey;
			calendar._changes.year[year].month[month].day[day].IdVariante=cmbDayData_Variant.selectedKey;
			calendar._changes.year[year].month[month].day[day].TipoVariante=chkDayData_Variant.GetCheck();
			calendar._changes.year[year].month[month].day[day].IdIncidencia=cmbDayData_Incidence.selectedKey;
			calendar._changes.year[year].month[month].day[day].IdHorario=cmbDayData_Workday.selectedKey;
			calendar._changes.year[year].month[month].day[day].Observaciones=txtDayData_Comments.GetText();
		}
		calendar.ClearSelected();
		cmbDayData_Daytype.SetSelectedIndex(0);
		cmbDayData_Variant.SetSelectedIndex(0);
		chkDayData_Variant.SetCheck(true);
		cmbDayData_Incidence.SetSelectedIndex(0);
		cmbDayData_Workday.SetSelectedIndex(0);
		txtDayData_Comments.SetText('');
	}
}
{//Default values
	for(var i=1;i<=12;i++){ cmbCalendarMonth.AddItem(Jade.Lang.Get('month_'+i),i); }
	cmbCalendarMonth.SetSelectedKey(Jade.DateTime.CurrentMonth());
	
	var currentYear=Jade.DateTime.CurrentYear();
	for(var i=currentYear-10,iLen=currentYear+10;i<=iLen;i++){ cmbCalendarYear.AddItem(i,i); }
	cmbCalendarYear.SetSelectedKey(currentYear);
	
	Jade.FX.Hide(cmdCalendar_Generate);
}