var args = {
	"type": "report",
	"title": "BAC by CAM",
	"L1": "1",
	"dataset" : "(|ContrName|) (|EndDate|) (|Structure|StruName|) (|Unit|UnitName|)",
	"header": "This report displays total BAC and key performance indicators by CAM.",
	"sql" : [
		{
		"query": "select coalesce(ProjOff, ''), sum(Bac), case when sum(BcwsCum) <> 0 then sum(BcwpCum)/sum(BcwsCum) else 0 end from EarnedValue inner join Element on Element.ElemID = EarnedValue.ElemID inner join Period on Period.PeriodID = EarnedValue.PeriodID inner join Contract on Contract.ContrID = Element.ContrID where ElemType = CaSym and StruID in (st_id, 3) and Period.PeriodID = cd_id and UnitID = un_id group by ProjOff order by ProjOff",
		"format": ["", "cdd", "n3"],
		"scale": ["", "ds", ""]
		}
	],
	"cols": ["CAM", "BAC", "SPI"],
	"align": ["l", "r", "r"]
};

(function() {
	"use strict";
	var em = [],
		td_h = "rpt rac rcgr1 rtb",
		td_c = "rpt ra",
		arr = [],
		s, i, ilen, j, jlen, vals, val;
	em.push('<div class="rpt">');
	em.push('<div class="rtitle">');
	s = args.L1 ? args.dataset : extra.title;
	if (args.sync && extra.pf && extra.pf.length) {
		s = s + ' (' + extra.pf + ')';
	}
	em.push(s);
	em.push('<br>');
	s = args.title;
	if (extra.scale.text.length) {
		s = s + ' (' + extra.scale.text + ')';
	}
	em.push(s);
	em.push('</div>');
	if( extra.dataset.ce === 'sum' ) {
		em.push('</div>');
		return em.join('');
	}
	if (args.header) {
		em.push('<br>');
		em.push(args.header);
	}
	em.push('<p>');
	em.push('<table class="rpt">');
	em.push('<tr>');
	for (i = 0, ilen = args.cols ? args.cols.length : 0; i < ilen; i++) {
		s = args.cols[i];
		em.push('<td class="' + td_h + '">' + s + '</td>');
	}
	em.push('</tr>');
	arr = args.sql[0];
	for (i = 0, ilen = arr.length; i < ilen; i++) {
		em.push('<tr>');
		vals = arr[i];
		if (args.sync) {
			vals.shift();
		}
		for (j = 0, jlen = vals.length; j < jlen; j++) {
			val = vals[j];
			s = args.align ? args.align[j] : 'l';
			em.push('<td class="' + td_c + s + '">' + val + '</td>');
		}
		em.push('</tr>');
	}
	em.push('</table>');
	em.push('</div>');
	return em.join('');
})();
