var options={};

var aEmployees=[];
var aTerminals=[];
var aItems={};

function Report_Load_Vars() {
	options.title=opener.txtTitle.GetText();
	options.fromEmployee=opener.cmbFromEmployee.selectedKey;
	options.fromEmployeeValue=opener.cmbFromEmployee.value;
	options.toEmployee=opener.cmbToEmployee.selectedKey;
	options.toEmployeeValue=opener.cmbToEmployee.value;
	options.department=opener.cmbDepartment.selectedKey;
	options.departmentValue=opener.cmbDepartment.value;
	options.center=opener.cmbCenter.selectedKey;
	options.centerValue=opener.cmbCenter.value;
	options.fromDate=opener.cmbFromDate.value;
	options.toDate=opener.cmbToDate.value;

	options.breakdown=iif(opener.cmbBreakdown.selectedKey=='Breakdown',true,false);
	options.breakdownValue=opener.cmbBreakdown.value;
	options.group=opener.cmbGroup.selectedKey;
	options.groupValue=opener.cmbGroup.value;
	options.total=opener.chkTotal.GetCheck();
}
function Report_Load_Model() {
	report.AddStyle('title','color:#592D00; font-size: 14pt; font-weight: bold; font-style: italic;');
	report.AddStyle('header_label','color:#592D00; font-weight: bold;');
	report.AddStyle('header_value','color:#592D00; font-style: italic;');
	report.AddStyle('headers','background: #E8E8E8 url(design.report.png) repeat-x 0 0; font-weight: bold;');
	report.AddStyle('employee','background: #DEDEDE url(design.report.png) repeat-x 0 -24px;');
	report.AddStyle('values.pair','background:#F0F0F0;');
	report.AddStyle('totals','background: #FFF5D4 url(design.report.png) repeat-x 0 -46px;');
	report.AddStyle('valueCenter','text-align: center;');

	report.AddTable('title',{height: 30, border_top: true, border_left: true, border_right: true, border_color: '#EEE', className: 'title'});
	report.tables['title'].Add();
	
	report.AddTable('head_single',{height: 16});
	report.tables['head_single'].Add({width: 100, border_left: true, border_color: '#EEE', className: 'header_label'});
	report.tables['head_single'].Add({border_right: true, border_color: '#EEE', className: 'header_value'});
	
	report.AddTable('head_double',{height: 16});
	report.tables['head_double'].Add({width: 100, border_left: true, border_color: '#EEE', className: 'header_label'});
	report.tables['head_double'].Add({width: 275, className: 'header_value'});
	report.tables['head_double'].Add({width: 100, className: 'header_label'});
	report.tables['head_double'].Add({border_right: true, border_color: '#EEE', className: 'header_value'});

	report.AddTable('head_issue',{height: 16});
	report.tables['head_issue'].Add({width: 100, border_left:true, border_color:'#EEE', className: 'header_label'});
	report.tables['head_issue'].Add({width: 400, className: 'header_value'});
	report.tables['head_issue'].Add({width: 140, className: 'header_label'});
	report.tables['head_issue'].Add({border_right:true, border_color: '#EEE', className: 'header_value'});
	
	report.AddTable('headers',{className: 'headers', height: 24});
	report.tables['headers'].Add({width: 100});
	report.tables['headers'].Add({width: 350});
	report.tables['headers'].Add({width: 80, align: 'center'});
	report.tables['headers'].Add({width: 80, align: 'center'});
	report.tables['headers'].Add({width: 80, align: 'center'});
	report.tables['headers'].Add();
	report.AddTable('headers_nobreak',{className: 'headers', height: 24});
	report.tables['headers_nobreak'].Add({width: 400});
	report.tables['headers_nobreak'].Add({width: 80, align: 'center'});
	report.tables['headers_nobreak'].Add({width: 80, align: 'center'});
	report.tables['headers_nobreak'].Add({width: 80, align: 'center'});	
	report.tables['headers_nobreak'].Add();
	
	report.AddTable('employees',{className: 'employee', border_left:true, border_right:true, border_color: '#D9D9D9'});
	report.tables['employees'].Add();

	report.AddTable('values',{className: 'values'});
	report.tables['values'].Add({width: 100});
	report.tables['values'].Add({width: 350});
	report.tables['values'].Add({width: 80, align: 'center'});
	report.tables['values'].Add({width: 80, align: 'center'});
	report.tables['values'].Add({width: 80, align: 'center'});
	report.tables['values'].Add();
	report.AddTable('values_nobreak',{className: 'values'});
	report.tables['values_nobreak'].Add({width: 400});
	report.tables['values_nobreak'].Add({width: 80, align: 'center'});
	report.tables['values_nobreak'].Add({width: 80, align: 'center'});
	report.tables['values_nobreak'].Add({width: 80, align: 'center'});	
	report.tables['values_nobreak'].Add();
	
	report.AddTable('totals',{className: 'totals'});
	report.tables['totals'].Add({width: 100});
	report.tables['totals'].Add({width: 350});
	report.tables['totals'].Add({width: 80, align: 'center'});
	report.tables['totals'].Add({width: 80, align: 'center'});
	report.tables['totals'].Add({width: 80, align: 'center'});
	report.tables['totals'].Add();
	report.AddTable('totals_nobreak',{className: 'totals'});
	report.tables['totals_nobreak'].Add({width: 400});
	report.tables['totals_nobreak'].Add({width: 80, align: 'center'});
	report.tables['totals_nobreak'].Add({width: 80, align: 'center'});
	report.tables['totals_nobreak'].Add({width: 80, align: 'center'});	
	report.tables['totals_nobreak'].Add();
	
}
function Report_Load_Data() {

	{ //Select all data

		var whereItems = [];
		whereItems.push("f.Fecha>='"+options.fromDate+"' AND f.Fecha<='"+options.toDate+"'");
		
		if (options.fromEmployee != 'All') {
			if (options.toEmployee == 'None') { whereItems.push("e.Codigo = '"+options.fromEmployee+"'"); }
			else { whereItems.push("e.Codigo >= '"+options.fromEmployee+"' AND e.Codigo <= '"+options.toEmployee+"'"); }
		}
		if(options.department != 'All') { whereItems.push("e.IdUbicacion = '" + options.department + "'"); }
		if(options.center != 'All') { whereItems.push("e.IdCentro = '" + options.center + "'"); }
		
		var strOrder='e.Codigo, f.IdTerminal,f.Fecha';
		if(options.group!='Employee') { strOrder='f.IdTerminal,f.Fecha,e.Codigo'; }
		
		var ajaxData = new Ajax(Convert.Format("SELECT f.IdEmpleado, e.Nombre, e.Apellidos, f.Fecha, f.IdTerminal, t.Descripcion, f.Denegado FROM (FICHAJES_ACCESO{c} f INNER JOIN EMPLEADOS{c} e ON " +
		"f.IdEmpleado = e.Codigo) LEFT OUTER JOIN TERMINALES{c} t ON f.IdTerminal = t.Codigo WHERE {0} ORDER BY {1} ASC",whereItems.join(" AND "),strOrder));
		ajaxData.oncomplete = function() {
			for(var i in this.json.row) {
				var row = this.json.row[i];
				if(IsInEmployees(row.IdEmpleado)){
					var key = iif(options.group=='Employee',row.IdEmpleado,row.IdTerminal);
					var subkey = iif(options.group=='Employee',row.IdTerminal,row.IdEmpleado)
					var employeeText = row.IdEmpleado+' - '+row.Nombre+' '+row.Apellidos;
					var terminalText = row.IdTerminal+' - '+row.Descripcion;
					if(typeof(aItems[key])=="undefined"){
						aItems[key] = {}
						aItems[key].text = iif(options.group=='Employee',employeeText,terminalText);
						aItems[key].dates={};
						aItems[key].nobreak={};
						aItems[key].total=0;
						aItems[key].totalGrant=0;
						aItems[key].totalDenny=0;
					}
					var item = aItems[key];
					if(typeof(item.nobreak[subkey])=="undefined"){
						item.nobreak[subkey]={};
						item.nobreak[subkey].text = iif(options.group=='Employee',terminalText,employeeText);
						item.nobreak[subkey].total=0;
						item.nobreak[subkey].totalGrant=0;
						item.nobreak[subkey].totalDenny=0;
					}
					var nobreak = item.nobreak[subkey];
					if(typeof(item.dates[row.Fecha])=="undefined"){
						item.dates[row.Fecha]={};
						item.dates[row.Fecha].items={};
						item.dates[row.Fecha].total=0;
						item.dates[row.Fecha].totalGrant=0;
						item.dates[row.Fecha].totalDenny=0;
					}
					var date = item.dates[row.Fecha];
					if(typeof(date.items[subkey])=="undefined"){
						date.items[subkey] = {};
						date.items[subkey].text = iif(options.group=='Employee',terminalText,employeeText);
						date.items[subkey].total=0;
						date.items[subkey].totalGrant=0;
						date.items[subkey].totalDenny=0;
					}
					var subitem = date.items[subkey];
					
					if(row.Denegado==0){
						item.totalGrant++;
						nobreak.totalGrant++;
						date.totalGrant++;
						subitem.totalGrant++;
					}
					else{
						item.totalDenny++;
						nobreak.totalDenny++;
						date.totalDenny++;
						subitem.totalDenny++;
					}
					item.total++;
					nobreak.total++;
					date.total++;
					subitem.total++;
				}
			}
		}

	}
	
	AjaxManager.Create('data');
	AjaxManager.group['data'].Add(ajaxData);
	AjaxManager.group['data'].Start();

}
function Report_Process_Data() {
	report.SetSection('head');
	report.Add('title',[options.title]);
	report.AddSpace(5,'head_single');
	report.Add('head_single',[LngStr('company',true),COMPANY.code+' - '+COMPANY.description]);
	report.Add('head_double',[LngStr('from',true),options.fromEmployeeValue,LngStr('to',true),options.toEmployeeValue]);
	report.Add('head_double',[COMPANY.department+':',options.departmentValue,COMPANY.center+':',options.centerValue]);
	report.Add('head_double',[LngStr('from_date',true),Convert.Date(options.fromDate),LngStr('to_date',true),Convert.Date(options.toDate)]);
	report.Add('head_double',[LngStr('breakdown',true),options.breakdownValue,LngStr('group_by',true),options.groupValue]);
	
	report.AddSpace(10,true);
	if(options.breakdown) { report.Add('headers',[LngStr('date'), LngStr('terminal'), LngStr('granted'), LngStr('denny'), LngStr('total')]); }
	else { report.Add('headers_nobreak',[ LngStr('terminal'), LngStr('granted'), LngStr('denny'), LngStr('total')]); }
	
	report.SetSection('body');
	for(var i in aItems){
		var item = aItems[i];
		report.Add('employees',['<div class="arrow"></div> '+aItems[i].text]);
		if(options.breakdown){
			for(var j in item.dates){
				var date = item.dates[j];
				report.Add('values',[Convert.Date(j,true),LngStr('day_punches'),date.totalGrant,date.totalDenny,date.total]);
				for(var k in date.items){
					var subitem = date.items[k];
					report.Add('values',['',subitem.text,subitem.totalGrant,subitem.totalDenny,subitem.total]);
				}
			}
			if(options.total) { report.Add('totals',['',LngStr('total_punches'),item.totalGrant,item.totalDenny,item.total] ); }
		}
		else{
			for(var j in item.nobreak){
				var nobreak=item.nobreak[j];
				report.Add('values_nobreak',[nobreak.text,nobreak.totalGrant,nobreak.totalDenny,nobreak.total]);
			}
			if(options.total) { report.Add('totals_nobreak',[LngStr('total_punches'),item.totalGrant,item.totalDenny,item.total] ); }
		}
	}
}